Ejemplo n.º 1
0
  def test_target_invalid(self):
    self.add_to_build_file('a/BUILD', 'target(name="a")')
    with self.assertRaises(AddressLookupError):
      self.build_graph.inject_address_closure(SyntheticAddress.parse('a:nope'))

    self.add_to_build_file('b/BUILD', 'target(name="a")')
    with self.assertRaises(AddressLookupError):
      self.build_graph.inject_address_closure(SyntheticAddress.parse('b'))
    with self.assertRaises(AddressLookupError):
      self.build_graph.inject_address_closure(SyntheticAddress.parse('b:b'))
Ejemplo n.º 2
0
  def _synthesize_resources_target(self):
    # Create an address for the synthetic target.
    spec = self.address.spec + '_synthetic_resources'
    synthetic_address = SyntheticAddress.parse(spec=spec)
    # For safety, ensure an address that's not used already, even though that's highly unlikely.
    while self._build_graph.contains_address(synthetic_address):
      spec += '_'
      synthetic_address = SyntheticAddress.parse(spec=spec)

    self._build_graph.inject_synthetic_target(synthetic_address, Resources,
                                              sources=self.payload.resources.source_paths,
                                              derived_from=self)
    return self._build_graph.get_target(synthetic_address)
Ejemplo n.º 3
0
 def test_inject_then_inject_closure(self):
   self.add_to_build_file('BUILD',
                          'target(name="a", '
                          '  dependencies=['
                          '    "other:b",'
                          '])')
   self.add_to_build_file('other/BUILD',
                          'target(name="b")')
   self.build_graph.inject_address(SyntheticAddress.parse('//:a'))
   self.build_graph.inject_address_closure(SyntheticAddress.parse('//:a'))
   a = self.build_graph.get_target_from_spec('//:a')
   b = self.build_graph.get_target_from_spec('//other:b')
   self.assertIn(b, a.dependencies)
Ejemplo n.º 4
0
 def java_sources(self):
   for spec in self._java_sources_specs:
     address = SyntheticAddress.parse(spec, relative_to=self.address.spec_path)
     target = self._build_graph.get_target(address)
     if target is None:
       raise TargetDefinitionException(self, 'No such java target: %s' % spec)
     yield target
Ejemplo n.º 5
0
  def imports(self):
    """Returns the set of JarDependencys to be included when compiling this target."""
    if self._imports is None:
      import_jars = set()
      for spec in self.payload.raw_imports:
        if not isinstance(spec, six.string_types):
          raise self.ExpectedAddressError(
            "{address}: expected imports to contain string addresses, got {found_class}."
            .format(address=self.address.spec,
                    found_class=spec.__class__.__name__))
        address = SyntheticAddress.parse(spec, relative_to=self.address.spec_path)
        target = self._build_graph.get_target(address)
        if isinstance(target, JarLibrary):
          import_jars.update(target.jar_dependencies)
        elif target is None:
          # TODO(pl, zundel): Not sure if we can ever reach this case. An address that
          # can't be resolved is caught when resolving the build graph.
          raise self.PrematureImportPokeError(
            "Internal Error: {address}: Failed to resolve import '{spec}'".format(
              address=self.address.spec,
              spec=address.spec))
        else:
          raise self.WrongTargetTypeError(
            "{address}: expected {spec} to be jar_library target type, got {found_class}"
            .format(address=self.address.spec,
                    spec=address.spec,
                    found_class=target.__class__.__name__))

      self._imports = list(import_jars)
    return self._imports
Ejemplo n.º 6
0
  def __init__(self,
               name,
               build_file,
               build_file_source_lines,
               target_source_lines,
               target_interval,
               dependencies,
               dependencies_interval):
    """See BuildFileManipulator.load() for how to construct one as a user."""
    self.name = name
    self.build_file = build_file
    self.target_address = BuildFileAddress(build_file, name)
    self._build_file_source_lines = build_file_source_lines
    self._target_source_lines = target_source_lines
    self._target_interval = target_interval
    self._dependencies_interval = dependencies_interval
    self._dependencies_by_address = {}

    for dep in dependencies:
      dep_address = SyntheticAddress.parse(dep.spec, relative_to=build_file.spec_path)
      if dep_address in self._dependencies_by_address:
        raise BuildTargetParseError('The address {dep_address} occurred multiple times in the '
                                    'dependency specs for target {name} in {build_file}. '
                                    .format(dep_address=dep_address.spec,
                                            name=name,
                                            build_file=build_file))
      self._dependencies_by_address[dep_address] = dep
