OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import os |
| 7 import re |
| 8 import sys |
| 9 |
| 10 import json |
| 11 import optparse |
| 12 |
| 13 # Matches the include statement in the braille table files. |
| 14 INCLUDE_RE = re.compile(r"^\s*include\s+([^#\s]+)") |
| 15 |
| 16 |
| 17 def Error(msg): |
| 18 print >> sys.stderr, 'liblouis_list_tables: %s' % msg |
| 19 sys.exit(1) |
| 20 |
| 21 |
| 22 def ToNativePath(pathname): |
| 23 return os.path.sep.join(pathname.split('/')) |
| 24 |
| 25 |
| 26 def LoadTablesFile(filename): |
| 27 with open(ToNativePath(filename), 'r') as fh: |
| 28 return json.load(fh) |
| 29 |
| 30 |
| 31 def FindFile(filename, directories): |
| 32 for d in directories: |
| 33 fullname = '/'.join([d, filename]) |
| 34 if os.path.isfile(ToNativePath(fullname)): |
| 35 return fullname |
| 36 Error('File not found: %s' % filename) |
| 37 |
| 38 |
| 39 def GetIncludeFiles(filename): |
| 40 result = [] |
| 41 with open(ToNativePath(filename), 'r') as fh: |
| 42 for line in fh.xreadlines(): |
| 43 match = INCLUDE_RE.match(line) |
| 44 if match: |
| 45 result.append(match.group(1)) |
| 46 return result |
| 47 |
| 48 |
| 49 def ProcessFile(output_set, filename, directories): |
| 50 fullname = FindFile(filename, directories) |
| 51 if fullname in output_set: |
| 52 return |
| 53 output_set.add(fullname) |
| 54 for include_file in GetIncludeFiles(fullname): |
| 55 ProcessFile(output_set, include_file, directories) |
| 56 |
| 57 |
| 58 def DoMain(argv): |
| 59 "Entry point for gyp's pymod_do_main command." |
| 60 parser = optparse.OptionParser() |
| 61 # Give a clearer error message when this is used as a module. |
| 62 parser.prog = 'liblouis_list_tables' |
| 63 parser.set_usage('usage: %prog [options] listfile') |
| 64 parser.add_option('-D', '--directory', dest='directories', |
| 65 action='append', help='Where to search for table files') |
| 66 (options, args) = parser.parse_args(argv) |
| 67 |
| 68 if len(args) != 1: |
| 69 parser.error('Expecting exactly one argument') |
| 70 if not options.directories: |
| 71 parser.error('At least one --directory option must be specified') |
| 72 |
| 73 tables = LoadTablesFile(args[0]) |
| 74 output_set = set() |
| 75 for table in tables: |
| 76 ProcessFile(output_set, table['fileName'], options.directories) |
| 77 return '\n'.join(output_set) |
| 78 |
| 79 |
| 80 def main(argv): |
| 81 print DoMain(argv[1:]) |
| 82 |
| 83 |
| 84 if __name__ == '__main__': |
| 85 try: |
| 86 sys.exit(main(sys.argv)) |
| 87 except KeyboardInterrupt: |
| 88 Error('interrupted') |
OLD | NEW |