Пример #1
0
  def test_sibling_references(self):
    with temporary_dir() as root_dir:
      buildfile = create_buildfile(root_dir, 'a', name='BUILD',
        content=dedent("""
          dependencies(name='util',
            dependencies=[
              jar(org='com.twitter', name='util', rev='0.0.1')
            ]
          )
        """).strip()
      )
      sibling = create_buildfile(root_dir, 'a', name='BUILD.sibling',
        content=dedent("""
          dependencies(name='util-ex',
            dependencies=[
              pants(':util'),
              jar(org='com.twitter', name='util-ex', rev='0.0.1')
            ]
          )
        """).strip()
      )
      ParseContext(buildfile).parse()

      utilex = Target.get(Address.parse(root_dir, 'a:util-ex', is_relative=False))
      utilex_deps = set(utilex.resolve())

      util = Target.get(Address.parse(root_dir, 'a:util', is_relative=False))
      util_deps = set(util.resolve())

      self.assertEquals(util_deps, util_deps.intersection(utilex_deps))
Пример #2
0
 def parse_jarcoordinate(coordinate):
     components = coordinate.split('#', 1)
     if len(components) == 2:
         org, name = components
         return org, name
     else:
         try:
             address = Address.parse(get_buildroot(), coordinate)
             try:
                 target = Target.get(address)
                 if not target:
                     siblings = Target.get_all_addresses(
                         address.buildfile)
                     prompt = 'did you mean' if len(
                         siblings
                     ) == 1 else 'maybe you meant one of these'
                     raise TaskError('%s => %s?:\n    %s' %
                                     (address, prompt, '\n    '.join(
                                         str(a) for a in siblings)))
                 if not target.is_exported:
                     raise TaskError('%s is not an exported target' %
                                     coordinate)
                 return target.provides.org, target.provides.name
             except (ImportError, SyntaxError, TypeError):
                 raise TaskError('Failed to parse %s' %
                                 address.buildfile.relpath)
         except IOError:
             raise TaskError('No BUILD file could be found at %s' %
                             coordinate)
Пример #3
0
 def parse_jarcoordinate(coordinate):
     components = coordinate.split('#', 1)
     if len(components) == 2:
         org, name = components
         return org, name
     else:
         try:
             # TODO(Eric Ayers) This code is suspect.  Target.get() is a very old method and almost certainly broken.
             # Refactor to use methods from BuildGraph or BuildFileAddressMapper
             address = Address.parse(get_buildroot(), coordinate)
             target = Target.get(address)
             if not target:
                 siblings = Target.get_all_addresses(address.build_file)
                 prompt = 'did you mean' if len(
                     siblings) == 1 else 'maybe you meant one of these'
                 raise TaskError('%s => %s?:\n    %s' %
                                 (address, prompt, '\n    '.join(
                                     str(a) for a in siblings)))
             if not target.is_exported:
                 raise TaskError('%s is not an exported target' %
                                 coordinate)
             return target.provides.org, target.provides.name
         except (BuildFile.BuildFileError,
                 BuildFileParser.BuildFileParserError,
                 AddressLookupError) as e:
             raise TaskError(
                 '{message}\n  Problem with BUILD file  at {coordinate}'
                 .format(message=e, coordinate=coordinate))
Пример #4
0
 def resolve(self):
   # De-reference this pants pointer to an actual parsed target.
   resolved = Target.get(self.address)
   if not resolved:
     raise TargetDefinitionException(self, '%s%s' % (self._DEFINITION_ERROR_MSG, self.address))
   for dep in resolved.resolve():
     yield dep
Пример #5
0
Файл: paths.py Проект: kn/pants
    def _find_path(cls, from_target, to_target, log):
        from_target, to_target = cls._coerce_to_targets(from_target, to_target)

        log.debug("Looking for path from %s to %s" % (from_target.address.reference(), to_target.address.reference()))

        queue = [([from_target], 0)]
        while True:
            if not queue:
                print("no path found from %s to %s!" % (from_target.address.reference(), to_target.address.reference()))
                break

            path, indent = queue.pop(0)
            next_target = path[-1]
            if next_target in cls.examined_targets:
                continue
            cls.examined_targets.add(next_target)

            log.debug("%sexamining %s" % ("  " * indent, next_target))

            if next_target == to_target:
                print("")
                for target in path:
                    print("%s" % target.address.reference())
                break

            if hasattr(next_target, "dependency_addresses"):
                for address in next_target.dependency_addresses:
                    dep = Target.get(address)
                    queue.append((path + [dep], indent + 1))
