Ejemplo n.º 1
0
 def _native_target_matchers(self):
     return {
         SubclassesOf(PythonDistribution):
         self.pydist_has_native_sources,
         SubclassesOf(NativeLibrary):
         NativeLibrary.produces_ctypes_native_library,
     }
Ejemplo n.º 2
0
  class TaskInvocationResult(datatype([
      'context',
      ('before_tasks', TypedCollection(SubclassesOf(Task))),
      ('this_task', SubclassesOf(Task)),
      ('after_tasks', TypedCollection(SubclassesOf(Task))),
  ])): pass

  def invoke_tasks(self, target_closure=None, **context_kwargs):
Ejemplo n.º 3
0
 class TaskInvocationResult(
         datatype([
             'context',
             ('before_tasks', TypedCollection(SubclassesOf(Task))),
             ('this_task', SubclassesOf(Task)),
             ('after_tasks', TypedCollection(SubclassesOf(Task))),
         ])):
     pass
Ejemplo n.º 4
0
    def test_str_and_repr(self):
        subclasses_of_b = SubclassesOf(self.B)
        self.assertEqual("SubclassesOf(B)", str(subclasses_of_b))
        self.assertEqual("SubclassesOf(B)", repr(subclasses_of_b))

        subclasses_of_multiple = SubclassesOf(self.A, self.B)
        self.assertEqual("SubclassesOf(A or B)", str(subclasses_of_multiple))
        self.assertEqual("SubclassesOf(A, B)", repr(subclasses_of_multiple))
Ejemplo n.º 5
0
class CCompile(NativeCompile):

    options_scope = 'c-compile'

    # Compile only C library targets.
    source_target_constraint = SubclassesOf(CLibrary)

    workunit_label = 'c-compile'

    @classmethod
    def implementation_version(cls):
        return super(CCompile, cls).implementation_version() + [('CCompile', 0)
                                                                ]

    @classmethod
    def subsystem_dependencies(cls):
        return super(CCompile, cls).subsystem_dependencies() + (
            CCompileSettings.scoped(cls), )

    def get_compile_settings(self):
        return CCompileSettings.scoped_instance(self)

    def get_compiler(self):
        return self._request_single(
            LLVMCToolchain, self._native_toolchain).c_toolchain.c_compiler
Ejemplo n.º 6
0
class UnpackJars(UnpackRemoteSourcesBase):
  """Unpack artifacts specified by unpacked_jars() targets.

  Adds an entry to SourceRoot for the contents.

  :API: public
  """

  source_target_constraint = SubclassesOf(UnpackedJars)

  @classmethod
  def prepare(cls, options, round_manager):
    super().prepare(options, round_manager)
    round_manager.require_data(JarImportProducts)

  @classmethod
  def implementation_version(cls):
    return super().implementation_version() + [('UnpackJars', 0)]

  def get_fingerprint_strategy(self):
    return UnpackJarsFingerprintStrategy()

  def unpack_target(self, unpacked_jars, unpack_dir):
    direct_coords = {jar.coordinate for jar in unpacked_jars.all_imported_jar_deps}
    unpack_filter = self.get_unpack_filter(unpacked_jars)
    jar_import_products = self.context.products.get_data(JarImportProducts)

    for coordinate, jar_path in jar_import_products.imports(unpacked_jars):
      if not unpacked_jars.payload.intransitive or coordinate in direct_coords:
        self.context.log.info('Unpacking jar {coordinate} from {jar_path} to {unpack_dir}.'.format(
          coordinate=coordinate, jar_path=jar_path, unpack_dir=unpack_dir))
        ZIP.extract(jar_path, unpack_dir, filter_func=unpack_filter)
