def _materialize()

in contrib/go/src/python/pants/contrib/go/tasks/go_buildgen.py [0:0]


  def _materialize(self, generation_result):
    remote = self.get_options().remote
    existing_go_buildfiles = set()

    def gather_go_buildfiles(rel_path):
      address_mapper = self.context.address_mapper
      for build_file in address_mapper.scan_build_files(base_path=rel_path):
        existing_go_buildfiles.add(build_file)

    gather_go_buildfiles(generation_result.local_root)
    if remote and generation_result.remote_root != generation_result.local_root:
      gather_go_buildfiles(generation_result.remote_root)

    targets = set(self.context.build_graph.targets(self.is_go))
    if remote and generation_result.remote_root:
      # Generation only walks out from local source, but we might have transitive remote
      # dependencies under the remote root which are not linked except by `resolve.go`.  Add all
      # the remotes we can find to ensure they are re-materialized too.
      remote_root = os.path.join(get_buildroot(), generation_result.remote_root)
      targets.update(self.context.scan(remote_root).targets(self.is_remote_lib))

    failed_results = []
    for result in self.generate_build_files(targets):
      existing_go_buildfiles.discard(result.build_file_path)
      result.log(self.context.log)
      if result.failed:
        failed_results.append(result)

    if existing_go_buildfiles:
      deleted = []
      for existing_go_buildfile in existing_go_buildfiles:
        spec_path = os.path.dirname(existing_go_buildfile)
        for address in self.context.address_mapper.addresses_in_spec_path(spec_path):
          target = self.context.build_graph.resolve_address(address)
          if isinstance(target, GoLocalSource):
            os.unlink(os.path.join(get_buildroot(), existing_go_buildfile))
            deleted.append(existing_go_buildfile)
      if deleted:
        self.context.log.info('Deleted the following obsolete BUILD files:\n\t{}'
                              .format('\n\t'.join(sorted(deleted))))

    if failed_results:
      self.context.log.error('Un-pinned (FLOATING) Go remote library dependencies are not '
                             'allowed in this repository!\n'
                             'Found the following FLOATING Go remote libraries:\n\t{}'
                             .format('\n\t'.join('{}'.format(result) for result in failed_results)))
      self.context.log.info('You can fix this by editing the target in each FLOATING BUILD file '
                            'listed above to include a `rev` parameter that points to a sha, tag '
                            'or commit id that pins the code in the source repository to a fixed, '
                            'non-FLOATING version.')
      raise self.FloatingRemoteError('Un-pinned (FLOATING) Go remote libraries detected.')