Python 如何在没有镜像的情况下创建本地自己的pypi存储库索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18052217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:48:53  来源:igfitidea点击:

how to create local own pypi repository index without mirror?

pythonpippypi

提问by Larry Cai

We have several own python packages and want to create local pypi repository for them using simple interface like https://pypi.python.org/simple/

我们有几个自己的 python 包,并希望使用像https://pypi.python.org/simple/这样的简单界面为它们创建本地 pypi 存储库

This repository I want to create for local only without any mirrors due to security reason, and it will be put under apache's control

由于安全原因,我只想为本地创建没有任何镜像的存储库,它将置于 apache 的控制之下

The command pypimirrorlooks has to be initialized once, which needs to mirror.

命令pypimirrorlook必须初始化一次,需要镜像。

How can I generate PyPi Simple Index based on local python packages.

如何基于本地 python 包生成 PyPi 简单索引。

Any other simple scripts for this ?

任何其他简单的脚本?

采纳答案by K Z

Since you asked to answer here:

既然你要求在这里回答:

Take a look at pip2pi, it seems to be exactly what you are looking for.

看一看pip2pi,这似乎正是你要找的。

回答by Steve Barnes

If you are talking about running simplepypi then you will have your server for adding packages and serve them out. To quote the documentation:

如果您正在谈论运行 simplepypi,那么您将拥有用于添加软件包并提供它们的服务器。引用文档:

- Running this on the setup.py of your favorite package:

    python setup.py sdist upload -r local
- Running this on the setup.py of your favorite package:

    python setup.py sdist upload -r local

If you were to use either os.walkor glob.globon your local site-packages directory you could quickly filter for setup.pyin each of the packages/directories and invoke the above on them.

如果您要在本地站点包目录中使用os.walkglob.glob,您可以setup.py在每个包/目录中快速过滤并在它们上调用上述内容。

If you just need to create a directory of tar.gz files complete with a .html list of them then you can use glob.glob on the top level of your site-packages directory - tar.gz each directory in turn and add the resulting filename to a list - you can then generate your index.html from that list.

如果您只需要创建一个包含 tar.gz 文件的目录并带有 .html 列表,那么您可以在站点包目录的顶层使用 glob.glob - tar.gz 依次添加每个目录并添加结果文件名到列表 - 然后您可以从该列表生成 index.html。

You can use any of a large number of templateengines for this or generate it yourself:

您可以为此使用大量模板引擎中的任何一个,也可以自己生成:

