Esempio n. 1
0
def list():
    """Lists all addresses under the current build root subject to `--spec-excludes` constraints."""
    build_root = get_buildroot()

    options, build_config = OptionsInitializer().setup()
    aliases = build_config.registered_aliases()

    symbol_table = {alias: Target for alias in aliases.target_types}

    object_table = aliases.objects

    def per_path_symbol_factory(path, global_symbols):
        per_path_symbols = {}

        symbols = global_symbols.copy()
        for alias, target_macro_factory in aliases.target_macro_factories.items(
        ):
            for target_type in target_macro_factory.target_types:
                symbols[
                    target_type] = lambda *args, **kwargs: per_path_symbols[
                        alias](*args, **kwargs)

        parse_context = ParseContext(rel_path=os.path.relpath(
            os.path.dirname(path), build_root),
                                     type_aliases=symbols)

        for alias, object_factory in aliases.context_aware_object_factories.items(
        ):
            per_path_symbols[alias] = object_factory(parse_context)

        for alias, target_macro_factory in aliases.target_macro_factories.items(
        ):
            target_macro = target_macro_factory.target_macro(parse_context)
            per_path_symbols[alias] = target_macro
            for target_type in target_macro_factory.target_types:
                per_path_symbols[target_type] = target_macro

        return per_path_symbols

    parser = legacy_python_callbacks_parser(
        symbol_table,
        object_table=object_table,
        per_path_symbol_factory=per_path_symbol_factory)
    mapper = AddressMapper(build_root, parser=parser)

    # Should use build_ignore_patterns instead.
    spec_excludes = None
    for address, obj in mapper.walk_addressables(path_excludes=spec_excludes):
        print(address.spec)
Esempio n. 2
0
def list():
  """Lists all addresses under the current build root subject to `--spec-excludes` constraints."""
  build_root = get_buildroot()

  options, build_config = OptionsInitializer().setup()
  aliases = build_config.registered_aliases()

  symbol_table = {alias: Target for alias in aliases.target_types}

  object_table = aliases.objects

  def per_path_symbol_factory(path, global_symbols):
    per_path_symbols = {}

    symbols = global_symbols.copy()
    for alias, target_macro_factory in aliases.target_macro_factories.items():
      for target_type in target_macro_factory.target_types:
        symbols[target_type] = lambda *args, **kwargs: per_path_symbols[alias](*args, **kwargs)

    parse_context = ParseContext(rel_path=os.path.relpath(os.path.dirname(path), build_root),
                                 type_aliases=symbols)

    for alias, object_factory in aliases.context_aware_object_factories.items():
      per_path_symbols[alias] = object_factory(parse_context)

    for alias, target_macro_factory in aliases.target_macro_factories.items():
      target_macro = target_macro_factory.target_macro(parse_context)
      per_path_symbols[alias] = target_macro
      for target_type in target_macro_factory.target_types:
        per_path_symbols[target_type] = target_macro

    return per_path_symbols

  parser = legacy_python_callbacks_parser(symbol_table,
                                          object_table=object_table,
                                          per_path_symbol_factory=per_path_symbol_factory)
  mapper = AddressMapper(build_root, parser=parser)

  spec_excludes = options.for_global_scope().spec_excludes
  for address, obj in mapper.walk_addressables(path_excludes=spec_excludes):
    print(address.spec)
