示例#1
0
def register_sourceroot():
  try:
    yield SourceRoot.register
  except (ValueError, IndexError) as e:
    print("SourceRoot Registration Failed.")
    raise e
  finally:
    SourceRoot.reset()
示例#2
0
def register_sourceroot():
    try:
        yield SourceRoot.register
    except (ValueError, IndexError) as e:
        print("SourceRoot Registration Failed.")
        raise e
    finally:
        SourceRoot.reset()
示例#3
0
 def setUpClass(self):
   super(TestPythonThriftBuilder, self).setUpClass()
   SourceRoot.register(os.path.realpath(os.path.join(self.build_root, 'test_thrift_replacement')),
                       PythonThriftLibrary)
   self.create_target('test_thrift_replacement', dedent('''
     python_thrift_library(name='one',
       sources=['thrift/keyword.thrift'],
       dependencies=None
     )
   '''))
示例#4
0
 def setUpClass(self):
     super(TestPythonThriftBuilder, self).setUpClass()
     SourceRoot.register(
         os.path.realpath(
             os.path.join(self.build_root, 'test_thrift_replacement')),
         PythonThriftLibrary)
     self.create_target(
         'test_thrift_replacement',
         dedent('''
   python_thrift_library(name='one',
     sources=['thrift/keyword.thrift'],
     dependencies=None
   )
 '''))
示例#5
0
    def __init__(self, name, sources=None, exclusives=None):
        Target.__init__(self, name, exclusives=exclusives)

        self.add_labels('sources')
        self.target_base = SourceRoot.find(self)
        self._unresolved_sources = sources or []
        self._resolved_sources = None
示例#6
0
  def parse(self, **global_args):
    """The entry point to parsing of a BUILD file.  Changes the working directory to the BUILD file
    directory and then evaluates the BUILD file with the ROOT_DIR and __file__ globals set in
    addition to any globals specified as kwargs.  As target methods are parsed they can examine the
    stack to find these globals and thus locate themselves for the purposes of finding files
    (see locate() and bind())."""

    from twitter.pants.targets.sources import SourceRoot

    if self.buildfile not in ParseContext._parsed:
      buildfile_family = tuple(self.buildfile.family())

      pants_context = {}
      for str_to_exec in self._strs_to_exec:
        ast = compile(str_to_exec, '<string>', 'exec')
        Compatibility.exec_function(ast, pants_context)

      with ParseContext.activate(self):
        for buildfile in buildfile_family:
          # We may have traversed a sibling already, guard against re-parsing it.
          if buildfile not in ParseContext._parsed:
            ParseContext._parsed.add(buildfile)

            buildfile_dir = os.path.dirname(buildfile.full_path)

            eval_globals = copy.copy(pants_context)
            eval_globals.update({
              'ROOT_DIR': buildfile.root_dir,
              '__file__': buildfile.full_path,
              'globs': Fileset.lazy_rel_globs(buildfile_dir),
              'rglobs': Fileset.lazy_rel_rglobs(buildfile_dir),
              'source_root': SourceRoot.lazy_rel_source_root(buildfile_dir),
            })
            eval_globals.update(global_args)
            Compatibility.exec_function(buildfile.code(), eval_globals)
示例#7
0
  def __init__(self, name, sources=None, exclusives=None):
    Target.__init__(self, name, exclusives=exclusives)

    self.add_labels('sources')
    self.target_base = SourceRoot.find(self)
    self._unresolved_sources = sources or []
    self._resolved_sources = None
示例#8
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(twitter.pants.base.build_file_context,
                               dependees_type):
                        type_name = getattr(
                            twitter.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)
