diff --git a/pipenv/core.py b/pipenv/core.py index 61787af0..aa367017 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -10,91 +10,13 @@ import pexpect import toml import _pipfile as pipfile +from .project import Project + __version__ = '0.1.4' -class Project(object): - """docstring for Project""" - def __init__(self): - super(Project, self).__init__() - - @property - def name(self): - return self.pipfile_location.split(os.sep)[-2] - - @property - def pipfile_exists(self): - return bool(self.pipfile_location) - - @property - def virtualenv_location(self): - return os.sep.join(self.pipfile_location.split(os.sep)[:-1] + ['.venv']) - - @property - def pipfile_location(self): - try: - return pipfile.Pipfile.find() - except RuntimeError: - return None - - @property - def lockfile_location(self): - return '{}.lock'.format(self.pipfile_location) - - @property - def lockfile_exists(self): - return os.path.isfile(self.lockfile_location) - - def create_pipfile(self): - data = {u'source': [{u'url': u'https://pypi.org/', u'verify_ssl': True}], u'packages': {}, 'dev-packages': {}} - with open('Pipfile', 'w') as f: - f.write(toml.dumps(data)) - - @staticmethod - def remove_package_from_pipfile(package_name, dev=False): - pipfile_path = pipfile.Pipfile.find() - - # Read and append Pipfile. - with open(pipfile_path, 'r') as f: - p = toml.loads(f.read()) - - key = 'dev-packages' if dev else 'packages' - if package_name in p[key]: - del p[key][package_name] - - # Write Pipfile. - data = format_toml(toml.dumps(p)) - with open(pipfile_path, 'w') as f: - f.write(data) - - @staticmethod - def add_package_to_pipfile(package_name, dev=False): - pipfile_path = pipfile.Pipfile.find() - - # Read and append Pipfile. - with open(pipfile_path, 'r') as f: - p = toml.loads(f.read()) - - key = 'dev-packages' if dev else 'packages' - - # Set empty group if it doesn't exist yet. - if key not in p: - p[key] = {} - - package = convert_deps_from_pip(package_name) - package_name = package.keys()[0] - - # Add the package to the group. - p[key][package_name] = package[package_name] - - # Write Pipfile. - data = format_toml(toml.dumps(p)) - with open(pipfile_path, 'w') as f: - f.write(data) - project = Project() - def ensure_latest_pip(): # Ensure that pip is installed. diff --git a/pipenv/project.py b/pipenv/project.py new file mode 100644 index 00000000..8b06c7d9 --- /dev/null +++ b/pipenv/project.py @@ -0,0 +1,82 @@ +import os +import _pipfile as pipfile + + +class Project(object): + """docstring for Project""" + def __init__(self): + super(Project, self).__init__() + + @property + def name(self): + return self.pipfile_location.split(os.sep)[-2] + + @property + def pipfile_exists(self): + return bool(self.pipfile_location) + + @property + def virtualenv_location(self): + return os.sep.join(self.pipfile_location.split(os.sep)[:-1] + ['.venv']) + + @property + def pipfile_location(self): + try: + return pipfile.Pipfile.find() + except RuntimeError: + return None + + @property + def lockfile_location(self): + return '{}.lock'.format(self.pipfile_location) + + @property + def lockfile_exists(self): + return os.path.isfile(self.lockfile_location) + + def create_pipfile(self): + data = {u'source': [{u'url': u'https://pypi.org/', u'verify_ssl': True}], u'packages': {}, 'dev-packages': {}} + with open('Pipfile', 'w') as f: + f.write(toml.dumps(data)) + + @staticmethod + def remove_package_from_pipfile(package_name, dev=False): + pipfile_path = pipfile.Pipfile.find() + + # Read and append Pipfile. + with open(pipfile_path, 'r') as f: + p = toml.loads(f.read()) + + key = 'dev-packages' if dev else 'packages' + if package_name in p[key]: + del p[key][package_name] + + # Write Pipfile. + data = format_toml(toml.dumps(p)) + with open(pipfile_path, 'w') as f: + f.write(data) + + @staticmethod + def add_package_to_pipfile(package_name, dev=False): + pipfile_path = pipfile.Pipfile.find() + + # Read and append Pipfile. + with open(pipfile_path, 'r') as f: + p = toml.loads(f.read()) + + key = 'dev-packages' if dev else 'packages' + + # Set empty group if it doesn't exist yet. + if key not in p: + p[key] = {} + + package = convert_deps_from_pip(package_name) + package_name = package.keys()[0] + + # Add the package to the group. + p[key][package_name] = package[package_name] + + # Write Pipfile. + data = format_toml(toml.dumps(p)) + with open(pipfile_path, 'w') as f: + f.write(data)