Esempio n. 3
0
class AddressMapperTest(unittest.TestCase):
    def setUp(self):
        self.work_dir = safe_mkdtemp()
        self.addCleanup(safe_rmtree, self.work_dir)
        self.build_root = os.path.join(self.work_dir, 'build_root')
        shutil.copytree(
            os.path.join(os.path.dirname(__file__), 'examples/mapper_test'),
            self.build_root)

        parser = partial(parse_json,
                         symbol_table={
                             'struct': Struct,
                             'target': Target
                         })
        self.address_mapper = AddressMapper(build_root=self.build_root,
                                            build_pattern=r'.+\.BUILD.json$',
                                            parser=parser)

        self.a_b_target = Target(
            name='b',
            dependencies=['//d:e'],
            configurations=['//a', Struct(embedded='yes')])

    def test_no_family(self):
        with self.assertRaises(ResolveError):
            self.address_mapper.family('a/c')

        # Errors are not cached.
        with self.assertRaises(ResolveError):
            self.address_mapper.family('a/c')

        build_file = os.path.join(self.build_root, 'a/c/c.BUILD.json')
        touch(build_file)
        address_family = self.address_mapper.family('a/c')
        self.assertEqual({}, address_family.addressables)

        # But success is cached.
        self.assertIs(address_family, self.address_mapper.family('a/c'))

    def test_no_address_no_family(self):
        with self.assertRaises(ResolveError):
            self.address_mapper.resolve(Address.parse('a/c'))

        # Errors are not cached.
        with self.assertRaises(ResolveError):
            self.address_mapper.resolve(Address.parse('a/c'))

        build_file = os.path.join(self.build_root, 'a/c/c.BUILD.json')
        with safe_open(build_file, 'w') as fp:
            fp.write('{"type_alias": "struct", "name": "c"}')

        resolved = self.address_mapper.resolve(Address.parse('a/c'))
        self.assertEqual(Struct(name='c'), resolved)

        # But success is cached.
        self.assertIs(resolved,
                      self.address_mapper.resolve(Address.parse('a/c')))

    def test_resolve(self):
        resolved = self.address_mapper.resolve(Address.parse('a/b'))
        self.assertEqual(self.a_b_target, resolved)

    def test_invalidate_build_file_added(self):
        address_family = self.address_mapper.family('a/b')

        self.assertEqual({Address.parse('a/b'): self.a_b_target},
                         address_family.addressables)

        with open(os.path.join(self.build_root, 'a/b/sibling.BUILD.json'),
                  'w') as fp:
            fp.write('{"type_alias": "struct", "name": "c"}')

        still_valid = self.address_mapper.family('a/b')
        self.assertIs(address_family, still_valid)

        self.address_mapper.invalidate_build_file('a/b/sibling.BUILD.json')
        newly_formed = self.address_mapper.family('a/b')
        self.assertIsNot(address_family, newly_formed)
        self.assertEqual(
            {
                Address.parse('a/b'): self.a_b_target,
                Address.parse('a/b:c'): Struct(name='c')
            }, newly_formed.addressables)

    def test_invalidate_build_file_changed(self):
        with self.assertRaises(ResolveError):
            self.address_mapper.resolve(Address.parse('a/b:c'))

        build_file = os.path.join(self.build_root, 'a/b/b.BUILD.json')
        with safe_open(build_file, 'w+') as fp:
            fp.write('{"type_alias": "struct", "name": "c"}')

        with self.assertRaises(ResolveError):
            self.address_mapper.resolve(Address.parse('a/b:c'))

        self.address_mapper.invalidate_build_file('a/b/b.BUILD.json')
        resolved = self.address_mapper.resolve(Address.parse('a/b:c'))
        self.assertEqual(Struct(name='c'), resolved)

        # But success is cached.
        self.assertIs(resolved,
                      self.address_mapper.resolve(Address.parse('a/b:c')))

    def test_invalidate_build_file_removed(self):
        resolved = self.address_mapper.resolve(Address.parse('a/b'))
        self.assertEqual(self.a_b_target, resolved)

        build_file = os.path.join(self.build_root, 'a/b/b.BUILD.json')
        os.unlink(build_file)
        self.assertIs(resolved,
                      self.address_mapper.resolve(Address.parse('a/b')))

        self.address_mapper.invalidate_build_file(build_file)
        with self.assertRaises(ResolveError):
            self.address_mapper.resolve(Address.parse('a/b'))

    def test_invalidation_un_normalized(self):
        resolved = self.address_mapper.resolve(Address.parse('a/b'))
        self.assertEqual(self.a_b_target, resolved)

        os.unlink(os.path.join(self.build_root, 'a/b/b.BUILD.json'))
        self.assertIs(resolved,
                      self.address_mapper.resolve(Address.parse('a/b')))

        un_normalized_build_root = os.path.join(self.work_dir,
                                                'build_root_linked')
        os.symlink(self.build_root, un_normalized_build_root)
        un_normalized_build_file = os.path.join(un_normalized_build_root,
                                                'a/b/b.BUILD.json')
        self.address_mapper.invalidate_build_file(un_normalized_build_file)
        with self.assertRaises(ResolveError):
            self.address_mapper.resolve(Address.parse('a/b'))

    def test_invalidation_relative(self):
        resolved = self.address_mapper.resolve(Address.parse('a/b'))
        self.assertEqual(self.a_b_target, resolved)

        build_file = os.path.join(self.build_root, 'a/b/b.BUILD.json')
        os.unlink(build_file)
        self.assertIs(resolved,
                      self.address_mapper.resolve(Address.parse('a/b')))

        self.address_mapper.invalidate_build_file('a/b/b.BUILD.json')
        with self.assertRaises(ResolveError):
            self.address_mapper.resolve(Address.parse('a/b'))

    @staticmethod
    def addr(spec):
        return Address.parse(spec)

    def test_walk_addressables(self):
        self.assertEqual(
            sorted([(self.addr('//:root'), Struct(name='root')),
                    (self.addr('a/b:b'), self.a_b_target),
                    (self.addr('a/d:d'), Target(name='d')),
                    (self.addr('a/d/e:e'), Target(name='e')),
                    (self.addr('a/d/e:e-prime'), Struct(name='e-prime'))]),
            sorted(self.address_mapper.walk_addressables()))

    def test_walk_addressables_rel_path(self):
        self.assertEqual(
            sorted([(self.addr('a/d:d'), Target(name='d')),
                    (self.addr('a/d/e:e'), Target(name='e')),
                    (self.addr('a/d/e:e-prime'), Struct(name='e-prime'))]),
            sorted(self.address_mapper.walk_addressables(rel_path='a/d')))

    def test_walk_addressables_path_excludes(self):
        self.assertEqual([(self.addr('//:root'), Struct(name='root')),
                          (self.addr('a/d:d'), Target(name='d'))],
                         list(
                             self.address_mapper.walk_addressables(
                                 path_excludes=['a/b', 'a/d/e'])))