Ejemplo n.º 7
0
 def test_jar_dependencies(self):
   jar1 = JarDependency(org='testOrg1', name='testName1', rev='123')
   jar2 = JarDependency(org='testOrg2', name='testName2', rev='456')
   lib = JarLibrary(name='foo', address=SyntheticAddress.parse('//:foo'),
                    build_graph=self.build_graph,
                    jars=[jar1, jar2])
   self.assertEquals((jar1, jar2), lib.jar_dependencies)
  def test_register_bad_target_alias(self):
    with self.assertRaises(TypeError):
      self.build_configuration.register_target_alias('fred', object())

    target = Target('fred', SyntheticAddress.parse('a:b'), BuildGraph(address_mapper=None))
    with self.assertRaises(TypeError):
      self.build_configuration.register_target_alias('fred', target)
Ejemplo n.º 9
0
  def create_sources_field(self, sources, sources_rel_path, address=None):
    """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!
    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """

    if isinstance(sources, Addresses):
      # Currently, this is only created by the result of from_target() which takes a single argument
      if len(sources.addresses) != 1:
        raise self.WrongNumberOfAddresses(
          "Expected a single address to from_target() as argument to {spec}"
          .format(spec=address.spec))
      referenced_address = SyntheticAddress.parse(sources.addresses[0],
                                                  relative_to=sources.rel_path)
      return DeferredSourcesField(ref_address=referenced_address)
    elif isinstance(sources, FilesetWithSpec):
      filespec = sources.filespec
    else:
      sources = sources or []
      assert_list(sources)
      filespec = {'globs' : [os.path.join(sources_rel_path, src) for src in (sources or [])]}

    return SourcesField(sources=sources, sources_rel_path=sources_rel_path, filespec=filespec)
Ejemplo n.º 10
0
  def test_ragel_gen(self):
    self.create_file(relpath='test_ragel_gen/atoi.rl', contents=ragel_file_contents)
    self.add_to_build_file('test_ragel_gen', dedent("""
      java_ragel_library(name='atoi',
        sources=['atoi.rl'],
        dependencies=[]
      )
    """))

    task = prepare_task(RagelGen,
                        build_graph=self.build_graph,
                        targets=[self.target('test_ragel_gen:atoi')],
                        build_file_parser=self.build_file_parser)

    task._ragel_binary = 'ragel'
    task.invalidate_for_files = lambda: []
    task._java_out = self.task_outdir

    sources = [os.path.join(self.task_outdir, 'com/example/atoi/Parser.java')]

    try:
      saved_add_new_target = Context.add_new_target
      Context.add_new_target = MagicMock()
      task.execute()
      relative_task_outdir = os.path.relpath(self.task_outdir, get_buildroot())
      spec = '{spec_path}:{name}'.format(spec_path=relative_task_outdir, name='test_ragel_gen.atoi')
      address = SyntheticAddress.parse(spec=spec)
      Context.add_new_target.assert_called_once_with(address,
                                                     JavaRagelLibrary,
                                                     sources=sources,
                                                     excludes=OrderedSet(),
                                                     dependencies=OrderedSet(),
                                                     provides=None)
    finally:
      Context.add_new_target = saved_add_new_target
  def test_target_insertion_middle(self):
    expected_build_string = dedent(
      """\
      # This comment should stay
      target_type(
        name = 'target_top',
        dependencies = [
          ':dep_a',
        ]
      )



      target_type(
        name = 'target_middle',
        dependencies = [
          ':dep_b',
          ':new_dep',
        ],
      )
      # This comment should be okay
      target_type(
        name = 'target_bottom',
      )
      # Also this one though it's weird"""
    )

    build_file = self.add_to_build_file('BUILD', self.multi_target_build_string)

    multi_targ_bfm = BuildFileManipulator.load(build_file, 'target_middle', set(['target_type']))
    multi_targ_bfm.add_dependency(SyntheticAddress.parse(':new_dep'))
    build_file_str = '\n'.join(multi_targ_bfm.build_file_lines())
    self.assertEqual(build_file_str, expected_build_string)
Ejemplo n.º 12
0
 def resolve_spec(self, spec):
   """Converts a spec to an address and maps it using `resolve`"""
   try:
     address = SyntheticAddress.parse(spec)
   except ValueError as e:
     raise self.InvalidAddressError(e)
   return self.resolve(address)
