Skip to content

Commit

Permalink
41-properly format pip install path on Windows
Browse files Browse the repository at this point in the history
it appears that on Windows based platforms the default pip install path(for Python < 2.7.9) is C:\Python27\Scripts however wagon assumed it is located along-side python inside os.path.dirname(sys.executable) which is C:\Python27 and therefore failed to work on Windows based platforms.

this patch resolves that issue.

Note that when in a virtualenv, wagon will try to provide the virtual env path so that pip is found in the correct path
  • Loading branch information
Nir Fuchs authored and nir0s committed Jun 9, 2016
1 parent 147404b commit c059b54
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
8 changes: 7 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ environment:
PYTHON_VERSION: 2.7.8
PYTHON_ARCH: 32

# init:
# - ps: iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))

install:

#################################
Expand Down Expand Up @@ -36,5 +39,8 @@ before_test:
- echo Installing tox (2.0.0)
- pip install tox==2.0.0

# on_finish:
# - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))

test_script:
- tox -e %TOX_ENV%
- tox -e %TOX_ENV%
3 changes: 2 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
nose
mock
nose-cov
testtools
testfixtures
testtools
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ passenv=ProgramFiles APPVEYOR LOGNAME USER LNAME USERNAME HOME USERPROFILE
deps =
flake8
-rdev-requirements.txt
commands=flake8 wagon
commands=flake8 wagon
21 changes: 18 additions & 3 deletions wagon/tests/test_wagon.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tempfile
from contextlib import closing

import mock
import testtools
import click.testing as clicktest

Expand All @@ -28,10 +29,10 @@
import wagon.codes as codes


TEST_FILE = 'https://github.com/cloudify-cosmo/cloudify-script-plugin/archive/1.2.tar.gz' # NOQA
TEST_ZIP = 'https://github.com/cloudify-cosmo/cloudify-script-plugin/archive/1.2.zip' # NOQA
TEST_FILE = 'https://github.com/cloudify-cosmo/cloudify-script-plugin/archive/1.4.tar.gz' # NOQA
TEST_ZIP = 'https://github.com/cloudify-cosmo/cloudify-script-plugin/archive/1.4.zip' # NOQA
TEST_PACKAGE_NAME = 'cloudify-script-plugin'
TEST_PACKAGE_VERSION = '1.2'
TEST_PACKAGE_VERSION = '1.4'
TEST_PACKAGE_PLATFORM = 'linux_x86_64'
TEST_PACKAGE = '{0}=={1}'.format(TEST_PACKAGE_NAME, TEST_PACKAGE_VERSION)

Expand Down Expand Up @@ -152,6 +153,20 @@ def test_install_package_failed(self):
self.assertEqual(
str(codes.errors['failed_to_install_package']), str(e))

@mock.patch('sys.executable', new='/a/b/c/python')
def test_pip_path_on_linux(self):
if utils.IS_WIN:
self.skipTest('Irrelevant on Windows')
self.assertEqual(utils._get_pip_path(virtualenv=''), '/a/b/c/pip')

@mock.patch('sys.executable',
new='C:\Python27\python.exe')
def test_pip_path_on_windows(self):
if utils.IS_LINUX:
self.skipTest('Irrelevant on Linux')
self.assertEqual(utils._get_pip_path(virtualenv=''),
'C:\Python27\Scripts\pip.exe')


