Skip to content

Commit

Permalink
Merge pull request #11 from kevinconway/pth-files
Browse files Browse the repository at this point in the history
Add support for .pth file rewrites
  • Loading branch information
kevinconway authored Aug 25, 2019
2 parents 205c65f + 843db40 commit 53cb9cc
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 117 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ docs/_build/

# PyBuilder
target/

# vscode
.vscode/

# mypy
.mypy_cache/

# pytest
.pytest_cache
5 changes: 3 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ confidence=
# Disable R0901 to hide warnings caused by use of mixins.
# Disable W0201 to hide warnings caused by use of mixins.
# Disable R0205 to hide Python3.X warnings of inheriting from object.
disable=F0401,R0901,W0201,R0205
# Disable C0330 to prevent indentation conflicts between pylint and black.
disable=F0401,R0901,W0201,R0205,C0330


[MISCELLANEOUS]
Expand Down Expand Up @@ -127,7 +128,7 @@ generated-members=
spelling-dict=en_US

# List of comma separated words that should not be checked.
spelling-ignore-words=config,namespace,iterable,json,ini,regex,namespaces,str,JSON,INI,init,behaviour,setattr,getattr,classmethod,instancemethod,dict,bool,metadata,iteritems,args,kwargs,cls,iter,subclass,subclasses,api,API,unicode,_namespaces,NAMESPACES,_NAMESPACES,proxied,hasattr,prepend,prepended,os,env,cli,CLI,arg,args,argv,url,URL,uninstall,mixin,Mixin,pip,subshell,csh,sh,symlink,symlinked,symlinking,pypi,cmd,txt,virtualenv,reinstall
spelling-ignore-words=config,namespace,iterable,json,ini,regex,namespaces,str,JSON,INI,init,behaviour,setattr,getattr,classmethod,instancemethod,dict,bool,metadata,iteritems,args,kwargs,cls,iter,subclass,subclasses,api,API,unicode,_namespaces,NAMESPACES,_NAMESPACES,proxied,hasattr,prepend,prepended,os,env,cli,CLI,arg,args,argv,url,URL,uninstall,mixin,Mixin,pip,subshell,csh,sh,symlink,symlinked,symlinking,pypi,cmd,txt,virtualenv,reinstall,pth,venv,recurse,xsh

# A path to a file that contains private dictionary; one word per line.
spelling-private-dict-file=
Expand Down
18 changes: 8 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@
from setuptools import find_packages


with open('README.rst', 'r') as readmefile:
with open("README.rst", "r") as readmefile:

README = readmefile.read()

setup(
name='venvctrl',
version='0.3.0',
url='https://github.com/kevinconway/venvctrl',
description='API for virtual environments.',
name="venvctrl",
version="0.4.0",
url="https://github.com/kevinconway/venvctrl",
description="API for virtual environments.",
author="Kevin Conway",
author_email="[email protected]",
long_description=README,
license='MIT',
packages=find_packages(exclude=['tests', 'build', 'dist', 'docs']),
license="MIT",
packages=find_packages(exclude=["tests", "build", "dist", "docs"]),
entry_points={
'console_scripts': [
'venvctrl-relocate = venvctrl.cli.relocate:main',
],
"console_scripts": ["venvctrl-relocate = venvctrl.cli.relocate:main"]
},
include_package_data=True,
)
78 changes: 51 additions & 27 deletions tests/test_virtual_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
from venvctrl import api


@pytest.fixture(scope='function')
@pytest.fixture(scope="function")
def random():
"""Get a random UUID."""
return str(uuid.uuid4())


@pytest.fixture(scope='function')
@pytest.fixture(scope="function")
def venv(random, tmpdir):
"""Get an initialized venv."""
v = api.VirtualEnvironment(str(tmpdir.join(random)))
Expand All @@ -37,22 +37,22 @@ def test_create(random, tmpdir):

def test_pip(venv):
"""Test the ability to manage packages with pip."""
venv.install_package('confpy')
assert venv.has_package('confpy')
venv.uninstall_package('confpy')
assert not venv.has_package('confpy')
path = os.path.join(venv.path, '..', 'requirements.txt')
with open(path, 'w') as req_file:
venv.install_package("confpy")
assert venv.has_package("confpy")
venv.uninstall_package("confpy")
assert not venv.has_package("confpy")
path = os.path.join(venv.path, "..", "requirements.txt")
with open(path, "w") as req_file:

