[Solved]-Is it possible to trick pip install –find-links into using a downloaded sdist for –editable requirements?

4👍

While it doesn’t appear that this is strictly possible using PIP, there is a workaround that accomplishes the same thing. The workaround is to automatically generate a second requirements file from the original requirements file and sdists directory (to be used only for that directory).

A simple implementation might look something like this (save in a file called “make_reqs.py”):

#!/usr/bin/env python

import re
import sys
import os.path

pat = '.+#egg=(.+)'
allowed_exts = ['.zip', '.tar.gz', 'tar.bz2']

def find_version(sdists_dir, name):
    files = [f for f in os.listdir(sdists_dir) if f.startswith(name)]
    if len(files) != 1:
        return ''
    version = files[0].replace(name+'-', '')
    for ext in allowed_exts:
        version = version.replace(ext, '')
    return version

def get_requirements(file_name, sdists_dir):
    out_reqs = ['--find-links file://%s' % os.path.abspath(sdists_dir)]
    with open(file_name) as req_file:
        reqs = [l.strip() for l in req_file.readlines()]
        for req in reqs:
            match = re.match(pat, req)
            if match and not req.startswith('#'):
                name = match.group(1)
                version = find_version(sdists_dir, name)
                if version:
                    out_reqs.append('%s==%s' % (name, version))
                else:
                    out_reqs.append(req)
            else:
                out_reqs.append(req)
    return out_reqs

if __name__ == '__main__':
    print '\n'.join(get_requirements(*sys.argv[1:]))

To use the script, you would do something like this:

python make_reqs.py requirements.txt /path/to/sdists > sdist_reqs.txt
pip install --no-index -r sdist_reqs.txt

Leave a comment