class TestCreateBadSources(testtools.TestCase):
def test_unsupported_url_schema(self):
Expand Down
48 changes: 25 additions & 23 deletions wagon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def run(cmd, suppress_errors=False, suppress_output=False):
def wheel(package, requirement_files=False, wheels_path='package',
excluded_packages=None, wheel_args=None, no_deps=False):
lgr.info('Downloading Wheels for {0}...'.format(package))
pip_executable = os.path.join(os.path.dirname(sys.executable), 'pip')
pip_executable = _get_pip_path(os.environ.get('VIRTUAL_ENV'))
wheel_cmd = [pip_executable, 'wheel']
wheel_cmd.append('--wheel-dir={0}'.format(wheels_path))
wheel_cmd.append('--find-links={0}'.format(wheels_path))
Expand All @@ -109,21 +109,18 @@ def wheel(package, requirement_files=False, wheels_path='package',
wheel_cmd_with_reqs = wheel_cmd
for req_file in requirement_files:
wheel_cmd_with_reqs.extend(['-r', req_file])
p = run(' '.join(wheel_cmd_with_reqs))
if not p.returncode == 0:
lgr.error('Could not download wheels for: {0}. '
'Please verify that the file you are trying '
'to wheel is wheelable.'.format(req_file))
process = run(' '.join(wheel_cmd_with_reqs))
if not process.returncode == 0:
lgr.error('Could not download wheels for: {0} ({1})'.format(
req_file, process.aggr_stderr))
sys.exit(codes.errors['failed_to_wheel'])
if wheel_args:
wheel_cmd.append(wheel_args)
wheel_cmd.append(package)
p = run(' '.join(wheel_cmd))
if not p.returncode == 0:
lgr.error('Could not download wheels for: {0}. '
'Please verify that the package you are trying '
'to wheel is wheelable.'.format(package))
lgr.error(p.aggr_stdout)
process = run(' '.join(wheel_cmd))
if not process.returncode == 0:
lgr.error('Could not download wheels for: {0} ({1})'.format(
package, process.aggr_stderr))
sys.exit(codes.errors['failed_to_wheel'])
wheels = get_downloaded_wheels(wheels_path)
excluded_packages = excluded_packages or []
Expand All @@ -146,7 +143,7 @@ def get_wheel_for_package(wheels_path, package):
return wheel


def install_package(package, wheels_path, virtualenv_path=None,
def install_package(package, wheels_path, virtualenv=None,
requirements_file=None, upgrade=False,
install_args=None):
"""This will install a Python package.
Expand All @@ -161,12 +158,8 @@ def install_package(package, wheels_path, virtualenv_path=None,
# install_args = install_args or []

lgr.info('Installing {0}...'.format(package))
pip_executable = os.path.join(os.path.dirname(sys.executable), 'pip')
pip_executable = _get_pip_path(virtualenv)
pip_cmd = [pip_executable, 'install']
if virtualenv_path:
pip_cmd = ['pip', 'install']
pip_cmd[0] = os.path.join(
_get_env_bin_path(virtualenv_path), pip_cmd[0])
if requirements_file:
pip_cmd.extend(['-r', requirements_file])
if install_args:
Expand All @@ -178,7 +171,7 @@ def install_package(package, wheels_path, virtualenv_path=None,
pip_cmd.append('--pre')
if upgrade:
pip_cmd.append('--upgrade')
if IS_VIRTUALENV and not virtualenv_path:
if IS_VIRTUALENV and not virtualenv:
lgr.info('Installing within current virtualenv: {0}...'.format(
IS_VIRTUALENV))
result = run(' '.join(pip_cmd))
Expand Down Expand Up @@ -289,13 +282,22 @@ def _get_env_bin_path(env_path):
return os.path.join(env_path, 'scripts' if IS_WIN else 'bin')


def _get_pip_path(virtualenv=None):
if virtualenv:
return os.path.join(
_get_env_bin_path(virtualenv),
'pip.exe' if IS_WIN else 'pip')
else:
return os.path.join(
os.path.dirname(sys.executable),
'Scripts' if IS_WIN else '',
'pip.exe' if IS_WIN else 'pip')


def check_installed(package, virtualenv):
"""Checks to see if a package is installed within a virtualenv.
"""
if virtualenv:
pip_executable = os.path.join(_get_env_bin_path(virtualenv), 'pip')
else:
pip_executable = os.path.join(os.path.dirname(sys.executable), 'pip')
pip_executable = _get_pip_path(virtualenv)
p = run('{0} freeze'.format(pip_executable), suppress_output=True)
if re.search(r'{0}'.format(package), p.aggr_stdout.lower()):
lgr.debug('Package {0} is installed in {1}'.format(
Expand Down

0 comments on commit c059b54

Please sign in to comment.