mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2015 Google Inc. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
| 8 | # This script does a very rough simulation of BUILD file expansion, |
| 9 | # mostly to see the effects of glob(). |
| 10 | |
| 11 | # We start by adding some symbols to our namespace that BUILD.public calls. |
| 12 | |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 13 | import glob |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 14 | import os |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 15 | import pprint |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 16 | import re |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 17 | |
| 18 | def noop(*args, **kwargs): |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 19 | pass |
| 20 | |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 21 | DOUBLE_STAR_RE = re.compile(r'/\*\*/') |
| 22 | STAR_RE = re.compile(r'\*') |
| 23 | DOUBLE_STAR_PLACEHOLDER = "xxxdoublestarxxx" |
| 24 | STAR_PLACEHOLDER = "xxxstarxxx" |
| 25 | |
| 26 | # Returns a set of files that match pattern. |
| 27 | def BUILD_glob_single(pattern): |
| 28 | if pattern.find('**') < 0: |
| 29 | # If pattern doesn't include **, glob.glob more-or-less does the right |
| 30 | # thing. |
| 31 | return glob.glob(pattern) |
| 32 | # First transform pattern into a regexp. |
| 33 | # Temporarily remove ** and *. |
| 34 | pattern2 = DOUBLE_STAR_RE.sub(DOUBLE_STAR_PLACEHOLDER, pattern) |
| 35 | pattern3 = STAR_RE.sub(STAR_PLACEHOLDER, pattern2) |
| 36 | # Replace any regexp special characters. |
| 37 | pattern4 = re.escape(pattern3) |
| 38 | # Replace * with [^/]* and ** with .*. |
| 39 | pattern5 = pattern4.replace(STAR_PLACEHOLDER, '[^/]*') |
| 40 | pattern6 = pattern5.replace(DOUBLE_STAR_PLACEHOLDER, '.*/') |
| 41 | # Anchor the match at the beginning and end. |
| 42 | pattern7 = "^" + pattern6 + "$" |
| 43 | pattern_re = re.compile(pattern7) |
| 44 | matches = set() |
| 45 | for root, _, files in os.walk('.'): |
| 46 | for fname in files: |
| 47 | # Remove initial "./". |
| 48 | path = os.path.join(root, fname)[2:] |
| 49 | if pattern_re.match(path): |
| 50 | matches.add(path) |
| 51 | return matches |
| 52 | |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 53 | # Simulates BUILD file glob(). |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 54 | def BUILD_glob(include, exclude=()): |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 55 | files = set() |
| 56 | for pattern in include: |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 57 | files.update(BUILD_glob_single(pattern)) |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 58 | for pattern in exclude: |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 59 | files.difference_update(BUILD_glob_single(pattern)) |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 60 | return list(sorted(files)) |
| 61 | |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 62 | # With these namespaces, we can treat BUILD.public as if it were |
| 63 | # Python code. This pulls its variable definitions (SRCS, HDRS, |
| 64 | # DEFINES, etc.) into local_names. |
| 65 | global_names = { |
| 66 | 'exports_files': noop, |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 67 | 'cc_library': noop, |
| 68 | 'cc_test': noop, |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 69 | 'glob': BUILD_glob, |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 70 | 'EXTERNAL_DEPS': [], |
| 71 | 'BASE_DIR': "", |
| 72 | 'DM_EXTERNAL_DEPS': [], |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 73 | } |
| 74 | local_names = {} |
| 75 | execfile('BUILD.public', global_names, local_names) |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 76 | |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 77 | with open('tools/BUILD.public.expected', 'w') as out: |
| 78 | print >>out, "This file is auto-generated by tools/BUILD_simulator.py." |
| 79 | print >>out, "It expands BUILD.public to make it easy to see changes." |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 80 | for name, value in sorted(local_names.items()): |
| 81 | print >>out, name, '= ', |
| 82 | pprint.pprint(value, out) |