diff --git a/pex/bdist_pex.py b/pex/bdist_pex.py new file mode 100644 index 000000000..503387724 --- /dev/null +++ b/pex/bdist_pex.py @@ -0,0 +1,62 @@ +# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). +# Licensed under the Apache License, Version 2.0 (see LICENSE). + +from __future__ import absolute_import, print_function + +import os +from distutils.core import Command +from distutils import log as logger + +import pkg_resources + +safe_name = pkg_resources.safe_name +safe_version = pkg_resources.safe_version + +from .bin.pex import main + + +def safer_name(name): + return safe_name(name).replace('-', '_') + +def safer_version(version): + return safe_version(version).replace('-', '_') + + +class bdist_pex(Command): + description = 'create a pex distribution' + user_options = [ + ('dist-dir=', 'd', + "directory to put final built distributions in"), + ('python=', 'p', + "python interpreter to use"), + ] + + def initialize_options(self): + self.dist_dir = None + self.python = None + + def finalize_options(self): + need_options = ('dist_dir',) + + self.set_undefined_options('bdist', *zip(need_options, need_options)) + + def run(self): + pexfile_path = self._get_pexfile_path() + logger.info('creating %s', pexfile_path) + args = ['.', '-o', pexfile_path] + + if self.python: + args.append('--python=%s' % self.python) + + logger.debug('invoking pex with args: %r', args) + main(args) + + def _get_pexfile_path(self): + return os.path.join(self.dist_dir, '%s.pex' % self.pex_dist_name) + + @property + def pex_dist_name(self): + """Return distribution full name with - replaced with _""" + return '-'.join(( + safer_name(self.distribution.get_name()), + safer_version(self.distribution.get_version()))) diff --git a/pex/bin/pex.py b/pex/bin/pex.py index c7b81ccca..a2b2a8d3f 100644 --- a/pex/bin/pex.py +++ b/pex/bin/pex.py @@ -491,11 +491,13 @@ def build_pex(args, options, resolver_option_builder): return pex_builder -def main(): +def main(args=None): parser, resolver_options_builder = configure_clp() - # split arguments early because optparse is dumb - args = sys.argv[1:] + if args is None: + # split arguments early because optparse is dumb + args = sys.argv[1:] + try: separator = args.index('--') args, cmdline = args[:separator], args[separator + 1:] diff --git a/setup.py b/setup.py index b50c313e2..01e8883f0 100644 --- a/setup.py +++ b/setup.py @@ -55,5 +55,8 @@ 'console_scripts': [ 'pex = pex.bin.pex:main', ], + 'distutils.commands': [ + 'bdist_pex = pex.bdist_pex:bdist_pex', + ], }, )