Ejemplo n.º 13
0
 def traversable_specs(self):
   for spec in super(PythonTarget, self).traversable_specs:
     yield spec
   if self._provides:
     for spec in self._provides._binaries.values():
       address = SyntheticAddress.parse(spec, relative_to=self.address.spec_path)
       yield address.spec
Ejemplo n.º 14
0
  def test_transitive_closure_spec(self):
    with self.workspace('./BUILD', 'a/BUILD', 'a/b/BUILD') as root_dir:
      with open(os.path.join(root_dir, './BUILD'), 'w') as build:
        build.write(dedent('''
          fake(name="foo",
               dependencies=[
                 'a',
               ])
        '''))

      with open(os.path.join(root_dir, 'a/BUILD'), 'w') as build:
        build.write(dedent('''
          fake(name="a",
               dependencies=[
                 'a/b:bat',
               ])
        '''))

      with open(os.path.join(root_dir, 'a/b/BUILD'), 'w') as build:
        build.write(dedent('''
          fake(name="bat")
        '''))

      build_configuration = BuildConfiguration()
      build_configuration.register_target_alias('fake', Target)
      parser = BuildFileParser(build_configuration, root_dir=root_dir)
      build_graph = BuildGraph(self.address_mapper)
      parser.inject_spec_closure_into_build_graph(':foo', build_graph)
      self.assertEqual(len(build_graph.dependencies_of(SyntheticAddress.parse(':foo'))), 1)
Ejemplo n.º 15
0
  def to_jar_dependencies(self, jar_library_specs):
    """Convenience method to resolve a list of specs to JarLibraries and return its jars attributes.

    Expects that the jar_libraries are declared relative to this target.

    :param Address relative_to: Address that references library_specs, for error messages
    :param library_specs: string specs to JavaLibrary targets. Note, this list should be returned
      by the caller's traversable_specs() implementation to make sure that the jar_dependency jars
      have been added to the build graph.
    :param build_graph: build graph instance used to search for specs
    :return: list of JarDependency instances represented by the library_specs
    """
    jar_deps = set()
    for spec in jar_library_specs:
      if not isinstance(spec, six.string_types):
        raise self.ExpectedAddressError(
          "{address}: expected imports to contain string addresses, got {found_class}."
          .format(address=self.address.spec,
                  found_class=type(spec).__name__))
      address = SyntheticAddress.parse(spec, relative_to=self.address.spec_path)
      target = self._build_graph.get_target(address)
      if isinstance(target, JarLibrary):
        jar_deps.update(target.jar_dependencies)
      else:
        raise self.WrongTargetTypeError(
          "{address}: expected {spec} to be jar_library target type, got {found_class}"
          .format(address=self.address.spec,
                  spec=address.spec,
                  found_class=type(target).__name__))
    return list(jar_deps)
Ejemplo n.º 16
0
 def test_synthetic_forms(self):
   self.assert_address('a/b', 'target', SyntheticAddress.parse('a/b:target'))
   self.assert_address('a/b', 'target', SyntheticAddress.parse('//a/b:target'))
   self.assert_address('a/b', 'b', SyntheticAddress.parse('a/b'))
   self.assert_address('a/b', 'b', SyntheticAddress.parse('//a/b'))
   self.assert_address('a/b', 'target', SyntheticAddress.parse(':target', relative_to='a/b'))
   self.assert_address('', 'target', SyntheticAddress.parse('//:target', relative_to='a/b'))
   self.assert_address('', 'target', SyntheticAddress.parse(':target'))
   self.assert_address('a/b', 'target', SyntheticAddress.parse(':target', relative_to='a/b'))
  def test_simple_targets(self):
    simple_targets = dedent(
      """
      target_type(
        name = 'no_deps',
      )

      target_type(
        name = 'empty_deps',
        dependencies = [
        ]
      )

      target_type(
        name = 'empty_deps_inline',
        dependencies = []
      )
      """
    )

    build_file = self.add_to_build_file('BUILD', simple_targets)

    for no_deps_name in ['no_deps', 'empty_deps', 'empty_deps_inline']:
      no_deps = BuildFileManipulator.load(build_file, no_deps_name, set(['target_type']))
      self.assertEqual(tuple(no_deps.dependency_lines()), tuple())
      no_deps.add_dependency(SyntheticAddress.parse(':fake_dep'))
      self.assertEqual(tuple(no_deps.dependency_lines()),
                       tuple(['  dependencies = [',
                              "    ':fake_dep',",
                              '  ],']))
      no_deps.add_dependency(SyntheticAddress.parse(':b_fake_dep'))
      no_deps.add_dependency(SyntheticAddress.parse(':a_fake_dep'))
      self.assertEqual(tuple(no_deps.dependency_lines()),
                       tuple(['  dependencies = [',
                              "    ':a_fake_dep',",
                              "    ':b_fake_dep',",
                              "    ':fake_dep',",
                              '  ],']))
      self.assertEqual(tuple(no_deps.target_lines()),
                       tuple(['target_type(',
                              "  name = '{0}',".format(no_deps_name),
                              '  dependencies = [',
                              "    ':a_fake_dep',",
                              "    ':b_fake_dep',",
                              "    ':fake_dep',",
                              '  ],',
                              ')']))