Пример #6
0
  def target(cls, address):
    """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.
    """
    return Target.get(Address.parse(cls.build_root, address, is_relative=False))
Пример #7
0
def _get_target(address):
  try:
    address = Address.parse(get_buildroot(), address, is_relative=False)
  except IOError as e:
    raise TaskError('Failed to parse address: %s: %s' % (address, e))
  match = Target.get(address)
  if not match:
    raise TaskError('Invalid target address: %s' % address)
  return match
Пример #8
0
  def parse(self, spec):
    """Parses the given target spec into one or more targets.

    Returns a generator of target, address pairs in which the target may be None if the address
    points to a non-existent target.
    """
    for address in self._parse_addresses(spec):
      target = Target.get(address)
      yield target, address
Пример #9
0
 def _find_targets(self):
   if len(self.context.target_roots) > 0:
     for target in self.context.target_roots:
       yield target
   else:
     for buildfile in BuildFile.scan_buildfiles(get_buildroot()):
       target_addresses = Target.get_all_addresses(buildfile)
       for target_address in target_addresses:
         yield Target.get(target_address)
Пример #10
0
 def parse_url(spec):
   match = MarkdownToHtml.PANTS_LINK.match(spec)
   if match:
     page = Target.get(Address.parse(get_buildroot(), match.group(1)))
     anchor = match.group(2) or ''
     if not page:
       raise TaskError('Invalid link %s' % match.group(1))
     alias, url = url_builder(page, config=get_config(page))
     return alias, url + anchor
   else:
     return spec, spec
Пример #11
0
Файл: paths.py Проект: kn/pants
    def _coerce_to_targets(cls, from_str, to_str):
        if isinstance(from_str, Compatibility.string):
            if not isinstance(to_str, Compatibility.string):
                raise TaskError("Finding paths from string %s to non-string %s" % (from_str, str(to_str)))

            from_address = Address.parse(get_buildroot(), from_str)
            to_address = Address.parse(get_buildroot(), to_str)

            from_target = Target.get(from_address)
            to_target = Target.get(to_address)

            if not from_target:
                raise TaskError("Target %s doesn't exist" % from_address.reference())
            if not to_target:
                raise TaskError("Target %s doesn't exist" % to_address.reference())

            return from_target, to_target

        elif isinstance(to_str, Compatibility.string):
            raise TaskError("Finding paths from string %s to non-string %s" % (to_str, str(from_str)))
        return from_str, to_str
Пример #12
0
 def parse_url(spec):
     match = MarkdownToHtml.PANTS_LINK.match(spec)
     if match:
         page = Target.get(
             Address.parse(get_buildroot(), match.group(1)))
         anchor = match.group(2) or ''
         if not page:
             raise TaskError('Invalid link %s' % match.group(1))
         alias, url = url_builder(page, config=get_config(page))
         return alias, url + anchor
     else:
         return spec, spec
Пример #13
0
  def _owning_targets(self, path):
    for build_file in self._candidate_owners(path):
      is_build_file = (build_file.full_path == os.path.join(get_buildroot(), path))
      for address in Target.get_all_addresses(build_file):
        target = Target.get(address)

        # A synthesized target can never own permanent files on disk
        if target != target.derived_from:
          # TODO(John Sirois): tighten up the notion of targets written down in a BUILD by a user
          # vs. targets created by pants at runtime.
          continue

        if target and (is_build_file or ((target.has_sources() or target.has_resources)
                                         and self._owns(target, path))):
          yield target
Пример #14
0
  def __init__(self, context):
    ConsoleTask.__init__(self, context)

    self._print_uptodate = context.options.check_deps_print_uptodate
    self.repos = context.config.getdict('jar-publish', 'repos')
    self._artifacts_to_targets = {}
    all_addresses = (address for buildfile in BuildFile.scan_buildfiles(get_buildroot())
                     for address in Target.get_all_addresses(buildfile))
    for address in all_addresses:
      target = Target.get(address)
      if target.is_exported:
        provided_jar, _, _ = target.get_artifact_info()
        artifact = (provided_jar.org, provided_jar.name)
        if not artifact in self._artifacts_to_targets:
          self._artifacts_to_targets[artifact] = target
