def __init__(self, address=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): payload = PythonPayload(sources_rel_path=address.spec_path, sources=sources or [], resources=resources) 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 '%s' object instead." % provides.__class__.__name__) self._provides = provides self._compatibility = maybe_list(compatibility or ()) # Check that the compatibility requirements are well-formed. for req in self._compatibility: try: PythonIdentity.parse_requirement(req) except ValueError as e: raise TargetDefinitionException(self, str(e))
def __init__(self, name, sources, resources=None, dependencies=None, provides=None, compatibility=None, exclusives=None): TargetWithSources.__init__(self, name, sources=sources, exclusives=exclusives) TargetWithDependencies.__init__(self, name, dependencies=dependencies, exclusives=exclusives) self.add_labels('python') self.resources = self._resolve_paths(resources) if resources else OrderedSet() if provides and not isinstance(provides, PythonArtifact): raise TargetDefinitionException(self, "Target must provide a valid pants setup_py object. Received a '%s' object instead." % provides.__class__.__name__) self.provides = provides self.compatibility = maybe_list(compatibility or ()) for req in self.compatibility: try: PythonIdentity.parse_requirement(req) except ValueError as e: raise TargetDefinitionException(self, str(e))
def __init__(self, name, sources, resources=None, dependencies=None, provides=None, compatibility=None, exclusives=None): TargetWithSources.__init__(self, name, sources=sources, exclusives=exclusives) TargetWithDependencies.__init__(self, name, dependencies=dependencies, exclusives=exclusives) self.add_labels('python') self.resources = self._resolve_paths( resources) if resources else OrderedSet() self.provides = provides self.compatibility = compatibility or [''] for req in self.compatibility: try: PythonIdentity.parse_requirement(req) except ValueError as e: raise TargetDefinitionException(str(e))
def __init__(self, address=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): payload = PythonPayload(sources_rel_path=address.spec_path, sources=sources or [], resources=resources) 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 '%s' object instead." % provides.__class__.__name__) self._provides = provides self.compatibility = maybe_list(compatibility or ()) for req in self.compatibility: try: PythonIdentity.parse_requirement(req) except ValueError as e: raise TargetDefinitionException(self, str(e))
def __init__(self, name, sources, resources=None, dependencies=None, provides=None, compatibility=None, exclusives=None): TargetWithSources.__init__(self, name, sources=sources, exclusives=exclusives) TargetWithDependencies.__init__(self, name, dependencies=dependencies, exclusives=exclusives) self.add_labels('python') self.resources = self._resolve_paths(resources) if resources else OrderedSet() if provides and not isinstance(provides, PythonArtifact): raise TargetDefinitionException(self, "Target must provide a valid pants setup_py object. Received a '%s' object instead." % provides.__class__.__name__) self.provides = provides self.compatibility = compatibility or [''] for req in self.compatibility: try: PythonIdentity.parse_requirement(req) except ValueError as e: raise TargetDefinitionException(str(e))
def __init__(self, target, root_dir, extra_targets=None): self._config = Config.load() self._target = target self._root = root_dir self._cache = BuildCache( os.path.join(self._config.get("python-setup", "artifact_cache"), "%s" % PythonIdentity.get()) ) self._extra_targets = list(extra_targets) if extra_targets is not None else [] self._extra_targets.append(self._get_common_python()) cachedir = self._config.get("python-setup", "cache") safe_mkdir(cachedir) self._eggcache = cachedir local_repo = "file://%s" % os.path.realpath(cachedir) self._repos = [local_repo] + self._config.getlist("python-setup", "repos") self._fetcher = ReqFetcher(repos=self._repos, cache=cachedir) self._index = None for index in self._config.getlist("python-setup", "indices"): if PythonChroot.can_contact_index(index): self._index = index break self._additional_reqs = set() distdir = self._config.getdefault("pants_distdir") distpath = tempfile.mktemp(dir=distdir, prefix=target.name) self.env = PythonEnvironment(distpath)
def interpreter_from_path(self, path): interpreter_dir = os.path.basename(path) identity = PythonIdentity.from_path(interpreter_dir) try: executable = os.readlink(os.path.join(path, 'python')) except OSError: return None interpreter = PythonInterpreter(executable, identity) return resolve(self._config, interpreter, logger=self._logger)
def __init__(self, root_dir, relpath, must_exist=True): """Creates a BuildFile object representing the BUILD file set at the specified path. root_dir: The base directory of the project relpath: The path relative to root_dir where the BUILD file is found - this can either point directly at the BUILD file or else to a directory which contains BUILD files must_exist: If True, the specified BUILD file must exist or else an IOError is thrown raises IOError if the specified path does not house a BUILD file and must_exist is True """ if not os.path.isabs(root_dir): raise IOError( 'BuildFile root_dir {root_dir} must be an absolute path.'. format(root_dir=root_dir)) path = os.path.join(root_dir, relpath) self._build_basename = BuildFile._BUILD_FILE_PREFIX buildfile = os.path.join( path, self._build_basename) if os.path.isdir(path) else path if os.path.isdir(buildfile): raise IOError( 'Path to buildfile ({buildfile}) is a directory, but it must be a file.' .format(buildfile=buildfile)) if must_exist: if not os.path.exists(os.path.dirname(buildfile)): raise IOError("Path to BUILD file does not exist at: %s" % os.path.dirname(buildfile)) # There is no BUILD file without a prefix so select any viable sibling if not os.path.exists(buildfile): for build in BuildFile._get_all_build_files( os.path.dirname(buildfile)): self._build_basename = build buildfile = os.path.join(path, self._build_basename) break if must_exist: if not os.path.exists(buildfile): raise IOError("BUILD file does not exist at: %s" % buildfile) if not BuildFile._is_buildfile_name(os.path.basename(buildfile)): raise IOError("%s is not a BUILD file" % buildfile) self.root_dir = os.path.realpath(root_dir) self.full_path = os.path.realpath(buildfile) self.name = os.path.basename(self.full_path) self.parent_path = os.path.dirname(self.full_path) self._bytecode_path = os.path.join( self.parent_path, '.%s.%s.pyc' % (self.name, PythonIdentity.get())) self.relpath = os.path.relpath(self.full_path, self.root_dir) self.spec_path = os.path.dirname(self.relpath)
def _interpreter_from_path(self, path, filters): interpreter_dir = os.path.basename(path) identity = PythonIdentity.from_path(interpreter_dir) try: executable = os.readlink(os.path.join(path, "python")) except OSError: return None interpreter = PythonInterpreter(executable, identity) if self._matches(interpreter, filters): return _resolve(self._config, interpreter, logger=self._logger) return None
def __init__(self, name, sources, resources=None, dependencies=None, provides=None, compatibility=None, exclusives=None): TargetWithSources.__init__(self, name, sources=sources, exclusives=exclusives) TargetWithDependencies.__init__(self, name, dependencies=dependencies, exclusives=exclusives) self.add_labels('python') self.resources = self._resolve_paths(resources) if resources else OrderedSet() self.provides = provides self.compatibility = compatibility or [''] for req in self.compatibility: try: PythonIdentity.parse_requirement(req) except ValueError as e: raise TargetDefinitionException(str(e))
def test_iter_supported_tags(): identity = PythonIdentity('CPython', 2, 6, 5) platform = 'linux-x86_64' def iter_solutions(): for interp in ('cp', 'py'): for interp_suffix in ('2', '20', '21', '22', '23', '24', '25', '26'): for platform in ('linux_x86_64', 'any'): yield (interp + interp_suffix, 'none', platform) assert set(PEP425.iter_supported_tags(identity, platform)) == set(iter_solutions())
def interpreter_from_path(cls, path): interpreter_dir = os.path.basename(path) identity = PythonIdentity.from_path(interpreter_dir) try: executable = os.readlink(os.path.join(path, 'python')) except OSError: return None try: distribute_path = os.readlink(os.path.join(path, 'distribute')) except OSError: distribute_path = None return PythonInterpreter(executable, identity, distribute_path)
def __init__(self, target, root_dir, extra_targets=None, builder=None, conn_timeout=None): self._config = Config.load() self._target = target self._root = root_dir self._key_generator = CacheKeyGenerator() self._extra_targets = list(extra_targets) if extra_targets is not None else [] self._resolver = MultiResolver.from_target(self._config, target, conn_timeout=conn_timeout) self._builder = builder or PEXBuilder(tempfile.mkdtemp()) # Note: unrelated to the general pants artifact cache. self._egg_cache_root = os.path.join(self._config.get('python-setup', 'artifact_cache'), '%s' % PythonIdentity.get())
def __init__(self, root_dir, relpath, must_exist=True): """Creates a BuildFile object representing the BUILD file set at the specified path. root_dir: The base directory of the project relpath: The path relative to root_dir where the BUILD file is found - this can either point directly at the BUILD file or else to a directory which contains BUILD files must_exist: If True, the specified BUILD file must exist or else an IOError is thrown raises IOError if the specified path does not house a BUILD file and must_exist is True """ if not os.path.isabs(root_dir): raise IOError('BuildFile root_dir {root_dir} must be an absolute path.' .format(root_dir=root_dir)) path = os.path.join(root_dir, relpath) self._build_basename = BuildFile._BUILD_FILE_PREFIX buildfile = os.path.join(path, self._build_basename) if os.path.isdir(path) else path if os.path.isdir(buildfile): raise IOError('Path to buildfile ({buildfile}) is a directory, but it must be a file.' .format(buildfile=buildfile)) if must_exist: if not os.path.exists(os.path.dirname(buildfile)): raise IOError("Path to BUILD file does not exist at: %s" % os.path.dirname(buildfile)) # There is no BUILD file without a prefix so select any viable sibling if not os.path.exists(buildfile): for build in BuildFile._get_all_build_files(os.path.dirname(buildfile)): self._build_basename = build buildfile = os.path.join(path, self._build_basename) break if must_exist: if not os.path.exists(buildfile): raise IOError("BUILD file does not exist at: %s" % buildfile) if not BuildFile._is_buildfile_name(os.path.basename(buildfile)): raise IOError("%s is not a BUILD file" % buildfile) self.root_dir = os.path.realpath(root_dir) self.full_path = os.path.realpath(buildfile) self.name = os.path.basename(self.full_path) self.parent_path = os.path.dirname(self.full_path) self._bytecode_path = os.path.join(self.parent_path, '.%s.%s.pyc' % ( self.name, PythonIdentity.get())) self.relpath = os.path.relpath(self.full_path, self.root_dir) self.spec_path = os.path.dirname(self.relpath)
def __init__(self, target, root_dir, extra_targets=None, builder=None, conn_timeout=None): self._config = Config.load() self._target = target self._root = root_dir self._key_generator = CacheKeyGenerator() self._extra_targets = list(extra_targets) if extra_targets is not None else [] self._resolver = MultiResolver.from_target(self._config, target, conn_timeout=conn_timeout) self._builder = builder or PEXBuilder(tempfile.mkdtemp()) artifact_cache_root = os.path.join(self._config.get('python-setup', 'artifact_cache'), '%s' % PythonIdentity.get()) self._artifact_cache = FileBasedArtifactCache(None, self._root, artifact_cache_root, self._builder.add_dependency_file)
def build(self, filename): self.freeze() try: os.unlink(filename + '~') print('WARNING: Previous binary unexpectedly exists, cleaning: %s' % (filename + '~')) except OSError: # The expectation is that the file does not exist, so continue pass with open(filename + '~', 'ab') as pexfile: assert os.path.getsize(pexfile.name) == 0 # TODO(wickman) Make this tunable pexfile.write(Compatibility.to_bytes('%s\n' % PythonIdentity.get().hashbang())) self._chroot.zip(filename + '~', mode='a') if os.path.exists(filename): os.unlink(filename) os.rename(filename + '~', filename) chmod_plus_x(filename)
def __init__(self, target, root_dir, extra_targets=None, builder=None): self._config = Config.load() self._target = target self._root = root_dir self._cache = BuildCache(os.path.join(self._config.get('python-setup', 'artifact_cache'), '%s' % PythonIdentity.get())) self._extra_targets = list(extra_targets) if extra_targets is not None else [] self._resolver = PythonResolver([self._target] + self._extra_targets) self._builder = builder or PEXBuilder(tempfile.mkdtemp()) self._platforms = (Platform.current(),) self._pythons = (sys.version[:3],) # TODO(wickman) Should this be in the binary builder? if isinstance(self._target, PythonBinary): self._platforms = self._target._platforms self._pythons = self._target._interpreters
def __init__(self, target, root_dir, extra_targets=None, builder=None): self._config = Config.load() self._target = target self._root = root_dir self._cache = BuildCache( os.path.join(self._config.get('python-setup', 'artifact_cache'), '%s' % PythonIdentity.get())) self._extra_targets = list( extra_targets) if extra_targets is not None else [] self._resolver = PythonResolver([self._target] + self._extra_targets) self._builder = builder or PEXBuilder(tempfile.mkdtemp()) self._platforms = (Platform.current(), ) self._pythons = (sys.version[:3], ) # TODO(wickman) Should this be in the binary builder? if isinstance(self._target, PythonBinary): self._platforms = self._target._platforms self._pythons = self._target._interpreters
def __init__(self, target, root_dir, extra_targets=None, builder=None, conn_timeout=None): self._config = Config.load() self._target = target self._root = root_dir self._key_generator = CacheKeyGenerator() self._extra_targets = list( extra_targets) if extra_targets is not None else [] self._resolver = MultiResolver.from_target(self._config, target, conn_timeout=conn_timeout) self._builder = builder or PEXBuilder(tempfile.mkdtemp()) # Note: unrelated to the general pants artifact cache. self._egg_cache_root = os.path.join( self._config.get('python-setup', 'artifact_cache'), '%s' % PythonIdentity.get())
def __init__(self, root_dir, relpath, must_exist=True): """Creates a BuildFile object representing the BUILD file set at the specified path. root_dir: The base directory of the project relpath: The path relative to root_dir where the BUILD file is found - this can either point directly at the BUILD file or else to a directory which contains BUILD files must_exist: If True, the specified BUILD file must exist or else an IOError is thrown raises IOError if the specified path does not house a BUILD file and must_exist is True """ path = os.path.abspath(os.path.join(root_dir, relpath)) buildfile = os.path.join( path, BuildFile._CANONICAL_NAME) if os.path.isdir(path) else path if os.path.isdir(buildfile): raise IOError("%s is a directory" % buildfile) if must_exist: if not os.path.exists(buildfile): raise IOError("BUILD file does not exist at: %s" % (buildfile)) if not BuildFile._is_buildfile_name(os.path.basename(buildfile)): raise IOError("%s is not a BUILD file" % buildfile) if not os.path.exists(buildfile): raise IOError("BUILD file does not exist at: %s" % buildfile) self.root_dir = root_dir self.full_path = buildfile self.name = os.path.basename(self.full_path) self.parent_path = os.path.dirname(self.full_path) self._bytecode_path = os.path.join( self.parent_path, '.%s.%s.pyc' % (self.name, PythonIdentity.get())) self.relpath = os.path.relpath(self.full_path, self.root_dir) self.canonical_relpath = os.path.join(os.path.dirname(self.relpath), BuildFile._CANONICAL_NAME)
def __init__(self, target, root_dir, extra_targets=None, builder=None, conn_timeout=None): self._config = Config.load() self._target = target self._root = root_dir self._key_generator = CacheKeyGenerator() self._extra_targets = list( extra_targets) if extra_targets is not None else [] self._resolver = MultiResolver.from_target(self._config, target, conn_timeout=conn_timeout) self._builder = builder or PEXBuilder(tempfile.mkdtemp()) artifact_cache_root = os.path.join( self._config.get('python-setup', 'artifact_cache'), '%s' % PythonIdentity.get()) self._artifact_cache = FileBasedArtifactCache( None, self._root, artifact_cache_root, self._builder.add_dependency_file)
def __init__(self, root_dir, relpath, must_exist=True): """Creates a BuildFile object representing the BUILD file set at the specified path. root_dir: The base directory of the project relpath: The path relative to root_dir where the BUILD file is found - this can either point directly at the BUILD file or else to a directory which contains BUILD files must_exist: If True, the specified BUILD file must exist or else an IOError is thrown raises IOError if the specified path does not house a BUILD file and must_exist is True """ path = os.path.abspath(os.path.join(root_dir, relpath)) buildfile = os.path.join(path, BuildFile._CANONICAL_NAME) if os.path.isdir(path) else path if os.path.isdir(buildfile): raise IOError("%s is a directory" % buildfile) if must_exist: if not os.path.exists(buildfile): raise IOError("BUILD file does not exist at: %s" % buildfile) if not BuildFile._is_buildfile_name(os.path.basename(buildfile)): raise IOError("%s is not a BUILD file" % buildfile) if not os.path.exists(buildfile): raise IOError("BUILD file does not exist at: %s" % buildfile) self.root_dir = os.path.realpath(root_dir) self.full_path = os.path.realpath(buildfile) self.name = os.path.basename(self.full_path) self.parent_path = os.path.dirname(self.full_path) self._bytecode_path = os.path.join(self.parent_path, '.%s.%s.pyc' % ( self.name, PythonIdentity.get())) self.relpath = os.path.relpath(self.full_path, self.root_dir) self.canonical_relpath = os.path.join(os.path.dirname(self.relpath), BuildFile._CANONICAL_NAME)
def __init__(self, environment, identity=PythonIdentity.get()): if not isinstance(environment, PythonEnvironment): raise ValueError('Expected environment to be of type PythonEnvironment! Got %s' % ( type(environment))) self._env = environment self._identity = identity