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

Commit

Permalink
Merge pull request #203 from conda/requirements-spec
Browse files Browse the repository at this point in the history
Requirements spec
  • Loading branch information
malev committed Nov 19, 2015
2 parents 5b359c4 + d10bd20 commit 6966196
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
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
44 changes: 44 additions & 0 deletions conda_env/specs/requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os

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 _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"
return False
else:
return True

def can_handle(self):
return self._valid_file() and self._valid_name()

@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
28 changes: 28 additions & 0 deletions tests/specs/test_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import unittest

from .. import utils

from conda_env import env
from conda_env.specs.requirements import RequirementsSpec


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

0 comments on commit 6966196

Please sign in to comment.