Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
Allow to create virtualenv outside project folder
Browse files Browse the repository at this point in the history
  • Loading branch information
constrict0r committed Nov 11, 2018
1 parent 4ee2bdc commit d858c37
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 23 deletions.
11 changes: 7 additions & 4 deletions amanita/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ def version_msg():

@click.command(context_settings=dict(help_option_names=[u'-h', u'--help']))
@click.version_option(__version__, u'-V', u'--version', message=version_msg())
@click.argument('destination', required=True)
@click.argument('path', required=True)
@click.option(
u'-d', u'--direnv', is_flag=True,
help=u'Install and configure direnv console enviroment switcher.')
@click.option(
u'-e', '--venv', is_flag=True,
u'-v', '--venv', is_flag=True,
help=u'Create and configure a virtual enviroment.')
@click.option(
u'-e', '--venv-path',
help=u'Create and configure a virtual enviroment on the given path.')
# TODO: add --dry-run
def main(destination, direnv, venv):
def main(path, direnv, venv, venv_path):
"""Creates a customizable python project"""

project.Project(destination, direnv, venv)
project.Project(path, direnv, venv, venv_path)
34 changes: 28 additions & 6 deletions amanita/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@ class Project:
tox = False
travis = False
venv = False
venv_path = False
web = False

# Create the project.
def __init__(self, destination, direnv=False, venv=False):
def __init__(self, path, direnv=False, venv=False, venv_path=''):
"""Create a customizable project.
Creates a python project with basic package directory layout
and optionally:
- A virtual enviroment inside or outside the project directory.
Args:
path (str): Path where to create the project.
direnv: Configure direnv?
venv: Create a virtual enviroment?
"""

# Create package folder structure.
try:
assert subprocess.call('poetry new -- ' +
destination, shell=True) == 0
path, shell=True) == 0

except AssertionError:
click.echo(click.style('An error occured creating the project',
Expand All @@ -37,16 +49,24 @@ def __init__(self, destination, direnv=False, venv=False):

# Create virtual enviroment.
if venv is True:
self.venv_setup(destination)
self.venv_setup(path)
elif venv_path is not None:
self.venv_setup(venv_path)

# Create virtual enviroment.
@staticmethod
def venv_setup(path):
"""Create a virtual enviroment.
from poetry.utils.env import Env
Creates a virtual enviroment on a given path.
# TODO: test this:
# path = '/dev/null'
Args:
path (str): Path where to create the virtual enviroment.
Returns:
bool: True for success, False otherwise.
"""
from poetry.utils.env import Env

# This will overwrite an existing virtual enviroment.
try:
Expand All @@ -68,3 +88,5 @@ def venv_setup(path):
click.style(path, fg='cyan') +
click.style(' is not a directory', fg='red'))
sys.exit(1)

return True
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 67 additions & 11 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,62 @@
import subprocess
import shutil


from amanita import project


# Create project.
def test_create_project():

if os.path.isdir('muscaria'):
shutil.rmtree(os.path.join('muscaria'))

subprocess.check_call('amanita muscaria',
env=os.environ.copy(),
shell=True)
shutil.rmtree(os.path.join('muscaria'))


# Create project with existing directory.
def test_create_project_existing_directory():
os.mkdir('muscaria')
subprocess.check_call('amanita muscaria',
env=os.environ.copy(),
shell=True)
shutil.rmtree(os.path.join('muscaria'))


# Create project with existing non-empty directory.
@pytest.mark.xfail
def test_create_project_existing_directory_non_empty():
subprocess.check_call('amanita amanita',
env=os.environ.copy(),
shell=True)


# Create project with non permission.
@pytest.mark.xfail
def test_create_project_forbidden():
subprocess.check_call('amanita /root',
env=os.environ.copy(),
shell=True)


# Create virtual enviroment.
def test_create_venv():

project.Project.venv_setup(os.path.join(''))
if not os.path.exists('.venv'):
return False
assert os.path.isdir('.venv')
shutil.rmtree(os.path.join('.venv'))


# Create virtual enviroment with non existing path
# Create virtual enviroment with nonexisting path.
@pytest.mark.xfail
def test_create_venv_nonexist():
def test_create_venv_nonexistent():

project.Project.venv_setup('/dev/null')


# Create virtual enviroment with non permission
# Create virtual enviroment with non permission.
@pytest.mark.xfail
def test_create_venv_forbidden():

Expand All @@ -45,17 +71,47 @@ def test_overwrite_venv():

project.Project.venv_setup(os.path.join(''))
project.Project.venv_setup(os.path.join(''))
if not os.path.exists('.venv'):
return False
assert os.path.isdir('.venv')
shutil.rmtree(os.path.join('.venv'))


# Create project with virtual enviroment.
def test_create_project_venv():

subprocess.check_call('amanita muscaria -e',
subprocess.check_call('amanita muscaria -v',
env=os.environ.copy(),
shell=True)
if not os.path.exists('muscaria/.venv'):
return False
assert os.path.isdir('muscaria/.venv')
shutil.rmtree(os.path.join('muscaria'))


# Create project with virtual enviroment outside the project directory.
def test_create_project_venv_out():

subprocess.check_call('amanita muscaria -e venv',
env=os.environ.copy(),
shell=True)
assert os.path.isdir('venv')
shutil.rmtree(os.path.join('muscaria'))
shutil.rmtree(os.path.join('venv'))


# Create project with virtual enviroment outside the project.
# with nonexisting path.
@pytest.mark.xfail
def test_create_project_venv_out_nonexistent():

subprocess.check_call('amanita muscaria -e /dev/null',
env=os.environ.copy(),
shell=True)


# Create project with virtual enviroment outside the project.
# with non permission.
@pytest.mark.xfail
def test_create_project_venv_out_forbidden():

shutil.rmtree(os.path.join('muscaria'))
subprocess.check_call('amanita muscaria -e /root',
env=os.environ.copy(),
shell=True)

0 comments on commit d858c37

Please sign in to comment.