Ejemplo n.º 18
0
 def test_contains_address(self):
   a = SyntheticAddress.parse('a')
   self.assertFalse(self.build_graph.contains_address(a))
   target = Target(name='a',
                   address=a,
                   build_graph=self.build_graph)
   self.build_graph.inject_target(target)
   self.assertTrue(self.build_graph.contains_address(a))
Ejemplo n.º 19
0
  def test_targets_synthetic(self):
    a = self.make_target('a')
    b = self.make_target('b', dependencies=[a])
    c = self.make_target('c', dependencies=[b])
    d = self.make_target('d', dependencies=[c, a])
    context = self.context(target_roots=[c])
    self.assertEquals([c, b, a], context.targets())

    syn_b = context.add_new_target(SyntheticAddress.parse('syn_b'), Target, derived_from=b)
    context.add_new_target(SyntheticAddress.parse('syn_d'), Target, derived_from=d)
    # We expect syn_b to be included now since it has been synthesized during this run from an
    # in-play target.
    self.assertEquals([c, b, a, syn_b], context.targets())

    # And verify the predicate operates over both normal and synthetic targets.
    self.assertEquals([syn_b], context.targets(lambda t: t.derived_from != t))
    self.assertEquals([c, b, a], context.targets(lambda t: t.derived_from == t))
Ejemplo n.º 20
0
    def assert_parsed_list(self, cmdline_spec_list, expected):
        def sort(addresses):
            return sorted(addresses, key=lambda address: address.spec)

        self.assertEqual(
            sort(SyntheticAddress.parse(addr) for addr in expected),
            sort(self.spec_parser.parse_addresses(cmdline_spec_list)),
        )
Ejemplo n.º 21
0
Archivo: filter.py Proyecto: aoen/pants
def _get_target(spec, build_graph):
  try:
    address = SyntheticAddress.parse(spec)
  except IOError as e:
    raise TaskError('Failed to parse address: %s: %s' % (address, e))
  match = build_graph.get_target(address)
  if not match:
    raise TaskError('Invalid target address: %s' % address)
  return match
Ejemplo n.º 22
0
 def create_target(files, deps, target_type):
   spec = '{spec_path}:{name}'.format(spec_path=outdir, name=gentarget.id)
   address = SyntheticAddress.parse(spec=spec)
   return self.context.add_new_target(address,
                                      target_type,
                                      sources=files,
                                      provides=gentarget.provides,
                                      dependencies=deps,
                                      excludes=gentarget.excludes)
Ejemplo n.º 23
0
 def create_target(files, deps):
   spec_path = os.path.join(self.combined_relpath, 'gen-py')
   spec = '{spec_path}:{name}'.format(spec_path=spec_path, name=target.id)
   address = SyntheticAddress.parse(spec=spec)
   return self.context.add_new_target(address,
                                      PythonLibrary,
                                      derived_from=target,
                                      sources=files,
                                      dependencies=deps)
Ejemplo n.º 24
0
    def test_smoke(self):
        contents = dedent(
            """namespace java com.pants.example
      struct Example {
      1: optional i64 number
      }
    """
        )

        self.create_file(relpath="test_smoke/a.thrift", contents=contents)
        self.add_to_build_file(
            "test_smoke",
            dedent(
                """
      java_thrift_library(name='a',
        sources=['a.thrift'],
        dependencies=[],
        compiler='scrooge',
        language='scala',
        rpc_style='finagle'
      )
    """
            ),
        )

        target = self.target("test_smoke:a")
        task = prepare_task(
            ScroogeGen, build_graph=self.build_graph, targets=[target], build_file_parser=self.build_file_parser
        )

        with patch("pants.backend.codegen.tasks.scrooge_gen.calculate_services"):
            task._outdir = MagicMock()
            task._outdir.return_value = self.task_outdir

            task.gen = MagicMock()
            sources = [os.path.join(self.task_outdir, "com/pants/example/Example.scala")]
            task.gen.return_value = {"test_smoke/a.thrift": sources}

            try:
                saved_add_new_target = Context.add_new_target
                Context.add_new_target = MagicMock()
                task.execute()
                relative_task_outdir = os.path.relpath(self.task_outdir, get_buildroot())
                spec = "{spec_path}:{name}".format(spec_path=relative_task_outdir, name="test_smoke.a")
                address = SyntheticAddress.parse(spec=spec)
                Context.add_new_target.assert_called_once_with(
                    address,
                    ScalaLibrary,
                    sources=sources,
                    excludes=OrderedSet(),
                    dependencies=OrderedSet(),
                    provides=None,
                    derived_from=target,
                )
            finally:
                Context.add_new_target = saved_add_new_target
