增加setup.py 文件 setuptools文档 setup.py样例
from setuptools import setup, find_packages
def sh(command):
try:
if isinstance(command, list):
command = ' '.join(command)
return subprocess.check_output(
command, shell=True,
stderr=subprocess.STDOUT).decode('utf-8').rstrip()
except Exception as e:
return None
VERSION = 'major.minor.patch'
git_branch = sh('git rev-parse --abbrev-ref HEAD')
git_commit_hash = sh('git log -1 --format=%h')
git_commit_count = sh('git rev-list --count HEAD')
git_diff_name_only = sh('git diff --name-only')
if git_diff_name_only:
git_diff_name_only = git_diff_name_only.replace('
', ',')
# this is a hack, the easiest way I found to inject these metedata
with open('worker_io/version.py', 'w') as f:
f.write(f'# DO NOT EDIT THIS FILE, IT IS GENERATED BY SETUP.PY
')
f.write(f'__version__ = "{VERSION}"
')
f.write(f'git_branch = "{git_branch}"
')
f.write(f'git_commit_hash = "{git_commit_hash}"
')
f.write(f'git_commit_count = {git_commit_count}
')
f.write(f'git_diff_name_only = "{git_diff_name_only}"
')
# 获取当前打包文件的依赖 建议通过读取requirements.txt 来实现确保更新后打包的依赖及时更新
with open('requirements.txt') as f:
lines = f.readlines()
install_requires = [
line.strip() for line in lines if line and not line.startswith('--')
]
setup(
# 这里列举部分 keywords 如有需要 https://setuptools.readthedocs.io/en/latest/references/keywords.html
name=PACKAGE_NAME,
version=VERSION,
keywords=KEYWORDS, # package 关键字和package相关即可
description=DESCRIPTION, # package 功能描述
url=URL, # 可增加package confluence说明页面
author='HDMap Platform R&D',
author_email='hdmap-platformrnd-group@momenta.ai',
packages=find_packages(), # 如果含有多个子目录,需要指定多个子目录。like packages=[ditu.inspect, ditu]
platforms='any',
zip_safe=False,
install_requires=install_requires,
python_requires='>=3', # 对所需python版本进行限制
)
Makefile
all:
@echo nothing special
lint:
python3 -m mdk_tools.cli.py_lint .
prepare:
python3 -m pip install -r requirements.txt --user
install:
@cp pre_commit.sh .git/hooks/pre-commit || true
@python3 setup.py install --user
upload:
python3 setup.py bdist_wheel upload -r local
package:clean prepare
python3 setup.py bdist_wheel
install: package
python3 -m pip install dist/*.whl -U --force-reinstall
clean:
rm -rf build
rm -rf dist
rm -rf *.egg-info
rm -rf __pycache__
rm -rf tests/__pycache__
rm -f *.pyc
DOCKER_BUILD_TAG := artifactory.momenta.works/docker-momenta/ubuntu1604-python36:v0.0.1
docker_test_build:
docker run --rm -v `pwd`:/workdir \
-it $(DOCKER_BUILD_TAG) zsh
DOCKER_RELEASE_TAG := artifactory.momenta.works/docker-momenta/smpy:v1.6.5
docker_build:
docker build --tag $(DOCKER_RELEASE_TAG) .
docker_push:
docker push $(DOCKER_RELEASE_TAG)
docker_test_release:
docker run --rm -v `pwd`:/workdir -it $(DOCKER_RELEASE_TAG) zsh
Makefile 中增加以下命令
package: # 打包当前项目
python3 setup.py bdist_wheel
package_install: package # 打包并在本机安装当前项目
python3 -m pip install dist/*.whl -U
upload: # 上传当前项目至artifactory
python3 setup.py bdist_wheel upload -r local
clean:
rm -rf __pycache__ dist *.egg
https://artifactory.momenta.works/ui/repos/tree/General/pypi-momenta 点击右上方登录,输入账号密码,然后点击 Set Me Up,然后参考下面的文档把账户密码存下来。 配置账户密码 在 .bashrc 或 .zshrc 中添加账户密码的环境变量
export ARTIFACTORY_USERNAME=***
export ARTIFACTORY_PASSWORD=***
其中 *** 是从页面拷贝过来的字符串(注意账户密码会过期) 上传 pip 包配置 pypirc 如果需要上传 pip,可以配置 pypirc: 配置.pypirc→ 需要放到~目录下``` $ cat ~/.pypirc [distutils] index-servers = local [local] repository: https://artifactory.momenta.works/artifactory/api/pypi/pypi-momenta username: *** password: ***
![在这里插入图片描述](https://img-blog.csdnimg.cn/aba23614cc674d6b9883d369ef1db75c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5oy95omL562J6aOO6LW3,size_20,color_FFFFFF,t_70,g_se,x_16)
参考文档:https://blog.csdn.net/dongfuye/article/details/46875539
|