2020-07-20 06:14:47 +00:00
|
|
|
#!/usr/bin/env python3
|
2014-11-17 15:47:41 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
2013-08-09 13:13:15 +00:00
|
|
|
|
2014-11-18 14:50:39 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2014-03-25 03:44:46 +00:00
|
|
|
import os.path
|
2013-08-09 13:13:15 +00:00
|
|
|
|
2019-04-05 06:47:40 +00:00
|
|
|
try:
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
except ImportError:
|
|
|
|
from urlparse import urlparse
|
|
|
|
|
2014-11-17 15:47:41 +00:00
|
|
|
|
2014-03-25 03:44:46 +00:00
|
|
|
def main():
|
|
|
|
home = os.path.expanduser("~")
|
|
|
|
backup_configs(home)
|
|
|
|
indices = []
|
2015-02-06 00:30:25 +00:00
|
|
|
if (os.environ.get('DIB_PYPI_MIRROR_URL') or
|
|
|
|
os.environ.get('PYPI_MIRROR_URL')):
|
|
|
|
candidates = [k for k in os.environ if
|
|
|
|
k.startswith('DIB_PYPI_MIRROR_URL')]
|
|
|
|
back_compat = [k for k in os.environ if
|
|
|
|
k.startswith('PYPI_MIRROR_URL')]
|
|
|
|
if len(back_compat) > 0:
|
|
|
|
print('DEPRECATION WARNING: Please use DIB_PYPI_MIRROR_URL* rather'
|
|
|
|
' than PYPI_MIRROR_URL*')
|
|
|
|
candidates += back_compat
|
2014-03-25 03:44:46 +00:00
|
|
|
indices = list(map(os.environ.get, sorted(candidates)))
|
|
|
|
else:
|
|
|
|
indices = ['file:///tmp/pypi']
|
2014-11-18 14:50:39 +00:00
|
|
|
print('WARNING: You are using the pypi element but no pypi mirror is '
|
|
|
|
'defined via the PYPI_MIRROR_URL env variable')
|
2014-03-25 03:44:46 +00:00
|
|
|
use_pypi_python_org = True
|
|
|
|
if os.environ.get('DIB_OFFLINE'):
|
|
|
|
use_pypi_python_org = False
|
|
|
|
if os.environ.get('DIB_NO_PYPI_PIP'):
|
|
|
|
use_pypi_python_org = False
|
|
|
|
if use_pypi_python_org:
|
|
|
|
indices.append('https://pypi.python.org/simple')
|
2015-02-19 23:11:52 +00:00
|
|
|
retries = os.environ.get('DIB_PIP_RETRIES')
|
2016-12-21 20:18:12 +00:00
|
|
|
with open(home + '/.pip/pip.conf', 'wt') as output:
|
2014-03-25 03:44:46 +00:00
|
|
|
output.write('[global]\n')
|
|
|
|
output.write('log = %s/pip.log\n' % (home,))
|
|
|
|
output.write('index-url = %s\n' % (indices[0],))
|
2019-04-05 06:47:40 +00:00
|
|
|
output.write('trusted-host = %s\n' % (urlparse(indices[0]).hostname,))
|
2015-02-19 23:11:52 +00:00
|
|
|
if retries is not None:
|
|
|
|
output.write('retries = %s\n' % retries)
|
2019-08-08 21:14:11 +00:00
|
|
|
if len(indices) > 1:
|
|
|
|
output.write('extra-index-url =\n')
|
|
|
|
for index in indices[1:]:
|
|
|
|
output.write(' %s\n' % (index,))
|
2013-08-09 13:13:15 +00:00
|
|
|
|
|
|
|
|
2014-03-25 03:44:46 +00:00
|
|
|
def backup_configs(home):
|
|
|
|
pip_dir = home + '/.pip'
|
|
|
|
if not os.path.exists(pip_dir):
|
|
|
|
os.mkdir(pip_dir)
|
|
|
|
if os.path.exists(pip_dir + '/pip.conf'):
|
|
|
|
os.rename(pip_dir + '/pip.conf', pip_dir + '/pip.conf.orig')
|
|
|
|
if os.path.exists(home + '/.pydistutils.cfg'):
|
|
|
|
os.rename(home + '/.pydistutils.cfg', home + '/.pydistutils.cfg.orig')
|
2013-08-09 13:13:15 +00:00
|
|
|
|
|
|
|
|
2014-03-25 03:44:46 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|