python pip打包记录

这是一个关于使用Makefile管理和自动化构建Python项目的博客。内容包括Makefile的常用规则,如`all`、`lint`、`install`等,以及如何打包、上传和配置`.pypirc`文件进行发布。此外,还展示了`setup.py`文件如何生成元数据并注入到代码中。
摘要由CSDN通过智能技术生成

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

requirement.txt

loguru

setup.py

from setuptools import setup, find_packages
import subprocess
import os
import sys
import glob


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


PACKAGE_NAME = 'point_cloud_tools'
VERSION = '1.2.2'

git_branch = sh('git rev-parse --abbrev-ref HEAD')
git_commit_hash = sh('git log -1 --format=%h --abbrev=8')
git_commit_date = sh(
    'git log -1 --date=format:"%Y/%m/%d %H:%M:%S" --format="%cd"')
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('\n', ',')

# this is a hack, the easiest way I found to inject these metedata
with open(f'{PACKAGE_NAME}/version.py', 'w') as f:
    f.write(f'# DO NOT EDIT THIS FILE, IT IS GENERATED BY SETUP.PY\n')
    f.write(f'__version__ = "{VERSION}"\n')
    f.write(f'git_branch = "{git_branch}"\n')
    f.write(f'git_commit_hash = "{git_commit_hash}"\n')
    f.write(f'git_commit_date = "{git_commit_date}"\n')
    f.write(f'git_commit_count = {git_commit_count}\n')
    f.write(f'git_diff_name_only = "{git_diff_name_only}"\n')
    f.write(
        f'\nrepo = "https://devops.momenta.works/Momenta/hdmap-workflow/_git/mpcv-point-cloud-layer-dectection"\n'
    )
    f.write(
        f'document = "https://momenta.feishu.cn/docs/doccnZUoVF5Cpgn4Khm3HTALMFd"\n'
    )
    f.write('\n\n')
    f.write('if __name__ == "__main__":\n')
    f.write('    for name, value in dict(locals()).items():\n')
    f.write('        if name.startswith("_") and name != "__version__":\n')
    f.write('            continue\n')
    f.write('        print(f"{name}: {value}")\n')

with open('requirements.txt') as f:
    lines = f.readlines()
install_requires = [
    line.strip() for line in lines if line and not line.startswith('--')
]
find_package = find_packages()

setup(
    name=PACKAGE_NAME,
    version=VERSION,
    keywords='point_cloud',
    description='point_cloud_layer_dectection tools',
    license='',
    url='https://devops.momenta.works/Momenta/hdmap-workflow/_git/mpcv-point-cloud-layer-dectection',
    author='xuhongyuan',
    author_email='xuhongyuan@momenta.ai',
    packages=[
        f'{PACKAGE_NAME}',
        f'{PACKAGE_NAME}.cli',
    ],
    python_requires='>=3.6.0',
    include_package_data=True,
    package_data={
        f'{PACKAGE_NAME}': [
            os.path.basename(p)
            for p in glob.glob(f'{PACKAGE_NAME}/*.*')
            if not p.endswith('.py')
        ],
    },
    platforms='any',
    zip_safe=False,
    install_requires=install_requires,
)

打包上传需要在本地的.pypirc文件中进行配置

[distutils]
index-servers = local
[local] #local 于上述上传upload -r local相对应
repository: https://artifactory.momenta.works/artifactory/api/pypi/pypi-momenta
username: xxx
password: xxx

配置的用户名密码来自jfrog
在这里插入图片描述
本地打包命令
在这里插入图片描述
(打包命令已在makefile中配置,可直接使用)
make package
make upload

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值