Пример #15
0
  def __init__(self, run_tracker, root_dir, parser, argv):
    Command.__init__(self, run_tracker, root_dir, parser, argv)

    if not self.args:
      self.error("A spec argument is required")

    self._config = Config.load()
    self._root = root_dir

    address = Address.parse(root_dir, self.args[0])
    self.target = Target.get(address)
    if self.target is None:
      self.error('%s is not a valid target!' % self.args[0])

    if not self.target.provides:
      self.error('Target must provide an artifact.')
Пример #16
0
        def configure_target(target):
            if target not in analyzed:
                analyzed.add(target)

                self.has_scala = not self.skip_scala and (self.has_scala
                                                          or is_scala(target))

                if target.has_resources:
                    resources_by_basedir = defaultdict(set)
                    for resources in target.resources:
                        resources_by_basedir[resources.target_base].update(
                            resources.sources)
                    for basedir, resources in resources_by_basedir.items():
                        self.resource_extensions.update(
                            Project.extract_resource_extensions(resources))
                        configure_source_sets(basedir,
                                              resources,
                                              is_test=False)

                if target.sources:
                    test = target.is_test
                    self.has_tests = self.has_tests or test
                    configure_source_sets(target.target_base,
                                          target.sources,
                                          is_test=test)

                # Other BUILD files may specify sources in the same directory as this target.  Those BUILD
                # files might be in parent directories (globs('a/b/*.java')) or even children directories if
                # this target globs children as well.  Gather all these candidate BUILD files to test for
                # sources they own that live in the directories this targets sources live in.
                target_dirset = find_source_basedirs(target)
                candidates = Target.get_all_addresses(target.address.buildfile)
                for ancestor in target.address.buildfile.ancestors():
                    candidates.update(Target.get_all_addresses(ancestor))
                for sibling in target.address.buildfile.siblings():
                    candidates.update(Target.get_all_addresses(sibling))
                for descendant in target.address.buildfile.descendants():
                    candidates.update(Target.get_all_addresses(descendant))

                def is_sibling(target):
                    return source_target(
                        target) and target_dirset.intersection(
                            find_source_basedirs(target))

                return filter(
                    is_sibling,
                    [Target.get(a) for a in candidates if a != target.address])
Пример #17
0
Файл: paths.py Проект: kn/pants
    def _find_paths_rec(cls, from_target, to_target):
        if from_target == to_target:
            return [[from_target]]

        if from_target not in cls.all_paths or to_target not in cls.all_paths[from_target]:
            paths = []
            if hasattr(from_target, "dependency_addresses"):
                for address in from_target.dependency_addresses:
                    dep = Target.get(address)
                    for path in cls._find_paths_rec(dep, to_target):
                        new_path = copy.copy(path)
                        new_path.insert(0, from_target)
                        paths.append(new_path)

            cls.all_paths[from_target][to_target] = paths

        return cls.all_paths[from_target][to_target]
Пример #18
0
  def console_output(self, _):
    buildfiles = OrderedSet()
    if self._dependees_type:
      base_paths = OrderedSet()
      for dependees_type in self._dependees_type:
        try:
          # Try to do a fully qualified import 1st for filtering on custom types.
          from_list, module, type_name = dependees_type.rsplit('.', 2)
          __import__('%s.%s' % (from_list, module), fromlist=[from_list])
        except (ImportError, ValueError):
          # Fall back on pants provided target types.
          if hasattr(pants.base.build_file_context, dependees_type):
            type_name = getattr(pants.base.build_file_context, dependees_type)
          else:
            raise TaskError('Invalid type name: %s' % dependees_type)
        # Find the SourceRoot for the given input type
        base_paths.update(SourceRoot.roots(type_name))
      if not base_paths:
        raise TaskError('No SourceRoot set for any target type in %s.' % self._dependees_type +
                        '\nPlease define a source root in BUILD file as:' +
                        '\n\tsource_root(\'<src-folder>\', %s)' % ', '.join(self._dependees_type))
      for base_path in base_paths:
        buildfiles.update(BuildFile.scan_buildfiles(get_buildroot(), base_path))
    else:
      buildfiles = BuildFile.scan_buildfiles(get_buildroot())

    dependees_by_target = defaultdict(set)
    for buildfile in buildfiles:
      for address in Target.get_all_addresses(buildfile):
        for target in Target.get(address).resolve():
          # TODO(John Sirois): tighten up the notion of targets written down in a BUILD by a
          # user vs. targets created by pants at runtime.
          target = self.get_concrete_target(target)
          if hasattr(target, 'dependencies'):
            for dependencies in target.dependencies:
              for dependency in dependencies.resolve():
                dependency = self.get_concrete_target(dependency)
                dependees_by_target[dependency].add(target)

    roots = set(self.context.target_roots)
    if self._closed:
      for root in roots:
        yield str(root.address)

    for dependant in self.get_dependants(dependees_by_target, roots):
      yield str(dependant.address)
