Esempio n. 1
0
    def generate_targets(self, local_go_targets):
        # TODO(John Sirois): support multiple source roots like GOPATH does?
        # The GOPATH's 1st element is read-write, the rest are read-only; ie: their sources build to
        # the 1st element's pkg/ and bin/ dirs.
        all_rooted_types = set()
        for types in SourceRoot.all_roots().values():
            all_rooted_types.update(types)

        def safe_get_source_roots(target_type):
            return set(SourceRoot.roots(
                target_type)) if target_type in all_rooted_types else set()

        local_roots = safe_get_source_roots(GoBinary) | safe_get_source_roots(
            GoLibrary)
        if not local_roots:
            raise self.NoLocalRootsError(
                'Can only BUILD gen if a Go local sources source root is'
                'defined.')
        if len(local_roots) > 1:
            raise self.InvalidLocalRootsError(
                'Can only BUILD gen for a single Go local sources source '
                'root, found:\n\t{}'.format('\n\t'.join(sorted(local_roots))))
        local_root = local_roots.pop()
        unrooted_locals = {
            t
            for t in local_go_targets if t.target_base != local_root
        }
        if unrooted_locals:
            raise self.UnrootedLocalSourceError(
                'Cannot BUILD gen until the following targets are '
                'relocated to the build root at {}:\n\t{}'.format(
                    local_root, '\n\t'.join(
                        sorted(t.address.reference()
                               for t in unrooted_locals))))

        remote_roots = set(safe_get_source_roots(GoRemoteLibrary))
        if len(remote_roots) > 1:
            raise self.InvalidRemoteRootsError(
                'Can only BUILD gen for a single Go remote library source '
                'root, found:\n\t{}'.format('\n\t'.join(sorted(remote_roots))))
        remote_root = remote_roots.pop() if remote_roots else None

        generator = GoTargetGenerator(
            self.context.new_workunit,
            self.go_dist,
            self.context.build_graph,
            local_root,
            Fetchers.global_instance(),
            generate_remotes=self.get_options().remote,
            remote_root=remote_root)
        with self.context.new_workunit('go.buildgen',
                                       labels=[WorkUnitLabel.MULTITOOL]):
            try:
                return generator.generate(local_go_targets)
            except generator.GenerationError as e:
                raise self.GenerationError(e)
Esempio n. 2
0
 def _create_fetch_context(self, zipdir):
   """Given a directory of zipfiles, creates a context for GoFetch."""
   self.set_options_for_scope('fetchers', mapping={r'.*': Fetchers.alias(ArchiveFetcher)})
   matcher = ArchiveFetcher.UrlInfo(url_format=os.path.join(zipdir, '\g<zip>.zip'),
                                    default_rev='HEAD',
                                    strip_level=0)
   self.set_options_for_scope('archive-fetcher', matchers={r'localzip/(?P<zip>[^/]+)': matcher})
   context = self.context()
   context.products.safe_create_data('go_remote_lib_src', lambda: defaultdict(str))
   return context
Esempio n. 3
0
 def _create_fetch_context(self, zipdir):
     """Given a directory of zipfiles, creates a context for GoFetch."""
     self.set_options_for_scope("fetchers", mapping={r".*": Fetchers.alias(ArchiveFetcher)})
     matcher = ArchiveFetcher.UrlInfo(
         url_format=os.path.join(zipdir, "\g<zip>.zip"), default_rev="HEAD", strip_level=0
     )
     self.set_options_for_scope("archive-fetcher", matchers={r"localzip/(?P<zip>[^/]+)": matcher})
     context = self.context()
     context.products.safe_create_data("go_remote_lib_src", lambda: defaultdict(str))
     return context
Esempio n. 4
0
 def _create_fetch_context(self, zipdir):
     """Given a directory of zipfiles, creates a context for GoFetch."""
     self.set_options_for_scope(
         'fetchers', mapping={r'.*': Fetchers.alias(ArchiveFetcher)})
     matcher = ArchiveFetcher.UrlInfo(url_format=os.path.join(
         zipdir, '\g<zip>.zip'),
                                      default_rev='HEAD',
                                      strip_level=0)
     self.set_options_for_scope(
         'archive-fetcher', matchers={r'localzip/(?P<zip>[^/]+)': matcher})
     context = self.context()
     context.products.safe_create_data('go_remote_lib_src',
                                       lambda: defaultdict(str))
     return context