req_file.write('confpy{0}'.format(os.linesep))
req_file.write("confpy{0}".format(os.linesep))

venv.install_requirements(path)
assert venv.has_package('confpy')
assert venv.has_package("confpy")


def test_relocate(venv):
"""Test the ability to relocate a venv."""
path = '/testpath'
path = "/testpath"
pypy_shebang = "#!/usr/bin/env pypy"
f = open(venv.bin.abspath + "/pypy_shebang.py", "w")
f.write(pypy_shebang)
Expand All @@ -66,17 +66,17 @@ def test_relocate(venv):

if script.shebang:

assert script.shebang == '#!{0}/bin/python'.format(
path,
)
assert script.shebang == "#!{0}/bin/python".format(path)


def test_relocate_long_shebang(venv):
"""Test the ability to relocate a venv."""
path = '/testpath'
long_shebang = "#!/bin/sh{0}" \
"'''exec' /tmp/rpmbuild/python \"$0\" \"$@\"{0}" \
"' '''{0}".format(os.linesep)
path = "/testpath"
long_shebang = (
"#!/bin/sh{0}"
"'''exec' /tmp/rpmbuild/python \"$0\" \"$@\"{0}"
"' '''{0}".format(os.linesep)
)
f = open(venv.bin.abspath + "/long_shebang.py", "w")
f.write(long_shebang)
f.close()
Expand All @@ -89,17 +89,41 @@ def test_relocate_long_shebang(venv):
if shebang:
shebang = shebang.split(os.linesep)
if len(shebang) == 1:
assert shebang == ['#!{0}/bin/python'.format(
path
)]
assert shebang == ["#!{0}/bin/python".format(path)]

elif len(shebang) == 3:
assert shebang == \
['#!/bin/sh',
'\'\'\'exec\' {0}/bin/python "$0" "$@"'.format(
path),
"' '''"]
assert shebang == [
"#!/bin/sh",
"'''exec' {0}/bin/python \"$0\" \"$@\"".format(path),
"' '''",
]

else:
assert False, "Invalid shebang length: {0}, {1}".format(
len(shebang), script.shebang)
len(shebang), script.shebang
)


def test_relocate_no_original_path(venv):
"""Test that the original path is not found in files."""
path = "/testpath"
original_path = venv.abspath
f = open(venv.bin.abspath + "/something.pth", "w")
f.write(original_path)
f.close()
venv.relocate(path)
dirs = list(venv.dirs)
files = list(venv.files)
while dirs or files:
for file_ in files:
with open(file_.abspath, "r") as source:
try:
lines = source.readlines()
except UnicodeDecodeError:
# Skip any non-text files. Binary files are out of
# scope for this test.
continue
for line in lines:
assert original_path not in line, file_.abspath
next_dir = dirs.pop()
files = list(next_dir.files)
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ commands=py.test tests/

[testenv:pep8]
commands=
pep8 venvctrl/
pep8 tests/
pycodestyle venvctrl/
pycodestyle tests/

[testenv:pyflakes]
commands=
Expand Down
10 changes: 5 additions & 5 deletions venvctrl/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@


class VirtualEnvironment(
base.VirtualEnvironment,
command.CommandMixin,
create.CreateMixin,
pip.PipMixin,
relocate.RelocateMixin,
base.VirtualEnvironment,
command.CommandMixin,
create.CreateMixin,
pip.PipMixin,
relocate.RelocateMixin,
):

"""Virtual environment management class."""
18 changes: 8 additions & 10 deletions venvctrl/cli/relocate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,27 @@ def relocate(source, destination, move=False):
def main():
"""Relocate a virtual environment."""
parser = argparse.ArgumentParser(
description='Relocate a virtual environment.'
description="Relocate a virtual environment."
)
parser.add_argument(
'--source',
help='The existing virtual environment.',
required=True,
"--source", help="The existing virtual environment.", required=True
)
parser.add_argument(
'--destination',
help='The location for which to configure the virtual environment.',
"--destination",
help="The location for which to configure the virtual environment.",
required=True,
)
parser.add_argument(
'--move',
help='Move the virtual environment to the destination.',
"--move",
help="Move the virtual environment to the destination.",
default=False,
action='store_true',
action="store_true",
)

args = parser.parse_args()
relocate(args.source, args.destination, args.move)


if __name__ == '__main__':
if __name__ == "__main__":

main()
Loading

0 comments on commit 53cb9cc

Please sign in to comment.