mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-03-03 01:10:13 +08:00
219 lines
9.0 KiB
Markdown
219 lines
9.0 KiB
Markdown
|
[#]: subject: "How I use Artipie, a PyPI repo"
|
|||
|
[#]: via: "https://opensource.com/article/22/12/python-package-index-repository-artipie"
|
|||
|
[#]: author: "Alena Gerasimova https://opensource.com/users/olena"
|
|||
|
[#]: collector: "lkxed"
|
|||
|
[#]: translator: "geekpi"
|
|||
|
[#]: reviewer: " "
|
|||
|
[#]: publisher: " "
|
|||
|
[#]: url: " "
|
|||
|
|
|||
|
我如何使用 Artipie,一个 PyPI 仓库
|
|||
|
======
|
|||
|
|
|||
|
在学生时代使用 Python 开发时,我发现我需要一些私人的集中存储。这样我就可以存储二进制和文本数据文件,以及 Python 软件包。我在 [Artipie][1] 中找到了答案,这是一个开源的自托管的软件仓库管理器。
|
|||
|
|
|||
|
在大学里,我和我的同事们进行研究,并与来自实验测量的大量数据一起工作。我使用 Python 来处理和可视化它们。当时我的大学同事是数学家,没有软件开发技术的经验。他们通常只是在闪存盘上或通过电子邮件传递数据和代码。我努力向他们介绍像 [Git][2] 这样的版本管理系统,但没有成功。
|
|||
|
|
|||
|
### Python 仓库
|
|||
|
|
|||
|
Artipie 支持 [PyPI][3] 仓库,使其与 [twine][4] 和 [pip][5] 兼容。这意味着你可以完全像在 [PyPI][3] 和 [TestPyPI][6] 仓库上安装或发布软件包那样使用 Artipie Python 仓库
|
|||
|
|
|||
|
要创建你自己的 Python 仓库,你可以使用 Artipie 的托管实例,称为 [Artipie Central][7] 。当你登录后,你会看到一个列出你的仓库的页面 (开始时是空的),以及一个添加新仓库的表格。为你的新仓库选择一个名字(例如,`mypython`),选择 “Python” 作为仓库类型,然后点击 **Add** 按钮。
|
|||
|
|
|||
|
接下来,你会看到一个以 [YAML][8] 格式显示仓库设置的页面:
|
|||
|
|
|||
|
```
|
|||
|
---
|
|||
|
repo:
|
|||
|
type: pypi
|
|||
|
storage: default
|
|||
|
permissions:
|
|||
|
olenagerasimova:
|
|||
|
- upload
|
|||
|
"*":
|
|||
|
- download
|
|||
|
```
|
|||
|
|
|||
|
配置中的 `type` 映射设置了仓库的类型。在这个例子中,Python 仓库被配置为默认的 Artipie Central 存储。
|
|||
|
|
|||
|
`storage` 映射定义了所有仓库包的存储位置。这可以是任何文件系统或 S3 存储兼容的位置。Artipie Central 有一个预先配置的 `default` 存储,任何人都可以使用它进行测试。
|
|||
|
|
|||
|
`permissions` 映射允许为用户 `olenagerasimova` 上传,并允许任何人下载任何软件包。
|
|||
|
|
|||
|
为了确保这个仓库的存在和工作,在你的浏览器中打开[索引页][9]。显示的是软件包列表。如果你刚刚创建了一个新的版本库,但还没有上传软件包,那么仓库的索引页是空白的。
|
|||
|
|
|||
|
### 二进制仓库
|
|||
|
|
|||
|
你可以在 Artipie 中存储任何种类的文件。存储类型称为文件或二进制,我用这个作为实验数据的存储。我把它作为 Python 可视化的输入。在 Artipie Central 可以创建一个文件仓库,与 Python 仓库的方式相同。你给它一个名字,选择**二进制**类型,然后点击**添加**按钮。
|
|||
|
|
|||
|
```
|
|||
|
---
|
|||
|
repo:
|
|||
|
type: file
|
|||
|
storage: default
|
|||
|
permissions:
|
|||
|
olenagerasimova:
|
|||
|
- upload
|
|||
|
- download
|
|||
|
"*":
|
|||
|
- download
|
|||
|
```
|
|||
|
|
|||
|
这些设置基本上与 Python 相同。只有仓库的类型不同。在这个例子中,二进制仓库被称为 `data`。它包含三个带有一些数字的文本文件:
|
|||
|
|
|||
|
```
|
|||
|
6
|
|||
|
3.5
|
|||
|
5
|
|||
|
4
|
|||
|
4.5
|
|||
|
3
|
|||
|
2.7
|
|||
|
5
|
|||
|
6
|
|||
|
3
|
|||
|
1.2
|
|||
|
3.2
|
|||
|
6
|
|||
|
```
|
|||
|
|
|||
|
另外两个文件的形式相同(只是数字不同)。要想自己看这些文件,请在浏览器中打开链接[一][10]、[二][11]和[三][12]并下载文件,或者你可以用 `httpie` 执行 GET 请求:
|
|||
|
|
|||
|
```
|
|||
|
httpie -a https://central.artipie.com/olenagerasimova/data/y1.dat > ./data/y1.da
|
|||
|
```
|
|||
|
|
|||
|
这些文件是用 PUT 请求上传到 Artipie Central 的 `data` 存储库的:
|
|||
|
|
|||
|
```
|
|||
|
httpie -a olenagerasimova:*** PUT
|
|||
|
https://central.artipie.com/olenagerasimova/data/y1.dat @data/y1.dat
|
|||
|
|
|||
|
httpie -a olenagerasimova:*** PUT
|
|||
|
https://central.artipie.com/olenagerasimova/data/y2.dat @data/y2.dat
|
|||
|
|
|||
|
httpie -a olenagerasimova:*** PUT
|
|||
|
https://central.artipie.com/olenagerasimova/data/y3.dat @data/y3.dat
|
|||
|
```
|
|||
|
|
|||
|
由于这个二进制仓库的 API 非常简单(HTTP `PUT` 和 `GET` 请求),用任何语言编写一段代码来上传和下载所需的文件都很容易。
|
|||
|
|
|||
|
### Python 项目
|
|||
|
|
|||
|
一个 Python 项目的例子的源代码可以从我的 [GitHub 仓库][13]中获得。这个例子的主要思想是,从 Artipie Central 下载三个数据文件,将数字读入数组,并使用这些数组来绘制一个图。使用 pip 来安装这个例子包并运行它:
|
|||
|
|
|||
|
```
|
|||
|
$ python3 -m pip install --index-url \
|
|||
|
https://central.artipie.com/olenagerasimova/pypi/ \
|
|||
|
pypiexample
|
|||
|
$ python3 -m pypiexample
|
|||
|
```
|
|||
|
|
|||
|
通过设置 `--index-url` 到 Artipie Central 的 Python 仓库,pip 从它那里下载软件包,而不是通常默认的 PyPi 仓库。运行这些命令后,会显示一个带有三条曲线的极坐标图,这是数据文件的可视化。
|
|||
|
|
|||
|
要将软件包发布到 Artipie Central 仓库,请用 twine 构建并上传:
|
|||
|
|
|||
|
```
|
|||
|
commandline
|
|||
|
$ python setup.py sdist bdist_wheel
|
|||
|
|
|||
|
$ twine upload --repository-url \
|
|||
|
https://central.artipie.com/olenagerasimova/pypi
|
|||
|
-u olenagerasimova -p *** dist/*
|
|||
|
```
|
|||
|
|
|||
|
在 Artipie Central 中设置 `files` 仓库,并创建一个 Python 示例项目是多么容易。不过,你不必使用 Artipie Central。Artipie 可以自托管,所以你可以在你自己的本地网络上运行一个仓库。
|
|||
|
|
|||
|
### 将 Artipie 作为一个容器运行
|
|||
|
|
|||
|
将 Artipie 作为一个容器运行,设置起来就像安装 Podman 或 Docker 一样容易。假设你已经安装了其中之一,打开终端:
|
|||
|
|
|||
|
```
|
|||
|
$ podman run -it -p 8080:8080 -p 8086:8086 artipie/artipie:latest
|
|||
|
|
|||
|
```
|
|||
|
|
|||
|
这将启动一个运行最新 Artipie 版本的新容器。它还映射了两个端口。你的仓库在 8080 端口提供服务。Artipie 的 Rest API 和 Swagger 文档在 8086 端口提供。一个新的镜像会生成一个默认的配置,打印一个正在运行的仓库列表,测试证书,以及一个指向 [Swagger][14] 文档的链接到你的控制台。
|
|||
|
|
|||
|
你也可以使用 Artipie Rest API 来查看现有的仓库:
|
|||
|
|
|||
|
- 进入 Swagger 文档页面 `http://localhost:8086/api/index-org.html`。
|
|||
|
- 在 **Select a definition** 列表中,选择 **Auth token**。
|
|||
|
- 生成并复制用户 artipie 的认证令牌,密码是 artipie。
|
|||
|
- 切换到 **Repositories** 定义,点击 **Authorize** 按钮,然后粘贴令牌。
|
|||
|
|
|||
|
![Image of the Swagger documentation page,][15]
|
|||
|
|
|||
|
对 `/api/v1/repository/list` 执行一个 GET 请求。在响应中,你会收到一个包含三个默认仓库的 JSON 列表:
|
|||
|
|
|||
|
```
|
|||
|
[
|
|||
|
"artipie/my-bin",
|
|||
|
"artipie/my-docker",
|
|||
|
"artipie/my-maven"
|
|||
|
]
|
|||
|
```
|
|||
|
|
|||
|
默认配置中不包括 Python 仓库。你可以通过从 Swagger 接口向 `/api/v1/repository/{user}/{repo}` 执行 PUT 请求来纠正。在这种情况下,`user` 是默认用户的名字(`artipie`),`repo` 是新仓库的名字。你可以把你的新 Python 代码库称为 `my-pypi`。下面是一个请求体的例子,包含带仓库设置的 JSON 对象:
|
|||
|
|
|||
|
```
|
|||
|
{
|
|||
|
"repo": {
|
|||
|
"type": "pypi",
|
|||
|
"storage": "default",
|
|||
|
"permissions": {
|
|||
|
"*": [
|
|||
|
"download"
|
|||
|
],
|
|||
|
"artipie": [
|
|||
|
"upload"
|
|||
|
]
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
所有的 JSON 字段都和你在仪表板上创建 YAML 格式的仓库时一样。我们版本库的类型是 `pypi`,使用默认存储,任何人都可以下载,但只有用户 `artipie` 可以上传。
|
|||
|
|
|||
|
再次向 `/api/v1/repository/list` 发出 GET 请求,确保你的仓库被创建。现在,你有四个仓库:
|
|||
|
|
|||
|
```
|
|||
|
[
|
|||
|
"artipie/my-bin",
|
|||
|
"artipie/my-docker",
|
|||
|
"artipie/my-maven",
|
|||
|
"artipie/my-pypi"
|
|||
|
]
|
|||
|
```
|
|||
|
|
|||
|
你已经创建了你自己的 Artipie 安装,包含了几个仓库! Artipie 镜像既可以在个人电脑上运行,也可以在私人网络内的远程服务器上运行。你可以用它来在一个公司、团体或大学内交换软件包。这是一个建立你自己的软件服务的简单方法,而且它不仅仅适用于 Python。花些时间来探索 Artipie,看看它能为你带来什么。
|
|||
|
|
|||
|
--------------------------------------------------------------------------------
|
|||
|
|
|||
|
via: https://opensource.com/article/22/12/python-package-index-repository-artipie
|
|||
|
|
|||
|
作者:[Alena Gerasimova][a]
|
|||
|
选题:[lkxed][b]
|
|||
|
译者:[geekpi](https://github.com/geekpi)
|
|||
|
校对:[校对者ID](https://github.com/校对者ID)
|
|||
|
|
|||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|||
|
|
|||
|
[a]: https://opensource.com/users/olena
|
|||
|
[b]: https://github.com/lkxed
|
|||
|
[1]: https://github.com/artipie
|
|||
|
[2]: https://opensource.com/tags/git
|
|||
|
[3]: https://pypi.org/
|
|||
|
[4]: https://github.com/pypa/twine
|
|||
|
[5]: https://pip.pypa.io/en/stable/
|
|||
|
[6]: https://test.pypi.org/
|
|||
|
[7]: https://central.artipie.com/signin
|
|||
|
[8]: https://www.redhat.com/sysadmin/yaml-beginners
|
|||
|
[9]: https://central.artipie.com/olenagerasimova/pypi
|
|||
|
[10]: https://central.artipie.com/olenagerasimova/data/y1.dat
|
|||
|
[11]: https://central.artipie.com/olenagerasimova/data/y2.dat
|
|||
|
[12]: https://central.artipie.com/olenagerasimova/data/y3.dat
|
|||
|
[13]: https://github.com/artipie/pypi-example
|
|||
|
[14]: https://swagger.io/
|
|||
|
[15]: https://opensource.com/sites/default/files/2022-11/artipie-swagger.png
|