OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 import argparse |
| 4 import os |
| 5 import sys |
| 6 import re |
| 7 import textwrap |
| 8 |
| 9 |
| 10 def _calc_header_guard(dirname, filename): |
| 11 result = '%s_%s' % (dirname, filename) |
| 12 result = ''.join(c if c.isalnum() else '_' for c in result.lower()) |
| 13 return result.upper() + '_' |
| 14 |
| 15 |
| 16 def convert_to_stub(dirname, filename, new_path, issue_number): |
| 17 """Converts a file in a dir to be just forwarding to a similarly named |
| 18 (header) file in another path.""" |
| 19 |
| 20 expected_header_guard = _calc_header_guard(dirname, filename) |
| 21 #print 'Processing: %s header guard: %s' % (filename, expected_header_guard) |
| 22 |
| 23 file_path = os.path.join(dirname, filename) |
| 24 with open(file_path) as f: |
| 25 content = f.read() |
| 26 |
| 27 source_matcher = re.compile(r'(.*#define %s\n)(.*)(#endif.*)' % |
| 28 expected_header_guard, re.DOTALL) |
| 29 match = re.search(source_matcher, content) |
| 30 |
| 31 if match: |
| 32 #print 'Found match in %s' % filename |
| 33 temp_file = filename + '.new' |
| 34 with open(temp_file, 'w') as out: |
| 35 out.write(match.group(1)) |
| 36 out.write(textwrap.dedent("""\n |
| 37 // This header is deprecated and is just left here temporarily during |
| 38 // refactoring. See https://bugs.webrtc.org/%d for more details.\n""" |
| 39 % issue_number)) |
| 40 out.write('#include "%s/%s"\n' % (new_path, filename)) |
| 41 out.write('\n') |
| 42 out.write(match.group(3)) |
| 43 os.rename(temp_file, file_path) |
| 44 else: |
| 45 print 'WARNING: Couldn\'t find expected header guard in %s' % file_path |
| 46 |
| 47 |
| 48 def main(): |
| 49 if os.path.basename(os.getcwd()) != 'src': |
| 50 print 'Make sure you run this script with cwd being src/' |
| 51 sys.exit(1) |
| 52 |
| 53 p = argparse.ArgumentParser() |
| 54 p.add_argument('-s', '--source-dir', |
| 55 help=('Source directory of headers to process.')) |
| 56 p.add_argument('-d', '--dest-dir', |
| 57 help=('Destination dir of where headers have been moved.')) |
| 58 p.add_argument('-i', '--issue-number', type=int, |
| 59 help=('Issue number at bugs.webrtc.org to reference.')) |
| 60 p.add_argument('--dry-run', action='store_true', default=False, |
| 61 help=('Don\'t perform any modifications.')) |
| 62 opts = p.parse_args() |
| 63 if not opts.source_dir or not opts.dest_dir or not opts.issue_number: |
| 64 print 'Please provide the -s, -d and -i flags.' |
| 65 sys.exit(2) |
| 66 |
| 67 print 'Listing headers in %s' % opts.source_dir |
| 68 for dirname, _, file_list in os.walk(opts.source_dir): |
| 69 for filename in file_list: |
| 70 if filename.endswith('.h') and filename != 'sslroots.h': |
| 71 convert_to_stub(dirname, filename, opts.dest_dir, opts.issue_number) |
| 72 |
| 73 |
| 74 if __name__ == '__main__': |
| 75 sys.exit(main()) |
OLD | NEW |