首页 > 编程语言 >Python3自动化打包项目发布到pypi

Python3自动化打包项目发布到pypi

时间:2022-10-25 15:33:18浏览次数:54  
标签:info 1.0 self plbm pypi Python3 egg 打包 build

效果

D:\Program\Python310\python.exe D:\data\git\PythonLinuxBasicModule\upload.py 
C:\Users\刘某
usage: twine [-h] [--version] [--no-color] {register,check,upload}

positional arguments:
  {register,check,upload}

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --no-color            disable colored output
running sdist
running egg_info
creating plbm.egg-info
writing plbm.egg-info\PKG-INFO
writing dependency_links to plbm.egg-info\dependency_links.txt
writing requirements to plbm.egg-info\requires.txt
writing top-level names to plbm.egg-info\top_level.txt
writing manifest file 'plbm.egg-info\SOURCES.txt'
reading manifest file 'plbm.egg-info\SOURCES.txt'
writing manifest file 'plbm.egg-info\SOURCES.txt'
running check
creating plbm-1.0.2
creating plbm-1.0.2\plbm.egg-info
copying files to plbm-1.0.2...
copying README.md -> plbm-1.0.2
copying setup.py -> plbm-1.0.2
copying plbm.egg-info\PKG-INFO -> plbm-1.0.2\plbm.egg-info
copying plbm.egg-info\SOURCES.txt -> plbm-1.0.2\plbm.egg-info
copying plbm.egg-info\dependency_links.txt -> plbm-1.0.2\plbm.egg-info
copying plbm.egg-info\requires.txt -> plbm-1.0.2\plbm.egg-info
copying plbm.egg-info\top_level.txt -> plbm-1.0.2\plbm.egg-info
Writing plbm-1.0.2\setup.cfg
creating dist
Creating tar archive
removing 'plbm-1.0.2' (and everything under it)
running bdist_wheel
running build
C:\Users\刘某\AppData\Roaming\Python\Python310\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
installing to build\bdist.win-amd64\wheel
running install
running install_egg_info
Copying plbm.egg-info to build\bdist.win-amd64\wheel\.\plbm-1.0.2-py3.10.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\plbm-1.0.2.dist-info\WHEEL
creating 'dist\plbm-1.0.2-py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'plbm-1.0.2.dist-info/METADATA'
adding 'plbm-1.0.2.dist-info/WHEEL'
adding 'plbm-1.0.2.dist-info/top_level.txt'
adding 'plbm-1.0.2.dist-info/RECORD'
removing build\bdist.win-amd64\wheel
构建成功
Uploading distributions to https://upload.pypi.org/legacy/
Uploading plbm-1.0.2-py3-none-any.whl
100% ---------------------------------------- 10.6/10.6 kB • 00:02 • ?
Uploading plbm-1.0.2.tar.gz
100% ---------------------------------------- 10.7/10.7 kB • 00:00 • ?

View at:
https://pypi.org/project/plbm/1.0.2/
上传成功

源码

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@File    :   UploadToPypi.py
@Time    :   2022-10-25 11:54
@Author  :   坐公交也用券
@Version :   1.0
@Contact :   [email protected]
@Homepage : https://liumou.site
@Desc    :   当前文件作用
"""
from os import system, chdir, path, getenv
from shutil import rmtree
import platform


class UploadToPypi:
	def __init__(self):
		"""
		上传项目到pypi
		"""
		self.work = './plbm'
		self.dist = path.join(self.work, 'dist')
		self.egg = path.join(self.work, 'plbm.egg-info')
		self.build_dir = path.join(self.work, 'build')
		self.home = getenv('HOME')
		if platform.system().lower() == 'windows':
			self.home = path.expanduser("~")
		print(self.home)
		# 这是保存用户密码的文件,将pypi的账号保存到这个文件即可(第一行: 用户名,第二行: 密码)
		file = path.join(self.home, 'pypi.txt')
		r = open(file=file, mode='r', encoding='utf-8')
		data = r.read()
		r.close()
		self.user = str(data).split("\n")[0]
		self.pd = str(data).split("\n")[1]

	def install_twine(self):
		"""
		:return:
		"""
		cmd = "pip install twine -i https://pypi.tuna.tsinghua.edu.cn/simple"
		if self.check_twine():
			if platform.system().lower() == 'linux':
				cmd = "pip3 install twine -i https://pypi.tuna.tsinghua.edu.cn/simple"
			system(cmd)

	def check_twine(self):
		"""
		检查是否需要安装twine工具
		:return:
		"""
		cmd = "twine -h"
		res = system(cmd)
		if int(res) == 0:
			return False
		return True

	def delete(self, dir_=None):
		"""
		删除文件夹
		:param dir_:
		:return:
		"""
		if dir_ is None:
			dir_ = self.dist
		if path.exists(dir_):
			try:
				rmtree(dir_)
			except Exception as e:
				print(e)

	def build(self):
		"""
		开始构建
		:return:
		"""
		chdir(self.work)
		c = "python setup.py sdist bdist_wheel"
		if platform.system().lower() == 'linux':
			c = "python3 setup.py sdist bdist_wheel"
		res = system(c)
		if int(res) == 0:
			print("构建成功")
			res = system(f"twine upload -u {self.user} -p {self.pd} dist/* ")
			if int(res) == 0:
				print("上传成功")
			else:
				print("上传失败")
		else:
			print("构建失败")

	def start(self):
		"""
		开始处理
		:return:
		"""
		self.install_twine()
		self.delete()
		self.delete(self.egg)
		self.delete(self.build_dir)
		self.build()


if __name__ == "__main__":
	upload = UploadToPypi()
	upload.start()

标签:info,1.0,self,plbm,pypi,Python3,egg,打包,build
From: https://www.cnblogs.com/liuyi778/p/16825020.html

相关文章