def test_get_script_from_distributions(tmpdir): whl_path = './tests/example_packages/aws_cfn_bootstrap-1.4-py2-none-any.whl' install_dir = os.path.join(str(tmpdir), os.path.basename(whl_path)) get_pip().spawn_install_wheel(wheel=whl_path, install_dir=install_dir).wait() dist = DistributionHelper.distribution_from_path(install_dir) assert 'aws-cfn-bootstrap' == dist.project_name dist_script = get_script_from_distributions('cfn-signal', [dist]) assert dist_script.dist is dist assert os.path.join(install_dir, 'bin/cfn-signal') == dist_script.path assert dist_script.read_contents().startswith('#!'), ( 'Expected a `scripts`-style script w/shebang.') assert None is get_script_from_distributions('non_existent_script', [dist])
def set_script(self, script): """Set the entry point of this PEX environment based upon a distribution script. :param script: The script name as defined either by a console script or ordinary script within the setup.py of one of the distributions added to the PEX. :raises: :class:`PEXBuilder.InvalidExecutableSpecification` if the script is not found in any distribution added to the PEX. """ # check if 'script' is a console_script dist, entry_point = get_entry_point_from_console_script( script, self._distributions.values()) if entry_point: self.set_entry_point(entry_point) TRACER.log("Set entrypoint to console_script %r in %r" % (entry_point, dist)) return # check if 'script' is an ordinary script dist_script = get_script_from_distributions( script, self._distributions.values()) if dist_script: if self._pex_info.entry_point: raise self.InvalidExecutableSpecification( "Cannot set both entry point and script of PEX!") self._pex_info.script = script TRACER.log("Set entrypoint to script %r in %r" % (script, dist_script.dist)) return raise self.InvalidExecutableSpecification( "Could not find script %r in any distribution %s within PEX!" % (script, ", ".join(str(d) for d in self._distributions.values())))
def set_script(self, script): """Set the entry point of this PEX environment based upon a distribution script. :param script: The script name as defined either by a console script or ordinary script within the setup.py of one of the distributions added to the PEX. :raises: :class:`PEXBuilder.InvalidExecutableSpecification` if the script is not found in any distribution added to the PEX. """ # check if 'script' is a console_script dist, entry_point = get_entry_point_from_console_script(script, self._distributions) if entry_point: self.set_entry_point(entry_point) TRACER.log('Set entrypoint to console_script %r in %r' % (entry_point, dist)) return # check if 'script' is an ordinary script dist, _, _ = get_script_from_distributions(script, self._distributions) if dist: if self._pex_info.entry_point: raise self.InvalidExecutableSpecification('Cannot set both entry point and script of PEX!') self._pex_info.script = script TRACER.log('Set entrypoint to script %r in %r' % (script, dist)) return raise self.InvalidExecutableSpecification( 'Could not find script %r in any distribution %s within PEX!' % ( script, ', '.join(str(d) for d in self._distributions)))
def test_get_script_from_distributions(tmpdir): # type: (Any) -> None whl_path = "./tests/example_packages/aws_cfn_bootstrap-1.4-py2-none-any.whl" install_dir = os.path.join(str(tmpdir), os.path.basename(whl_path)) get_pip().spawn_install_wheel(wheel=whl_path, install_dir=install_dir).wait() dist = DistributionHelper.distribution_from_path(install_dir) assert "aws-cfn-bootstrap" == dist.project_name dist_script = get_script_from_distributions("cfn-signal", [dist]) assert dist_script.dist is dist assert os.path.join(install_dir, "bin/cfn-signal") == dist_script.path assert dist_script.read_contents().startswith( "#!"), "Expected a `scripts`-style script w/shebang." assert None is get_script_from_distributions("non_existent_script", [dist])
def execute_script(self, script_name): dists = list(self._activate()) dist, entry_point = get_entry_point_from_console_script(script_name, dists) if entry_point: TRACER.log('Found console_script %r in %r' % (entry_point, dist)) sys.exit(self.execute_entry(entry_point)) dist, script_path, script_content = get_script_from_distributions(script_name, dists) if not dist: raise self.NotFound('Could not find script %r in pex!' % script_name) TRACER.log('Found script %r in %r' % (script_name, dist)) return self.execute_content(script_path, script_content, argv0=script_name)
def execute_script(self, script_name): dists = list(self._activate()) dist, entry_point = get_entry_point_from_console_script(script_name, dists) if entry_point: TRACER.log('Found console_script %r in %r' % (entry_point, dist)) sys.exit(self.execute_entry(entry_point)) dist, script_path, script_content = get_script_from_distributions(script_name, dists) if not dist: raise self.NotFound('Could not find script %r in pex!' % script_name) TRACER.log('Found script %r in %r' % (script_name, dist)) return self.execute_content(script_path, script_content, argv0=script_name)
def execute_script(self, script_name): dists = list(self.activate()) dist, entry_point = get_entry_point_from_console_script(script_name, dists) if entry_point: TRACER.log("Found console_script %r in %r" % (entry_point, dist)) sys.exit(self.execute_entry(entry_point)) dist_script = get_script_from_distributions(script_name, dists) if not dist_script: raise self.NotFound("Could not find script %r in pex!" % script_name) TRACER.log("Found script %r in %r" % (script_name, dist)) return self.execute_content( dist_script.path, dist_script.read_contents(), argv0=script_name )
def execute_script(self, script_name): # type: (str) -> Any dists = list(self.activate()) dist, entry_point = get_entry_point_from_console_script(script_name, dists) if entry_point: TRACER.log("Found console_script {!r} in {!r}.".format(entry_point, dist)) return self.execute_entry(entry_point) dist_script = get_script_from_distributions(script_name, dists) if not dist_script: return "Could not find script {!r} in pex!".format(script_name) TRACER.log("Found script {!r} in {!r}.".format(script_name, dist)) return self.execute_content( dist_script.path, dist_script.read_contents(), argv0=script_name )
def set_script(self, script): """Set the entry point of this PEX environment based upon a distribution script. :param script: The script name as defined either by a console script or ordinary script within the setup.py of one of the distributions added to the PEX. :raises: :class:`PEXBuilder.InvalidExecutableSpecification` if the script is not found in any distribution added to the PEX. """ distributions = OrderedSet(self._distributions.values()) if self._pex_info.pex_path: for pex in self._pex_info.pex_path.split(":"): if os.path.exists(pex): distributions.update( PEX(pex, interpreter=self._interpreter).resolve()) # Check if 'script' is a console_script. dist, entry_point = get_entry_point_from_console_script( script, distributions) if entry_point: self.set_entry_point(entry_point) TRACER.log("Set entrypoint to console_script {!r} in {!r}".format( entry_point, dist)) return # Check if 'script' is an ordinary script. dist_script = get_script_from_distributions(script, distributions) if dist_script: if self._pex_info.entry_point: raise self.InvalidExecutableSpecification( "Cannot set both entry point and script of PEX!") self._pex_info.script = script TRACER.log("Set entrypoint to script {!r} in {!r}".format( script, dist_script.dist)) return raise self.InvalidExecutableSpecification( "Could not find script {!r} in any distribution {} within PEX!". format(script, ", ".join(str(d) for d in distributions)))