示例#9
0
  def setUpClass(cls):
    super(JarCreateExecuteTest, cls).setUpClass()

    def get_source_root_fs_path(path):
        return os.path.realpath(os.path.join(cls.build_root, path))

    SourceRoot.register(get_source_root_fs_path('src/resources'), Resources)
    SourceRoot.register(get_source_root_fs_path('src/java'), JavaLibrary)
    SourceRoot.register(get_source_root_fs_path('src/scala'), ScalaLibrary)
    SourceRoot.register(get_source_root_fs_path('src/thrift'), JavaThriftLibrary)

    cls.res = cls.resources('src/resources/com/twitter', 'spam', 'r.txt')
    cls.jl = cls.java_library('src/java/com/twitter', 'foo', ['a.java'],
                              resources='src/resources/com/twitter:spam')
    cls.sl = cls.scala_library('src/scala/com/twitter', 'bar', ['c.scala'])
    cls.jtl = cls.java_thrift_library('src/thrift/com/twitter', 'baz', 'd.thrift')
示例#10
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(twitter.pants.base.build_file_context, dependees_type):
            type_name = getattr(twitter.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)
示例#11
0
  def __init__(self, name, is_meta=False):
    Target.__init__(self, name, is_meta)

    self.target_base = SourceRoot.find(self)
示例#12
0
 def setUpClass(cls):
   super(TargetsHelpTest, cls).setUpClass()
   SourceRoot.register(os.path.join(get_buildroot(), 'fakeroot'), TargetsHelpTest.MyTarget)
示例#13
0
 def root(path, *types):
     SourceRoot.register(
         os.path.join(basedir, path) if basedir else path, *types)
示例#14
0
 def root(path, *types):
   SourceRoot.register(os.path.join(basedir, path) if basedir else path, *types)
示例#15
0
 def console_output(self, targets):
     for src_root, targets in SourceRoot.all_roots().items():
         all_targets = ','.join(sorted([tgt.__name__ for tgt in targets]))
         yield '%s: %s' % (src_root, all_targets or '*')
示例#16
0
 def here(*allowed_target_types):
     """Registers the cwd as a source root for the given target types."""
     SourceRoot.register(buildfile_dir,
                         *allowed_target_types)
示例#17
0
 def tearDownClass(cls):
   BuildRoot().reset()
   SourceRoot.reset()
   safe_rmtree(cls.build_root)
示例#18
0
  def __init__(self, name, is_meta=False):
    Target.__init__(self, name, is_meta)

    self.add_label('sources')
    self.target_base = SourceRoot.find(self)
示例#19
0
 def console_output(self, targets):
   for src_root, targets in SourceRoot.all_roots().items():
     all_targets = ','.join(sorted([tgt.__name__ for tgt in targets]))
     yield '%s: %s' % (src_root, all_targets or '*')
示例#20
0
 def _create_new_target(self, target_base, target_type, *args, **kwargs):
   if not os.path.exists(target_base):
     os.makedirs(target_base)
   SourceRoot.register(target_base, target_type)
   with ParseContext.temp(target_base):
     return target_type(*args, **kwargs)
示例#21
0
 def __init__(self, basedir, *allowed_target_types):
   SourceRoot.register(os.path.join(buildfile_dir, basedir), *allowed_target_types)
示例#22
0
 def tearDownClass(cls):
     BuildRoot().reset()
     SourceRoot.reset()
     safe_rmtree(cls.build_root)
示例#23
0
 def __init__(self, basedir, *allowed_target_types):
     SourceRoot.register(
         os.path.join(buildfile_dir, basedir),
         *allowed_target_types)
示例#24
0
 def _create_new_target(self, target_base, target_type, *args, **kwargs):
     if not os.path.exists(target_base):
         os.makedirs(target_base)
     SourceRoot.register(target_base, target_type)
     with ParseContext.temp(target_base):
         return target_type(*args, **kwargs)
示例#25
0
 def _set_up_mocks(self, class_type, src_roots):
   self.mox.StubOutWithMock(SourceRoot, 'roots')
   SourceRoot.roots(class_type).AndReturn(src_roots)
   self.mox.ReplayAll()
示例#26
0
 def here(*allowed_target_types):
   """Registers the cwd as a source root for the given target types."""
   SourceRoot.register(buildfile_dir, *allowed_target_types)