Esempio n. 5
0
  def generate_targets(self, local_go_targets):
    # TODO(John Sirois): support multiple source roots like GOPATH does?
    # The GOPATH's 1st element is read-write, the rest are read-only; ie: their sources build to
    # the 1st element's pkg/ and bin/ dirs.
    all_rooted_types = set()
    for types in SourceRoot.all_roots().values():
      all_rooted_types.update(types)

    def safe_get_source_roots(target_type):
      return set(SourceRoot.roots(target_type)) if target_type in all_rooted_types else set()

    local_roots = safe_get_source_roots(GoBinary) | safe_get_source_roots(GoLibrary)
    if not local_roots:
      raise self.NoLocalRootsError('Can only BUILD gen if a Go local sources source root is'
                                   'defined.')
    if len(local_roots) > 1:
      raise self.InvalidLocalRootsError('Can only BUILD gen for a single Go local sources source '
                                        'root, found:\n\t{}'
                                        .format('\n\t'.join(sorted(local_roots))))
    local_root = local_roots.pop()
    unrooted_locals = {t for t in local_go_targets if t.target_base != local_root}
    if unrooted_locals:
      raise self.UnrootedLocalSourceError('Cannot BUILD gen until the following targets are '
                                          'relocated to the build root at {}:\n\t{}'
                                          .format(local_root,
                                                  '\n\t'.join(sorted(t.address.reference()
                                                                     for t in unrooted_locals))))

    remote_roots = set(safe_get_source_roots(GoRemoteLibrary))
    if len(remote_roots) > 1:
      raise self.InvalidRemoteRootsError('Can only BUILD gen for a single Go remote library source '
                                         'root, found:\n\t{}'
                                         .format('\n\t'.join(sorted(remote_roots))))
    remote_root = remote_roots.pop() if remote_roots else None

    generator = GoTargetGenerator(self.context.new_workunit,
                                  self.go_dist,
                                  self.context.build_graph,
                                  local_root,
                                  Fetchers.global_instance(),
                                  generate_remotes=self.get_options().remote,
                                  remote_root=remote_root)
    with self.context.new_workunit('go.buildgen', labels=[WorkUnitLabel.MULTITOOL]):
      try:
        return generator.generate(local_go_targets)
      except generator.GenerationError as e:
        raise self.GenerationError(e)
Esempio n. 6
0
    def generate_targets(self, local_go_targets=None):
        """Generate Go targets in memory to form a complete Go graph.

    :param local_go_targets: The local Go targets to fill in a complete target graph for.  If
                             `None`, then all local Go targets under the Go source root are used.
    :type local_go_targets: :class:`collections.Iterable` of
                            :class:`pants.contrib.go.targets.go_local_source import GoLocalSource`
    :returns: A generation result if targets were generated, else `None`.
    :rtype: :class:`GoBuildgen.GenerationResult`
    """
        # TODO(John Sirois): support multiple source roots like GOPATH does?
        # The GOPATH's 1st element is read-write, the rest are read-only; ie: their sources build to
        # the 1st element's pkg/ and bin/ dirs.

        # TODO: Add "find source roots for lang" functionality to SourceRoots and use that instead.
        all_roots = list(self.context.source_roots.all_roots())
        local_roots = [sr.path for sr in all_roots if "go" in sr.langs]
        if not local_roots:
            raise self.NoLocalRootsError("Can only BUILD gen if a Go local sources source root is " "defined.")
        if len(local_roots) > 1:
            raise self.InvalidLocalRootsError(
                "Can only BUILD gen for a single Go local sources source "
                "root, found:\n\t{}".format("\n\t".join(sorted(local_roots)))
            )
        local_root = local_roots.pop()

        if local_go_targets:
            unrooted_locals = {t for t in local_go_targets if t.target_base != local_root}
            if unrooted_locals:
                raise self.UnrootedLocalSourceError(
                    "Cannot BUILD gen until the following targets are "
                    "relocated to the source root at {}:\n\t{}".format(
                        local_root, "\n\t".join(sorted(t.address.reference() for t in unrooted_locals))
                    )
                )
        else:
            root = os.path.join(get_buildroot(), local_root)
            local_go_targets = self.context.scan(root=root).targets(self.is_local_src)
            if not local_go_targets:
                return None

        remote_roots = [sr.path for sr in all_roots if "go_remote" in sr.langs]
        if len(remote_roots) > 1:
            raise self.InvalidRemoteRootsError(
                "Can only BUILD gen for a single Go remote library source "
                "root, found:\n\t{}".format("\n\t".join(sorted(remote_roots)))
            )
        remote_root = remote_roots.pop() if remote_roots else None

        generator = GoTargetGenerator(
            self.import_oracle,
            self.context.build_graph,
            local_root,
            Fetchers.global_instance(),
            generate_remotes=self.get_options().remote,
            remote_root=remote_root,
        )
        with self.context.new_workunit("go.buildgen", labels=[WorkUnitLabel.MULTITOOL]):
            try:
                generated = generator.generate(local_go_targets)
                return self.GenerationResult(generated=generated, local_root=local_root, remote_root=remote_root)
            except generator.GenerationError as e:
                raise self.GenerationError(e)
