[docs]classVariables(object):"""Environment variables supported by the PEX runtime."""@classmethoddefprocess_pydoc(cls,pydoc):ifpydocisNone:return'Unknown','Unknown'pydoc=pydoc.splitlines()variable_type=pydoc[0]variable_text=' '.join(filter(None,(line.strip()forlineinpydoc[2:])))returnvariable_type,variable_text@classmethoddefiter_help(cls):forvariable_name,valueinsorted(cls.__dict__.items()):ifnotvariable_name.startswith('PEX_'):continuevariable_type,variable_text=cls.process_pydoc(getattr(value,'__doc__'))yieldvariable_name,variable_type,variable_textdef__init__(self,environ=None,use_defaults=True):self._environ=environ.copy()ifenvironisnotNoneelseos.environself._use_defaults=use_defaultsdefcopy(self):returnself._environ.copy()defdelete(self,variable):self._environ.pop(variable,None)defset(self,variable,value):self._environ[variable]=str(value)def_defaulted(self,default):returndefaultifself._use_defaultselseNonedef_get_bool(self,variable,default=False):value=self._environ.get(variable)ifvalueisnotNone:ifvalue.lower()in('0','false'):returnFalseelifvalue.lower()in('1','true'):returnTrueelse:die('Invalid value for %s, must be 0/1/false/true, got %r'%(variable,value))else:returnself._defaulted(default)def_get_string(self,variable,default=None):returnself._environ.get(variable,self._defaulted(default))def_get_path(self,variable,default=None):value=self._get_string(variable,default=default)ifvalueisnotNone:returnos.path.realpath(os.path.expanduser(value))def_get_int(self,variable,default=None):try:returnint(self._environ[variable])exceptValueError:die('Invalid value for %s, must be an integer, got %r'%(variable,self._environ[variable]))exceptKeyError:returnself._defaulted(default)

[docs]defstrip_defaults(self):"""Returns a copy of these variables but with defaults stripped. Any variables not explicitly set in the environment will have a value of `None`. """returnVariables(environ=self.copy(),use_defaults=False)

@contextmanager

[docs]defpatch(self,**kw):"""Update the environment for the duration of a context."""old_environ=self._environself._environ=self._environ.copy()self._environ.update(kw)yieldself._environ=old_environ