Ejemplo n.º 25
0
  def provides(self):
    if not self._provides:
      return None

    # TODO(pl): This is an awful hack
    for key, binary in self._provides._binaries.iteritems():
      if isinstance(binary, Compatibility.string):
        address = SyntheticAddress.parse(binary, relative_to=self.address.spec_path)
        self._provides._binaries[key] = self._build_graph.get_target(address)
    return self._provides
Ejemplo n.º 26
0
    def make_target(self, spec="", target_type=Target, dependencies=None, resources=None, derived_from=None, **kwargs):
        address = SyntheticAddress.parse(spec)
        target = target_type(name=address.target_name, address=address, build_graph=self.build_graph, **kwargs)
        dependencies = dependencies or []
        dependencies.extend(resources or [])

        self.build_graph.inject_target(
            target, dependencies=[dep.address for dep in dependencies], derived_from=derived_from
        )
        return target
Ejemplo n.º 27
0
  def target(self, spec):
    """Resolves the given target address to a Target object.

    address: The BUILD target address to resolve.

    Returns the corresponding Target or else None if the address does not point to a defined Target.
    """
    address = SyntheticAddress.parse(spec)
    self.build_graph.inject_address_closure(address)
    return self.build_graph.get_target(address)
Ejemplo n.º 28
0
 def setUp(self):
   super(JavaWireLibraryTest, self).setUp()
   self.build_file_parser._build_configuration.register_target_alias('java_wire_library', JavaWireLibrary)
   self.add_to_build_file('BUILD', dedent('''
     java_wire_library(name='foo',
       sources=[],
       service_writer='com.squareup.wire.RetrofitServiceWriter'
     )'''))
   self.build_graph.inject_spec_closure('//:foo')
   self.target = self.build_graph.get_target(SyntheticAddress.parse('//:foo'))
Ejemplo n.º 29
0
    def provides(self):
        if not self.payload.provides:
            return None

        # TODO(pl): This is an awful hack
        if isinstance(self.payload.provides.repo, Compatibility.string):
            address = SyntheticAddress.parse(
                self.payload.provides.repo, relative_to=self.address.spec_path)
            repo_target = self._build_graph.get_target(address)
            self.payload.provides.repo = repo_target
        return self.payload.provides
Ejemplo n.º 30
0
 def create_target(files, deps, target_type):
     spec = '{spec_path}:{name}'.format(spec_path=outdir,
                                        name=gentarget.id)
     address = SyntheticAddress.parse(spec=spec)
     return self.context.add_new_target(address,
                                        target_type,
                                        sources=files,
                                        provides=gentarget.provides,
                                        dependencies=deps,
                                        excludes=gentarget.excludes,
                                        derived_from=gentarget)
Ejemplo n.º 31
0
  def provides(self):
    if not self.payload.provides:
      return None

    # TODO(pl): This is an awful hack
    if isinstance(self.payload.provides.repo, Compatibility.string):
      address = SyntheticAddress.parse(self.payload.provides.repo,
                                       relative_to=self.address.spec_path)
      repo_target = self._build_graph.get_target(address)
      self.payload.provides.repo = repo_target
    return self.payload.provides
Ejemplo n.º 32
0
 def create_target(files, deps):
   spec_path = os.path.join(self.combined_relpath, 'gen-java')
   spec = '{spec_path}:{name}'.format(spec_path=spec_path, name=target.id)
   address = SyntheticAddress.parse(spec=spec)
   return self.context.add_new_target(address,
                                      JavaLibrary,
                                      derived_from=target,
                                      sources=files,
                                      provides=target.provides,
                                      dependencies=deps,
                                      excludes=target.payload.excludes)