Ejemplo n.º 7
0
class CppCompile(NativeCompile):

    # Compile only C++ library targets.
    source_target_constraint = SubclassesOf(CppLibrary)

    workunit_label = 'cpp-compile'

    @classmethod
    def implementation_version(cls):
        return super(CppCompile,
                     cls).implementation_version() + [('CppCompile', 0)]

    @classmethod
    def subsystem_dependencies(cls):
        return super(CppCompile, cls).subsystem_dependencies() + (
            CppCompileSettings.scoped(cls),
            NativeToolchain.scoped(cls),
        )

    @memoized_property
    def _native_toolchain(self):
        return NativeToolchain.scoped_instance(self)

    def get_compile_settings(self):
        return CppCompileSettings.scoped_instance(self)

    @memoized_property
    def _cpp_toolchain(self):
        return self._request_single(LLVMCppToolchain,
                                    self._native_toolchain).cpp_toolchain

    def get_compiler(self):
        return self._cpp_toolchain.cpp_compiler
Ejemplo n.º 8
0
 def test_single(self):
   subclasses_of_b = SubclassesOf(self.B)
   self.assertEqual((self.B,), subclasses_of_b.types)
   self.assertFalse(subclasses_of_b.satisfied_by(self.A()))
   self.assertTrue(subclasses_of_b.satisfied_by(self.B()))
   self.assertFalse(subclasses_of_b.satisfied_by(self.BPrime()))
   self.assertTrue(subclasses_of_b.satisfied_by(self.C()))
Ejemplo n.º 9
0
 def test_multiple(self):
   subclasses_of_b_or_c = SubclassesOf(self.B, self.C)
   self.assertEqual((self.B, self.C), subclasses_of_b_or_c.types)
   self.assertTrue(subclasses_of_b_or_c.satisfied_by(self.B()))
   self.assertTrue(subclasses_of_b_or_c.satisfied_by(self.C()))
   self.assertFalse(subclasses_of_b_or_c.satisfied_by(self.BPrime()))
   self.assertFalse(subclasses_of_b_or_c.satisfied_by(self.A()))
Ejemplo n.º 10
0
class CCompile(NativeCompile):

    # Compile only C library targets.
    source_target_constraint = SubclassesOf(CLibrary)

    workunit_label = 'c-compile'

    @classmethod
    def implementation_version(cls):
        return super(CCompile, cls).implementation_version() + [('CCompile', 0)
                                                                ]

    @classmethod
    def subsystem_dependencies(cls):
        return super(CCompile, cls).subsystem_dependencies() + (
            CCompileSettings.scoped(cls),
            NativeToolchain.scoped(cls),
        )

    @memoized_property
    def _toolchain(self):
        return NativeToolchain.scoped_instance(self)

    def get_compile_settings(self):
        return CCompileSettings.scoped_instance(self)

    def get_compiler(self):
        return self._request_single(CCompiler, self._toolchain)

    # FIXME(#5951): don't have any command-line args in the task or in the subsystem -- rather,
    # subsystem options should be used to populate an `Executable` which produces its own arguments.
    def extra_compile_args(self):
        return ['-x', 'c', '-std=c11']
Ejemplo n.º 11
0
 class CFFIExternMethodRuntimeErrorInfo(
         datatype([
             ('exc_type', type),
             ('exc_value', SubclassesOf(Exception)),
             'traceback',
         ])):
     """Encapsulates an exception raised when a CFFI extern is called so that it can be displayed.
Ejemplo n.º 12
0
class CppCompile(NativeCompile):

    options_scope = 'cpp-compile'

    # Compile only C++ library targets.
    source_target_constraint = SubclassesOf(CppLibrary)

    workunit_label = 'cpp-compile'

    @classmethod
    def implementation_version(cls):
        return super(CppCompile,
                     cls).implementation_version() + [('CppCompile', 0)]

    @classmethod
    def subsystem_dependencies(cls):
        return super(CppCompile, cls).subsystem_dependencies() + (
            CppCompileSettings.scoped(cls), )

    def get_compile_settings(self):
        return CppCompileSettings.scoped_instance(self)

    def get_compiler(self, native_library_target):
        return self.get_cpp_toolchain_variant(
            native_library_target).cpp_compiler
Ejemplo n.º 13
0
 def test_collection_multiple(self):
     collection_constraint = TypedCollection(
         SubclassesOf(self.B, self.BPrime))
     self.assertTrue(
         collection_constraint.satisfied_by(
             [self.B(), self.C(), self.BPrime()]))
     self.assertFalse(
         collection_constraint.satisfied_by([self.B(), self.A()]))
Ejemplo n.º 14
0
class GlobsWithConjunction(datatype([
    ('non_path_globs', SubclassesOf(BaseGlobs)),
    ('conjunction', GlobExpansionConjunction),
])):

  @classmethod
  def for_literal_files(cls, file_paths, spec_path):
    return cls(Files(*file_paths, spec_path=spec_path), GlobExpansionConjunction.all_match)
Ejemplo n.º 15
0
class Target(Struct):
    def __init__(self, name=None, configurations=None, **kwargs):
        super().__init__(name=name, **kwargs)
        self.configurations = configurations

    @addressable_list(SubclassesOf(Struct))
    def configurations(self):
        pass
Ejemplo n.º 16
0
 class CFFIExternMethodRuntimeErrorInfo(datatype([
     ('exc_type', type),
     # See https://docs.python.org/3.6/library/exceptions.html#BaseException -- this is the base
     # exception type for all built-in exceptions, including `Exception`.
     ('exc_value', SubclassesOf(BaseException)),
     'traceback',
 ])):
   """Encapsulates an exception raised when a CFFI extern is called so that it can be displayed.
