def make_distribution(name='my_project', zipped=False, zip_safe=True): interp = {'project_name': name} if zip_safe: interp['content'] = dedent(''' def do_something(): print('hello world!') ''') else: interp['content'] = dedent(''' if __file__ == 'derp.py': print('i am an idiot') ''') with temporary_content(PROJECT_CONTENT, interp=interp) as td: installer = Installer(td) distribution = installer.distribution() distiller = Distiller(distribution, debug=True) dist_location = distiller.distill(into=safe_mkdtemp()) if zipped: yield DistributionHelper.distribution_from_path(dist_location) else: with temporary_dir() as td: extract_path = os.path.join(td, os.path.basename(dist_location)) with contextlib.closing(zipfile.ZipFile(dist_location)) as zf: zf.extractall(extract_path) yield DistributionHelper.distribution_from_path(extract_path)
def setup_distribute(self, interpreter, dest): obtainer = Obtainer(self._crawler, self._fetchers, []) obtainer_iterator = obtainer.iter(self._setuptools_requirement) links = [link for link in obtainer_iterator if isinstance(link, SourceLink)] for link in links: self._logger('Fetching %s' % link) sdist = link.fetch() self._logger('Installing %s' % sdist) installer = Installer(sdist, strict=False, interpreter=interpreter) dist = installer.distribution() self._logger('Distilling %s' % dist) egg = Distiller(dist).distill(into=dest) safe_link(egg, os.path.join(dest, 'distribute')) break
def setup_distribute(self, interpreter, dest): obtainer = Obtainer(self._crawler, self._fetchers, []) obtainer_iterator = obtainer.iter(self._setuptools_requirement) links = [ link for link in obtainer_iterator if isinstance(link, SourceLink) ] for link in links: self._logger('Fetching %s' % link) sdist = link.fetch() self._logger('Installing %s' % sdist) installer = Installer(sdist, strict=False, interpreter=interpreter) dist = installer.distribution() self._logger('Distilling %s' % dist) egg = Distiller(dist).distill(into=dest) safe_link(egg, os.path.join(dest, 'distribute')) break
def obtain(self, req, *ignore_args, **ignore_kwargs): if not all(subcache.activated for subcache in self._subcaches): # Only fetch once all subcaches have been exhausted. return None with self.timed('Fetching %s' % req): fetched_req = self.fetcher.fetch(req) if not fetched_req: print('Failed to fetch %s' % req) return None installer = Installer(fetched_req) with self.timed('Building %s' % req): try: dist = installer.distribution() except Installer.InstallFailure as e: print('Failed to install %s' % req, file=sys.stderr) return None if self._install_cache: with self.timed('Distilling %s' % req): distilled = Distiller(dist).distill(into=self._install_cache) with self.timed('Constructing distribution %s' % req): metadata = EggMetadata(EggZipImporter(distilled)) dist = Distribution.from_filename(distilled, metadata) self.add(dist) return dist
def main(cls): from itertools import chain from twitter.common.python.distiller import Distiller from twitter.common.python.fetcher import Fetcher, PyPIFetcher from twitter.common.python.http import Crawler from twitter.common.python.installer import Installer from twitter.common.python.obtainer import Obtainer from twitter.common.python.resolver import Resolver from twitter.common.python.translator import Translator parser = cls.configure_clp() options, args = parser.parse_args() cls.process_logging_options(options) cls.exit_on_erroneous_inputs(options, parser) pex_builder = PEXBuilder() fetchers = [Fetcher(options.repos)] if options.use_pypi: fetchers.append(PyPIFetcher()) resolver = Resolver(cache=options.cache_dir, fetchers=fetchers, install_cache=options.cache_dir) reqs = cls.get_all_valid_reqs(options.requirements, options.requirements_txt) cls.logger.info("Requirements specified: " + str(reqs)) resolveds = resolver.resolve(reqs) cls.logger.info("Resolved requirements: " + str(resolveds)) for pkg in resolveds: cls.logger.info("Adding to PEX: Distribution: {0}".format(pkg)) pex_builder.add_distribution(pkg) pex_builder.add_requirement(pkg.as_requirement()) for source_dir in options.source_dirs: dist = Installer(source_dir).distribution() egg_path = Distiller(dist).distill() cls.logger.info( "Adding source dir to PEX: {0} distilled into egg {1}".format( source_dir, egg_path)) pex_builder.add_egg(egg_path) if options.entry_point is not None: if options.entry_point.endswith(".py"): cls.logger.info("Adding entry point to PEX: File: {0}".format( options.entry_point)) pex_builder.set_executable(options.entry_point) elif ":" in options.entry_point: cls.logger.info( "Adding entry point to PEX: Function: {0}".format( options.entry_point)) pex_builder.info().entry_point = options.entry_point else: cls.logger.warn("Invalid entry point: {0}".format( options.entry_point)) if options.pex_name is not None: cls.logger.info("Saving PEX file at {0}.pex".format( options.pex_name)) pex_builder.build(options.pex_name + '.pex') else: pex_builder.freeze() cls.logger.info("Running PEX file at {0} with args {1}".format( pex_builder.path(), args)) from .pex import PEX pex = PEX(pex_builder.path()) return pex.run(args=list(args)) logging.shutdown()