Ejemplo n.º 33
0
 def create_target(files, deps):
   spec_path = os.path.join(self.combined_relpath, 'gen-java')
   spec = '{spec_path}:{name}'.format(spec_path=spec_path, name=target.id)
   address = SyntheticAddress.parse(spec=spec)
   return self.context.add_new_target(address,
                                      JavaLibrary,
                                      derived_from=target,
                                      sources=files,
                                      provides=target.provides,
                                      dependencies=deps,
                                      excludes=target.payload.get_field_value('excludes'))
Ejemplo n.º 34
0
 def setUp(self):
     super(JavaWireLibraryTest, self).setUp()
     self.build_file_parser._build_configuration.register_target_alias(
         'java_wire_library', JavaWireLibrary)
     self.add_to_build_file(
         'BUILD',
         dedent('''
   java_wire_library(name='foo',
     sources=[],
     service_writer='com.squareup.wire.RetrofitServiceWriter'
   )'''))
     self.build_graph.inject_spec_closure('//:foo')
     self.target = self.build_graph.get_target(
         SyntheticAddress.parse('//:foo'))
Ejemplo n.º 35
0
 def test_empty_traversable_properties(self):
     build_file = self.add_to_build_file(
         'BUILD',
         dedent('''
 java_library(
   name='foo',
   sources=["foo.java"],
 )
 '''))
     self.build_graph.inject_address_closure(
         BuildFileAddress(build_file, 'foo'))
     target = self.build_graph.get_target(SyntheticAddress.parse('//:foo'))
     self.assertSequenceEqual([], list(target.traversable_specs))
     self.assertSequenceEqual([], list(target.traversable_dependency_specs))
Ejemplo n.º 36
0
  def test_traversable_dependency_specs(self):
    build_file = self.add_to_build_file('BUILD', dedent('''
    jvm_target(name='foo',
      resources=[':resource_target'],
    )
    resources(name='resource_target',
      sources=['foo.txt'],
    )
    '''))

    self.build_graph.inject_address_closure(BuildFileAddress(build_file, 'foo'))
    target = self.build_graph.get_target(SyntheticAddress.parse('//:foo'))
    self.assertSequenceEqual([], list(target.traversable_specs))
    self.assertSequenceEqual([':resource_target'], list(target.traversable_dependency_specs))
Ejemplo n.º 37
0
 def test_synthetic_forms(self):
     self.assert_address('a/b', 'target',
                         SyntheticAddress.parse('a/b:target'))
     self.assert_address('a/b', 'target',
                         SyntheticAddress.parse('//a/b:target'))
     self.assert_address('a/b', 'b', SyntheticAddress.parse('a/b'))
     self.assert_address('a/b', 'b', SyntheticAddress.parse('//a/b'))
     self.assert_address(
         'a/b', 'target',
         SyntheticAddress.parse(':target', relative_to='a/b'))
     self.assert_address(
         '', 'target', SyntheticAddress.parse('//:target',
                                              relative_to='a/b'))
     self.assert_address('', 'target', SyntheticAddress.parse(':target'))
     self.assert_address(
         'a/b', 'target',
         SyntheticAddress.parse(':target', relative_to='a/b'))
Ejemplo n.º 38
0
    def provides(self):
        if not self.payload.provides:
            return None

        # TODO(pl): This is an awful hack
        if isinstance(self.payload.provides.repo, Compatibility.string):
            repo_spec = self.payload.provides.repo
            address = SyntheticAddress.parse(
                repo_spec, relative_to=self.address.spec_path)
            repo_target = self._build_graph.get_target(address)
            if repo_target is None:
                raise TargetDefinitionException(
                    self, 'No such repo target: %s' % repo_spec)
            self.payload.provides.repo = repo_target
        return self.payload.provides
