예제 #1
0
  def create_scaladoc_command(self, classpath, gendir, *targets):
    sources = []
    for target in targets:
      sources.extend(target.sources_relative_to_buildroot())
      # TODO(Tejal Desai): pantsbuild/pants/65: Remove java_sources attribute for ScalaLibrary
      # A '.scala' owning target may not have java_sources, eg: junit_tests
      if hasattr(target, 'java_sources'):
        for java_target in target.java_sources:
          sources.extend(java_target.sources_relative_to_buildroot())

    if not sources:
      return None

    scala_platform = ScalaPlatform.global_instance()
    tool_classpath = [cp_entry.path for cp_entry in scala_platform.compiler_classpath_entries(
      self.context.products, self.context._scheduler)]

    args = ['-usejavacp',
            '-classpath', ':'.join(classpath),
            '-d', gendir]

    args.extend(self.args)

    args.extend(sources)

    java_executor = SubprocessExecutor(DistributionLocator.cached())
    runner = java_executor.runner(jvm_options=self.jvm_options,
                                  classpath=tool_classpath,
                                  main='scala.tools.nsc.ScalaDoc',
                                  args=args)
    return runner.command
예제 #2
0
    def test_scala_exclude(self):
        init_subsystem(ScalaPlatform)

        name = "foo_lib"
        suffixed_name = ScalaPlatform.global_instance().suffix_version(name)

        self.assertEqual(suffixed_name, ScalaExclude(org="example.com", name=name).name)
예제 #3
0
  def create_scaladoc_command(self, classpath, gendir, *targets):
    sources = []
    for target in targets:
      sources.extend(target.sources_relative_to_buildroot())
      # TODO(Tejal Desai): pantsbuild/pants/65: Remove java_sources attribute for ScalaLibrary
      for java_target in target.java_sources:
        sources.extend(java_target.sources_relative_to_buildroot())

    if not sources:
      return None

    scala_platform = ScalaPlatform.global_instance()
    tool_classpath = scala_platform.compiler_classpath(self.context.products)

    args = ['-usejavacp',
            '-classpath', ':'.join(classpath),
            '-d', gendir]

    args.extend(self.args)

    args.extend(sources)

    java_executor = SubprocessExecutor()
    runner = java_executor.runner(jvm_options=self.jvm_options,
                                  classpath=tool_classpath,
                                  main='scala.tools.nsc.ScalaDoc',
                                  args=args)
    return runner.command
예제 #4
0
 def setup_repl_session(self, targets):
     repl_name = ScalaPlatform.global_instance().repl
     return (
         self.tool_classpath("pants-runner")
         + self.tool_classpath(repl_name, scope=ScalaPlatform.options_scope)
         + self.classpath(targets)
     )
예제 #5
0
 def setup_repl_session(self, targets):
     repl_name = ScalaPlatform.global_instance().repl
     return (
         self.tool_classpath('pants-runner') +
         self.tool_classpath(repl_name, scope=ScalaPlatform.options_scope) +
         self.classpath(targets),
         self.preferred_jvm_distribution_for_targets(targets))
예제 #6
0
파일: ide_gen.py 프로젝트: CaitieM20/pants
  def execute(self):
    """Stages IDE project artifacts to a project directory and generates IDE configuration files."""
    # Grab the targets in-play before the context is replaced by `self._prepare_project()` below.
    targets = self.context.targets()

    self._prepare_project()

    if self.context.options.is_known_scope('compile.checkstyle'):
      checkstyle_classpath = self.tool_classpath('checkstyle', scope='compile.checkstyle')
    else:  # Checkstyle not enabled.
      checkstyle_classpath = []

    if self.skip_scala:
      scalac_classpath = []
    else:
      scalac_classpath = ScalaPlatform.global_instance().compiler_classpath(self.context.products)

    self._project.set_tool_classpaths(checkstyle_classpath, scalac_classpath)
    self.map_internal_jars(targets)
    self.map_external_jars(targets)

    idefile = self.generate_project(self._project)
    if idefile:
      try:
        desktop.ui_open(idefile)
      except desktop.OpenError as e:
        raise TaskError(e)
예제 #7
0
    def compute_dependency_specs(cls, kwargs=None, payload=None):
        for spec in super().compute_dependency_specs(kwargs, payload):
            yield spec

        for spec in ScalaPlatform.global_instance().injectables_specs_for_key(
                'scalac'):
            yield spec
예제 #8
0
파일: scaladoc_gen.py 프로젝트: wiwa/pants
    def create_scaladoc_command(self, classpath, gendir, *targets):
        sources = []
        for target in targets:
            sources.extend(target.sources_relative_to_buildroot())
            # TODO(Tejal Desai): pantsbuild/pants/65: Remove java_sources attribute for ScalaLibrary
            # A '.scala' owning target may not have java_sources, eg: junit_tests
            if hasattr(target, "java_sources"):
                for java_target in target.java_sources:
                    sources.extend(java_target.sources_relative_to_buildroot())

        if not sources:
            return None

        scala_platform = ScalaPlatform.global_instance()
        tool_classpath = [
            cp_entry.path
            for cp_entry in scala_platform.compiler_classpath_entries(self.context.products)
        ]

        args = ["-usejavacp", "-classpath", ":".join(classpath), "-d", gendir]

        args.extend(self.args)

        args.extend(sources)

        java_executor = SubprocessExecutor(self.preferred_jvm_distribution_for_targets(targets))
        runner = java_executor.runner(
            jvm_options=self.jvm_options,
            classpath=tool_classpath,
            main="scala.tools.nsc.ScalaDoc",
            args=args,
        )
        return runner.command
