Skip to content
This repository has been archived by the owner on Jan 30, 2019. It is now read-only.

Requirements spec #203

Merged
merged 4 commits into from
Nov 19, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conda_env/cli/main_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
conda env create -n name
conda env create vader/deathstar
conda env create -f=/path/to/environment.yml
conda env create -f=/path/to/requirements.txt -n deathstar
"""


Expand Down
4 changes: 3 additions & 1 deletion conda_env/specs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from .binstar import BinstarSpec
from .yaml_file import YamlFileSpec
from .notebook import NotebookSpec
from .requirements import RequirementsSpec
from ..exceptions import SpecNotFound

all_specs = [
BinstarSpec,
NotebookSpec,
YamlFileSpec
YamlFileSpec,
RequirementsSpec
]


Expand Down
43 changes: 43 additions & 0 deletions conda_env/specs/requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
from .. import env

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is nice to have imports separated by the origin like...

# Standard library imports
import os

# Local Imports
from .. import env


class RequirementsSpec(object):
'''
Reads depedencies from a requirements.txt file
and returns an Environment object from it.
'''
msg = None

def __init__(self, filename=None, name=None, **kwargs):
self.filename = filename
self.name = name
self.msg = None

def can_handle(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have normal methods after private ones...

so

def __init__  ...

def _valid_file ...

def _valid_name ....

def can_handle ...

return self._valid_file() and self._valid_name()

def _valid_file(self):
if os.path.exists(self.filename):
return True
else:
self.msg = "There is no requirements.txt"
return False

def _valid_name(self):
if self.name is None:
self.msg = "Environment with requierements.txt file needs a name"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo here:
requierements.txt should be requirements.txt

return False
else:
return True

@property
def environment(self):
dependencies = []
with open(self.filename) as reqfile:
for line in reqfile:
dependencies.append(line)
return env.Environment(
name=self.name,
dependencies=dependencies
)
3 changes: 3 additions & 0 deletions conda_env/specs/yaml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def can_handle(self):
except EnvironmentFileNotFound as e:
self.msg = str(e)
return False
except TypeError:
self.msg = "{} is not a valid yaml file.".format(self.filename)
return False

@property
def environment(self):
Expand Down
26 changes: 26 additions & 0 deletions tests/specs/test_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
from conda_env import env
from conda_env.specs.requirements import RequirementsSpec
from .. import utils

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as comment from before :-)


class TestRequiremets(unittest.TestCase):
def test_no_environment_file(self):
spec = RequirementsSpec(name=None, filename='not-a-file')
self.assertEqual(spec.can_handle(), False)

def test_no_name(self):
spec = RequirementsSpec(filename=utils.support_file('requirements.txt'))
self.assertEqual(spec.can_handle(), False)

def test_req_file_and_name(self):
spec = RequirementsSpec(filename=utils.support_file('requirements.txt'), name='env')
self.assertTrue(spec.can_handle())

def test_environment(self):
spec = RequirementsSpec(filename=utils.support_file('requirements.txt'), name='env')
self.assertIsInstance(spec.environment, env.Environment)
self.assertEqual(
spec.environment.dependencies['conda'][0],
'flask ==0.10.1'
)
1 change: 1 addition & 0 deletions tests/support/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flask==0.10.1