• 对于注定会优秀的人来说,他所需要的,只是时间!
  • 手懒得,必受贫穷,手勤的,必得富足----《圣经》
  • 帮助别人,成就自己。愿君在本站能真正有所收获!
  • 如果你在本站中发现任何问题,欢迎留言指正!
  • 宝剑锋从磨砺出,梅花香自苦寒来!

<十二>企业级开源仓库nexus3实战应用–通过命令行往Python私仓传包

nexus eryajf 4个月前 (12-24) 307°C 已收录 0个评论
本文预计阅读时间 14 分钟

1,前言

提供了Python私仓之后,除了通过私仓拉包方便之外,小伙伴们难免会有往私仓传包的需求,因为稍微有点绕弯,因此这里记录一下。

其实用一句话来表述,就是通过私服拉包的时候,走group的代理,从本地上传包的时候,要走local的仓库。

接下来就记录一下配置方法并用测试包来验证下。

创建私服的过程这里不详述了,已知现有Python私仓,物料信息如下:

  • group:http://nexus.test.com/repository/pyg/
  • local:http://nexus.test.com/repository/pyg/

2,上传

1,先安装依赖包

$ pip3 install wheel --user
$ pip3 install twine --user

2,添加配置

有了如上物料之后,我们首先来验证下上传功能,在本地用户家目录添加如下配置:

$ cat ~/.pypirc
[distutils]
index-servers =
    pypi
    nexus

[pypi]
repository:https://pypi.python.org/pypi
username:eryajf
password:test

[nexus]
repository=http://nexus.test.com/repository/pyg/
username=dev
password=GVLaX-E|yq$vE[/k

3,编写代码

然后在本地打一个测试包

$ mkdir helloworld

$ touch helloworld/__init__.py

$ cat helloworld/demo.py
class Demo:
    @staticmethod
    def hello():
        print('helloworld')

编写setup.py:

$ cat setup.py
from setuptools import setup

setup(
    name='helloworld',
    version='1.0',
    author="techlog",
    license="MIT",
    packages=[
        'helloworld'
    ],
    install_requires=[
    ],
    classifiers=[
        "Topic :: Utilities",
        "Topic :: Internet",
        "Topic :: Software Development :: Libraries :: Python Modules"
    ],
)

具体的 setup.py 的编写规范可以参考:https://packaging.python.org/tutorials/distributing-packages/#setup-py

目录结构:

$ tree
.
├── helloworld
│   ├── __init__.py
│   └── demo.py
└── setup.py

1 directory, 3 files

4,打包

$ python3 setup.py sdist bdist_wheel
running sdist
running egg_info
creating helloworld.egg-info
writing helloworld.egg-info/PKG-INFO
writing dependency_links to helloworld.egg-info/dependency_links.txt
writing top-level names to helloworld.egg-info/top_level.txt
writing manifest file 'helloworld.egg-info/SOURCES.txt'
reading manifest file 'helloworld.egg-info/SOURCES.txt'
writing manifest file 'helloworld.egg-info/SOURCES.txt'
warning: sdist: standard file not found: should have one of README, README.rst, README.txt, README.md

running check
warning: check: missing required meta-data: url

warning: check: missing meta-data: if 'author' supplied, 'author_email' must be supplied too

creating helloworld-1.0
creating helloworld-1.0/helloworld
creating helloworld-1.0/helloworld.egg-info
copying files to helloworld-1.0...
copying setup.py -> helloworld-1.0
copying helloworld/__init__.py -> helloworld-1.0/helloworld
copying helloworld/demo.py -> helloworld-1.0/helloworld
copying helloworld.egg-info/PKG-INFO -> helloworld-1.0/helloworld.egg-info
copying helloworld.egg-info/SOURCES.txt -> helloworld-1.0/helloworld.egg-info
copying helloworld.egg-info/dependency_links.txt -> helloworld-1.0/helloworld.egg-info
copying helloworld.egg-info/top_level.txt -> helloworld-1.0/helloworld.egg-info
Writing helloworld-1.0/setup.cfg
creating dist
Creating tar archive
removing 'helloworld-1.0' (and everything under it)
running bdist_wheel
running build
running build_py
creating build
creating build/lib
creating build/lib/helloworld
copying helloworld/__init__.py -> build/lib/helloworld
copying helloworld/demo.py -> build/lib/helloworld
installing to build/bdist.macosx-10.14-x86_64/wheel
running install
running install_lib
creating build/bdist.macosx-10.14-x86_64
creating build/bdist.macosx-10.14-x86_64/wheel
creating build/bdist.macosx-10.14-x86_64/wheel/helloworld
copying build/lib/helloworld/__init__.py -> build/bdist.macosx-10.14-x86_64/wheel/helloworld
copying build/lib/helloworld/demo.py -> build/bdist.macosx-10.14-x86_64/wheel/helloworld
running install_egg_info
Copying helloworld.egg-info to build/bdist.macosx-10.14-x86_64/wheel/helloworld-1.0-py3.7.egg-info
running install_scripts
creating build/bdist.macosx-10.14-x86_64/wheel/helloworld-1.0.dist-info/WHEEL
creating 'dist/helloworld-1.0-py3-none-any.whl' and adding 'build/bdist.macosx-10.14-x86_64/wheel' to it
adding 'helloworld/__init__.py'
adding 'helloworld/demo.py'
adding 'helloworld-1.0.dist-info/METADATA'
adding 'helloworld-1.0.dist-info/WHEEL'
adding 'helloworld-1.0.dist-info/top_level.txt'
adding 'helloworld-1.0.dist-info/RECORD'
removing build/bdist.macosx-10.14-x86_64/wheel

打包之后,会生成一个build目录和dist目录:

$ tree
.
├── build
│   ├── bdist.macosx-10.14-x86_64
│   └── lib
│       └── helloworld
│           ├── __init__.py
│           └── demo.py
├── dist
│   ├── helloworld-1.0-py3-none-any.whl
│   └── helloworld-1.0.tar.gz
├── helloworld
│   ├── __init__.py
│   └── demo.py
├── helloworld.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   └── top_level.txt
└── setup.py

7 directories, 11 files

5,上传

$ python3 -m twine upload --repository nexus dist/*
Uploading distributions to http://nexus.test.com/repository/pygl/
Uploading helloworld-1.0-py3-none-any.whl
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.38k/4.38k [00:00<00:00, 46.9kB/s]
Uploading helloworld-1.0.tar.gz
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3.77k/3.77k [00:00<00:00, 112kB/s]

看到如上内容,就说明上传成功了。

可以浏览器上看下:

image-20201120164214736

3,下载

拉包就比较简单了,首先配置如下信息:

mkdir ~/.pip

cat > ~/.pip/pip.conf << EOF
[global]
timeout = 60
trusted-host =  nexus.test.com
index-url = http://nexus.test.com/repository/pypi/simple
EOF

然后直接安装对应包即可。


weinxin
扫码订阅本站,第一时间获得更新
微信扫描二维码,订阅我们网站的动态,另外不定时发送WordPress小技巧,你可以随时退订,欢迎订阅哦~

二丫讲梵 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明<十二>企业级开源仓库nexus3实战应用–通过命令行往Python私仓传包
喜欢 (0)
[如果想支持本站,可支付宝赞助]
分享 (0)
eryajf
关于作者:
学无止境,我愿意无止境学。书山有路,我愿意举身投火,淬炼成金!永远不要忘记,激情的奋进,就是美好的未来!

您必须 登录 才能发表评论!