예제 #9
0
    def execute(self):
        """Stages IDE project artifacts to a project directory and generates IDE configuration files."""
        # Grab the targets in-play before the context is replaced by `self._prepare_project()` below.
        targets = self.context.targets()

        self._prepare_project()

        if self.context.options.is_known_scope('lint.checkstyle'):
            checkstyle_classpath = self.tool_classpath('checkstyle',
                                                       scope='lint.checkstyle')
        else:  # Checkstyle not enabled.
            checkstyle_classpath = []

        if self.skip_scala:
            scalac_classpath = []
        else:
            scalac_classpath = ScalaPlatform.global_instance(
            ).compiler_classpath(self.context.products)

        self._project.set_tool_classpaths(checkstyle_classpath,
                                          scalac_classpath)
        self.map_internal_jars(targets)
        self.map_external_jars(targets)

        idefile = self.generate_project(self._project)
        if idefile:
            try:
                desktop.ui_open(idefile)
            except desktop.OpenError as e:
                raise TaskError(e)
예제 #10
0
  def _language_platform_version_info(self):
    ret = []

    # Go through all the bootstrap tools required to compile.
    targets = (ScalaPlatform.global_instance().tool_targets(self.context, 'scalac') +
               self.tool_targets(self.context, 'zinc'))
    for lib in (t for t in targets if isinstance(t, JarLibrary)):
      for jar in lib.jar_dependencies:
        ret.append(jar.cache_key())

    # We must invalidate on the set of plugins and their settings.
    ret.extend(self.plugin_args())

    # Invalidate if any compiler args change.
    # Note that while some args are obviously important for invalidation (e.g., the jvm target
    # version), some might not be. However we must invalidated on all the args, because Zinc
    # ignores analysis files if the compiler args they were created with are different from the
    # current ones, and does a full recompile. So if we allow cached artifacts with those analysis
    # files to be used, Zinc will do unnecessary full recompiles on subsequent edits.
    ret.extend(self._args)

    # Invalidate if use of name hashing changes.
    ret.append('name-hashing-{0}'.format('on' if self.get_options().name_hashing else 'off'))

    return ret
예제 #11
0
    def create_scaladoc_command(self, classpath, gendir, *targets):
        sources = []
        for target in targets:
            sources.extend(target.sources_relative_to_buildroot())
            # TODO(Tejal Desai): pantsbuild/pants/65: Remove java_sources attribute for ScalaLibrary
            # A '.scala' owning target may not have java_sources, eg: junit_tests
            if hasattr(target, 'java_sources'):
                for java_target in target.java_sources:
                    sources.extend(java_target.sources_relative_to_buildroot())

        if not sources:
            return None

        scala_platform = ScalaPlatform.global_instance()
        tool_classpath = scala_platform.compiler_classpath(
            self.context.products)

        args = ['-usejavacp', '-classpath', ':'.join(classpath), '-d', gendir]

        args.extend(self.args)

        args.extend(sources)

        java_executor = SubprocessExecutor(DistributionLocator.cached())
        runner = java_executor.runner(jvm_options=self.jvm_options,
                                      classpath=tool_classpath,
                                      main='scala.tools.nsc.ScalaDoc',
                                      args=args)
        return runner.command
예제 #12
0
    def _compile_vts(self, vts, ctx, upstream_analysis, dependency_classpath,
                     progress_message, settings, compiler_option_sets,
                     zinc_file_manager, counter):
        """Compiles sources for the given vts into the given output dir.

    :param vts: VersionedTargetSet with one entry for the target.
    :param ctx: - A CompileContext instance for the target.
    :param dependency_classpath: A list of classpath entries of type ClasspathEntry for dependencies

    May be invoked concurrently on independent target sets.

    Postcondition: The individual targets in vts are up-to-date, as if each were
                   compiled individually.
    """
        if not ctx.sources:
            self.context.log.warn(
                'Skipping {} compile for targets with no sources:\n  {}'.
                format(self.name(), vts.targets))
        else:
            counter_val = str(counter()).rjust(counter.format_length(), ' ')
            counter_str = '[{}/{}] '.format(counter_val, counter.size)
            # Do some reporting.
            self.context.log.info(
                counter_str, 'Compiling ',
                items_to_report_element(ctx.sources,
                                        '{} source'.format(self.name())),
                ' in ',
                items_to_report_element(
                    [t.address.reference() for t in vts.targets], 'target'),
                ' (', progress_message, ').')
            with self.context.new_workunit('compile',
                                           labels=[WorkUnitLabel.COMPILER
                                                   ]) as compile_workunit:
                try:
                    directory_digest = self.compile(
                        ctx,
                        self._args,
                        dependency_classpath,
                        upstream_analysis,
                        settings,
                        compiler_option_sets,
                        zinc_file_manager,
                        self._get_plugin_map('javac', Java.global_instance(),
                                             ctx.target),
                        self._get_plugin_map('scalac',
                                             ScalaPlatform.global_instance(),
                                             ctx.target),
                    )
                    self._capture_logs(compile_workunit, ctx.log_dir)
                    return directory_digest
                except TaskError:
                    if self.get_options().suggest_missing_deps:
                        logs = [
                            path for _, name, _, path in self._find_logs(
                                compile_workunit) if name == self.name()
                        ]
                        if logs:
                            self._find_missing_deps(logs, ctx.target)
                    raise
예제 #13
0
    def traversable_dependency_specs(self):
        for spec in super(ScalaLibrary, self).traversable_dependency_specs:
            yield spec

        # TODO(John Sirois): Targets should be able to set their scala platform version
        # explicitly, and not have to conform to this global setting.
        for library_spec in ScalaPlatform.global_instance().runtime:
            yield library_spec
