Artipie：可用于 Python 的开源仓库管理器
仓库 Artipie
![](/data/attachment/album/202303/02/232208fgy56v5egv7ipgg2.jpg)



> 
> Artipie 是一个开源的自托管的仓库管理器，它不仅可以用于 Python。
> 
> 
> 


在学生时代使用 Python 开发时，我发现我需要一些私人的集中存储。这样我就可以存储二进制和文本数据文件，以及 Python 软件包。我在 [Artipie](https://github.com/artipie) 中找到了答案，这是一个开源的自托管的软件仓库管理器。


在大学里，我和我的同事们对来自实验测量的大量数据进行研究。我使用 Python 来处理和可视化它们。当时我的大学同事是数学家，没有软件开发技术的经验。他们通常只是在闪存盘上或通过电子邮件传递数据和代码。我努力向他们介绍像 [Git](https://opensource.com/tags/git) 这样的版本管理系统，但没有成功。


### Python 仓库


Artipie 支持 [PyPI](https://pypi.org/) 仓库，与 [twine](https://github.com/pypa/twine) 和 [pip](https://pip.pypa.io/en/stable/) 兼容。这意味着你可以完全像在 [PyPI](https://pypi.org/) 和 [TestPyPI](https://test.pypi.org/) 仓库上安装或发布软件包那样使用 Artipie Python 仓库。


要创建你自己的 Python 仓库，你可以使用名为 [Artipie Central](https://central.artipie.com/signin) 的 Artipie 托管实例。当你登录后，你会看到一个列出你的仓库的页面（开始时是空的），以及一个添加新仓库的表单。为你的新仓库选择一个名字（例如，`mypython`），选择 `Python` 作为仓库类型，然后点击 “<ruby> 添加 <rt>  Add </rt></ruby>” 按钮。


接下来，你会看到一个以 [YAML](https://www.redhat.com/sysadmin/yaml-beginners) 格式显示仓库设置的页面：



```
---
repo:
  type: pypi
  storage: default
  permissions:
    olenagerasimova:
      - upload
    "*":
      - download

```

配置中的 `type` 映射设置了仓库的类型。在这个例子中，Python 仓库被配置为默认的 Artipie Central 存储。


`storage` 映射定义了所有仓库包的存储位置。这可以是任何文件系统或 S3 存储兼容的位置。Artipie Central 有一个预先配置的 `default` 存储，可以使用它进行测试。


`permissions` 映射允许为用户 `olenagerasimova` 上传，并允许任何人下载任何软件包。


为了确保这个仓库的存在和工作，在你的浏览器中打开 [索引页](https://central.artipie.com/olenagerasimova/pypi)。显示的是软件包列表。如果你刚刚创建了一个新的版本库，但还没有上传软件包，那么仓库的索引页是空白的。


### 二进制仓库


你可以在 Artipie 中存储任何种类的文件。存储类型是 `file` 或 `binary`，我用这个作为实验数据的存储。我把它作为 Python 可视化的输入。在 Artipie Central 可以创建一个文件仓库，与 Python 仓库的方式相同。你给它一个名字，选择 `binary` 类型，然后点击 “<ruby> 添加 <rt>  Add </rt></ruby>” 按钮。



```
---
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

```

另外两个文件的形式相同（只是数字不同）。要想自己看这些文件，请在浏览器中打开链接 [一](https://central.artipie.com/olenagerasimova/data/y1.dat)、[二](https://central.artipie.com/olenagerasimova/data/y2.dat) 和 [三](https://central.artipie.com/olenagerasimova/data/y3.dat) 并下载文件，或者你可以用 `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 项目


可以从我的 [GitHub 仓库](https://github.com/artipie/pypi-example)中获得一个 Python 项目的示例源代码。这个示例的主要想法是，从 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](https://swagger.io/) 文档的链接到你的控制台。


你也可以使用 Artipie Rest API 来查看现有的仓库：


* 进入 Swagger 文档页面 `http://localhost:8086/api/index-org.html`。
* 在 “<ruby> 选择一个定义 <rt>  Select a definition </rt></ruby>” 列表中，选择 “<ruby> 认证令牌 <rt>  Auth token </rt></ruby>”。
* 生成并复制用户 `artipie` 的认证令牌，密码是 `artipie`。
* 切换到 “<ruby> 仓库 <rt>  Repositories </rt></ruby>” 定义，点击 “<ruby> 认证 <rt>  Authorize </rt></ruby>” 按钮，然后粘贴令牌。


![Image of the Swagger documentation page,](/data/attachment/album/202303/02/232239crq8q68rim18699r.jpg)


对 `/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](https://opensource.com/users/olena) 选题：[lkxed](https://github.com/lkxed) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
While developing with Python as a student, I found that I needed some private centralized storage. This was so I could store binary and text data files, as well as Python packages. I found the answer in [Artipie](https://github.com/artipie), an open source self-hosted software repository manager.

At university, my colleagues and I conducted research and worked with a lot of data from experimental measurements. I used Python to process and visualize them. My university colleagues at the time were mathematicians and didn't have experience with software development techniques. They usually just passed data and code on a flash drive or over email. My efforts to introduce them to a versioning system like [Git](https://opensource.com/tags/git) were unsuccessful.

## Python repository

Artipie supports the [PyPI](https://pypi.org/) repository, making it compatible with both [twine](https://github.com/pypa/twine) and [pip](https://pip.pypa.io/en/stable/). This means you can work with the Artipie Python repository exactly as you would when installing or publishing packages on the [PyPI](https://pypi.org/) and [TestPyPI](https://test.pypi.org/) repositories.

To create your own Python repository, you can use the hosted instance of Artipie called [Artipie Central](https://central.artipie.com/signin). Once you sign in, you see a page with your repositories listed (which is empty to begin with) and a form to add a new repository. Choose a name for your new repository (for example, `mypython`

), select "Python" as the repository type, and then click the **Add** button.

Next, you see a page with repository settings in the [YAML](https://www.redhat.com/sysadmin/yaml-beginners) format:

```
``````
---
repo:
type: pypi
storage: default
permissions:
olenagerasimova:
- upload
"*":
- download
```


The `type`

mapping in the configuration sets the repository type. In this example, the Python repository is configured with the default Artipie Central storage.

The `storage`

mapping defines where all of the repository packages are stored. This can be any file system or S3 storage compatible location. Artipie Central has a preconfigured `default`

storage that can be used for tests by anyone.

The `permissions`

mapping allows uploads for the user `olenagerasimova`

, and allows anyone to download any package.

To make sure this repository exists and works, open the [index page](https://central.artipie.com/olenagerasimova/pypi) in your browser. The packages list is displayed. If you've just created a new repository but have yet to upload a package, then the repository index page is blank.

## Binary repository

You can store any kind of file in Artipie. The storage type is called file or binary, and I use this as storage for experimental data. I use this as input for Python visualizations. A file repository can be created in Artipie Central the same way as a Python repository. You give it a name, choose the type **binary**, and then click the **Add** button.

```
``````
---
repo:
type: file
storage: default
permissions:
olenagerasimova:
- upload
- download
"*":
- download
```


The settings are basically the same as for Python. Only the repository type differs. The binary repository, in this example, is called `data`

. It contains three text files with some numbers:

```
``````
6
3.5
5
4
4.5
3
2.7
5
6
3
1.2
3.2
6
```


The other two files take the same form (only the numbers are different.) To see the files yourself, open the links [one](https://central.artipie.com/olenagerasimova/data/y1.dat), [two](https://central.artipie.com/olenagerasimova/data/y2.dat), and [three](https://central.artipie.com/olenagerasimova/data/y3.dat) in your browser and download the files, or you can perform a GET request using `httpie`

:

```
````httpie -a https://central.artipie.com/olenagerasimova/data/y1.dat > ./data/y1.da`


These files were uploaded to the Artipie Central `data`

repository with PUT requests:

```
``````
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
```


As this binary repository API is very simple (HTTP `PUT`

and `GET `

requests), it's easy to write a piece of code in any language to upload and download the required files.


## Python project

The source code of an example Python project is available from my [GitHub repository](https://github.com/artipie/pypi-example). The main idea of the example is to download three data files from Artipie Central, read the numbers into arrays, and use these arrays to draw a plot. Use pip to install the example package and run it:

```
``````
$ python3 -m pip install --index-url \
https://central.artipie.com/olenagerasimova/pypi/ \
pypiexample
$ python3 -m pypiexample
```


By setting the `--index-url`

to the Artipie Central Python repository, pip downloads the packages from it rather than the PyPi repository that serves as the usual default. After running the commands, a polar plot with three curves, a visualization of the data files is displayed.

To publish the package to the Artipie Central repository, build it with and use twine to upload it:

```
``````
commandline
$ python setup.py sdist bdist_wheel
$ twine upload --repository-url \
https://central.artipie.com/olenagerasimova/pypi
-u olenagerasimova -p *** dist/*
```


That's how easy it is to set up a `files`

repositories in Artipie Central, create a sample Python project, publish, and install it. You don't have to use Artipie Central, though. Artipie can be self-hosted, so you can run a repository on your own local network.

## Run Artipie as a container

Running Artipie as a container makes setup as easy as installing either Podman or Docker. Assuming you have one of these installed, open a terminal:

```
``````
$ podman run -it -p 8080:8080 -p 8086:8086 artipie/artipie:latest
```


This starts a new container running the latest Artipie version. It also maps two ports. Your repositories are served on port 8080. The Artipie Rest API and Swagger documentation are provided on port 8086. A new image generates a default configuration, printing a list of running repositories, test credentials, and a link to the [Swagger](https://swagger.io/) documentation to your console.

You can also use the Artipie Rest API to see existing repositories:

-
Go to the Swagger documentation page at

`http://localhost:8086/api/index-org.html`

**.** -
In the

**Select a definition**list, choose**Auth token** -
Generate and copy the authentication token for the user artipie with the password artipie

-
Switch to the

**Repositories**definition and click the**Authorize**button, and then paste in the token

![Image of the Swagger documentation page,](https://opensource.com/sites/default/files/2022-11/artipie-swagger.png)

(Seth Kenlon, CC BY-SA 4.0)


Perform a GET request for `/api/v1/repository/list`

. In response, you receive a JSON list with three default repositories:


```
``````
[ "artipie/my-bin",
"artipie/my-docker",
"artipie/my-maven" ]
```


The Python repository isn't included in the default configuration. You can correct that by performing a PUT request to `/api/v1/repository/{user}/{repo}`

from the Swagger interface. In this case, `user`

is the name of the default user (`artipie`

) and `repo`

is the name of the new repository. You can call your new Python repository `my-pypi`

. Here's an example request body, containing a JSON object with the repository settings:

```
``````
{ "repo": {
"type": "pypi",
"storage": "default",
"permissions": {
"*": [
"download"
],
"artipie": [
"upload"
] }
} }
```


All the JSON fields are the same as when you create a repository in the dashboard in YAML format. The type of our repository is `pypi`

, the default storage is used, and anyone can download but only the user `artipie`

can upload.

Make a GET request to `/api/v1/repository/list`

again to make sure your repository was created. Now, you have four repositories:

```
``````
[ "artipie/my-bin",
"artipie/my-docker",
"artipie/my-maven",
"artipie/my-pypi" ]
```


You've created your own Artipie installation, containing several repositories! The Artipie image can run both on a personal computer or on a remote server inside a private network. You can use it to exchange packages within a company, group, or university. It's an easy way to set up your own software services, and it's not just for Python. Take some time to explore Artipie and see what it can make possible for you.

## Comments are closed.