5
5
from argparse import ArgumentParser
6
6
from collections import defaultdict
7
7
from glob import glob
8
-
9
- script_dir : str = os . path . dirname ( os . path . abspath ( __file__ ));
8
+ from io import TextIOWrapper
9
+ from sys import stdout , stderr
10
10
11
11
parser = ArgumentParser ();
12
12
group = parser .add_mutually_exclusive_group ();
13
13
group .add_argument ('--layout' , type = str , help = "target layout, e.g. '90747013'" );
14
14
group .add_argument ('--build' , type = str , help = "target build, e.g. '10.0.0.43342'" );
15
15
parser .add_argument ('dbds' , type = str , nargs = '*' , help = 'directory with / list of for dbd files to process' );
16
- parser .add_argument ('--output' , type = str , default = os . path . join ( script_dir , 'dbds.sql' ), help = 'file or directory to dump sql to' );
16
+ parser .add_argument ('--output' , type = str , default = stdout , help = 'file to dump sql to' );
17
17
args = parser .parse_args ();
18
18
19
19
dbds :list [str ] = args .dbds or os .path .join (
20
20
os .path .dirname ( # WoWDBDefs/
21
21
os .path .dirname ( # code/
22
- script_dir # Python/
23
- )),
22
+ os .path .dirname ( # Python/
23
+ os .path .abspath (__file__ ) # ./dbd_to_sql.py
24
+ ))),
24
25
'definitions'
25
26
);
26
27
if not dbds [0 ].endswith (dbd .file_suffix ):
27
28
dbds = glob (os .path .join (dbds [0 ], '*.dbd' ));
28
29
29
- print (f"Found { len (dbds )} definitions to process" );
30
+ outfile :TextIOWrapper = args .output ;
31
+ if type (outfile ) != TextIOWrapper :
32
+ outfile = open (outfile , 'a' );
33
+
34
+ def log (* args , ** kwargs )-> None :
35
+ print (* args , file = stderr if outfile == stdout else stdout , ** kwargs );
30
36
31
- outfile :str = args .output ;
32
- outdir :str = '' ;
33
- if outfile .endswith ('.sql' ):
34
- with open (outfile , 'w' ) as file :
35
- file .write ("SET SESSION FOREIGN_KEY_CHECKS=0;\n " );
36
- else :
37
- if not os .path .isdir (outfile ):
38
- os .makedirs (outfile );
39
- outdir = outfile ;
40
- outfile = None ;
37
+ log (f"Found { len (dbds )} definitions to process" );
41
38
42
- print (f"Outputting to { outdir or outfile } " );
39
+ log (f"Outputting to { outfile } " );
40
+ outfile .write ("SET SESSION FOREIGN_KEY_CHECKS=0;\n " );
43
41
44
42
def get_sql_type (type :str , int_width :int = 0 , is_unsigned :bool = False )-> str :
45
43
type = {
@@ -72,7 +70,7 @@ def get_sql_type(type:str, int_width:int=0, is_unsigned:bool=False)->str:
72
70
def process_dbd (file :str )-> bool :
73
71
parsed :dbd .dbd_file = dbd .parse_dbd_file (file );
74
72
if not len (parsed .definitions ):
75
- print (f"No definitions found in { file } ! Skipping" );
73
+ log (f"No definitions found in { file } ! Skipping" );
76
74
return False ;
77
75
78
76
dirname :str = os .path .dirname (file );
@@ -92,7 +90,7 @@ def process_dbd(file:str)->bool:
92
90
if args .layout :
93
91
definition = next (defn for defn in parsed .definitions if args .layout in defn .layouts );
94
92
if not definition :
95
- print (f"No definition found for layout { args .layout } ! Skipping" );
93
+ log (f"No definition found for layout { args .layout } ! Skipping" );
96
94
return False ;
97
95
elif args .build :
98
96
definition = next (defn for defn in parsed .definitions if args .build in defn .builds );
@@ -119,10 +117,10 @@ def process_dbd(file:str)->bool:
119
117
foreign_dbd :str = next ((f for f in dbds if os .path .basename (f ) == f"{ foreign .table } .dbd" ), None );
120
118
if foreign_dbd :
121
119
if not process_dbd (foreign_dbd ):
122
- print (f"Could not process table { foreign .table } referenced by { name } .{ entry .column } " );
120
+ log (f"Could not process table { foreign .table } referenced by { name } .{ entry .column } " );
123
121
return False ;
124
122
if not foreign_dbd :
125
- print (f"FK { name } .{ entry .column } references { foreign .column } in { foreign .table } which was not supplied" );
123
+ log (f"FK { name } .{ entry .column } references { foreign .column } in { foreign .table } which was not supplied" );
126
124
127
125
sql_type = keys [foreign .table .string ].get (foreign .column .string , None ) or sql_type ;
128
126
fkeys .append (
@@ -144,22 +142,12 @@ def process_dbd(file:str)->bool:
144
142
if len (fkeys ):
145
143
fields .append (', ' .join (fkeys ));
146
144
147
- stmt :str = f"CREATE OR REPLACE TABLE `{ name } ` ({ ', ' .join (fields )} )" ;
148
-
149
- if outfile :
150
- with open (outfile , 'a' ) as file :
151
- file .write (f"{ stmt } ;\n " );
152
- elif outdir :
153
- with open (os .path .join (outdir , f"{ name } .sql" ), 'w' ) as file :
154
- file .write (stmt );
155
-
145
+ outfile .write (f"CREATE OR REPLACE TABLE `{ name } ` ({ ', ' .join (fields )} );\n " );
156
146
return True ;
157
147
158
148
for file in dbds :
159
149
process_dbd (file );
160
150
161
- if outfile :
162
- with open (outfile , 'a' ) as file :
163
- file .write ("SET SESSION FOREIGN_KEY_CHECKS=1;\n " );
164
-
165
- print ('Done.' );
151
+ outfile .write ("SET SESSION FOREIGN_KEY_CHECKS=1;\n " );
152
+ outfile .close ();
153
+ log ('Done.' );
0 commit comments