예제 #14
0
  def traversable_dependency_specs(self):
    for spec in super(ScalaLibrary, self).traversable_dependency_specs:
      yield spec

    # TODO(John Sirois): Targets should be able to set their scala platform version
    # explicitly, and not have to conform to this global setting.
    for library_spec in ScalaPlatform.global_instance().runtime:
      yield library_spec
예제 #15
0
 def _fetch_tool_jar_from_scalac_classpath(self, products, jar_name):
   scala_version = ScalaPlatform.global_instance().version
   classpath = self.tool_classpath_from_products(products,
                                                 ScalaPlatform.versioned_tool_name('scalac', scala_version),
                                                 scope=self.options_scope)
   candidates = [jar for jar in classpath if jar_name in jar]
   assert(len(candidates) == 1)
   return candidates[0]
예제 #16
0
파일: zinc.py 프로젝트: baroquebobcat/pants
  def scalac_compiler_plugins_src(self, zinc_compile_instance=None):
    """Returns an instance of JvmToolMixin that should provide scalac compiler plugins.

    TODO: Remove this method once the deprecation of `(scalac|javac)_plugins` on Zinc has
    completed in `1.9.0.dev0`.
    """
    return Zinc._select_jvm_tool_mixin(zinc_compile_instance,
                                       ScalaPlatform.global_instance(),
                                       ['scalac_plugins', 'scalac_plugin_args', 'scalac_plugin_dep'])
예제 #17
0
파일: zinc.py 프로젝트: hrfuller/pants
 def _fetch_tool_jar_from_scalac_classpath(self, products, jar_name):
     scala_version = ScalaPlatform.global_instance().version
     classpath = self.tool_classpath_from_products(
         products,
         ScalaPlatform.versioned_tool_name('scalac', scala_version),
         scope=self.options_scope)
     candidates = [jar for jar in classpath if jar_name in jar]
     assert (len(candidates) == 1)
     return candidates[0]
예제 #18
0
파일: zinc.py 프로젝트: tglstory/pants
  def scalac_compiler_plugins_src(self, zinc_compile_instance=None):
    """Returns an instance of JvmToolMixin that should provide scalac compiler plugins.

    TODO: Remove this method once the deprecation of `(scalac|javac)_plugins` on Zinc has
    completed in `1.9.0.dev0`.
    """
    return Zinc._select_jvm_tool_mixin(zinc_compile_instance,
                                       ScalaPlatform.global_instance(),
                                       ['scalac_plugins', 'scalac_plugin_args', 'scalac_plugin_dep'])
예제 #19
0
  def test_scala_exclude(self):
    init_subsystem(ScalaPlatform)

    name = 'foo_lib'
    suffixed_name = ScalaPlatform.global_instance().suffix_version(name)

    self.assertEqual(
        suffixed_name,
        ScalaExclude(org='example.com', name=name).name
      )
예제 #20
0
  def _compile_vts(self, vts, ctx, upstream_analysis, classpath, progress_message, settings, 
                   compiler_option_sets, zinc_file_manager, counter):
    """Compiles sources for the given vts into the given output dir.

    :param vts: VersionedTargetSet with one entry for the target.
    :param ctx: - A CompileContext instance for the target.
    :param classpath: A list of classpath entries

    May be invoked concurrently on independent target sets.

    Postcondition: The individual targets in vts are up-to-date, as if each were
                   compiled individually.
    """
    if not ctx.sources:
      self.context.log.warn('Skipping {} compile for targets with no sources:\n  {}'
                            .format(self.name(), vts.targets))
    else:
      counter_val = str(counter()).rjust(counter.format_length(), b' ')
      counter_str = '[{}/{}] '.format(counter_val, counter.size)
      # Do some reporting.
      self.context.log.info(
        counter_str,
        'Compiling ',
        items_to_report_element(ctx.sources, '{} source'.format(self.name())),
        ' in ',
        items_to_report_element([t.address.reference() for t in vts.targets], 'target'),
        ' (',
        progress_message,
        ').')
      with self.context.new_workunit('compile', labels=[WorkUnitLabel.COMPILER]) as compile_workunit:
        if self.get_options().capture_classpath:
          self._record_compile_classpath(classpath, vts.targets, ctx.classes_dir)

        try:
          self.compile(
            ctx,
            self._args,
            classpath,
            upstream_analysis,
            settings,
            compiler_option_sets,
            zinc_file_manager,
            self._get_plugin_map('javac', Java.global_instance(), ctx.target),
            self._get_plugin_map('scalac', ScalaPlatform.global_instance(), ctx.target),
          )
          self._capture_logs(compile_workunit, ctx.log_dir)
        except TaskError:
          if self.get_options().suggest_missing_deps:
            logs = [path
                    for _, name, _, path in self._find_logs(compile_workunit)
                    if name == self.name()]
            if logs:
              self._find_missing_deps(logs, ctx.target)
          raise
예제 #21
0
  def _compiler_plugins_cp_entries(self):
    """Any additional global compiletime classpath entries for compiler plugins."""
    java_options_src = Java.global_instance()
    scala_options_src = ScalaPlatform.global_instance()

    def cp(instance, toolname):
      scope = instance.options_scope
      return instance.tool_classpath_entries_from_products(self._products, toolname, scope=scope)
    classpaths = (cp(java_options_src, 'javac-plugin-dep') +
                  cp(scala_options_src, 'scalac-plugin-dep'))
    return [(conf, jar) for conf in self.DEFAULT_CONFS for jar in classpaths]
