Skip to content

Commit

Permalink
Rework virtualenv handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljoseph committed Jul 6, 2014
1 parent ca8b607 commit d51d9b1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
51 changes: 20 additions & 31 deletions changes/packaging.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
import logging
import tempfile

from path import path
import sh
import virtualenv

from changes import config, shell, probe, verification
from changes import config, shell, probe, util, venv, verification

log = logging.getLogger(__name__)


def make_virtualenv():
tmp_dir = tempfile.mkdtemp()
virtualenv.create_environment(tmp_dir, site_packages=False)
return tmp_dir


def install():
module_name, dry_run, new_version = config.common_arguments()
commands = ['setup.py', 'clean', 'sdist']
if probe.has_requirement('wheel'):
commands.append('bdist_wheel')
commands = ['setup.py', 'clean', 'bdist_wheel']

result = shell.handle_dry_run(sh.python, tuple(commands))

if result:
tmp_dir = make_virtualenv()
package_name = config.arguments.get('--package-name') or module_name
try:
virtualenv.install_sdist(
config.arguments['<module_name>'],
'dist/%s-%s.tar.gz' % (package_name, new_version),
'%s/bin/python' % tmp_dir
)
log.info('Successfully installed %s sdist', module_name)
if verification.run_test_command():
log.info('Successfully ran test command: %s',
config.arguments['--test-command'])
except:
raise Exception('Error installing %s sdist', module_name)

path(tmp_dir).rmtree(path(tmp_dir))
with util.mktmpdir() as tmp_dir:
venv.create_venv(tmp_dir=tmp_dir)
package_name = config.arguments.get('--package-name') or module_name
try:
print(package_name)
print(tmp_dir)
venv.install(package_name, tmp_dir)
log.info('Successfully installed %s sdist', module_name)
if verification.run_test_command():
log.info('Successfully ran test command: %s',
config.arguments['--test-command'])
except Exception, e:
raise Exception('Error installing %s sdist', module_name, e)


def upload():
Expand All @@ -59,7 +48,7 @@ def upload():
def pypi():
module_name, dry_run, _ = config.common_arguments()

tmp_dir = make_virtualenv()
tmp_dir = venv.create_venv()
install_cmd = '%s/bin/pip install %s' % (tmp_dir, module_name)

package_index = 'pypi'
Expand All @@ -78,12 +67,12 @@ def pypi():
module_name, package_index)

verification.run_test_command()
except:
except Exception, e:
log.exception(
'error installing %s from %s', module_name, package_index
)
raise Exception(
'Error installing %s from %s', module_name, package_index
'Error installing %s from %s', module_name, package_index, e
)

path(tmp_dir).rmtree(path(tmp_dir))
21 changes: 21 additions & 0 deletions changes/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import contextlib
import tempfile

from path import path


def extract(dictionary, keys):
"""
Extract only the specified keys from a dict
Expand All @@ -12,6 +18,10 @@ def extract(dictionary, keys):


def extract_arguments(arguments, long_keys, key_prefix='--'):
"""
:param arguments: dict of command line arguments
"""
long_arguments = extract(
arguments,
long_keys,
Expand All @@ -20,3 +30,14 @@ def extract_arguments(arguments, long_keys, key_prefix='--'):
(key.replace(key_prefix, ''), value)
for key, value in long_arguments.items()
])


@contextlib.contextmanager
def mktmpdir():
tmp_dir = tempfile.mkdtemp()
try:
yield tmp_dir
finally:
path(tmp_dir).rmtree(path(tmp_dir))


3 changes: 0 additions & 3 deletions tests/test_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ def test_install_with_module_name(self):
config.arguments['--package-name'] = 'thing'
packaging.install()

def test_make_virtualenv(self):
packaging.make_virtualenv()

def test_upload(self):
packaging.upload()

Expand Down

0 comments on commit d51d9b1

Please sign in to comment.