Esempio n. 4
0
class AddressMapperTest(unittest.TestCase):
    def setUp(self):
        self.work_dir = safe_mkdtemp()
        self.addCleanup(safe_rmtree, self.work_dir)
        self.build_root = os.path.join(self.work_dir, 'build_root')
        shutil.copytree(
            os.path.join(os.path.dirname(__file__), 'examples/mapper_test'),
            self.build_root)

        self.address_mapper = AddressMapper(build_root=self.build_root,
                                            symbol_table_cls=TargetTable,
                                            parser_cls=JsonParser,
                                            build_pattern=r'.+\.BUILD.json$')

        self.a_b_target = Target(
            name='b',
            dependencies=['//d:e'],
            configurations=['//a', Struct(embedded='yes')])

    def test_no_family(self):
        with self.assertRaises(ResolveError):
            self.address_mapper.family('a/c')

        # Errors are not cached.
        with self.assertRaises(ResolveError):
            self.address_mapper.family('a/c')

        build_file = os.path.join(self.build_root, 'a/c/c.BUILD.json')
        touch(build_file)
        address_family = self.address_mapper.family('a/c')
        self.assertEqual({}, address_family.addressables)

    def test_no_address_no_family(self):
        with self.assertRaises(ResolveError):
            self.address_mapper.resolve(Address.parse('a/c'))

        # Errors are not cached.
        with self.assertRaises(ResolveError):
            self.address_mapper.resolve(Address.parse('a/c'))

        build_file = os.path.join(self.build_root, 'a/c/c.BUILD.json')
        with safe_open(build_file, 'w') as fp:
            fp.write('{"type_alias": "struct", "name": "c"}')

        resolved = self.address_mapper.resolve(Address.parse('a/c'))
        self.assertEqual(Struct(name='c'), resolved)

    def test_resolve(self):
        resolved = self.address_mapper.resolve(Address.parse('a/b'))
        self.assertEqual(self.a_b_target, resolved)

    @staticmethod
    def addr(spec):
        return Address.parse(spec)

    def test_walk_addressables(self):
        self.assertEqual(
            sorted([(self.addr('//:root'), Struct(name='root')),
                    (self.addr('a/b:b'), self.a_b_target),
                    (self.addr('a/d:d'), Target(name='d')),
                    (self.addr('a/d/e:e'), Target(name='e')),
                    (self.addr('a/d/e:e-prime'), Struct(name='e-prime'))]),
            sorted(self.address_mapper.walk_addressables()))

    def test_walk_addressables_rel_path(self):
        self.assertEqual(
            sorted([(self.addr('a/d:d'), Target(name='d')),
                    (self.addr('a/d/e:e'), Target(name='e')),
                    (self.addr('a/d/e:e-prime'), Struct(name='e-prime'))]),
            sorted(self.address_mapper.walk_addressables(rel_path='a/d')))

    def test_walk_addressables_path_excludes(self):
        self.assertEqual([(self.addr('//:root'), Struct(name='root')),
                          (self.addr('a/d:d'), Target(name='d'))],
                         list(
                             self.address_mapper.walk_addressables(
                                 path_excludes=['a/b', 'a/d/e'])))