예제 #22
0
    def initialize_graph_info(self):
        scala_platform = ScalaPlatform.global_instance()
        scala_platform_map = {
            "scala_version":
            scala_platform.version,
            "compiler_classpath": [
                cp_entry.path
                for cp_entry in scala_platform.compiler_classpath_entries(
                    self.context.products)
            ],
        }

        jvm_platforms_map = {
            "default_platform":
            JvmPlatform.global_instance().default_platform.name,
            "platforms": {
                str(platform_name): {
                    "target_level": str(platform.target_level),
                    "source_level": str(platform.source_level),
                    "args": platform.args,
                }
                for platform_name, platform in
                JvmPlatform.global_instance().platforms_by_name.items()
            },
        }

        graph_info = {
            "version": DEFAULT_EXPORT_VERSION,
            "targets": {},
            "jvm_platforms": jvm_platforms_map,
            "scala_platform": scala_platform_map,
            # `jvm_distributions` are static distribution settings from config,
            # `preferred_jvm_distributions` are distributions that pants actually uses for the
            # given platform setting.
            "preferred_jvm_distributions": {},
        }

        for platform_name, platform in JvmPlatform.global_instance(
        ).platforms_by_name.items():
            preferred_distributions = {}
            for strict, strict_key in [(True, "strict"),
                                       (False, "non_strict")]:
                try:
                    dist = JvmPlatform.preferred_jvm_distribution(
                        [platform], strict=strict)
                    preferred_distributions[strict_key] = dist.home
                except DistributionLocator.Error:
                    pass

            if preferred_distributions:
                graph_info["preferred_jvm_distributions"][
                    platform_name] = preferred_distributions

        return graph_info
예제 #23
0
  def _compiler_plugins_cp_entries(self):
    """Any additional global compiletime classpath entries for compiler plugins."""
    java_options_src = Java.global_instance()
    scala_options_src = ScalaPlatform.global_instance()

    def cp(instance, toolname):
      scope = instance.options_scope
      return instance.tool_classpath_from_products(self._products, toolname, scope=scope)
    classpaths = (cp(java_options_src, 'javac-plugin-dep') +
                  cp(scala_options_src, 'scalac-plugin-dep'))
    return [(conf, ClasspathEntry(jar)) for conf in self.DEFAULT_CONFS for jar in classpaths]
예제 #24
0
    def initialize_graph_info(self):
        scala_platform = ScalaPlatform.global_instance()
        scala_platform_map = {
            'scala_version':
            scala_platform.version,
            'compiler_classpath': [
                cp_entry.path
                for cp_entry in scala_platform.compiler_classpath_entries(
                    self.context.products)
            ],
        }

        jvm_platforms_map = {
            'default_platform':
            JvmPlatform.global_instance().default_platform.name,
            'platforms': {
                str(platform_name): {
                    'target_level': str(platform.target_level),
                    'source_level': str(platform.source_level),
                    'args': platform.args,
                }
                for platform_name, platform in
                JvmPlatform.global_instance().platforms_by_name.items()
            },
        }

        graph_info = {
            'version': DEFAULT_EXPORT_VERSION,
            'targets': {},
            'jvm_platforms': jvm_platforms_map,
            'scala_platform': scala_platform_map,
            # `jvm_distributions` are static distribution settings from config,
            # `preferred_jvm_distributions` are distributions that pants actually uses for the
            # given platform setting.
            'preferred_jvm_distributions': {}
        }

        for platform_name, platform in JvmPlatform.global_instance(
        ).platforms_by_name.items():
            preferred_distributions = {}
            for strict, strict_key in [(True, 'strict'),
                                       (False, 'non_strict')]:
                try:
                    dist = JvmPlatform.preferred_jvm_distribution(
                        [platform], strict=strict)
                    preferred_distributions[strict_key] = dist.home
                except DistributionLocator.Error:
                    pass

            if preferred_distributions:
                graph_info['preferred_jvm_distributions'][
                    platform_name] = preferred_distributions

        return graph_info
예제 #25
0
    def compute_dependency_specs(cls, kwargs=None, payload=None):
        for spec in super().compute_dependency_specs(kwargs, payload):
            yield spec

        for spec in ScalaPlatform.global_instance().injectables_specs_for_key(
                'scala-library'):
            yield spec

        if ScoveragePlatform.global_instance().get_options().enable_scoverage:
            for spec in ScoveragePlatform.global_instance(
            ).injectables_specs_for_key('scoverage'):
                yield spec
예제 #26
0
        def call(srcs):
          def to_java_boolean(x):
            return str(x).lower()

          cp = ScalaPlatform.global_instance().style_classpath(self.context.products)
          scalastyle_args = [
            '-c', scalastyle_config,
            '-v', to_java_boolean(scalastyle_verbose),
            '-q', to_java_boolean(scalastyle_quiet),
            ]
          return self.runjava(classpath=cp,
                              main=self._MAIN,
                              jvm_options=self.get_options().jvm_options,
                              args=scalastyle_args + srcs)
