Skip to content

Commit 986d302

Browse files
committed
add ci
1 parent e5033c9 commit 986d302

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

.github/workflows/pypi.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Pypi
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
pypi-publish:
10+
name: upload release to PyPI
11+
runs-on: ubuntu-latest
12+
environment: release
13+
permissions:
14+
id-token: write
15+
steps:
16+
- name: Check out the repository
17+
uses: actions/checkout@v2
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v2
21+
with:
22+
python-version: '3.10'
23+
24+
- name: Install dependencies
25+
run: |
26+
python -m pip install --upgrade pip
27+
pip install setuptools wheel twine
28+
29+
- name: Build package
30+
run: |
31+
python setup.py bdist_wheel --auto-increment-version
32+
33+
- name: Publish package distributions to PyPI
34+
uses: pypa/gh-action-pypi-publish@release/v1

setup.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,86 @@
1+
import json
2+
import random
13
import setuptools
24

5+
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
6+
from urllib import request
7+
8+
9+
def ver_num(_v):
10+
num = int(_v.replace('.', ''))
11+
if num < 1000:
12+
num *= 10
13+
return num
14+
15+
16+
def get_new_version():
17+
pypi = json.loads(request.urlopen('https://pypi.python.org/pypi/amiyalog/json').read())
18+
v_list = {ver_num(v): v for v in pypi['releases'].keys()}
19+
s_list = sorted(v_list)
20+
latest = v_list[s_list[-1]]
21+
22+
print(f'latest: {latest}')
23+
24+
return f'{latest}'
25+
26+
27+
# Auto increment the version number.
28+
# 1.0.9 -> 1.1.0 , 1.9.9 -> 2.0.0 but 9.9.9 -> 10.0.0
29+
def incr_version(v):
30+
v = v.split('.')
31+
if len(v) == 3:
32+
if int(v[2]) >= 9:
33+
v[2] = '0'
34+
if int(v[1]) >= 9:
35+
v[1] = '0'
36+
v[0] = str(int(v[0]) + 1)
37+
else:
38+
v[1] = str(int(v[1]) + 1)
39+
else:
40+
v[2] = str(int(v[2]) + 1)
41+
else:
42+
v.append('1')
43+
return '.'.join(v)
44+
45+
46+
class CustomBdistWheelCommand(_bdist_wheel):
47+
user_options = _bdist_wheel.user_options + [
48+
(
49+
'auto-increment-version',
50+
None,
51+
'Auto increment the version number before building with special rule: 1.0.9 -> 1.1.0 , 1.9.9 -> 2.0.0 . However 9.9.9 -> 10.0.0',
52+
)
53+
]
54+
55+
def initialize_options(self):
56+
_bdist_wheel.initialize_options(self)
57+
self.auto_increment_version = False
58+
59+
def finalize_options(self):
60+
latest_version = get_new_version()
61+
if self.auto_increment_version:
62+
new_version = incr_version(latest_version)
63+
print(f'Auto-incrementing version to: {new_version}')
64+
self.distribution.metadata.version = new_version
65+
else:
66+
new_version = incr_version(latest_version)
67+
release_new = input(f'new?: {new_version} (Y/n)')
68+
69+
if not (not release_new or release_new.lower() == 'y'):
70+
new_version = input('version: ')
71+
72+
self.distribution.metadata.version = new_version
73+
74+
# 加入一个随机数的BuildNumber保证Action可以重复执行
75+
build_number = random.randint(0, 1000)
76+
self.build_number = f'{build_number}'
77+
78+
_bdist_wheel.finalize_options(self)
79+
80+
def run(self):
81+
_bdist_wheel.run(self)
82+
83+
384
with open('README.md', mode='r', encoding='utf-8') as md:
485
description = md.read()
586

@@ -23,6 +104,9 @@
23104
include_package_data=True,
24105
python_requires='>=3.10',
25106
install_requires=requirements,
107+
cmdclass={
108+
'bdist_wheel': CustomBdistWheelCommand,
109+
},
26110
)
27111

28112
# python setup.py bdist_wheel

0 commit comments

Comments
 (0)