diff --git a/pipenv/project.py b/pipenv/project.py index 47b3175a..1f4b6376 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -12,13 +12,18 @@ import delegator import pipfile import toml +from .patched.pip.baseparser import ConfigOptionParser from .utils import ( mkdir_p, convert_deps_from_pip, pep423_name, recase_file, find_requirements, is_file, is_vcs, python_version, cleanup_toml, is_installable_file, is_valid_url, normalize_drive ) -from .environments import PIPENV_MAX_DEPTH, PIPENV_VENV_IN_PROJECT -from .environments import PIPENV_VIRTUALENV, PIPENV_PIPFILE +from .environments import ( + PIPENV_MAX_DEPTH, + PIPENV_PIPFILE, + PIPENV_VENV_IN_PROJECT, + PIPENV_VIRTUALENV, +) if PIPENV_PIPFILE: if not os.path.isfile(PIPENV_PIPFILE): @@ -399,11 +404,22 @@ class Project(object): def create_pipfile(self, python=None): """Creates the Pipfile, filled with juicy defaults.""" + config_parser = ConfigOptionParser(name=self.name) + install = dict(config_parser.get_config_section('install')) + indexes = install.get('extra-index-url', '').lstrip('\n').split('\n') + + # Default source. + pypi_source = {u'url': u'https://pypi.python.org/simple', u'verify_ssl': True, u'name': 'pypi'} + sources = [pypi_source] + + for num, index in enumerate(indexes): + if not index: + continue + source_name = 'pip_index_{}'.format(num+1) + sources.append({u'url': index, u'verify_ssl': True, u'name': source_name}) + data = { - # Default source. - u'source': [ - {u'url': u'https://pypi.python.org/simple', u'verify_ssl': True, 'name': 'pypi'} - ], + u'source': sources, # Default packages. u'packages': {}, diff --git a/tests/test_project.py b/tests/test_project.py index 3375415c..93e5c5b1 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -1,3 +1,5 @@ +import os + import pipenv.project from pipenv.vendor import delegator @@ -14,6 +16,41 @@ class TestProject(): assert proj.virtualenv_location in proj.download_location assert proj.download_location.endswith('downloads') + def test_create_pipfile(self): + proj = pipenv.project.Project() + + # Create test space. + delegator.run('mkdir test_pipfile') + with open('test_pipfile/pip.conf', 'w') as f: + f.write('[install]\nextra-index-url = \n' + ' https://pypi.host.com/simple\n' + ' https://remote.packagehost.net/simple') + os.chdir('test_pipfile') + os.environ['PIP_CONFIG_FILE'] = 'pip.conf' + proj.create_pipfile() + proj._pipfile_location = 'Pipfile' + pfile = proj.parsed_pipfile + os.chdir('..') + + # Cleanup test space. + delegator.run('rm -fr test_pipfile') + + # Confirm source added correctly. + default_source = pfile['source'][0] + assert default_source['url'] == 'https://pypi.python.org/simple' + assert default_source['name'] == 'pypi' + assert default_source['verify_ssl'] is True + + config_source_1 = pfile['source'][1] + assert config_source_1['url'] == 'https://pypi.host.com/simple' + assert config_source_1['name'] == 'pip_index_1' + assert config_source_1['verify_ssl'] is True + + config_source_2 = pfile['source'][2] + assert config_source_2['url'] == 'https://remote.packagehost.net/simple' + assert config_source_2['name'] == 'pip_index_2' + assert config_source_2['verify_ssl'] is True + def test_parsed_pipfile(self): proj = pipenv.project.Project()