예제 #27
0
    def create_extra_products_for_targets(self, targets):
        if not targets:
            return
        if self.context.products.is_required_data("zinc_args"):
            zinc_args = self.context.products.get_data("zinc_args")
            with self.invalidated(
                    targets, invalidate_dependents=False,
                    topological_order=True) as invalidation_check:

                compile_contexts = {
                    vt.target:
                    self.create_compile_context(vt.target, vt.results_dir)
                    for vt in invalidation_check.all_vts
                }
                runtime_compile_contexts = {
                    target: self.select_runtime_context(cc)
                    for target, cc in compile_contexts.items()
                }
                for vt in invalidation_check.all_vts:
                    dependency_classpath = self._zinc.compile_classpath_entries(
                        "runtime_classpath",
                        vt.target,
                        extra_cp_entries=self._extra_compile_time_classpath,
                    )
                    dep_context = DependencyContext.global_instance()
                    compiler_option_sets = dep_context.defaulted_property(
                        vt.target, "compiler_option_sets")
                    zinc_file_manager = dep_context.defaulted_property(
                        vt.target, "zinc_file_manager")
                    ctx = runtime_compile_contexts[vt.target]
                    absolute_classpath = (ctx.classes_dir.path, ) + tuple(
                        ce.path for ce in dependency_classpath)
                    upstream_analysis = dict(
                        self._upstream_analysis(compile_contexts,
                                                dependency_classpath))
                    zinc_args[vt.target] = self.create_zinc_args(
                        ctx,
                        self._args,
                        upstream_analysis,
                        absolute_classpath,
                        vt.target.platform,
                        compiler_option_sets,
                        zinc_file_manager,
                        self._get_plugin_map("javac", Java.global_instance(),
                                             ctx.target),
                        self._get_plugin_map("scalac",
                                             ScalaPlatform.global_instance(),
                                             ctx.target),
                    )
예제 #28
0
  def defaulted_property(self, target, option_name):
    """Computes a language property setting for the given JvmTarget.

    :param selector A function that takes a target or platform and returns the boolean value of the
                    property for that target or platform, or None if that target or platform does
                    not directly define the property.

    If the target does not override the language property, returns true iff the property
    is true for any of the matched languages for the target.
    """
    if target.has_sources('.java'):
      matching_subsystem = Java.global_instance()
    elif target.has_sources('.scala'):
      matching_subsystem = ScalaPlatform.global_instance()
    else:
      return getattr(target, option_name)

    return matching_subsystem.get_scalar_mirrored_target_option(option_name, target)
예제 #29
0
  def defaulted_property(self, target, selector):
    """Computes a language property setting for the given JvmTarget.

    :param selector A function that takes a target or platform and returns the boolean value of the
                    property for that target or platform, or None if that target or platform does
                    not directly define the property.

    If the target does not override the language property, returns true iff the property
    is true for any of the matched languages for the target.
    """
    if selector(target) is not None:
      return selector(target)

    prop = False
    if target.has_sources('.java'):
      prop |= selector(Java.global_instance())
    if target.has_sources('.scala'):
      prop |= selector(ScalaPlatform.global_instance())
    return prop
예제 #30
0
    def defaulted_property(self, target, selector):
        """Computes a language property setting for the given JvmTarget.

    :param selector A function that takes a target or platform and returns the boolean value of the
                    property for that target or platform, or None if that target or platform does
                    not directly define the property.

    If the target does not override the language property, returns true iff the property
    is true for any of the matched languages for the target.
    """
        if selector(target) is not None:
            return selector(target)

        prop = False
        if target.has_sources('.java'):
            prop |= selector(Java.global_instance())
        if target.has_sources('.scala'):
            prop |= selector(ScalaPlatform.global_instance())
        return prop
예제 #31
0
    def defaulted_property(self, target, option_name):
        """Computes a language property setting for the given JvmTarget.

    :param selector A function that takes a target or platform and returns the boolean value of the
                    property for that target or platform, or None if that target or platform does
                    not directly define the property.

    If the target does not override the language property, returns true iff the property
    is true for any of the matched languages for the target.
    """
        if target.has_sources('.java'):
            matching_subsystem = Java.global_instance()
        elif target.has_sources('.scala'):
            matching_subsystem = ScalaPlatform.global_instance()
        else:
            return getattr(target, option_name)

        return matching_subsystem.get_scalar_mirrored_target_option(
            option_name, target)
예제 #32
0
    def _compute_language_property(self, target, selector):
        """Computes the a language property setting for the given target sources.

    :param target The target whose language property will be calculated.
    :param selector A function that takes a target or platform and returns the boolean value of the
                    property for that target or platform, or None if that target or platform does
                    not directly define the property.

    If the target does not override the language property, returns true iff the property
    is true for any of the matched languages for the target.
    """
        if selector(target) is not None:
            return selector(target)

        property = False
        if target.has_sources(".java"):
            property |= selector(Java.global_instance())
        if target.has_sources(".scala"):
            property |= selector(ScalaPlatform.global_instance())
        return property
예제 #33
0
 def name(self):
     return ScalaPlatform.global_instance().suffix_version(self._base_name)
예제 #34
0
 def scalac_classpath(self):
     return ScalaPlatform.global_instance().compiler_classpath(
         self.context.products)
예제 #35
0
  def compute_dependency_specs(cls, kwargs=None, payload=None):
    for spec in super(ScalacPlugin, cls).compute_dependency_specs(kwargs, payload):
      yield spec

    for spec in ScalaPlatform.global_instance().injectables_specs_for_key('scalac'):
      yield spec
예제 #36
0
 def scalac_classpath_entries(self):
   """Returns classpath entries for the scalac classpath."""
   return ScalaPlatform.global_instance().compiler_classpath_entries(
     self.context.products, self.context._scheduler)
예제 #37
0
파일: scala_exclude.py 프로젝트: wiwa/pants
 def name(self):
     base_name = super().name
     return ScalaPlatform.global_instance().suffix_version(base_name)
예제 #38
0
 def scalac_classpath_entries(self):
     """Returns classpath entries for the scalac classpath."""
     return ScalaPlatform.global_instance().compiler_classpath_entries(
         self.context.products)
예제 #39
0
 def compiler_classpath(self):
   return ScalaPlatform.global_instance().compiler_classpath(self.context.products)
예제 #40
0
 def _scala_platform(self):
     return ScalaPlatform.global_instance()
