|
| 1 | +"""Make new release on github and pypi. |
| 2 | +
|
| 3 | +Description |
| 4 | +----------- |
| 5 | +A new release is created by taking the underneath steps: |
| 6 | + 1. List all files in current directory and exclude all except the directory-of-interest |
| 7 | + 2. Extract the version from the __init__.py file |
| 8 | + 3. Remove old build directories such as dist, build and x.egg-info |
| 9 | + 4. Git pull |
| 10 | + 5. Get latest version from github |
| 11 | + 6. Check if the current version is newer then github lates--version. |
| 12 | + a. Make new wheel, build and install package |
| 13 | + b. Set tag to newest version and push to git |
| 14 | + c. Upload to pypi (credentials required) |
| 15 | +""" |
| 16 | + |
| 17 | +import os |
| 18 | +import re |
| 19 | +import numpy as np |
| 20 | +import urllib.request |
| 21 | +import yaml |
| 22 | +import shutil |
| 23 | +from packaging import version |
| 24 | +yaml.warnings({'YAMLLoadWarning': False}) |
| 25 | +# GITHUB |
| 26 | +GITHUBNAME = 'erdogant' |
| 27 | +TWINE_PATH = 'C://Users/Erdogan/AppData/Roaming/Python/Python36/Scripts/twine.exe upload dist/*' |
| 28 | + |
| 29 | + |
| 30 | +# %% Main function |
| 31 | +if __name__ == '__main__': |
| 32 | + # Clean screen |
| 33 | + os.system('cls') |
| 34 | + # List all files in dir |
| 35 | + filesindir = np.array(list(map(lambda x: x.lower(), np.array(os.listdir())))) |
| 36 | + # Remove all the known not relevant files and dirs |
| 37 | + exclude = np.array(['__pycache__','_version','make_new_release.py','get_release_github.sh','setup.py','get_version.py','readme.md','.git','.gitignore','build','dist','docs','license','make_new_build.sh','make_new_realease.sh','manifest.in','requirements.txt','setup.cfg','test.py']) # noqa |
| 38 | + getfile = filesindir[np.isin(filesindir, exclude)==False][0] # noqa |
| 39 | + # This must be the dir of interest |
| 40 | + INIT_FILE = getfile + "/__init__.py" |
| 41 | + print('Working on package: [%s]' %(getfile)) |
| 42 | + |
| 43 | + # Find version now |
| 44 | + if os.path.isfile(INIT_FILE): |
| 45 | + # Extract version from __init__.py |
| 46 | + getversion = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", open(INIT_FILE, "rt").read(), re.M) |
| 47 | + if getversion: |
| 48 | + # Remove build directories |
| 49 | + print('Removing local build directories..') |
| 50 | + if os.path.isdir('dist'): shutil.rmtree('dist') |
| 51 | + if os.path.isdir('build'): shutil.rmtree('build') |
| 52 | + if os.path.isdir(getfile + '.egg-info'): shutil.rmtree(getfile + '.egg-info') |
| 53 | + |
| 54 | + # Pull latest from github |
| 55 | + print('git pull') |
| 56 | + os.system('git pull') |
| 57 | + |
| 58 | + # Get version number |
| 59 | + current_version = getversion.group(1) |
| 60 | + # This is the current version described in our __init__.py file |
| 61 | + print('Current version: %s' %(current_version)) |
| 62 | + # Get latest version of github release |
| 63 | + github_url = 'https://api.github.com/repos/' + GITHUBNAME + '/' + getfile + '/releases/latest' |
| 64 | + github_page = urllib.request.urlopen(github_url) |
| 65 | + github_data = github_page.read() |
| 66 | + github_version = yaml.load(github_data)['tag_name'] |
| 67 | + print('Github version: %s' %(github_version)) |
| 68 | + print('Github version requested from: %s' %(github_url)) |
| 69 | + |
| 70 | + # Continue with the process of building a new version if the current version is really newer then the one on github! |
| 71 | + VERSION_OK = version.parse(current_version)>version.parse(github_version) |
| 72 | + if VERSION_OK: |
| 73 | + # Make new build |
| 74 | + print('Making new wheel..') |
| 75 | + os.system('python setup.py bdist_wheel') |
| 76 | + # Make new build |
| 77 | + print('Making source build..') |
| 78 | + os.system('python setup.py sdist') |
| 79 | + # Make new build |
| 80 | + print('Installing new wheel..') |
| 81 | + os.system('pip install -U dist/' + getfile + '-' + current_version + '-py3-none-any.whl') |
| 82 | + # git commit |
| 83 | + print('git add->commit->push') |
| 84 | + os.system('git add .') |
| 85 | + # os.system('git commit -m v'+current_version) |
| 86 | + # os.system('git push') |
| 87 | + # Set tag for this version |
| 88 | + print('Set new version tag: %s' %(current_version)) |
| 89 | + os.system('git tag -a v' + current_version + ' -m "' + current_version + '"') |
| 90 | + os.system('git push origin --tags') |
| 91 | + print('Upload to pypi..') |
| 92 | + os.system(TWINE_PATH) |
| 93 | + else: |
| 94 | + print('Not released! You need to increase your version: [%s]' %(INIT_FILE)) |
| 95 | + |
| 96 | + # f = open("_version", "w") |
| 97 | + # f.write(new_version) |
| 98 | + # f.close() |
| 99 | + else: |
| 100 | + raise RuntimeError("Unable to find version string in %s." % (INIT_FILE,)) |
| 101 | + else: |
| 102 | + print('Oh noo File not found: %s' %(INIT_FILE)) |
0 commit comments