OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 """Tool for listing Dart source files. | 6 """Tool for listing Dart source files. |
7 | 7 |
| 8 If the first argument is 'relative', the script produces paths relative to the |
| 9 current working directory. If the first argument is 'absolute', the script |
| 10 produces absolute paths. |
| 11 |
8 Usage: | 12 Usage: |
9 python tools/list_dart_files.py <directory> <root directory pattern> | 13 python tools/list_dart_files.py {absolute, relative} <directory> <pattern> |
10 """ | 14 """ |
11 | 15 |
12 import os | 16 import os |
13 import re | 17 import re |
14 import sys | 18 import sys |
15 | 19 |
16 | 20 |
17 def main(argv): | 21 def main(argv): |
18 directory = argv[1] | 22 mode = argv[1] |
| 23 if mode not in ['absolute', 'relative']: |
| 24 raise Exception("First argument must be 'absolute' or 'relative'") |
| 25 directory = argv[2] |
| 26 if mode in 'absolute' and not os.path.isabs(directory): |
| 27 directory = os.path.realpath(directory) |
19 | 28 |
20 pattern = None | 29 pattern = None |
21 if len(argv) > 2: | 30 if len(argv) > 3: |
22 pattern = re.compile(argv[2]) | 31 pattern = re.compile(argv[3]) |
23 | 32 |
24 for root, directories, files in os.walk(directory): | 33 for root, directories, files in os.walk(directory): |
25 # We only care about actual source files, not generated code or tests. | 34 # We only care about actual source files, not generated code or tests. |
26 for skip_dir in ['.git', 'gen', 'test']: | 35 for skip_dir in ['.git', 'gen', 'test']: |
27 if skip_dir in directories: | 36 if skip_dir in directories: |
28 directories.remove(skip_dir) | 37 directories.remove(skip_dir) |
29 | 38 |
30 # If we are looking at the root directory, filter the immediate | 39 # If we are looking at the root directory, filter the immediate |
31 # subdirectories by the given pattern. | 40 # subdirectories by the given pattern. |
32 if pattern and root == directory: | 41 if pattern and root == directory: |
33 directories[:] = filter(pattern.match, directories) | 42 directories[:] = filter(pattern.match, directories) |
34 | 43 |
35 for filename in files: | 44 for filename in files: |
36 if filename.endswith('.dart') and not filename.endswith('_test.dart'): | 45 if filename.endswith('.dart') and not filename.endswith('_test.dart'): |
37 fullname = os.path.relpath(os.path.join(root, filename)) | 46 if mode in 'absolute': |
| 47 fullname = os.path.join(directory, root, filename) |
| 48 else: |
| 49 fullname = os.path.relpath(os.path.join(root, filename)) |
38 fullname = fullname.replace(os.sep, '/') | 50 fullname = fullname.replace(os.sep, '/') |
39 print fullname | 51 print fullname |
40 | 52 |
41 | 53 |
42 if __name__ == '__main__': | 54 if __name__ == '__main__': |
43 sys.exit(main(sys.argv)) | 55 sys.exit(main(sys.argv)) |
OLD | NEW |