Esempio n. 7
0
    def generate_targets(self, local_go_targets=None):
        """Generate Go targets in memory to form a complete Go graph.

    :param local_go_targets: The local Go targets to fill in a complete target graph for.  If
                             `None`, then all local Go targets under the Go source root are used.
    :type local_go_targets: :class:`collections.Iterable` of
                            :class:`pants.contrib.go.targets.go_local_source import GoLocalSource`
    :returns: A generation result if targets were generated, else `None`.
    :rtype: :class:`GoBuildgen.GenerationResult`
    """
        # TODO(John Sirois): support multiple source roots like GOPATH does?
        # The GOPATH's 1st element is read-write, the rest are read-only; ie: their sources build to
        # the 1st element's pkg/ and bin/ dirs.

        # TODO: Add "find source roots for lang" functionality to SourceRoots and use that instead.
        all_roots = list(self.context.source_roots.all_roots())
        local_roots = [sr.path for sr in all_roots if 'go' in sr.langs]
        if not local_roots:
            raise self.NoLocalRootsError(
                'Can only BUILD gen if a Go local sources source root is '
                'defined.')
        if len(local_roots) > 1:
            raise self.InvalidLocalRootsError(
                'Can only BUILD gen for a single Go local sources source '
                'root, found:\n\t{}'.format('\n\t'.join(sorted(local_roots))))
        local_root = local_roots.pop()

        if local_go_targets:
            unrooted_locals = {
                t
                for t in local_go_targets if t.target_base != local_root
            }
            if unrooted_locals:
                raise self.UnrootedLocalSourceError(
                    'Cannot BUILD gen until the following targets are '
                    'relocated to the source root at {}:\n\t{}'.format(
                        local_root, '\n\t'.join(
                            sorted(t.address.reference()
                                   for t in unrooted_locals))))
        else:
            root = os.path.join(get_buildroot(), local_root)
            local_go_targets = self.context.scan(root=root).targets(
                self.is_local_src)
            if not local_go_targets:
                return None

        remote_roots = [sr.path for sr in all_roots if 'go_remote' in sr.langs]
        if len(remote_roots) > 1:
            raise self.InvalidRemoteRootsError(
                'Can only BUILD gen for a single Go remote library source '
                'root, found:\n\t{}'.format('\n\t'.join(sorted(remote_roots))))
        remote_root = remote_roots.pop() if remote_roots else None

        generator = GoTargetGenerator(
            self.import_oracle,
            self.context.build_graph,
            local_root,
            Fetchers.global_instance(),
            generate_remotes=self.get_options().remote,
            remote_root=remote_root)
        with self.context.new_workunit('go.buildgen',
                                       labels=[WorkUnitLabel.MULTITOOL]):
            try:
                generated = generator.generate(local_go_targets)
                return self.GenerationResult(generated=generated,
                                             local_root=local_root,
                                             remote_root=remote_root)
            except generator.GenerationError as e:
                raise self.GenerationError(e)
Esempio n. 8
0
 def _get_fetcher(self, import_path):
     return Fetchers.global_instance().get_fetcher(import_path)
Esempio n. 9
0
 def _get_fetcher(self, import_path):
   return Fetchers.global_instance().get_fetcher(import_path)