From 7318cf6815ac17cb362223214d8934422d059fe8 Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Sun, 7 May 2017 09:42:04 -0600 Subject: [PATCH] create Pipfile from requirements.txt without installing The install process is dependent upon the virtualenv existing which isn't always the case for fresh projects. This is the most common case this code will execute in, so we need to be able to create the Pipfile without installing. --- pipenv/cli.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/pipenv/cli.py b/pipenv/cli.py index da7dd588..e27b2d09 100644 --- a/pipenv/cli.py +++ b/pipenv/cli.py @@ -17,6 +17,7 @@ import pexpect import requests import pipfile from blindspin import spinner +from pip.req.req_file import parse_requirements from requests.packages.urllib3.exceptions import InsecureRequestWarning from .project import Project @@ -39,7 +40,7 @@ else: # |_| # Packages that should be ignored later. -BAD_PACKAGES = ['setuptools', 'pip', 'wheel', 'six', 'packaging', 'pyparsing', 'appdirs'] +BAD_PACKAGES = ('setuptools', 'pip', 'wheel', 'six', 'packaging', 'pyparsing', 'appdirs') # Enable shell completion. click_completion.init() @@ -100,21 +101,14 @@ def ensure_pipfile(validate=True): # Create a Pipfile... project.create_pipfile() - click.echo(crayons.yellow('Installing dependencies from \'requirements.txt\'...')) - - # Install everything from the requirements.txt. - delegator.run('{0} install -r {1}'.format(which('pip'), project.requirements_location)) - installed = delegator.run('{0} freeze'.format(which('pip'))).out.split('\n') + # Parse requirements.txt file with Pip's parser. + # Pip requires a `PipSession` which is a subclass of requests.Session. + # Since we're not making any network calls, it's initialized to nothing. + reqs = [r.req for r in parse_requirements(project.requirements_location, session='')] - # Remove setuptools and friends from installed, if present. - for package_name in BAD_PACKAGES: - for i, package in enumerate(installed): - if package.startswith(package_name): - del installed[i] - - for package in installed: - if package: - project.add_package_to_pipfile(package) + for package in reqs: + if package.name not in BAD_PACKAGES: + project.add_package_to_pipfile(str(package)) else: click.echo(crayons.yellow('Creating a Pipfile for this project...'), err=True)