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 | 86ea33e | 2015-10-26 10:46:25 -0700 | [diff] [blame] | 21 | def select_simulator(d): |
| 22 | result = [] |
| 23 | for k in d: |
| 24 | result.append("*** BEGIN %s ***" % k) |
| 25 | result.extend(d[k]) |
| 26 | result.append("*** END %s ***" % k) |
| 27 | return result |
| 28 | |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 29 | DOUBLE_STAR_RE = re.compile(r'/\*\*/') |
| 30 | STAR_RE = re.compile(r'\*') |
| 31 | DOUBLE_STAR_PLACEHOLDER = "xxxdoublestarxxx" |
| 32 | STAR_PLACEHOLDER = "xxxstarxxx" |
| 33 | |
| 34 | # Returns a set of files that match pattern. |
| 35 | def BUILD_glob_single(pattern): |
| 36 | if pattern.find('**') < 0: |
| 37 | # If pattern doesn't include **, glob.glob more-or-less does the right |
| 38 | # thing. |
| 39 | return glob.glob(pattern) |
| 40 | # First transform pattern into a regexp. |
| 41 | # Temporarily remove ** and *. |
| 42 | pattern2 = DOUBLE_STAR_RE.sub(DOUBLE_STAR_PLACEHOLDER, pattern) |
| 43 | pattern3 = STAR_RE.sub(STAR_PLACEHOLDER, pattern2) |
| 44 | # Replace any regexp special characters. |
| 45 | pattern4 = re.escape(pattern3) |
| 46 | # Replace * with [^/]* and ** with .*. |
| 47 | pattern5 = pattern4.replace(STAR_PLACEHOLDER, '[^/]*') |
| 48 | pattern6 = pattern5.replace(DOUBLE_STAR_PLACEHOLDER, '.*/') |
| 49 | # Anchor the match at the beginning and end. |
| 50 | pattern7 = "^" + pattern6 + "$" |
| 51 | pattern_re = re.compile(pattern7) |
| 52 | matches = set() |
| 53 | for root, _, files in os.walk('.'): |
| 54 | for fname in files: |
| 55 | # Remove initial "./". |
| 56 | path = os.path.join(root, fname)[2:] |
| 57 | if pattern_re.match(path): |
| 58 | matches.add(path) |
| 59 | return matches |
| 60 | |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 61 | # Simulates BUILD file glob(). |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 62 | def BUILD_glob(include, exclude=()): |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 63 | files = set() |
| 64 | for pattern in include: |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 65 | files.update(BUILD_glob_single(pattern)) |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 66 | for pattern in exclude: |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 67 | files.difference_update(BUILD_glob_single(pattern)) |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 68 | return list(sorted(files)) |
| 69 | |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 70 | # With these namespaces, we can treat BUILD.public as if it were |
| 71 | # Python code. This pulls its variable definitions (SRCS, HDRS, |
| 72 | # DEFINES, etc.) into local_names. |
| 73 | global_names = { |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 74 | 'cc_library': noop, |
| 75 | 'cc_test': noop, |
benjaminwagner | 86ea33e | 2015-10-26 10:46:25 -0700 | [diff] [blame] | 76 | 'exports_files': noop, |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 77 | 'glob': BUILD_glob, |
benjaminwagner | 86ea33e | 2015-10-26 10:46:25 -0700 | [diff] [blame] | 78 | 'select': select_simulator, |
benjaminwagner | 39e7aa4 | 2015-11-18 13:26:10 -0800 | [diff] [blame] | 79 | 'BASE_DIR': '', |
| 80 | 'BASE_EXTERNAL_DEPS_ANDROID': [], |
| 81 | 'BASE_EXTERNAL_DEPS_IOS': [], |
| 82 | 'BASE_EXTERNAL_DEPS_UNIX': [], |
| 83 | 'CONDITION_ANDROID': 'CONDITION_ANDROID', |
| 84 | 'CONDITION_IOS': 'CONDITION_IOS', |
benjaminwagner | 6f6bef8 | 2015-10-15 08:09:44 -0700 | [diff] [blame] | 85 | 'DM_EXTERNAL_DEPS': [], |
benjaminwagner | 8a3760f | 2015-10-29 13:40:27 -0700 | [diff] [blame] | 86 | 'EXTERNAL_DEPS_ALL': [], |
benjaminwagner | 2211a7b | 2015-12-01 11:12:05 -0800 | [diff] [blame] | 87 | 'EXTERNAL_INCLUDES': [], |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 88 | } |
| 89 | local_names = {} |
| 90 | execfile('BUILD.public', global_names, local_names) |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 91 | |
mtklein | df5b760 | 2015-08-17 15:02:57 -0700 | [diff] [blame] | 92 | with open('tools/BUILD.public.expected', 'w') as out: |
| 93 | print >>out, "This file is auto-generated by tools/BUILD_simulator.py." |
| 94 | print >>out, "It expands BUILD.public to make it easy to see changes." |
halcanary | bf3dde2 | 2015-08-18 08:35:45 -0700 | [diff] [blame] | 95 | for name, value in sorted(local_names.items()): |
| 96 | print >>out, name, '= ', |
| 97 | pprint.pprint(value, out) |