Ejemplo n.º 17
0
    def dependent_target_constraint(cls):
        """Return a type contraint which is evaluated to determine dependencies for a target.

    This is used to make strict_deps() calculation automatic and declarative.

    :return: :class:`pants.util.objects.TypeConstraint`
    """
        return SubclassesOf(NativeLibrary)
Ejemplo n.º 18
0
 def test_validate(self):
   subclasses_of_a_or_b = SubclassesOf(self.A, self.B)
   self.assertEqual(self.A(), subclasses_of_a_or_b.validate_satisfied_by(self.A()))
   self.assertEqual(self.B(), subclasses_of_a_or_b.validate_satisfied_by(self.B()))
   self.assertEqual(self.C(), subclasses_of_a_or_b.validate_satisfied_by(self.C()))
   with self.assertRaisesWithMessage(
       TypeConstraintError,
       "value 1 (with type 'int') must satisfy this type constraint: SubclassesOf(A or B)."):
     subclasses_of_a_or_b.validate_satisfied_by(1)
Ejemplo n.º 19
0
class ImportJarsMixin(ImportRemoteSourcesMixin):

  expected_target_constraint = SubclassesOf(JarLibrary)

  @memoized_property
  def all_imported_jar_deps(self):
    jar_deps = OrderedSet()
    for jar_lib in self.imported_targets:
      jar_deps.update(jar_lib.jar_dependencies)
    return list(jar_deps)
Ejemplo n.º 20
0
class NativeCompileRequest(
        datatype([
            ('compiler', SubclassesOf(Executable)),
            # TODO: add type checking for Collection.of(<type>)!
            'include_dirs',
            'sources',
            ('fatal_warnings', bool),
            'output_dir',
        ])):
    pass
Ejemplo n.º 21
0
class ImportWheelsMixin(ImportRemoteSourcesMixin):

  expected_target_constraint = SubclassesOf(PythonRequirementLibrary)

  @memoized_property
  def all_imported_requirements(self):
    # TODO: figure out if this OrderedSet is necessary.
    all_requirements = OrderedSet()
    for req_lib in self.imported_targets:
      all_requirements.update(req_lib.requirements)
    return list(all_requirements)
Ejemplo n.º 22
0
class NativeCompileRequest(
        datatype([
            ('compiler', SubclassesOf(Executable)),
            # TODO: add type checking for Collection.of(<type>)!
            'include_dirs',
            'sources',
            'compiler_options',
            'output_dir',
            'header_file_extensions',
        ])):
    pass
Ejemplo n.º 23
0
  def test_str_and_repr(self):
    collection_of_exactly_b = TypedCollection(Exactly(self.B))
    self.assertEqual("TypedCollection(Exactly(B))", str(collection_of_exactly_b))
    self.assertEqual("TypedCollection(Exactly(B))", repr(collection_of_exactly_b))

    collection_of_multiple_subclasses = TypedCollection(
      SubclassesOf(self.A, self.B))
    self.assertEqual("TypedCollection(SubclassesOf(A or B))",
                     str(collection_of_multiple_subclasses))
    self.assertEqual("TypedCollection(SubclassesOf(A, B))",
                     repr(collection_of_multiple_subclasses))