예제 #41
0
 def name(self, value):
     self._base_name = ScalaPlatform.global_instance().suffix_version(value)
예제 #42
0
 def setup_repl_session(self, targets):
     repl_name = ScalaPlatform.global_instance().repl
     return self.tool_classpath('pants-runner') +\
            self.tool_classpath(repl_name, scope=ScalaPlatform.options_scope) +\
            self.classpath(targets)
예제 #43
0
  def generate_targets_map(self, targets, runtime_classpath):
    """Generates a dictionary containing all pertinent information about the target graph.

    The return dictionary is suitable for serialization by json.dumps.
    :param targets: The list of targets to generate the map for.
    :param classpath_products: Optional classpath_products. If not provided when the --libraries
      option is `True`, this task will perform its own jar resolution.
    """

    def _get_target_type(tgt):
      def is_test(t):
        return isinstance(t, JUnitTests)

      if is_test(tgt):
        return SourceRootTypes.TEST
      else:
        if (isinstance(tgt, Resources) and
          tgt in resource_target_map and
          is_test(resource_target_map[tgt])):
          return SourceRootTypes.TEST_RESOURCE
        elif isinstance(tgt, Resources):
          return SourceRootTypes.RESOURCE
        else:
          return SourceRootTypes.SOURCE

    targets_map = {}
    resource_target_map = {}
    target_roots_set = set(self.context.target_roots)

    def process_target(current_target):
      """
      :type current_target:pants.build_graph.target.Target
      """
      info = {
        # this means 'dependencies'
        'targets': [],
        'libraries': [],
        'roots': [],
        'id': current_target.id,
        'target_type': _get_target_type(current_target),
        'is_synthetic': current_target.is_synthetic,
        'pants_target_type': self._get_pants_target_alias(type(current_target)),
        'is_target_root': current_target in target_roots_set,
        'transitive': current_target.transitive,
        'scope': str(current_target.scope)
      }

      if not current_target.is_synthetic:
        info['globs'] = current_target.globs_relative_to_buildroot()

      def iter_transitive_jars(jar_lib):
        """
        :type jar_lib: :class:`pants.backend.jvm.targets.jar_library.JarLibrary`
        :rtype: :class:`collections.Iterator` of
                :class:`pants.java.jar.M2Coordinate`
        """
        if runtime_classpath:
          jar_products = runtime_classpath.get_artifact_classpath_entries_for_targets((jar_lib,))
          for _, jar_entry in jar_products:
            coordinate = jar_entry.coordinate
            # We drop classifier and type_ since those fields are represented in the global
            # libraries dict and here we just want the key into that dict (see `_jar_id`).
            yield M2Coordinate(org=coordinate.org, name=coordinate.name, rev=coordinate.rev)

      target_libraries = OrderedSet()
      if isinstance(current_target, JarLibrary):
        target_libraries = OrderedSet(iter_transitive_jars(current_target))
      for dep in current_target.dependencies:
        info['targets'].append(dep.address.spec)
        if isinstance(dep, JarLibrary):
          for jar in dep.jar_dependencies:
            target_libraries.add(M2Coordinate(jar.org, jar.name, jar.rev))
          # Add all the jars pulled in by this jar_library
          target_libraries.update(iter_transitive_jars(dep))
        if isinstance(dep, Resources):
          resource_target_map[dep] = current_target

      if isinstance(current_target, ScalaLibrary):
        for dep in current_target.java_sources:
          info['targets'].append(dep.address.spec)

      if isinstance(current_target, JvmTarget):
        info['excludes'] = [self._exclude_id(exclude) for exclude in current_target.excludes]
        info['platform'] = current_target.platform.name
        if hasattr(current_target, 'test_platform'):
          info['test_platform'] = current_target.test_platform.name

      if runtime_classpath:
        info['libraries'].extend(self._jar_id(lib) for lib in target_libraries)

      if current_target in target_roots_set:
        info['roots'] = [{
          'source_root': os.path.realpath(source_root_package_prefix[0]),
          'package_prefix': source_root_package_prefix[1]
        } for source_root_package_prefix in self._source_roots_for_target(current_target)]

      targets_map[current_target.address.spec] = info

    additional_java_targets = []
    for t in targets:
      if isinstance(t, ScalaLibrary):
        additional_java_targets.extend(t.java_sources)

    targets.extend(additional_java_targets)

    for target in targets:
      process_target(target)

    scala_platform = ScalaPlatform.global_instance()
    scala_platform_map = {
      'scala_version': scala_platform.version,
      'compiler_classpath': [
        cp_entry.path
        for cp_entry in scala_platform.compiler_classpath_entries(self.context.products)
      ],
    }

    jvm_platforms_map = {
      'default_platform': JvmPlatform.global_instance().default_platform.name,
      'platforms': {
        str(platform_name): {
          'target_level': str(platform.target_level),
          'source_level': str(platform.source_level),
          'args': platform.args,
        } for platform_name, platform in JvmPlatform.global_instance().platforms_by_name.items()},
    }

    graph_info = {
      'version': DEFAULT_EXPORT_VERSION,
      'targets': targets_map,
      'jvm_platforms': jvm_platforms_map,
      'scala_platform': scala_platform_map,
      # `jvm_distributions` are static distribution settings from config,
      # `preferred_jvm_distributions` are distributions that pants actually uses for the
      # given platform setting.
      'preferred_jvm_distributions': {}
    }

    for platform_name, platform in JvmPlatform.global_instance().platforms_by_name.items():
      preferred_distributions = {}
      for strict, strict_key in [(True, 'strict'), (False, 'non_strict')]:
        try:
          dist = JvmPlatform.preferred_jvm_distribution([platform], strict=strict)
          preferred_distributions[strict_key] = dist.home
        except DistributionLocator.Error:
          pass

      if preferred_distributions:
        graph_info['preferred_jvm_distributions'][platform_name] = preferred_distributions

    def zip_sources(target, location, suffix='.jar'):
      with temporary_file(root_dir=location, cleanup=False, suffix=suffix) as f:
        with zipfile.ZipFile(f, 'a') as zip_file:
          for src_from_source_root, src_from_build_root in zip(target.sources_relative_to_source_root(), target.sources_relative_to_buildroot()):
            zip_file.write(os.path.join(get_buildroot(), src_from_build_root), src_from_source_root)
      return f

    if runtime_classpath:
      graph_info['libraries'] = self._resolve_jars_info(targets, runtime_classpath)
      # Using resolved path in preparation for VCFS.
      resource_jar_root = os.path.realpath(self.versioned_workdir)
      for t in targets:
        target_type = _get_target_type(t)
        # If it is a target root or it is already a jar_library target, then no-op.
        if t in target_roots_set or targets_map[t.address.spec]['pants_target_type'] == 'jar_library':
          continue

        if target_type == SourceRootTypes.RESOURCE or target_type == SourceRootTypes.TEST_RESOURCE:
          # yic assumed that the cost to fingerprint the target may not be that lower than
          # just zipping up the resources anyway.
          jarred_resources = zip_sources(t, resource_jar_root)
          targets_map[t.address.spec]['pants_target_type'] = 'jar_library'
          targets_map[t.address.spec]['libraries'] = [t.id]
          graph_info['libraries'][t.id]['default'] = jarred_resources.name
          continue

        targets_map[t.address.spec]['pants_target_type'] = 'jar_library'
        targets_map[t.address.spec]['libraries'] = [t.id]
        jar_products = runtime_classpath.get_for_target(t)
        for conf, jar_entry in jar_products:
          # TODO(yic): check --compile-rsc-use-classpath-jars is enabled.
          # If not, zip up the classes/ dir here.
          if 'z.jar' in jar_entry:
            graph_info['libraries'][t.id][conf] = jar_entry
        if self.get_options().sources:
          # NB: We create the jar in the same place as we create the resources
          # (as opposed to where we store the z.jar), because the path to the z.jar depends
          # on tasks outside of this one.
          # In addition to that, we may not want to depend on z.jar existing to export source jars.
          jarred_sources = zip_sources(t, resource_jar_root, suffix='-sources.jar')
          graph_info['libraries'][t.id]['sources'] = jarred_sources.name


    return graph_info