Пример #19
0
Файл: build.py Проект: kn/pants
  def __init__(self, run_tracker, root_dir, parser, argv):
    Command.__init__(self, run_tracker, root_dir, parser, argv)

    if not self.args:
      self.error("A spec argument is required")

    self.config = Config.load()

    interpreters = self.options.interpreters or [b'']
    self.interpreter_cache = PythonInterpreterCache(self.config, logger=self.debug)
    self.interpreter_cache.setup(filters=interpreters)
    interpreters = self.interpreter_cache.select_interpreter(
        list(self.interpreter_cache.matches(interpreters)))
    if len(interpreters) != 1:
      self.error('Unable to detect suitable interpreter.')
    else:
      self.debug('Selected %s' % interpreters[0])
    self.interpreter = interpreters[0]

    try:
      specs_end = self.args.index('--')
      if len(self.args) > specs_end:
        self.build_args = self.args[specs_end+1:len(self.args)+1]
      else:
        self.build_args = []
    except ValueError:
      specs_end = 1
      self.build_args = self.args[1:] if len(self.args) > 1 else []

    self.targets = OrderedSet()
    for spec in self.args[0:specs_end]:
      try:
        address = Address.parse(root_dir, spec)
      except:
        self.error("Problem parsing spec %s: %s" % (spec, traceback.format_exc()))

      try:
        target = Target.get(address)
      except:
        self.error("Problem parsing BUILD target %s: %s" % (address, traceback.format_exc()))

      if not target:
        self.error("Target %s does not exist" % address)
      self.targets.update(tgt for tgt in target.resolve() if tgt.is_concrete)
Пример #20
0
 def parse_jarcoordinate(coordinate):
   components = coordinate.split('#', 1)
   if len(components) == 2:
     org, name = components
     return org, name
   else:
     try:
       address = Address.parse(get_buildroot(), coordinate)  # TODO: This is broken.
       try:
         target = Target.get(address)
         if not target:
           siblings = Target.get_all_addresses(address.buildfile)
           prompt = 'did you mean' if len(siblings) == 1 else 'maybe you meant one of these'
           raise TaskError('%s => %s?:\n    %s' % (address, prompt,
                                                   '\n    '.join(str(a) for a in siblings)))
         if not target.is_exported:
           raise TaskError('%s is not an exported target' % coordinate)
         return target.provides.org, target.provides.name
       except (ImportError, SyntaxError, TypeError):
         raise TaskError('Failed to parse %s' % address.buildfile.relpath)
     except IOError:
       raise TaskError('No BUILD file could be found at %s' % coordinate)
Пример #21
0
 def parse_jarcoordinate(coordinate):
   components = coordinate.split('#', 1)
   if len(components) == 2:
     org, name = components
     return org, name
   else:
     try:
       # TODO(Eric Ayers) This code is suspect.  Target.get() is a very old method and almost certainly broken.
       # Refactor to use methods from BuildGraph or BuildFileAddressMapper
       address = Address.parse(get_buildroot(), coordinate)
       target = Target.get(address)
       if not target:
         siblings = Target.get_all_addresses(address.build_file)
         prompt = 'did you mean' if len(siblings) == 1 else 'maybe you meant one of these'
         raise TaskError('%s => %s?:\n    %s' % (address, prompt,
                                                 '\n    '.join(str(a) for a in siblings)))
       if not target.is_exported:
         raise TaskError('%s is not an exported target' % coordinate)
       return target.provides.org, target.provides.name
     except (BuildFile.BuildFileError, BuildFileParser.BuildFileParserError, AddressLookupError) as e:
       raise TaskError('{message}\n  Problem with BUILD file  at {coordinate}'
       .format(message=e, coordinate=coordinate))
