1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # transform_sql.py -- create a header file with the appropriate SQL variables |
---|
4 | # from an SQL file |
---|
5 | # |
---|
6 | |
---|
7 | |
---|
8 | import os |
---|
9 | import re |
---|
10 | import sys |
---|
11 | |
---|
12 | |
---|
13 | def usage_and_exit(msg): |
---|
14 | if msg: |
---|
15 | sys.stderr.write('%s\n\n' % msg) |
---|
16 | sys.stderr.write( |
---|
17 | 'USAGE: %s SQLITE_FILE [OUTPUT_FILE]\n' |
---|
18 | ' stdout will be used if OUTPUT_FILE is not provided.\n' |
---|
19 | % os.path.basename(sys.argv[0])) |
---|
20 | sys.stderr.flush() |
---|
21 | sys.exit(1) |
---|
22 | |
---|
23 | |
---|
24 | def main(input, output, filename): |
---|
25 | input = input.read() |
---|
26 | |
---|
27 | var_name = re.sub('[-.]', '_', filename) |
---|
28 | |
---|
29 | output.write( |
---|
30 | '/* This file is automatically generated from %s.\n' |
---|
31 | ' * Do not edit this file -- edit the source and rerun gen-make.py */\n' |
---|
32 | '\n' |
---|
33 | % (filename,)) |
---|
34 | |
---|
35 | output.write('#define %s \\\n' % var_name.upper()) |
---|
36 | |
---|
37 | regex = re.compile(r'/\*.*?\*/', re.MULTILINE|re.DOTALL) |
---|
38 | input = regex.sub('', input) |
---|
39 | |
---|
40 | for line in input.split('\n'): |
---|
41 | line = line.replace('"', '\\"') |
---|
42 | |
---|
43 | if line.strip(): |
---|
44 | # got something besides whitespace. write it out. |
---|
45 | output.write(' "' + line + '"\\\n') |
---|
46 | |
---|
47 | output.write(' ""\n') |
---|
48 | |
---|
49 | |
---|
50 | if __name__ == '__main__': |
---|
51 | if len(sys.argv) < 2 or len(sys.argv) > 3: |
---|
52 | usage_and_exit('Incorrect number of arguments') |
---|
53 | |
---|
54 | # Note: we could use stdin, but then we'd have no var_name |
---|
55 | input_file = open(sys.argv[1], 'r') |
---|
56 | |
---|
57 | if len(sys.argv) > 2: |
---|
58 | output_file = open(sys.argv[2], 'w') |
---|
59 | else: |
---|
60 | output_file = sys.stdout |
---|
61 | |
---|
62 | main(input_file, output_file, os.path.basename(sys.argv[1])) |
---|