@propertydefPEX_ALWAYS_CACHE(self):"""Boolean Always write PEX dependencies to disk prior to invoking regardless whether or not the dependencies are zip-safe. For certain dependencies that are very large such as numpy, this can reduce the RAM necessary to launch the PEX. The data will be written into $PEX_ROOT, which by default is $HOME/.pex. Default: false. """returnself._get_bool('PEX_ALWAYS_CACHE',default=False)@propertydefPEX_COVERAGE(self):"""Boolean Enable coverage reporting for this PEX file. This requires that the "coverage" module is available in the PEX environment. Default: false. """returnself._get_bool('PEX_COVERAGE',default=False)@propertydefPEX_COVERAGE_FILENAME(self):"""Filename Write the coverage data to the specified filename. If PEX_COVERAGE_FILENAME is not specified but PEX_COVERAGE is, coverage information will be printed to stdout and not saved. """returnself._get_path('PEX_COVERAGE_FILENAME',default=None)@propertydefPEX_FORCE_LOCAL(self):"""Boolean Force this PEX to be not-zip-safe. This forces all code and dependencies to be written into $PEX_ROOT prior to invocation. This is an option for applications with static assets that refer to paths relative to __file__ instead of using pkgutil/pkg_resources. Default: false. """returnself._get_bool('PEX_FORCE_LOCAL',default=False)@propertydefPEX_IGNORE_ERRORS(self):"""Boolean Ignore any errors resolving dependencies when invoking the PEX file. This can be useful if you know that a particular failing dependency is not necessary to run the application. Default: false. """returnself._get_bool('PEX_IGNORE_ERRORS',default=False)@propertydefPEX_INHERIT_PATH(self):"""Boolean Allow inheriting packages from site-packages. By default, PEX scrubs any packages and namespace packages from sys.path prior to invoking the application. This is generally not advised, but can be used in situations when certain dependencies do not conform to standard packaging practices and thus cannot be bundled into PEX files. Default: false. """returnself._get_bool('PEX_INHERIT_PATH',default=False)@propertydefPEX_INTERPRETER(self):"""Boolean Drop into a REPL instead of invoking the predefined entry point of this PEX. This can be useful for inspecting the PEX environment interactively. It can also be used to treat the PEX file as an interpreter in order to execute other scripts in the context of the PEX file, e.g. "PEX_INTERPRETER=1 ./app.pex my_script.py". Equivalent to setting PEX_MODULE to empty. Default: false. """returnself._get_bool('PEX_INTERPRETER',default=False)@propertydefPEX_MODULE(self):"""String Override the entry point into the PEX file. Can either be a module, e.g. 'SimpleHTTPServer', or a specific entry point in module:symbol form, e.g. "myapp.bin:main". """returnself._get_string('PEX_MODULE',default=None)@propertydefPEX_PROFILE(self):"""Boolean Enable application profiling. If specified and PEX_PROFILE_FILENAME is not specified, PEX will print profiling information to stdout. """returnself._get_path('PEX_PROFILE',default=None)@propertydefPEX_PROFILE_FILENAME(self):"""Filename Profile the application and dump a profile into the specified filename in the standard "profile" module format. """returnself._get_path('PEX_PROFILE_FILENAME',default=None)@propertydefPEX_PROFILE_SORT(self):"""String Toggle the profile sorting algorithm used to print out profile columns. Default: 'cumulative'. """returnself._get_string('PEX_PROFILE_SORT',default='cumulative')@propertydefPEX_PYTHON(self):"""String Override the Python interpreter used to invoke this PEX. Can be either an absolute path to an interpreter or a base name e.g. "python3.3". If a base name is provided, the $PATH will be searched for an appropriate match. """returnself._get_string('PEX_PYTHON',default=None)@propertydefPEX_ROOT(self):"""Directory The directory location for PEX to cache any dependencies and code. PEX must write not-zip-safe eggs and all wheels to disk in order to activate them. Default: ~/.pex """returnself._get_path('PEX_ROOT',default=None)@propertydefPEX_PATH(self):"""A set of one or more PEX files Merge the packages from other PEX files into the current environment. This allows you to do things such as create a PEX file containing the "coverage" module or create PEX files containing plugin entry points to be consumed by a main application. Paths should be specified in the same manner as $PATH, e.g. PEX_PATH=/path/to/pex1.pex:/path/to/pex2.pex and so forth. """returnself._get_string('PEX_PATH',default='')@propertydefPEX_SCRIPT(self):"""String The script name within the PEX environment to execute. This must either be an entry point as defined in a distribution's console_scripts, or a script as defined in a distribution's scripts section. While Python supports any script including shell scripts, PEX only supports invocation of Python scripts in this fashion. """returnself._get_string('PEX_SCRIPT',default=None)@propertydefPEX_TEARDOWN_VERBOSE(self):"""Boolean Enable verbosity for when the interpreter shuts down. This is mostly only useful for debugging PEX itself. Default: false. """returnself._get_bool('PEX_TEARDOWN_VERBOSE',default=False)@propertydefPEX_VERBOSE(self):"""Integer Set the verbosity level of PEX debug logging. The higher the number, the more logging, with 0 being disabled. This environment variable can be extremely useful in debugging PEX environment issues. Default: 0 """returnself._get_int('PEX_VERBOSE',default=0)# TODO(wickman) Remove and push into --flags. #94@propertydefPEX_HTTP_RETRIES(self):"""Integer The number of HTTP retries when performing dependency resolution when building a PEX file. Default: 5. """returnself._get_int('PEX_HTTP_RETRIES',default=5)# Global singleton environment