Esempio n. 5
0
class AddressMapperTest(unittest.TestCase):
  def setUp(self):
    self.work_dir = safe_mkdtemp()
    self.addCleanup(safe_rmtree, self.work_dir)
    self.build_root = os.path.join(self.work_dir, 'build_root')
    shutil.copytree(os.path.join(os.path.dirname(__file__), 'examples/mapper_test'),
                    self.build_root)

    parser = partial(parse_json, symbol_table={'configuration': Configuration, 'target': Target})
    self.address_mapper = AddressMapper(build_root=self.build_root,
                                        build_pattern=r'.+\.BUILD.json$',
                                        parser=parser)

    self.a_b_target = Target(name='b',
                             dependencies=['//d:e'],
                             configurations=['//a', Configuration(embedded='yes')])

  def test_no_family(self):
    with self.assertRaises(ResolveError):
      self.address_mapper.family('a/c')

    # Errors are not cached.
    with self.assertRaises(ResolveError):
      self.address_mapper.family('a/c')

    build_file = os.path.join(self.build_root, 'a/c/c.BUILD.json')
    touch(build_file)
    address_family = self.address_mapper.family('a/c')
    self.assertEqual({}, address_family.addressables)

    # But success is cached.
    self.assertIs(address_family, self.address_mapper.family('a/c'))

  def test_no_address_no_family(self):
    with self.assertRaises(ResolveError):
      self.address_mapper.resolve(Address.parse('a/c'))

    # Errors are not cached.
    with self.assertRaises(ResolveError):
      self.address_mapper.resolve(Address.parse('a/c'))

    build_file = os.path.join(self.build_root, 'a/c/c.BUILD.json')
    with safe_open(build_file, 'w') as fp:
      fp.write('{"type_alias": "configuration", "name": "c"}')

    resolved = self.address_mapper.resolve(Address.parse('a/c'))
    self.assertEqual(Configuration(name='c'), resolved)

    # But success is cached.
    self.assertIs(resolved, self.address_mapper.resolve(Address.parse('a/c')))

  def test_resolve(self):
    resolved = self.address_mapper.resolve(Address.parse('a/b'))
    self.assertEqual(self.a_b_target, resolved)

  def test_invalidate_build_file_added(self):
    address_family = self.address_mapper.family('a/b')

    self.assertEqual({Address.parse('a/b'): self.a_b_target},
                     address_family.addressables)

    with open(os.path.join(self.build_root, 'a/b/sibling.BUILD.json'), 'w') as fp:
      fp.write('{"type_alias": "configuration", "name": "c"}')

    still_valid = self.address_mapper.family('a/b')
    self.assertIs(address_family, still_valid)

    self.address_mapper.invalidate_build_file('a/b/sibling.BUILD.json')
    newly_formed = self.address_mapper.family('a/b')
    self.assertIsNot(address_family, newly_formed)
    self.assertEqual({Address.parse('a/b'): self.a_b_target,
                      Address.parse('a/b:c'): Configuration(name='c')},
                     newly_formed.addressables)

  def test_invalidate_build_file_changed(self):
    with self.assertRaises(ResolveError):
      self.address_mapper.resolve(Address.parse('a/b:c'))

    build_file = os.path.join(self.build_root, 'a/b/b.BUILD.json')
    with safe_open(build_file, 'w+') as fp:
      fp.write('{"type_alias": "configuration", "name": "c"}')

    with self.assertRaises(ResolveError):
      self.address_mapper.resolve(Address.parse('a/b:c'))

    self.address_mapper.invalidate_build_file('a/b/b.BUILD.json')
    resolved = self.address_mapper.resolve(Address.parse('a/b:c'))
    self.assertEqual(Configuration(name='c'), resolved)

    # But success is cached.
    self.assertIs(resolved, self.address_mapper.resolve(Address.parse('a/b:c')))

  def test_invalidate_build_file_removed(self):
    resolved = self.address_mapper.resolve(Address.parse('a/b'))
    self.assertEqual(self.a_b_target, resolved)

    build_file = os.path.join(self.build_root, 'a/b/b.BUILD.json')
    os.unlink(build_file)
    self.assertIs(resolved, self.address_mapper.resolve(Address.parse('a/b')))

    self.address_mapper.invalidate_build_file(build_file)
    with self.assertRaises(ResolveError):
      self.address_mapper.resolve(Address.parse('a/b'))

  def test_invalidation_un_normalized(self):
    resolved = self.address_mapper.resolve(Address.parse('a/b'))
    self.assertEqual(self.a_b_target, resolved)

    os.unlink(os.path.join(self.build_root, 'a/b/b.BUILD.json'))
    self.assertIs(resolved, self.address_mapper.resolve(Address.parse('a/b')))

    un_normalized_build_root = os.path.join(self.work_dir, 'build_root_linked')
    os.symlink(self.build_root, un_normalized_build_root)
    un_normalized_build_file = os.path.join(un_normalized_build_root, 'a/b/b.BUILD.json')
    self.address_mapper.invalidate_build_file(un_normalized_build_file)
    with self.assertRaises(ResolveError):
      self.address_mapper.resolve(Address.parse('a/b'))

  def test_invalidation_relative(self):
    resolved = self.address_mapper.resolve(Address.parse('a/b'))
    self.assertEqual(self.a_b_target, resolved)

    build_file = os.path.join(self.build_root, 'a/b/b.BUILD.json')
    os.unlink(build_file)
    self.assertIs(resolved, self.address_mapper.resolve(Address.parse('a/b')))

    self.address_mapper.invalidate_build_file('a/b/b.BUILD.json')
    with self.assertRaises(ResolveError):
      self.address_mapper.resolve(Address.parse('a/b'))

  @staticmethod
  def addr(spec):
    return Address.parse(spec)

  def test_walk_addressables(self):
    self.assertEqual(sorted([(self.addr('//:root'), Configuration(name='root')),
                             (self.addr('a/b:b'), self.a_b_target),
                             (self.addr('a/d:d'), Target(name='d')),
                             (self.addr('a/d/e:e'), Target(name='e')),
                             (self.addr('a/d/e:e-prime'), Configuration(name='e-prime'))]),
                     sorted(self.address_mapper.walk_addressables()))

  def test_walk_addressables_rel_path(self):
    self.assertEqual(sorted([(self.addr('a/d:d'), Target(name='d')),
                             (self.addr('a/d/e:e'), Target(name='e')),
                             (self.addr('a/d/e:e-prime'), Configuration(name='e-prime'))]),
                     sorted(self.address_mapper.walk_addressables(rel_path='a/d')))

  def test_walk_addressables_path_excludes(self):
    self.assertEqual([(self.addr('//:root'), Configuration(name='root')),
                      (self.addr('a/d:d'), Target(name='d'))],
                     list(self.address_mapper.walk_addressables(path_excludes=['a/b', 'a/d/e'])))
