def find_site_python(module_name, paths=None): """Find the rez native python package that contains the given module. This function is used by python 'native' rez installers to find the native rez python package that represents the python installation that this module is installed into. Note: This function is dependent on the behavior found in the python '_native' package found in the 'rez-recipes' repository. Specifically, it expects to find a python package with a '_site_paths' list attribute listing the site directories associated with the python installation. Args: module_name (str): Target python module. paths (list of str, optional): paths to search for packages, defaults to `config.packages_path`. Returns: `Package`: Native python package containing the named module. """ from rez.packages import iter_packages import subprocess import ast import os py_cmd = 'import {x}; print({x}.__path__)'.format(x=module_name) p = Popen(["python", "-c", py_cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) out, err = p.communicate() if p.returncode: raise InvalidPackageError( "Failed to find installed python module '%s':\n%s" % (module_name, err)) module_paths = ast.literal_eval(out.strip()) def issubdir(path, parent_path): return path.startswith(parent_path + os.sep) for package in iter_packages("python", paths=paths): if not hasattr(package, "_site_paths"): continue contained = True for module_path in module_paths: if not any(issubdir(module_path, x) for x in package._site_paths): contained = False if contained: return package raise InvalidPackageError( "Failed to find python installation containing the module '%s'. Has " "python been installed as a rez package?" % module_name)
def exec_python(attr, src, executable="python"): """Runs a python subproc to calculate a package attribute. Args: attr (str): Name of package attribute being created. src (list of str): Python code to execute, will be converted into semicolon-delimited single line of code. Returns: str: Output of python process. """ import subprocess if isinstance(src, basestring): src = [src] p = popen([executable, "-c", "; ".join(src)], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() if p.returncode: from rez.exceptions import InvalidPackageError raise InvalidPackageError( "Error determining package attribute '%s':\n%s" % (attr, err)) return out.strip()
def _validate_includes(self): if not self.includes: return definition_python_path = self.config.package_definition_python_path if not definition_python_path: raise InvalidPackageError( "Package %s uses @include decorator, but no include path " "has been configured with the 'package_definition_python_path' " "setting." % self.filepath) for name in self.includes: filepath = os.path.join(definition_python_path, name) filepath += ".py" if not os.path.exists(filepath): raise InvalidPackageError( "@include decorator requests module '%s', but the file " "%s does not exist." % (name, filepath))
def package_preprocess_function(this, data): from rez.utils.formatting import PackageRequest from rez.exceptions import InvalidPackageError # Ozark profile # Must be built with ozark profile build tool # ozark_profile = getattr(this, "ozark_profile", False) if ozark_profile and not bool(os.getenv("REZ_OZARK_BUILD")): raise InvalidPackageError("This package is an Ozark profile, please " "use Ozark profile build tool instead.") # Must have variant # Unless explicitly set `no_variant=True` # Or it's the 'default' one # if this.name != "default": no_variants = getattr(this, "no_variants", False) if not no_variants and not this.variants: # Add "default" if no variant data["variants"] = [["default"]] # Replacing package requirements # REQUIREMENT_MAP = { # Example "installing_package_name": { "required_request_string": "replacement_request_string", }, } if this.name in REQUIREMENT_MAP: remapped_requires = list() map_ = REQUIREMENT_MAP[this.name] for package in this.requires: if str(package) in map_: package = PackageRequest(map_[str(package)]) elif package.name in map_: package = PackageRequest(map_[package.name]) remapped_requires.append(package) data["requires"] = remapped_requires
def exec_command(attr, cmd): """Runs a subproc to calculate a package attribute. """ import subprocess p = popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() if p.returncode: from rez.exceptions import InvalidPackageError raise InvalidPackageError( "Error determining package attribute '%s':\n%s" % (attr, err)) return out.strip(), err.strip()