Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def compilation(valid_paths=None, invalid_paths=None, compile_paths=None):
with temporary_dir() as root:
for path in valid_paths:
write_source(os.path.join(root, path))
for path in invalid_paths:
write_source(os.path.join(root, path), valid=False)
compiler = Compiler(PythonInterpreter.get())
yield root, compiler.compile(root, compile_paths)
)
self.assertIn(
"Unable to detect a suitable interpreter for compatibilities",
pants_run.stdout_data
)
self.assertIn(
"CPython<2.7",
pants_run.stdout_data,
"Did not output requested compatibiility."
)
self.assertIn(f"Conflicting targets: {binary_target}", pants_run.stdout_data)
# NB: we expect the error message to print *all* interpreters resolved by Pants. However,
# to simplify the tests and for hermicity, here we only test that the current interpreter
# gets printed as a proxy for the overall behavior.
self.assertIn(
PythonInterpreter.get().version_string,
pants_run.stdout_data,
"Did not output interpreters discoved by Pants."
)
def _iter_interpreters():
seen = set()
paths = None
current_interpreter = PythonInterpreter.get()
if path:
paths = OrderedSet(os.path.realpath(p) for p in path.split(os.pathsep))
# Prefer the current interpreter if present on the `path`.
candidate_paths = frozenset((current_interpreter.binary,
os.path.dirname(current_interpreter.binary)))
candidate_paths_in_path = candidate_paths.intersection(paths)
if candidate_paths_in_path:
for p in candidate_paths_in_path:
paths.remove(p)
seen.add(current_interpreter)
yield current_interpreter
else:
# We may have been invoked with a specific interpreter, make sure our sys.executable is
# included as a candidate in this case.
seen.add(current_interpreter)
def get_interpreter(self):
return self._interpreter or PythonInterpreter.get()
def __init__(self, options_bootstrapper, *, interpreter=None):
self._options_bootstrapper = options_bootstrapper
self._interpreter = interpreter or PythonInterpreter.get()
bootstrap_options = self._options_bootstrapper.get_bootstrap_options().for_global_scope()
self._plugin_requirements = bootstrap_options.plugins
self._plugin_cache_dir = bootstrap_options.plugin_cache_dir
self._plugins_force_resolve = bootstrap_options.plugins_force_resolve
pex_specs.append((name if name in console_scripts else None, target))
args = ['-m', 'pex', package_dir] + reqs + self.pex_args
if self.get_log_level() < log.INFO and options.verbosity == 0:
args.append('-v')
for script_name, target in pex_specs:
pex_cmd = args + ['--output-file', target]
if script_name:
log.info('Writing %s to %s' % (script_name, target))
pex_cmd += ['--script', script_name]
else:
# The package has no namesake entry point, so build an environment pex.
log.info('Writing environment pex into %s' % target)
cmd, process = PythonInterpreter.get().open_process(
args=pex_cmd,
stderr=subprocess.PIPE,
# In order for code to run to here, pex is on the sys.path - make sure to propagate the
# sys.path so the subprocess can find us.
pythonpath=sys.path
)
_, stderr = process.communicate()
result = process.returncode
if result != 0:
die('Failed to create pex via {}:\n{}'.format(' '.join(cmd), stderr.decode('utf-8')),
result)
def __init__(self, options_bootstrapper, *, interpreter=None):
self._options_bootstrapper = options_bootstrapper
self._interpreter = interpreter or PythonInterpreter.get()
bootstrap_options = self._options_bootstrapper.get_bootstrap_options().for_global_scope()
self._plugin_requirements = bootstrap_options.plugins
self._plugin_cache_dir = bootstrap_options.plugin_cache_dir
def _select_interpreter(candidate_interpreters_iter):
current_interpreter = PythonInterpreter.get()
candidate_interpreters = []
for interpreter in candidate_interpreters_iter:
if current_interpreter == interpreter:
# Always prefer continuing with the current interpreter when possible.
return current_interpreter
else:
candidate_interpreters.append(interpreter)
if not candidate_interpreters:
return None
# TODO: Allow the selection strategy to be parameterized:
# https://github.com/pantsbuild/pex/issues/430
return min(candidate_interpreters)
def expand_platform():
expanded_platform = Platform(platform=cur_plat.platform,
impl=interpreter.identity.abbr_impl,
version=interpreter.identity.impl_ver,
abi=interpreter.identity.abi_tag)
TRACER.log("""
Modifying given platform of {given_platform!r}:
Using the current platform of {current_platform!r}
Under current interpreter {current_interpreter!r}
To match given interpreter {given_interpreter!r}.
Calculated platform: {calculated_platform!r}""".format(
given_platform=platform,
current_platform=cur_plat,
current_interpreter=PythonInterpreter.get(),
given_interpreter=interpreter,
calculated_platform=expanded_platform),
V=9
)
return expanded_platform
def interpreter_from_options(options):
interpreter = None
if options.python:
if os.path.exists(options.python):
interpreter = PythonInterpreter.from_binary(options.python)
else:
interpreter = PythonInterpreter.from_env(options.python)
if interpreter is None:
die('Failed to find interpreter: %s' % options.python)
else:
interpreter = PythonInterpreter.get()
with TRACER.timed('Setting up interpreter %s' % interpreter.binary, V=2):
resolve = functools.partial(resolve_interpreter, options.interpreter_cache_dir, options.repos)
# resolve setuptools
interpreter = resolve(interpreter, SETUPTOOLS_REQUIREMENT)
# possibly resolve wheel
if interpreter and options.use_wheel:
interpreter = resolve(interpreter, WHEEL_REQUIREMENT)
return interpreter