Esempio n. 6
0
class AddressMapperTest(unittest.TestCase):
  def setUp(self):
    self.work_dir = safe_mkdtemp()
    self.addCleanup(safe_rmtree, self.work_dir)
    self.build_root = os.path.join(self.work_dir, 'build_root')
    shutil.copytree(os.path.join(os.path.dirname(__file__), 'examples/mapper_test'),
                    self.build_root)

    self.address_mapper = AddressMapper(build_root=self.build_root,
                                        symbol_table_cls=TargetTable,
                                        parser_cls=JsonParser,
                                        build_pattern=r'.+\.BUILD.json$')

    self.a_b_target = Target(name='b',
                             dependencies=['//d:e'],
                             configurations=['//a', Struct(embedded='yes')])

  def test_no_family(self):
    with self.assertRaises(ResolveError):
      self.address_mapper.family('a/c')

    # Errors are not cached.
    with self.assertRaises(ResolveError):
      self.address_mapper.family('a/c')

    build_file = os.path.join(self.build_root, 'a/c/c.BUILD.json')
    touch(build_file)
    address_family = self.address_mapper.family('a/c')
    self.assertEqual({}, address_family.addressables)

  def test_no_address_no_family(self):
    with self.assertRaises(ResolveError):
      self.address_mapper.resolve(Address.parse('a/c'))

    # Errors are not cached.
    with self.assertRaises(ResolveError):
      self.address_mapper.resolve(Address.parse('a/c'))

    build_file = os.path.join(self.build_root, 'a/c/c.BUILD.json')
    with safe_open(build_file, 'w') as fp:
      fp.write('{"type_alias": "struct", "name": "c"}')

    resolved = self.address_mapper.resolve(Address.parse('a/c'))
    self.assertEqual(Struct(name='c'), resolved)

  def test_resolve(self):
    resolved = self.address_mapper.resolve(Address.parse('a/b'))
    self.assertEqual(self.a_b_target, resolved)

  @staticmethod
  def addr(spec):
    return Address.parse(spec)

  def test_walk_addressables(self):
    self.assertEqual(sorted([(self.addr('//:root'), Struct(name='root')),
                             (self.addr('a/b:b'), self.a_b_target),
                             (self.addr('a/d:d'), Target(name='d')),
                             (self.addr('a/d/e:e'), Target(name='e')),
                             (self.addr('a/d/e:e-prime'), Struct(name='e-prime'))]),
                     sorted(self.address_mapper.walk_addressables()))

  def test_walk_addressables_rel_path(self):
    self.assertEqual(sorted([(self.addr('a/d:d'), Target(name='d')),
                             (self.addr('a/d/e:e'), Target(name='e')),
                             (self.addr('a/d/e:e-prime'), Struct(name='e-prime'))]),
                     sorted(self.address_mapper.walk_addressables(rel_path='a/d')))

  def test_walk_addressables_path_excludes(self):
    self.assertEqual([(self.addr('//:root'), Struct(name='root')),
                      (self.addr('a/d:d'), Target(name='d'))],
                     list(self.address_mapper.walk_addressables(path_excludes=['a/b', 'a/d/e'])))