Pavel Moravec | dbbf082 | 2016-03-11 16:12:59 +0000 | [diff] [blame] | 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 | |
Pavel Moravec | dbbf082 | 2016-03-11 16:12:59 +0000 | [diff] [blame] | 8 | import re |
| 9 | import argparse |
| 10 | import os |
| 11 | |
| 12 | # Blacklist of qstrings that are specially handled in further |
| 13 | # processing and should be ignored |
| 14 | QSTRING_BLACK_LIST = {'NULL', 'number_of', } |
| 15 | |
| 16 | |
Paul Sokolovsky | c618f91 | 2016-04-19 11:30:06 +0300 | [diff] [blame] | 17 | def write_out(fname, output): |
| 18 | if output: |
| 19 | fname = fname.replace("/", "__").replace("..", "@@") |
| 20 | with open(args.output_dir + "/" + fname + ".qstr", "w") as f: |
| 21 | f.write("\n".join(output) + "\n") |
| 22 | |
Pavel Moravec | dbbf082 | 2016-03-11 16:12:59 +0000 | [diff] [blame] | 23 | def process_file(f): |
| 24 | output = [] |
Paul Sokolovsky | c618f91 | 2016-04-19 11:30:06 +0300 | [diff] [blame] | 25 | last_fname = None |
Pavel Moravec | dbbf082 | 2016-03-11 16:12:59 +0000 | [diff] [blame] | 26 | for line in f: |
Paul Sokolovsky | 8dd704b | 2016-04-19 12:52:57 +0300 | [diff] [blame] | 27 | if line and line[0:2] == "# ": |
Paul Sokolovsky | c618f91 | 2016-04-19 11:30:06 +0300 | [diff] [blame] | 28 | comp = line.split() |
| 29 | fname = comp[2] |
| 30 | assert fname[0] == '"' and fname[-1] == '"' |
| 31 | fname = fname[1:-1] |
| 32 | if fname[0] == "/" or not fname.endswith(".c"): |
| 33 | continue |
| 34 | if fname != last_fname: |
| 35 | write_out(last_fname, output) |
| 36 | output = [] |
| 37 | last_fname = fname |
| 38 | continue |
Pavel Moravec | dbbf082 | 2016-03-11 16:12:59 +0000 | [diff] [blame] | 39 | for match in re.findall(r'MP_QSTR_[_a-zA-Z0-9]+', line): |
| 40 | name = match.replace('MP_QSTR_', '') |
| 41 | if name not in QSTRING_BLACK_LIST: |
| 42 | output.append('Q(' + name + ')') |
| 43 | |
Paul Sokolovsky | c618f91 | 2016-04-19 11:30:06 +0300 | [diff] [blame] | 44 | write_out(last_fname, output) |
| 45 | return "" |
Pavel Moravec | dbbf082 | 2016-03-11 16:12:59 +0000 | [diff] [blame] | 46 | |
Paul Sokolovsky | c618f91 | 2016-04-19 11:30:06 +0300 | [diff] [blame] | 47 | |
| 48 | def cat_together(): |
| 49 | import glob |
| 50 | import hashlib |
| 51 | hasher = hashlib.md5() |
| 52 | all_lines = [] |
| 53 | outf = open(args.output_dir + "/out", "wb") |
| 54 | for fname in glob.glob(args.output_dir + "/*.qstr"): |
| 55 | with open(fname, "rb") as f: |
| 56 | lines = f.readlines() |
| 57 | all_lines += lines |
| 58 | all_lines.sort() |
| 59 | all_lines = b"\n".join(all_lines) |
| 60 | outf.write(all_lines) |
| 61 | outf.close() |
| 62 | hasher.update(all_lines) |
| 63 | new_hash = hasher.hexdigest() |
| 64 | #print(new_hash) |
| 65 | old_hash = None |
| 66 | try: |
| 67 | with open(args.output_file + ".hash") as f: |
| 68 | old_hash = f.read() |
| 69 | except IOError: |
| 70 | pass |
| 71 | if old_hash != new_hash: |
| 72 | print("QSTR updated") |
| 73 | os.rename(args.output_dir + "/out", args.output_file) |
| 74 | with open(args.output_file + ".hash", "w") as f: |
| 75 | f.write(new_hash) |
| 76 | else: |
| 77 | print("QSTR not updated") |
Pavel Moravec | dbbf082 | 2016-03-11 16:12:59 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | if __name__ == "__main__": |
| 81 | parser = argparse.ArgumentParser(description='Generates qstr definitions from a specified source') |
| 82 | |
Paul Sokolovsky | 1b60a6d | 2016-04-19 14:39:08 +0300 | [diff] [blame] | 83 | parser.add_argument('command', |
| 84 | help='Command (split/cat)') |
Paul Sokolovsky | c618f91 | 2016-04-19 11:30:06 +0300 | [diff] [blame] | 85 | parser.add_argument('input_filename', |
| 86 | help='Name of the input file (when not specified, the script reads standard input)') |
| 87 | parser.add_argument('output_dir', |
| 88 | help='Output directory to store individual qstr files') |
| 89 | parser.add_argument('output_file', |
| 90 | help='Name of the output file with collected qstrs') |
Pavel Moravec | dbbf082 | 2016-03-11 16:12:59 +0000 | [diff] [blame] | 91 | |
| 92 | args = parser.parse_args() |
Paul Sokolovsky | c618f91 | 2016-04-19 11:30:06 +0300 | [diff] [blame] | 93 | try: |
| 94 | os.makedirs(args.output_dir) |
| 95 | except OSError: |
| 96 | pass |
Pavel Moravec | dbbf082 | 2016-03-11 16:12:59 +0000 | [diff] [blame] | 97 | |
Paul Sokolovsky | 1b60a6d | 2016-04-19 14:39:08 +0300 | [diff] [blame] | 98 | if args.command == "split": |
| 99 | with open(args.input_filename) as infile: |
| 100 | process_file(infile) |
Pavel Moravec | dbbf082 | 2016-03-11 16:12:59 +0000 | [diff] [blame] | 101 | |
Paul Sokolovsky | 1b60a6d | 2016-04-19 14:39:08 +0300 | [diff] [blame] | 102 | if args.command == "cat": |
| 103 | cat_together() |