是否有可能欺骗安装 - 找到链接使用下载的sdist可用的要求? [英] Is it possible to trick pip install --find-links into using a downloaded sdist for --editable requirements?

查看:173
本文介绍了是否有可能欺骗安装 - 找到链接使用下载的sdist可用的要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下命令:

pip install -r requirements.txt -d sdists/

您可以轻松创建与项目分发的要求归档。如果您的要求如下所示,这很有用:

You can easily create an archive of requirements for distributing with your project. This works great if your requirements look like this:

Django==1.3.1
django-tagging==0.3.1
django-robots==0.6.1

然后,您可以安装这些要求,而不需要触摸PyPI,如下所示:

You can then install those requirements without touching PyPI at all, like so:

pip install -r requirements.txt --find-links sdists/ --no-index

可以使用相同的方法 - 可编辑要求?例如:

Is it possible to use the same method for --editable requirements? E.g.:

-e hg+https://bitbucket.org/ubernostrum/django-contact-form/@1d3791fa4dfb#egg=django-contact-form

据我所知, pip install -d 愉快地下载可编辑的要求并为您创建一个sdist,但 pip install --find-links 没有任何方法来匹配下载sdist与您的需求文件中的相关行,因此它会忽略下载的sdist,并像往常一样从VCS继续检查代码。

As far as I can tell, pip install -d happily downloads editable requirements and creates an sdist for you, but pip install --find-links does not have any way to match up the downloaded sdist with the associated line in your requirements file, so it ignores the downloaded sdist and continues checking out the code from VCS as usual.

推荐答案

虽然使用PIP并不是完全可以实现的,但是有一个解决方法可以实现同样的事情。解决方法是从原始需求文件和sdists目录自动生成第二个需求文件(仅用于该目录)。

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).

一个简单的实现可能看起来像这个(保存在一个名为make_reqs.py的文件):

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

这篇关于是否有可能欺骗安装 - 找到链接使用下载的sdist可用的要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