Ejemplo n.º 24
0
class Target(Struct, HasProducts):
  def __init__(self, name=None, configurations=None, **kwargs):
    super(Target, self).__init__(name=name, **kwargs)
    self.configurations = configurations

  @property
  def products(self):
    return self.configurations

  @addressable_list(SubclassesOf(Struct))
  def configurations(self):
    pass
Ejemplo n.º 25
0
class NativeCompileRequest(datatype([
    ('compiler', SubclassesOf(Executable)),
    # TODO: add type checking for Collection.of(<type>)!
    'include_dirs',
    'sources',
    ('fatal_warnings', bool),
    'output_dir',
])): pass


# FIXME(#5950): perform all process execution in the v2 engine!
class ObjectFiles(datatype(['root_dir', 'filenames'])):
Ejemplo n.º 26
0
class StructWithDeps(Struct):
    """A subclass of Struct with dependencies."""
    def __init__(self, dependencies=None, **kwargs):
        """
    :param list dependencies: The direct dependencies of this struct.
    """
        # TODO: enforce the type of variants using the Addressable framework.
        super(StructWithDeps, self).__init__(**kwargs)
        self.dependencies = dependencies

    @addressable_list(SubclassesOf(Struct))
    def dependencies(self):
        """The direct dependencies of this target.
Ejemplo n.º 27
0
class ExecuteProcessRequest(
        datatype([
            ('argv', tuple),
            ('env', tuple),
            ('input_files', DirectoryDigest),
            ('output_files', tuple),
            ('output_directories', tuple),
            # NB: timeout_seconds covers the whole remote operation including queuing and setup.
            ('timeout_seconds', Exactly(float, int)),
            ('description', SubclassesOf(*six.string_types)),
        ])):
    """Request for execution with args and snapshots to extract."""
    @classmethod
    def create_from_snapshot(cls,
                             argv,
                             env,
                             snapshot,
                             output_files=(),
                             output_directories=(),
                             timeout_seconds=_default_timeout_seconds,
                             description='process'):
        cls._verify_env_is_dict(env)
        return ExecuteProcessRequest(
            argv=argv,
            env=tuple(env.items()),
            input_files=snapshot.directory_digest,
            output_files=output_files,
            output_directories=output_directories,
            timeout_seconds=timeout_seconds,
            description=description,
        )

    @classmethod
    def create_with_empty_snapshot(cls,
                                   argv,
                                   env,
                                   output_files=(),
                                   output_directories=(),
                                   timeout_seconds=_default_timeout_seconds,
                                   description='process'):
        return cls.create_from_snapshot(argv, env, EMPTY_SNAPSHOT,
                                        output_files, output_directories,
                                        timeout_seconds, description)

    @classmethod
    def _verify_env_is_dict(cls, env):
        if not isinstance(env, dict):
            raise TypeCheckError(
                cls.__name__,
                "arg 'env' was invalid: value {} (with type {}) must be a dict"
                .format(env, type(env)))
Ejemplo n.º 28
0
    class ParseArgsRequest(
            datatype([
                ('flag_value_map', SubclassesOf(dict)),
                'namespace',
                'get_all_scoped_flag_names',
                ('levenshtein_max_distance', int),
            ])):
        @staticmethod
        def _create_flag_value_map(flags):
            """Returns a map of flag -> list of values, based on the given flag strings.

      None signals no value given (e.g., -x, --foo).
      The value is a list because the user may specify the same flag multiple times, and that's
      sometimes OK (e.g., when appending to list-valued options).
      """
            flag_value_map = defaultdict(list)
            for flag in flags:
                key, has_equals_sign, flag_val = flag.partition('=')
                if not has_equals_sign:
                    if not flag.startswith('--'):  # '-xfoo' style.
                        key = flag[0:2]
                        flag_val = flag[2:]
                    if not flag_val:
                        # Either a short option with no value or a long option with no equals sign.
                        # Important so we can distinguish between no value ('--foo') and setting to an empty
                        # string ('--foo='), for options with an implicit_value.
                        flag_val = None
                flag_value_map[key].append(flag_val)
            return flag_value_map

        def __new__(cls, flags_in_scope, namespace, get_all_scoped_flag_names,
                    levenshtein_max_distance):
            """
      :param Iterable flags_in_scope: Iterable of arg strings to parse into flag values.
      :param namespace: The object to register the flag values on
      :param function get_all_scoped_flag_names: A 0-argument function which returns an iterable of
                                                 all registered option names in all their scopes. This
                                                 is used to create an error message with suggestions
                                                 when raising a `ParseError`.
      :param int levenshtein_max_distance: The maximum Levenshtein edit distance between option names
                                           to determine similarly named options when an option name
                                           hasn't been registered.
      """
            flag_value_map = cls._create_flag_value_map(flags_in_scope)
            return super(Parser.ParseArgsRequest,
                         cls).__new__(cls, flag_value_map, namespace,
                                      get_all_scoped_flag_names,
                                      levenshtein_max_distance)
Ejemplo n.º 29
0
class UnpackJars(UnpackRemoteSourcesBase):
    """Unpack artifacts specified by unpacked_jars() targets.

    Adds an entry to SourceRoot for the contents.

    :API: public
    """

    source_target_constraint = SubclassesOf(UnpackedJars)

    @classmethod
    def prepare(cls, options, round_manager):
        super().prepare(options, round_manager)
        round_manager.require_data(JarImportProducts)

    @classmethod
    def implementation_version(cls):
        return super().implementation_version() + [("UnpackJars", 0)]

    def get_fingerprint_strategy(self):
        return UnpackJarsFingerprintStrategy()

    def unpack_target(self, unpacked_jars, unpack_dir):
        deprecated_conditional(
            lambda: True,
            removal_version="1.31.0.dev0",
            entity_description="The `unpack-jars` goal",
            hint_message=
            "Contact the Pants team on Slack or [email protected] "
            "if you need this functionality.",
        )

        direct_coords = {
            jar.coordinate
            for jar in unpacked_jars.all_imported_jar_deps
        }
        unpack_filter = self.get_unpack_filter(unpacked_jars)
        jar_import_products = self.context.products.get_data(JarImportProducts)

        for coordinate, jar_path in jar_import_products.imports(unpacked_jars):
            if not unpacked_jars.payload.intransitive or coordinate in direct_coords:
                self.context.log.info(
                    "Unpacking jar {coordinate} from {jar_path} to {unpack_dir}."
                    .format(coordinate=coordinate,
                            jar_path=jar_path,
                            unpack_dir=unpack_dir))
                ZIP.extract(jar_path, unpack_dir, filter_func=unpack_filter)
Ejemplo n.º 30
0
class TaskRule(datatype([
  ('output_type', _type_field),
  ('input_selectors', TypedCollection(SubclassesOf(type))),
  ('input_gets', tuple),
  'func',
  ('dependency_rules', tuple),
  ('dependency_optionables', tuple),
  ('cacheable', bool),
]), Rule):
  """A Rule that runs a task function when all of its input selectors are satisfied.

  NB: This API is experimental, and not meant for direct consumption. To create a `TaskRule` you
  should always prefer the `@rule` constructor, and in cases where that is too constraining
  (likely due to #4535) please bump or open a ticket to explain the usecase.
  """

  def __new__(cls,
              output_type,
              input_selectors,
              func,
              input_gets,
              dependency_optionables=None,
              dependency_rules=None,
              cacheable=True):

    # Create.
    return super().__new__(
        cls,
        output_type,
        input_selectors,
        input_gets,
        func,
        dependency_rules or tuple(),
        dependency_optionables or tuple(),
        cacheable,
      )

  def __str__(self):
    return ('({}, {!r}, {}, gets={}, opts={})'
            .format(self.output_type.__name__,
                    self.input_selectors,
                    self.func.__name__,
                    self.input_gets,
                    self.dependency_optionables))