Skip to content

Commit

Permalink
First cut at bdist_pex
Browse files Browse the repository at this point in the history
  • Loading branch information
msabramo committed May 7, 2015
1 parent 3b55df7 commit 72ebead
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
74 changes: 74 additions & 0 deletions pex/bdist_pex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# 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 CANNOT_SETUP_INTERPRETER
from .common import die
from .interpreter import PythonInterpreter
from .pex_builder import PEXBuilder


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):
pex_builder = PEXBuilder(interpreter=self.interpreter)
pexfile_path = self._get_pexfile_path()
logger.info('creating %s', pexfile_path)
pex_builder.build(pexfile_path)

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())))

@property
def interpreter(self):
if self.python is None:
interpreter = None
elif os.path.exists(self.python):
interpreter = PythonInterpreter.from_binary(self.python)
else:
interpreter = PythonInterpreter.from_env(self.python)

if interpreter is None:
die('Could not find compatible interpreter', CANNOT_SETUP_INTERPRETER)

return interpreter
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@
'console_scripts': [
'pex = pex.bin.pex:main',
],
'distutils.commands': [
'bdist_pex = pex.bdist_pex:bdist_pex',
],
},
)

0 comments on commit 72ebead

Please sign in to comment.