def test_freeze(self): payload = Payload() payload.add_field('foo', PrimitiveField()) payload.freeze() with self.assertRaises(PayloadFrozenError): payload.add_field('bar', PrimitiveField())
def test_field_duplication(self): payload = Payload() payload.add_field('foo', PrimitiveField()) payload.freeze() with self.assertRaises(PayloadFieldAlreadyDefinedError): payload.add_field('foo', PrimitiveField())
def __init__(self, address=None, payload=None, sources=None, provides=None, excludes=None, resources=None, services=None, platform=None, strict_deps=None, fatal_warnings=None, **kwargs): """ :API: public :param excludes: List of `exclude <#exclude>`_\s to filter this target's transitive dependencies against. :param sources: Source code files to build. Paths are relative to the BUILD file's directory. :type sources: ``Fileset`` (from globs or rglobs) or list of strings :param services: A dict mapping service interface names to the classes owned by this target that implement them. Keys are fully qualified service class names, values are lists of strings, each string the fully qualified class name of a class owned by this target that implements the service interface and should be discoverable by the jvm service provider discovery mechanism described here: https://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html :param platform: The name of the platform (defined under the jvm-platform subsystem) to use for compilation (that is, a key into the --jvm-platform-platforms dictionary). If unspecified, the platform will default to the first one of these that exist: (1) the default_platform specified for jvm-platform, (2) a platform constructed from whatever java version is returned by DistributionLocator.cached().version. :type platform: str :param strict_deps: When True, only the directly declared deps of the target will be used at compilation time. This enforces that all direct deps of the target are declared, and can improve compilation speed due to smaller classpaths. Transitive deps are always provided at runtime. :type strict_deps: bool :param fatal_warnings: Whether to turn warnings into errors for this target. If present, takes priority over the language's fatal-warnings option. :type fatal_warnings: bool """ self.address = address # Set in case a TargetDefinitionException is thrown early payload = payload or Payload() excludes = ExcludesField(self.assert_list(excludes, expected_type=Exclude, key_arg='excludes')) payload.add_fields({ 'sources': self.create_sources_field(sources, address.spec_path, key_arg='sources'), 'provides': provides, 'excludes': excludes, 'platform': PrimitiveField(platform), 'strict_deps': PrimitiveField(strict_deps), 'fatal_warnings': PrimitiveField(fatal_warnings), }) self._resource_specs = self.assert_list(resources, key_arg='resources') super(JvmTarget, self).__init__(address=address, payload=payload, **kwargs) # Service info is only used when generating resources, it should not affect, for example, a # compile fingerprint or javadoc fingerprint. As such, its not a payload field. self._services = services or {} self.add_labels('jvm')
def __init__( self, address=None, payload=None, sources=None, resources=None, # Old-style resources (file list, Fileset). resource_targets=None, # New-style resources (Resources target specs). provides=None, compatibility=None, **kwargs): """ :param dependencies: Other targets that this target depends on. These dependencies may be ``python_library``-like targets (``python_library``, ``python_thrift_library``, ``python_antlr_library`` and so forth) or ``python_requirement_library`` targets. :type dependencies: List of target specs :param sources: Files to "include". Paths are relative to the BUILD file's directory. :type sources: ``Fileset`` or list of strings :param resources: non-Python resources, e.g. templates, keys, other data (it is recommended that your application uses the pkgutil package to access these resources in a .zip-module friendly way.) :param provides: The `setup_py <#setup_py>`_ to publish that represents this target outside the repo. :param compatibility: either a string or list of strings that represents interpreter compatibility for this target, using the Requirement-style format, e.g. ``'CPython>=3', or just ['>=2.7','<3']`` for requirements agnostic to interpreter class. """ self.address = address payload = payload or Payload() payload.add_fields({ 'sources': self.create_sources_field(sources, address.spec_path, key_arg='sources'), 'resources': self.create_sources_field(resources, address.spec_path, key_arg='resources'), 'provides': provides, 'compatibility': PrimitiveField(maybe_list(compatibility or ())), }) super(PythonTarget, self).__init__(address=address, payload=payload, **kwargs) self._resource_target_specs = resource_targets self.add_labels('python') self._synthetic_resources_target = None if provides and not isinstance(provides, PythonArtifact): raise TargetDefinitionException( self, "Target must provide a valid pants setup_py object. Received a '{}' object instead." .format(provides.__class__.__name__)) self._provides = provides # Check that the compatibility requirements are well-formed. for req in self.payload.compatibility: try: PythonIdentity.parse_requirement(req) except ValueError as e: raise TargetDefinitionException(self, str(e))
def test_none(self): payload = Payload() payload.add_field('foo', None) payload2 = Payload() payload2.add_field('foo', PrimitiveField(None)) self.assertNotEqual(payload.fingerprint(), payload2.fingerprint())
def __init__(self, name, address, build_graph, type_alias=None, payload=None, tags=None, description=None, no_cache=False, scope=None, _transitive=None, **kwargs): """ :API: public :param string name: The name of this target, which combined with this build file defines the target address. :param dependencies: Target address specs of other targets that this target depends on. :type dependencies: list of strings :param address: The Address that maps to this Target in the BuildGraph. :type address: :class:`pants.build_graph.address.Address` :param build_graph: The BuildGraph that this Target lives within. :type build_graph: :class:`pants.build_graph.build_graph.BuildGraph` :param string type_alias: The type_alias used to construct this target, may be None if constructed directly. :param payload: The configuration encapsulated by this target. Also in charge of most fingerprinting details. :type payload: :class:`pants.base.payload.Payload` :param tags: Arbitrary string tags that describe this target. Usable by downstream/custom tasks for reasoning about the build graph. NOT included in payloads and thus not used in fingerprinting, thus not suitable for anything that affects how a particular target is built. :type tags: :class:`collections.Iterable` of strings :param no_cache: If True, results for this target should not be stored in the artifact cache. :param string description: Human-readable description of this target. :param string scope: The scope of this target, used to determine its inclusion on the classpath (and possibly more things in the future). See :class:`pants.build_graph.target_scopes.Scopes`. A value of None, '', or 'default' results in the default scope, which is included everywhere. """ # NB: dependencies are in the pydoc above as a BUILD dictionary hack only; implementation hides # the dependencies via TargetAddressable. self.payload = payload or Payload() self._scope = Scope(scope) self.payload.add_field('scope_string', PrimitiveField(str(scope))) self.payload.add_field( 'transitive', PrimitiveField(True if _transitive is None else _transitive)) self.payload.freeze() self.name = name self.address = address self._build_graph = build_graph self._type_alias = type_alias self._tags = set(tags or []) self.description = description self.labels = set() self._cached_fingerprint_map = {} self._cached_all_transitive_fingerprint_map = {} self._cached_direct_transitive_fingerprint_map = {} if no_cache: self.add_labels('no_cache') if kwargs: self.Arguments.check(self, kwargs)
def __init__( self, address=None, payload=None, sources=None, provides=None, excludes=None, resources=None, services=None, platform=None, strict_deps=None, exports=None, fatal_warnings=None, zinc_file_manager=None, # Some subclasses can have both .java and .scala sources # (e.g., JUnitTests, JvmBinary, even ScalaLibrary), so it's convenient # to have both plugins settings here, even though for other subclasses # (e.g., JavaLibrary) only one will be relevant. javac_plugins=None, javac_plugin_args=None, scalac_plugins=None, scalac_plugin_args=None, **kwargs): """ :API: public :param excludes: List of `exclude <#exclude>`_\s to filter this target's transitive dependencies against. :param sources: Source code files to build. Paths are relative to the BUILD file's directory. :type sources: ``Fileset`` (from globs or rglobs) or list of strings :param services: A dict mapping service interface names to the classes owned by this target that implement them. Keys are fully qualified service class names, values are lists of strings, each string the fully qualified class name of a class owned by this target that implements the service interface and should be discoverable by the jvm service provider discovery mechanism described here: https://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html :param str platform: The name of the platform (defined under the jvm-platform subsystem) to use for compilation (that is, a key into the --jvm-platform-platforms dictionary). If unspecified, the platform will default to the first one of these that exist: (1) the default_platform specified for jvm-platform, (2) a platform constructed from whatever java version is returned by DistributionLocator.cached().version. :param bool strict_deps: When True, only the directly declared deps of the target will be used at compilation time. This enforces that all direct deps of the target are declared, and can improve compilation speed due to smaller classpaths. Transitive deps are always provided at runtime. :param list exports: A list of exported targets, which will be accessible to dependents even with strict_deps turned on. A common use case is for library targets to to export dependencies that it knows its dependents will need. Then any dependents of that library target will have access to those dependencies even when strict_deps is True. Note: exports is transitive, which means dependents have access to the closure of exports. An example will be that if A exports B, and B exports C, then any targets that depends on A will have access to both B and C. :param bool fatal_warnings: Whether to turn warnings into errors for this target. If present, takes priority over the language's fatal-warnings option. :param bool zinc_file_manager: Whether to use zinc provided file manager that allows transactional rollbacks, but in certain cases may conflict with user libraries. :param javac_plugins: names of compiler plugins to use when compiling this target with javac. :param dict javac_plugin_args: Map from javac plugin name to list of arguments for that plugin. :param scalac_plugins: names of compiler plugins to use when compiling this target with scalac. :param dict scalac_plugin_args: Map from scalac plugin name to list of arguments for that plugin. """ deprecated_conditional( lambda: resources is not None, '1.5.0.dev0', 'The `resources=` JVM target argument found on target {}'.format( address.spec), 'Use `dependencies=` instead.') self.address = address # Set in case a TargetDefinitionException is thrown early payload = payload or Payload() excludes = ExcludesField( self.assert_list(excludes, expected_type=Exclude, key_arg='excludes')) payload.add_fields({ 'sources': self.create_sources_field(sources, address.spec_path, key_arg='sources'), 'provides': provides, 'excludes': excludes, 'resources': PrimitiveField(self.assert_list(resources, key_arg='resources')), 'platform': PrimitiveField(platform), 'strict_deps': PrimitiveField(strict_deps), 'exports': SetOfPrimitivesField(exports), 'fatal_warnings': PrimitiveField(fatal_warnings), 'zinc_file_manager': PrimitiveField(zinc_file_manager), 'javac_plugins': SetOfPrimitivesField(javac_plugins), 'javac_plugin_args': PrimitiveField(javac_plugin_args), 'scalac_plugins': SetOfPrimitivesField(scalac_plugins), 'scalac_plugin_args': PrimitiveField(scalac_plugin_args), }) super(JvmTarget, self).__init__(address=address, payload=payload, **kwargs) # Service info is only used when generating resources, it should not affect, for example, a # compile fingerprint or javadoc fingerprint. As such, its not a payload field. self._services = services or {} self.add_labels('jvm')
def __init__( self, source=None, entry_point=None, inherit_path=False, # pex option zip_safe=True, # pex option always_write_cache=False, # pex option repositories=None, # pex option indices=None, # pex option ignore_errors=False, # pex option shebang=None, # pex option platforms=(), **kwargs): """ :param source: relative path to one python source file that becomes this binary's __main__. If None specified, drops into an interpreter by default. :param string entry_point: the default entry point for this binary. if None, drops into the entry point that is defined by source. Something like "pants.bin.pants_exe:main", where "pants.bin.pants_exe" is the package name and "main" is the function name (if ommitted, the module is executed directly, presuming it has a ``__main.py__``). :param sources: Overridden by source. To specify more than one source file, use a python_library and have the python_binary depend on that library. :param inherit_path: inherit the sys.path of the environment that this binary runs in :param zip_safe: whether or not this binary is safe to run in compacted (zip-file) form :param always_write_cache: whether or not the .deps cache of this PEX file should always be written to disk. :param repositories: a list of repositories to query for dependencies. :param indices: a list of indices to use for packages. :param ignore_errors: should we ignore inability to resolve dependencies? :param platforms: extra platforms to target when building this binary. If this is, e.g., ``['current', 'linux-x86_64', 'macosx-10.4-x86_64']``, then when building the pex, then for any platform-dependent modules, Pants will include ``egg``\s for Linux (64-bit Intel), Mac OS X (version 10.4 or newer), and the current platform (whatever is being used when making the PEX). :param compatibility: either a string or list of strings that represents interpreter compatibility for this target, using the Requirement-style format, e.g. ``'CPython>=3', or just ['>=2.7','<3']`` for requirements agnostic to interpreter class. """ payload = Payload() payload.add_fields({ 'entry_point': PrimitiveField(entry_point), 'inherit_path': PrimitiveField(bool(inherit_path)), 'zip_safe': PrimitiveField(bool(zip_safe)), 'always_write_cache': PrimitiveField(bool(always_write_cache)), 'repositories': PrimitiveField(maybe_list(repositories or [])), 'indices': PrimitiveField(maybe_list(indices or [])), 'ignore_errors': PrimitiveField(bool(ignore_errors)), 'platforms': PrimitiveField(tuple(maybe_list(platforms or []))), 'shebang': PrimitiveField(shebang), }) sources = [] if source is None else [source] super(PythonBinary, self).__init__(sources=sources, payload=payload, **kwargs) if source is None and entry_point is None: raise TargetDefinitionException( self, 'A python binary target must specify either source or entry_point.' ) if not isinstance(platforms, (list, tuple)) and not isinstance( platforms, string_types): raise TargetDefinitionException( self, 'platforms must be a list, tuple or string.') if source and entry_point: entry_point_module = entry_point.split(':', 1)[0] entry_source = list(self.sources_relative_to_source_root())[0] source_entry_point = self._translate_to_entry_point(entry_source) if entry_point_module != source_entry_point: raise TargetDefinitionException( self, 'Specified both source and entry_point but they do not agree: {} vs {}' .format(source_entry_point, entry_point_module))
def __init__(self, contents, **kwargs): payload = Payload() payload.add_field('contents', PrimitiveField(contents)) super(MinimalImplResourcesTaskTest.TestTarget, self).__init__(payload=payload, **kwargs)
def __init__(self, name=None, address=None, payload=None, main=None, basename=None, sources=None, deploy_excludes=None, deploy_jar_rules=None, manifest_entries=None, shading_rules=None, extra_jvm_options=None, **kwargs): """ :API: public :param string main: The name of the ``main`` class, e.g., ``'org.pantsbuild.example.hello.main.HelloMain'``. This class may be present as the source of this target or depended-upon library. :param string basename: Base name for the generated ``.jar`` file, e.g., ``'hello'``. (By default, uses ``name`` param) Note this is unsafe because of the possible conflict when multiple binaries are built. :param EagerFilesetWithSpec sources: Zero or one source files. If more than one source is required, they should be put in a library target which should be added to dependencies. :param dependencies: Targets (probably ``java_library`` and ``scala_library`` targets) to "link" in. :type dependencies: list of target specs :param deploy_excludes: List of `exclude <#exclude>`_\\s to apply at deploy time. If you, for example, deploy a java servlet that has one version of ``servlet.jar`` onto a Tomcat environment that provides another version, they might conflict. ``deploy_excludes`` gives you a way to build your code but exclude the conflicting ``jar`` when deploying. :param deploy_jar_rules: `Jar rules <#jar_rules>`_ for packaging this binary in a deploy jar. :param manifest_entries: dict that specifies entries for `ManifestEntries <#manifest_entries>`_ for adding to MANIFEST.MF when packaging this binary. :param list shading_rules: Optional list of shading rules to apply when building a shaded (aka monolithic aka fat) binary jar. The order of the rules matters: the first rule which matches a fully-qualified class name is used to shade it. See shading_relocate(), shading_exclude(), shading_relocate_package(), and shading_exclude_package(). :param list extra_jvm_options: A list of options to be passed to the jvm when running the binary. Example: ['-Dexample.property=1', '-DMyFlag', '-Xmx4G'] If unspecified, no extra jvm options will be added. """ self.address = address # Set in case a TargetDefinitionException is thrown early if main and not isinstance(main, str): raise TargetDefinitionException( self, 'main must be a fully qualified classname') if deploy_jar_rules and not isinstance(deploy_jar_rules, JarRules): raise TargetDefinitionException( self, 'deploy_jar_rules must be a JarRules specification. got {}'. format(type(deploy_jar_rules).__name__)) if manifest_entries and not isinstance(manifest_entries, dict): raise TargetDefinitionException( self, 'manifest_entries must be a dict. got {}'.format( type(manifest_entries).__name__)) payload = payload or Payload() payload.add_fields({ 'basename': PrimitiveField(basename or name), 'deploy_excludes': ExcludesField( self.assert_list(deploy_excludes, expected_type=Exclude, key_arg='deploy_excludes')), 'deploy_jar_rules': FingerprintedField(deploy_jar_rules or JarRules.default()), 'manifest_entries': FingerprintedField(ManifestEntries(manifest_entries)), 'main': PrimitiveField(main), 'shading_rules': PrimitiveField(shading_rules or ()), 'extra_jvm_options': PrimitiveField(list(extra_jvm_options or ())), }) super().__init__(name=name, address=address, payload=payload, sources=sources, **kwargs)
def __init__(self, address=None, payload=None, prep_executable=None, prep_args=None, prep_environ=False, goal=None, goals=None, **kwargs): """ :API: public :param prep_executable: The path to the executable that should be run. :param prep_args: A list of command-line args to the excutable. :param prep_environ: If True, the output of the command will be treated as a \\\\0-separated list of key=value pairs to insert into the environment. Note that this will pollute the environment for all future tests, so avoid it if at all possible. :param goal: (deprecated) Pants goal to run this command in [test, binary or compile]. If not specified, runs in the 'test' goal. :param goals: One or more pants goals to run this command in [test, binary or compile]. If not specified, runs in the 'test' goal. """ def raise_bad_target(msg): raise TargetDefinitionException(address.spec, msg) if not prep_executable: raise_bad_target('prep_executable must be specified.') if goal and goals: raise_bad_target( 'Either `goal` or `goals` (preferred) should be specified, but not both.' ) if goals: goals = sorted(goals) elif goal: warn_or_error('1.5.0', 'goal', hint='Use `goals=[{!r}]` instead.'.format(goal)) goals = [goal] else: goals = ['test'] bad_goals = set(goals) - self.allowed_goals() if bad_goals: raise_bad_target( 'Got unrecognized goal{maybe_pluralize} {unrecognized}. ' 'Goal must be one of {allowed}.'.format( maybe_pluralize='' if len(bad_goals) == 1 else 's', unrecognized=', '.join(sorted(bad_goals)), allowed=self.allowed_goals())) payload = payload or Payload() payload.add_fields({ 'goals': PrimitiveField(goals), 'prep_command_executable': PrimitiveField(prep_executable), 'prep_command_args': PrimitiveField(prep_args or []), 'prep_environ': PrimitiveField(prep_environ), }) super(PrepCommand, self).__init__(address=address, payload=payload, **kwargs)