import glob
filelist = glob.glob("*.tar.gz")
tags = ['<A href="file:Where/%s">%s</A>' % (s,s) for s in tags]
head = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="Generator" CONTENT="Python Script">
<META NAME="Keywords" CONTENT="Cheeseshop">
<META NAME="Description" CONTENT="List of local python packages">
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF" BACKGROUND="?">
"""
tail = """</BODY></HTML>"""
tags.insert(0,head)
tags.append(tail)
page = "\n".join(tags)

Then save or serve you page.

然后保存或为您提供页面。

回答by Burhan Khalid

There is nothing special about the mirror, and you can use mod_rewriteto set it up yourself.

镜子没有什么特别的,你可以用mod_rewrite它自己设置。

  1. Dump your packages in a directory that is mapped to a URL. Here I am using /url/to/my/pypi/an an example. The folder hierarchy should be /foo/bar/simple/[name of package]/[name of tarball]

  2. Add the following to .htaccessor the global configuration for that directory where you packages are. The last block of lines is a fall back to the global pypi index:

    Options +Indexes
    
    RewriteEngine On
    
    RewriteRule ^/robots.txt - [L]
    RewriteRule ^/icons/.* - [L]
    RewriteRule ^/index\..* - [L]
    
    RewriteCond /foo/bar/simple/ !-f
    RewriteCond /foo/bar/simple/ !-d
    RewriteRule ^/(.*)/?$ http://pypi.python.org/ [R,L]
    
  3. Update your ~/.pip/pip.confto point to the new repository:

    [global]
    index-url = http://localhost/url/to/my/pypi/
    

    Or use the -i http://localhost/url/to/my/pypi/option at the command line.

  1. 将您的包转储到映射到 URL 的目录中。这里我用/url/to/my/pypi/了一个例子。文件夹层次结构应该是/foo/bar/simple/[name of package]/[name of tarball]

  2. 将以下内容添加到.htaccess包所在的目录或全局配置中。最后一行是回退到全局 pypi 索引:

    Options +Indexes
    
    RewriteEngine On
    
    RewriteRule ^/robots.txt - [L]
    RewriteRule ^/icons/.* - [L]
    RewriteRule ^/index\..* - [L]
    
    RewriteCond /foo/bar/simple/ !-f
    RewriteCond /foo/bar/simple/ !-d
    RewriteRule ^/(.*)/?$ http://pypi.python.org/ [R,L]
    
  3. 更新您~/.pip/pip.conf以指向新的存储库:

    [global]
    index-url = http://localhost/url/to/my/pypi/
    

    或者使用-i http://localhost/url/to/my/pypi/命令行中的选项。

回答by Laurent Brack

We had a similar need at my company. Basically how can we upload "closed source" packages to an index while being able to install them as if they were on PyPI?

我们公司也有类似的需求。基本上我们如何将“闭源”包上传到索引,同时能够像在 PyPI 上一样安装它们?

We have sponsored a project called devpiwhich acts as a PyPI cache (packages you access from PyPI will be cached on your server) as well as a powerful and fast index server. The documentation is available at http://doc.devpi.net/latest/.

我们赞助了一个名为devpi的项目,它充当 PyPI 缓存(您从 PyPI 访问的包将缓存在您的服务器上)以及一个强大而快速的索引服务器。该文档可从http://doc.devpi.net/latest/ 获得

Next on the roadmap is mirroring for multi geos deployment. To kick the tires on your machine takes about 5 minutes (look at the quick start guides). Finally devpi is compatible with both pip and easy_install (i.e. you do not need the devpi client installed on your machine).

路线图的下一个是多地理部署的镜像。在您的机器上踢轮胎大约需要 5 分钟(查看快速入门指南)。最后,devpi 兼容 pip 和 easy_install(即你不需要在你的机器上安装 devpi 客户端)。

Hope this help.

希望这有帮助。

回答by hoefling

The simplest way is to organize the package distfiles into package-named dirs and run a simple HTTP server. No extra packages needed, Python's stdlib is enough. Directory structure example:

最简单的方法是将包 distfiles 组织成包命名的目录并运行一个简单的 HTTP 服务器。不需要额外的包,Python 的 stdlib 就足够了。目录结构示例:

└── repodir
    ├── setuptools
    │   └── setuptools-38.1.0-py2.py3-none-any.whl 
    │   └── setuptools-38.1.0.zip
    │   └── setuptools-39.2.0-py2.py3-none-any.whl 
    │   └── setuptools-39.2.0.zip
    ├── wheel
    │   └── wheel-0.31.1-py2.py3-none-any.whl 
    ...

Start the server:

启动服务器:

$ cd repodir/
$ python3 -m http.server 9000
$ # or for Python 2:
$ python2 -m SimpleHTTPServer 9000

The local repo is up and running. Now you can pass the repo to pip:

本地存储库已启动并正在运行。现在您可以将 repo 传递给pip

$ pip install wheel --extra-index-url=http://127.0.0.1:9000

or even persist the repo URL in the pip.confto not to enter it each time:

或者甚至将 repo URL 保存在 中pip.conf,不要每次都输入它:

# pip.conf
[global]
extra-index-url=http://127.0.0.1:9000

Reference: Python Packaging user Guide, Hosting your own simple repository

参考:Python Packaging 用户指南,托管您自己的简单存储库