Пример #22
0
    def configure_target(target):
      if target not in analyzed:
        analyzed.add(target)

        self.has_scala = not self.skip_scala and (self.has_scala or is_scala(target))

        if target.has_resources:
          resources_by_basedir = defaultdict(set)
          for resources in target.resources:
            resources_by_basedir[resources.target_base].update(resources.sources)
          for basedir, resources in resources_by_basedir.items():
            self.resource_extensions.update(Project.extract_resource_extensions(resources))
            configure_source_sets(basedir, resources, is_test=False)

        if target.sources:
          test = target.is_test
          self.has_tests = self.has_tests or test
          configure_source_sets(target.target_base, target.sources, is_test=test)

        # Other BUILD files may specify sources in the same directory as this target.  Those BUILD
        # files might be in parent directories (globs('a/b/*.java')) or even children directories if
        # this target globs children as well.  Gather all these candidate BUILD files to test for
        # sources they own that live in the directories this targets sources live in.
        target_dirset = find_source_basedirs(target)
        candidates = Target.get_all_addresses(target.address.buildfile)
        for ancestor in target.address.buildfile.ancestors():
          candidates.update(Target.get_all_addresses(ancestor))
        for sibling in target.address.buildfile.siblings():
          candidates.update(Target.get_all_addresses(sibling))
        for descendant in target.address.buildfile.descendants():
          candidates.update(Target.get_all_addresses(descendant))

        def is_sibling(target):
          return source_target(target) and target_dirset.intersection(find_source_basedirs(target))

        return filter(is_sibling, [Target.get(a) for a in candidates if a != target.address])
Пример #23
0
 def print_provides(column_extractors, address):
   target = Target.get(address)
   if target.is_exported:
     return " ".join(extractor(target) for extractor in column_extractors)
Пример #24
0
 def print_documented(address):
   target = Target.get(address)
   if target.description:
     return '%s\n  %s' % (address, '\n  '.join(target.description.strip().split('\n')))
Пример #25
0
  def __init__(self, run_tracker, root_dir, parser, argv):
    Command.__init__(self, run_tracker, root_dir, parser, argv)

    self.target = None
    self.extra_targets = []
    self.config = Config.load()
    self.interpreter_cache = PythonInterpreterCache(self.config, logger=self.debug)
    self.interpreter_cache.setup()
    interpreters = self.interpreter_cache.select_interpreter(
        list(self.interpreter_cache.matches([self.options.interpreter]
            if self.options.interpreter else [b''])))
    if len(interpreters) != 1:
      self.error('Unable to detect suitable interpreter.')
    self.interpreter = interpreters[0]

    for req in self.options.extra_requirements:
      with ParseContext.temp():
        self.extra_targets.append(PythonRequirement(req, use_2to3=True))

    # We parse each arg in the context of the cli usage:
    #   ./pants command (options) [spec] (build args)
    #   ./pants command (options) [spec]... -- (build args)
    # Our command token and our options are parsed out so we see args of the form:
    #   [spec] (build args)
    #   [spec]... -- (build args)
    binaries = []
    for k in range(len(self.args)):
      arg = self.args.pop(0)
      if arg == '--':
        break

      def not_a_target(debug_msg):
        self.debug('Not a target, assuming option: %s.' % e)
        # We failed to parse the arg as a target or else it was in valid address format but did not
        # correspond to a real target.  Assume this is the 1st of the build args and terminate
        # processing args for target addresses.
        self.args.insert(0, arg)

      target = None
      try:
        address = Address.parse(root_dir, arg)
        target = Target.get(address)
        if target is None:
          not_a_target(debug_msg='Unrecognized target')
          break
      except Exception as e:
        not_a_target(debug_msg=e)
        break

      for resolved in filter(lambda t: t.is_concrete, target.resolve()):
        if isinstance(resolved, PythonBinary):
          binaries.append(resolved)
        else:
          self.extra_targets.append(resolved)

    if len(binaries) == 0:
      # treat as a chroot
      pass
    elif len(binaries) == 1:
      # We found a binary and are done, the rest of the args get passed to it
      self.target = binaries[0]
    else:
      self.error('Can only process 1 binary target, %s contains %d:\n\t%s' % (
        arg, len(binaries), '\n\t'.join(str(binary.address) for binary in binaries)
      ))

    if self.target is None:
      if not self.extra_targets:
        self.error('No valid target specified!')
      self.target = self.extra_targets.pop(0)