예제 #44
0
파일: export.py 프로젝트: OniOni/pants
    def generate_targets_map(self, targets, classpath_products=None):
        """Generates a dictionary containing all pertinent information about the target graph.

    The return dictionary is suitable for serialization by json.dumps.
    :param targets: The list of targets to generate the map for.
    :param classpath_products: Optional classpath_products. If not provided when the --libraries
      option is `True`, this task will perform its own jar resolution.
    """
        targets_map = {}
        resource_target_map = {}
        python_interpreter_targets_mapping = defaultdict(list)

        if self.get_options().libraries:
            # NB(gmalmquist): This supports mocking the classpath_products in tests.
            if classpath_products is None:
                classpath_products = self.resolve_jars(targets)
        else:
            classpath_products = None

        target_roots_set = set(self.context.target_roots)

        def process_target(current_target):
            """
      :type current_target:pants.build_graph.target.Target
      """
            def get_target_type(tgt):
                def is_test(t):
                    return isinstance(t, JUnitTests) or isinstance(
                        t, PythonTests)

                if is_test(tgt):
                    return SourceRootTypes.TEST
                else:
                    if (isinstance(tgt, Resources)
                            and tgt in resource_target_map
                            and is_test(resource_target_map[tgt])):
                        return SourceRootTypes.TEST_RESOURCE
                    elif isinstance(tgt, Resources):
                        return SourceRootTypes.RESOURCE
                    else:
                        return SourceRootTypes.SOURCE

            info = {
                'targets': [],
                'libraries': [],
                'roots': [],
                'id':
                current_target.id,
                'target_type':
                get_target_type(current_target),
                # NB: is_code_gen should be removed when export format advances to 1.1.0 or higher
                'is_code_gen':
                current_target.is_synthetic,
                'is_synthetic':
                current_target.is_synthetic,
                'pants_target_type':
                self._get_pants_target_alias(type(current_target)),
            }

            if not current_target.is_synthetic:
                info['globs'] = current_target.globs_relative_to_buildroot()
                if self.get_options().sources:
                    info['sources'] = list(
                        current_target.sources_relative_to_buildroot())

            info['transitive'] = current_target.transitive
            info['scope'] = str(current_target.scope)
            info['is_target_root'] = current_target in target_roots_set

            if isinstance(current_target, PythonRequirementLibrary):
                reqs = current_target.payload.get_field_value(
                    'requirements', set())
                """:type : set[pants.backend.python.python_requirement.PythonRequirement]"""
                info['requirements'] = [req.key for req in reqs]

            if isinstance(current_target, PythonTarget):
                interpreter_for_target = self._interpreter_cache.select_interpreter_for_targets(
                    [current_target])
                if interpreter_for_target is None:
                    raise TaskError(
                        'Unable to find suitable interpreter for {}'.format(
                            current_target.address))
                python_interpreter_targets_mapping[
                    interpreter_for_target].append(current_target)
                info['python_interpreter'] = str(
                    interpreter_for_target.identity)

            def iter_transitive_jars(jar_lib):
                """
        :type jar_lib: :class:`pants.backend.jvm.targets.jar_library.JarLibrary`
        :rtype: :class:`collections.Iterator` of
                :class:`pants.java.jar.M2Coordinate`
        """
                if classpath_products:
                    jar_products = classpath_products.get_artifact_classpath_entries_for_targets(
                        (jar_lib, ))
                    for _, jar_entry in jar_products:
                        coordinate = jar_entry.coordinate
                        # We drop classifier and type_ since those fields are represented in the global
                        # libraries dict and here we just want the key into that dict (see `_jar_id`).
                        yield M2Coordinate(org=coordinate.org,
                                           name=coordinate.name,
                                           rev=coordinate.rev)

            target_libraries = OrderedSet()
            if isinstance(current_target, JarLibrary):
                target_libraries = OrderedSet(
                    iter_transitive_jars(current_target))
            for dep in current_target.dependencies:
                info['targets'].append(dep.address.spec)
                if isinstance(dep, JarLibrary):
                    for jar in dep.jar_dependencies:
                        target_libraries.add(
                            M2Coordinate(jar.org, jar.name, jar.rev))
                    # Add all the jars pulled in by this jar_library
                    target_libraries.update(iter_transitive_jars(dep))
                if isinstance(dep, Resources):
                    resource_target_map[dep] = current_target

            if isinstance(current_target, ScalaLibrary):
                for dep in current_target.java_sources:
                    info['targets'].append(dep.address.spec)
                    process_target(dep)

            if isinstance(current_target, JvmTarget):
                info['excludes'] = [
                    self._exclude_id(exclude)
                    for exclude in current_target.excludes
                ]
                info['platform'] = current_target.platform.name
                if hasattr(current_target, 'test_platform'):
                    info['test_platform'] = current_target.test_platform.name

            info['roots'] = [{
                'source_root': source_root_package_prefix[0],
                'package_prefix': source_root_package_prefix[1]
            } for source_root_package_prefix in self._source_roots_for_target(
                current_target)]

            if classpath_products:
                info['libraries'] = [
                    self._jar_id(lib) for lib in target_libraries
                ]
            targets_map[current_target.address.spec] = info

        for target in targets:
            process_target(target)

        scala_platform = ScalaPlatform.global_instance()
        scala_platform_map = {
            'scala_version':
            scala_platform.version,
            'compiler_classpath': [
                cp_entry.path
                for cp_entry in scala_platform.compiler_classpath_entries(
                    self.context.products)
            ],
        }

        jvm_platforms_map = {
            'default_platform':
            JvmPlatform.global_instance().default_platform.name,
            'platforms': {
                str(platform_name): {
                    'target_level': str(platform.target_level),
                    'source_level': str(platform.source_level),
                    'args': platform.args,
                }
                for platform_name, platform in
                JvmPlatform.global_instance().platforms_by_name.items()
            },
        }

        graph_info = {
            'version': DEFAULT_EXPORT_VERSION,
            'targets': targets_map,
            'jvm_platforms': jvm_platforms_map,
            'scala_platform': scala_platform_map,
            # `jvm_distributions` are static distribution settings from config,
            # `preferred_jvm_distributions` are distributions that pants actually uses for the
            # given platform setting.
            'preferred_jvm_distributions': {}
        }

        for platform_name, platform in JvmPlatform.global_instance(
        ).platforms_by_name.items():
            preferred_distributions = {}
            for strict, strict_key in [(True, 'strict'),
                                       (False, 'non_strict')]:
                try:
                    dist = JvmPlatform.preferred_jvm_distribution(
                        [platform], strict=strict)
                    preferred_distributions[strict_key] = dist.home
                except DistributionLocator.Error:
                    pass

            if preferred_distributions:
                graph_info['preferred_jvm_distributions'][
                    platform_name] = preferred_distributions

        if classpath_products:
            graph_info['libraries'] = self._resolve_jars_info(
                targets, classpath_products)

        if python_interpreter_targets_mapping:
            # NB: We've selected a python interpreter compatible with each python target individually into
            # the `python_interpreter_targets_mapping`. These python targets may not be compatible, ie: we
            # could have a python target requiring 'CPython>=2.7<3' (ie: CPython-2.7.x) and another
            # requiring 'CPython>=3.6'. To pick a default interpreter then from among these two choices
            # is arbitrary and not to be relied on to work as a default interpreter if ever needed by the
            # export consumer.
            #
            # TODO(John Sirois): consider either eliminating the 'default_interpreter' field and pressing
            # export consumers to make their own choice of a default (if needed) or else use
            # `select.select_interpreter_for_targets` and fail fast if there is no interpreter compatible
            # across all the python targets in-play.
            #
            # For now, make our arbitrary historical choice of a default interpreter explicit and use the
            # lowest version.
            default_interpreter = min(
                python_interpreter_targets_mapping.keys())

            interpreters_info = {}
            for interpreter, targets in python_interpreter_targets_mapping.items(
            ):
                req_libs = [
                    target for target in Target.closure_for_targets(targets)
                    if has_python_requirements(target)
                ]
                chroot = self.resolve_requirements(interpreter, req_libs)
                interpreters_info[str(interpreter.identity)] = {
                    'binary': interpreter.binary,
                    'chroot': chroot.path()
                }

            graph_info['python_setup'] = {
                'default_interpreter': str(default_interpreter.identity),
                'interpreters': interpreters_info
            }

        if self.get_options().available_target_types:
            graph_info['available_target_types'] = self._target_types()

        return graph_info
예제 #45
0
 def name(self):
   return ScalaPlatform.global_instance().suffix_version(self.base_name)
예제 #46
0
 def name(self):
   base_name = super(ScalaExclude, self).name
   return ScalaPlatform.global_instance().suffix_version(base_name)