Skip to content

Commit

Permalink
Merge pull request #81 from hsyhhssyy/master
Browse files Browse the repository at this point in the history
新增Pypi的GithubAction
  • Loading branch information
vivien8261 authored Nov 17, 2023
2 parents 8f89393 + 3498de2 commit 1b320f5
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 14 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Pylint

on:
push:
branches:
- master

jobs:
pypi-publish:
name: upload release to PyPI
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write
steps:
- name: Check out the repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build package
run: |
python setup.py bdist_wheel --auto-increment-version
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
77 changes: 63 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,78 @@
import os
import json
import random
import setuptools
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel

from urllib import request


def ver_num(_v):
num = int(_v.replace('.', ''))
if num < 1000:
num *= 10
return num

def get_new_version():
pypi = json.loads(request.urlopen('https://pypi.python.org/pypi/amiyabot/json').read())
v_list = {ver_num(v): v for v in pypi['releases'].keys()}
s_list = sorted(v_list)
latest = v_list[s_list[-1]]

print(f'latest: {latest}')

return f"{latest}"
# return f"1.10.10"

# Auto increment the version number.
# 1.0.9 -> 1.1.0 , 1.9.9 -> 2.0.0 but 9.9.9 -> 10.0.0
def incr_version(v):
v = v.split('.')
if len(v) == 3:
if int(v[2]) >= 9:
v[2] = '0'
if int(v[1]) >= 9:
v[1] = '0'
v[0] = str(int(v[0]) + 1)
else:
v[1] = str(int(v[1]) + 1)
else:
v[2] = str(int(v[2]) + 1)
else:
v.append('1')
return '.'.join(v)

class CustomBdistWheelCommand(_bdist_wheel):
user_options = _bdist_wheel.user_options + [
('auto-increment-version', None, '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')
]

def initialize_options(self):
_bdist_wheel.initialize_options(self)
self.auto_increment_version = False

def finalize_options(self):
latest_version = get_new_version()
if self.auto_increment_version:
new_version = incr_version(latest_version)
print(f'Auto-incrementing version to: {new_version}')
self.distribution.metadata.version = new_version
else:
new_version = incr_version(latest_version)
release_new = input(f'new?: {new_version} (Y/n)')

pypi = json.loads(request.urlopen('https://pypi.python.org/pypi/amiyabot/json').read())
v_list = {ver_num(v): v for v in pypi['releases'].keys()}
s_list = sorted(v_list)
latest = v_list[s_list[-1]]
if not (not release_new or release_new.lower() == 'y'):
new_version = input('version: ')

print(f'latest: {latest}')
self.distribution.metadata.version = new_version

latest = latest.split('.')
latest[-1] = str(int(latest[-1]) + 1)
# 加入一个随机数的BuildNumber保证Action可以重复执行
build_number = random.randint(0, 1000)
self.build_number = f"{build_number}"

new_version = '.'.join(latest)
release_new = input(f'new?: {new_version} (Y/n)')
_bdist_wheel.finalize_options(self)

if not (not release_new or release_new.lower() == 'y'):
new_version = ''
def run(self):
_bdist_wheel.run(self)

with open('README.md', mode='r', encoding='utf-8') as md:
description = md.read()
Expand All @@ -44,7 +90,7 @@ def ver_num(_v):

setuptools.setup(
name='amiyabot',
version=new_version or input('version: '),
version="0.0.1",
author='vivien8261',
author_email='[email protected]',
url='https://www.amiyabot.com',
Expand All @@ -57,6 +103,9 @@ def ver_num(_v):
include_package_data=True,
python_requires='>=3.8',
install_requires=requirements,
cmdclass={
'bdist_wheel': CustomBdistWheelCommand,
},
)

# python setup.py bdist_wheel
# python setup.py bdist_wheel --auto-increment-version

0 comments on commit 1b320f5

Please sign in to comment.