Ejemplo n.º 39
0
  def test_smoke(self):
    contents = dedent('''namespace java com.pants.example
      struct Example {
      1: optional i64 number
      }
    ''')

    self.create_file(relpath='test_smoke/a.thrift', contents=contents)
    self.add_to_build_file('test_smoke', dedent('''
      java_thrift_library(name='a',
        sources=['a.thrift'],
        dependencies=[],
        compiler='scrooge',
        language='scala',
        rpc_style='finagle'
      )
    '''))

    target = self.target('test_smoke:a')
    task = prepare_task(ScroogeGen,
                        build_graph=self.build_graph,
                        targets=[target],
                        build_file_parser=self.build_file_parser)

    with patch('pants.backend.codegen.tasks.scrooge_gen.calculate_services'):
      task._outdir = MagicMock()
      task._outdir.return_value = self.task_outdir

      task.gen = MagicMock()
      sources = [os.path.join(self.task_outdir, 'com/pants/example/Example.scala')]
      task.gen.return_value = {'test_smoke/a.thrift': sources}

      try:
        saved_add_new_target = Context.add_new_target
        Context.add_new_target = MagicMock()
        task.execute()
        relative_task_outdir = os.path.relpath(self.task_outdir, get_buildroot())
        spec = '{spec_path}:{name}'.format(spec_path=relative_task_outdir, name='test_smoke.a')
        address = SyntheticAddress.parse(spec=spec)
        Context.add_new_target.assert_called_once_with(address,
                                                       ScalaLibrary,
                                                       sources=sources,
                                                       excludes=OrderedSet(),
                                                       dependencies=OrderedSet(),
                                                       provides=None,
                                                       derived_from=target)
      finally:
        Context.add_new_target = saved_add_new_target
Ejemplo n.º 40
0
 def _create_python_target(self, target, dependees):
     genfiles = []
     for source in target.sources_relative_to_source_root():
         path = os.path.join(target.target_base, source)
         genfiles.extend(calculate_genfiles(path, source).get('py', []))
     spec_path = os.path.relpath(self.py_out, get_buildroot())
     spec = '{spec_path}:{name}'.format(spec_path=spec_path, name=target.id)
     address = SyntheticAddress.parse(spec=spec)
     tgt = self.context.add_new_target(address,
                                       PythonLibrary,
                                       derived_from=target,
                                       sources=genfiles,
                                       dependencies=self.pythondeps)
     for dependee in dependees:
         dependee.inject_dependency(tgt.address)
     return tgt
Ejemplo n.º 41
0
 def parse_url(spec):
     match = MarkdownToHtml.PANTS_LINK.match(spec)
     if match:
         address = SyntheticAddress.parse(match.group(1),
                                          relative_to=get_buildroot())
         page = self.context.build_graph.get_target(address)
         anchor = match.group(2) or ''
         if not page:
             raise TaskError(
                 'Invalid markdown link to pants target: "%s". ' %
                 match.group(1) +
                 'Is your page missing a dependency on this target?')
         alias, url = url_builder(page, config=get_config(page))
         return alias, url + anchor
     else:
         return spec, spec
Ejemplo n.º 42
0
 def imports(self):
     """Returns the set of JarDependencys to be included when compiling this target."""
     if self._imports is None:
         libraries = OrderedSet(self._library_imports)
         import_jars = self.raw_imports - libraries
         for spec in libraries:
             address = SyntheticAddress.parse(
                 spec, relative_to=self.address.spec_path)
             target = self._build_graph.get_target(address)
             if isinstance(target, (JarLibrary, JvmTarget)):
                 import_jars.update(target.jar_dependencies)
             else:
                 raise self.PrematureImportPokeError(
                     "{address}: Failed to resolve import '{spec}'.".format(
                         address=self.address.spec, spec=address.spec))
         self._imports = import_jars
     return self._imports
Ejemplo n.º 43
0
    def provides(self):
        if not self.payload.provides:
            return None

        for p in self.payload.provides:
            if isinstance(p.wiki, Wiki):
                # We have already resolved this string into an object, so skip it.
                continue
            if isinstance(p.wiki, Compatibility.string):
                address = SyntheticAddress.parse(
                    p.wiki, relative_to=self.address.spec_path)
                repo_target = self._build_graph.get_target(address)
                p.wiki = repo_target
            else:
                raise ValueError(
                    'A WikiArtifact must depend on a string pointer to a Wiki. Found %s instead.'
                    % p.wiki)
        return self.payload.provides
