Skip to content

Commit dbbf082

Browse files
morpavdpgeorge
authored andcommitted
py/makeqstrdefs: Add script to automate extraction of qstr from sources.
This script will search for patterns of the form Q(...) and generate a list of them. The original code by Pavel Moravec has been significantly simplified to remove the part that searched for C preprocessor directives (eg #if). This is because all source is now run through CPP before being fed into this script.
1 parent 050e645 commit dbbf082

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

py/makeqstrdefs.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
This script processes the output from the C preprocessor and extracts all
3+
qstr. Each qstr is transformed into a qstr definition of the form 'Q(...)'.
4+
5+
This script works with Python 2.6, 2.7, 3.3 and 3.4.
6+
"""
7+
8+
import sys
9+
import re
10+
import argparse
11+
import os
12+
13+
# Blacklist of qstrings that are specially handled in further
14+
# processing and should be ignored
15+
QSTRING_BLACK_LIST = {'NULL', 'number_of', }
16+
17+
18+
def debug(message):
19+
#sys.stderr.write("%s\n" % message)
20+
pass
21+
22+
23+
def process_file(f):
24+
output = []
25+
for line in f:
26+
for match in re.findall(r'MP_QSTR_[_a-zA-Z0-9]+', line):
27+
name = match.replace('MP_QSTR_', '')
28+
if name not in QSTRING_BLACK_LIST:
29+
output.append('Q(' + name + ')')
30+
31+
# make sure there is a newline at the end of the output
32+
output.append('')
33+
34+
return '\n'.join(output)
35+
36+
37+
if __name__ == "__main__":
38+
parser = argparse.ArgumentParser(description='Generates qstr definitions from a specified source')
39+
40+
parser.add_argument('-o', '--output-file', dest='output_filename',
41+
help='Output filename (defaults to stdout)')
42+
parser.add_argument('input_filename', nargs='?',
43+
help='Name of the input file (when not specified, the script reads standard input')
44+
parser.add_argument('-s', '--skip-write-when-same', dest='skip_write_when_same',
45+
action='store_true', default=False,
46+
help="Don't write the output file if it already exists and the contents have not changed (disabled by default)")
47+
48+
args = parser.parse_args()
49+
50+
# Check if the file contents changed from last time
51+
write_file = True
52+
53+
# By default write into STDOUT
54+
outfile = sys.stdout
55+
real_output_filename = 'STDOUT'
56+
57+
if args.input_filename:
58+
infile = open(args.input_filename, 'r')
59+
else:
60+
infile = sys.stdin
61+
62+
file_data = process_file(infile)
63+
infile.close()
64+
65+
# Detect custom output file name
66+
if args.output_filename:
67+
real_output_filename = args.output_filename
68+
if os.path.isfile(args.output_filename) and args.skip_write_when_same:
69+
with open(args.output_filename, 'r') as f:
70+
existing_data = f.read()
71+
if existing_data == file_data:
72+
debug("Skip regeneration of: %s\n" % real_output_filename)
73+
write_file = False
74+
else:
75+
debug("File HAS changed, overwriting\n")
76+
outfile = open(args.output_filename, 'w')
77+
else:
78+
outfile = open(args.output_filename, 'w')
79+
80+
# Only write the file if we the data has changed
81+
if write_file:
82+
sys.stderr.write("QSTR %s\n" % real_output_filename)
83+
outfile.write(file_data)
84+
outfile.close()

0 commit comments

Comments
 (0)