Ejemplo n.º 44
0
    def test_to_jar_dependencies(self):
        def assert_dep(dep, org, name, rev):
            self.assertTrue(isinstance(dep, JarDependency))
            self.assertEquals(org, dep.org)
            self.assertEquals(name, dep.name)
            self.assertEquals(rev, dep.rev)

        self.add_to_build_file(
            'BUILD',
            dedent('''
    jar_library(name='lib1',
      jars=[
        jar(org='testOrg1', name='testName1', rev='123'),
      ],
    )
    jar_library(name='lib2',
      jars=[
        jar(org='testOrg2', name='testName2', rev='456'),
        jar(org='testOrg3', name='testName3', rev='789'),
      ],
    )
    '''))
        lib1 = self.target('//:lib1')
        self.assertIsInstance(lib1, JarLibrary)
        self.assertEquals(1, len(lib1.jar_dependencies))
        assert_dep(lib1.jar_dependencies[0], 'testOrg1', 'testName1', '123')

        lib2 = self.target('//:lib2')
        self.assertIsInstance(lib2, JarLibrary)
        self.assertEquals(2, len(lib2.jar_dependencies))
        assert_dep(lib2.jar_dependencies[0], 'testOrg2', 'testName2', '456')
        assert_dep(lib2.jar_dependencies[1], 'testOrg3', 'testName3', '789')

        jvm_target = JarLibrary(name='dummy',
                                address=SyntheticAddress.parse("//:dummy"),
                                build_graph=self.build_graph)
        deps = jvm_target.to_jar_dependencies(jvm_target.address,
                                              [':lib1', ':lib2'],
                                              self.build_graph)
        self.assertEquals(3, len(deps))
        assert_dep(lib1.jar_dependencies[0], 'testOrg1', 'testName1', '123')
        assert_dep(lib2.jar_dependencies[0], 'testOrg2', 'testName2', '456')
        assert_dep(lib2.jar_dependencies[1], 'testOrg3', 'testName3', '789')
Ejemplo n.º 45
0
    def _create_java_target(self, target, dependees):
        genfiles = []
        for source in target.sources_relative_to_source_root():
            path = os.path.join(get_buildroot(), target.target_base, source)
            genfile = calculate_genfile(path)
            genfiles.append(os.path.join(self._java_out, genfile))

        spec_path = os.path.relpath(self._java_out, get_buildroot())
        spec = '{spec_path}:{name}'.format(spec_path=spec_path, name=target.id)
        address = SyntheticAddress.parse(spec=spec)
        tgt = self.context.add_new_target(address,
                                          JavaRagelLibrary,
                                          sources=genfiles,
                                          provides=target.provides,
                                          dependencies=self.javadeps,
                                          excludes=target.payload.excludes)
        for dependee in dependees:
            dependee.inject_dependency(tgt.address)
        return tgt
Ejemplo n.º 46
0
  def _test_help(self, build_string, library_type, sources):
    contents = dedent('''#@namespace android org.pantsbuild.android_example
      namespace java org.pantsbuild.example
      struct Example {
      1: optional i64 number
      }
    ''')

    self.create_file(relpath='test_smoke/a.thrift', contents=contents)
    self.add_to_build_file('test_smoke', dedent(build_string))

    target = self.target('test_smoke:a')
    context = self.context(target_roots=[target])
    task = self.create_task(context)

    task._declares_service = lambda source: False
    task._outdir = MagicMock()
    task._outdir.return_value = self.task_outdir

    task.gen = MagicMock()
    task.gen.return_value = {'test_smoke/a.thrift': sources}

    saved_add_new_target = Context.add_new_target
    try:
      Context.add_new_target = MagicMock()
      task.execute()
      relative_task_outdir = os.path.relpath(self.task_outdir, get_buildroot())
      spec = '{spec_path}:{name}'.format(spec_path=relative_task_outdir, name='test_smoke.a')
      address = SyntheticAddress.parse(spec=spec)
      Context.add_new_target.assert_called_once_with(address,
                                                     library_type,
                                                     sources=sources,
                                                     excludes=OrderedSet(),
                                                     dependencies=OrderedSet(),
                                                     provides=None,
                                                     derived_from=target)
    finally:
      Context.add_new_target = saved_add_new_target
Ejemplo n.º 47
0
    def create_sources_field(self,
                             sources,
                             sources_rel_path,
                             address=None,
                             build_graph=None):
        """Factory method to create a SourcesField appropriate for the type of the sources object.

    Note that this method is called before the call to Target.__init__ so don't expect fields to
    be populated!
    :return: a payload field object representing the sources parameter
    :rtype: SourcesField
    """

        if isinstance(sources, Addresses):
            # Currently, this is only created by the result of from_target() which takes a single argument
            if len(sources.addresses) != 1:
                raise self.WrongNumberOfAddresses(
                    "Expected a single address to from_target() as argument to {spec}"
                    .format(spec=address.spec))
            referenced_address = SyntheticAddress.parse(
                sources.addresses[0], relative_to=sources.rel_path)
            return DeferredSourcesField(ref_address=referenced_address)
        return SourcesField(sources=sources, sources_rel_path=sources_rel_path)