-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_setup.py
126 lines (89 loc) · 2.99 KB
/
helper_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
some helper function for setup.py file
use: copy it alongside setup.py and in the setup.py file:
from helper_setup import read_readme, activate_cmd_build, activate_cmd_publish
"""
import sys
import os
import shutil
from pathlib import PurePath
from setuptools import find_packages
from importlib import import_module
def read(*paths):
with open(*paths, 'r') as f:
return f.read()
def read_readme():
"""will read the README.* file, it can be any extention"""
return read(next(filter(lambda f: 'README.' in f, os.listdir('.'))))
context = {}
def moveAtBuild(liToMove):
context['moveAtBuild'] = \
[[PurePath(os.path.dirname(__file__), path) for path in tuple] for tuple in liToMove]
def activate_cmd_publish():
"""
need to be run in setup.py to take action,
if `python setup.py publish`: will build / upload / clean
"""
if sys.argv[-1] == 'publish':
publish()
sys.exit()
def activate_cmd_build():
"""
need to be run in setup.py to take action,
if `python setup.py build`: will build
"""
if sys.argv[-1] == 'build':
wheel()
sys.exit()
def wheel():
clean_dirs()
check_installed_tools()
moveBeforeBuild()
build_wheel()
moveAfterBuild()
def moveBeforeBuild():
for mvFrom, mvTo in context.get('moveAtBuild', []):
shutil.move(mvFrom, mvTo)
def moveAfterBuild():
for mvFrom, mvTo in context.get('moveAtBuild', []):
shutil.move(mvTo, mvFrom)
def publish():
wheel()
comfirme_publish_version()
upload_wheel()
print_git_tag_info()
def check_installed_tools():
if os.system('pip freeze | grep twine > /dev/null'):
print('twine not installed.\n\tUse `pip install twine`')
sys.exit()
def build_wheel():
print('\nbuilding ...')
os.system('python setup.py bdist_wheel > /dev/null') # omit sdist, build only the wheel
def comfirme_publish_version():
__init__ = import_module(find_packages()[0])
yesNo = input("do you want to publish version: {} to pypi ? [Y/n]".format(__init__.__version__))
if yesNo.lower() in ['', 'y', 'yes']:
return
else:
raise SystemExit()
def upload_wheel():
print('\nuploading ...')
os.system('twine upload dist/*') # upload the package to PyPI
def is_ext(ext):
ext = ext[1:] if ext[0] == '.' else ext
def with_file(file):
splited = file.split('.')
return len(splited) > 1 and splited[-1] == ext
return with_file
def get_files_ext(ext):
return filter(is_ext(ext), os.listdir('.'))
def clean_dirs():
for dir in ['dist', 'build']:
shutil.rmtree(dir, ignore_errors=True)
for egg in get_files_ext('.egg-info'):
shutil.rmtree(egg)
def print_git_tag_info():
__init__ = import_module(find_packages()[0])
print('\nYou probably want to also tag the version now:')
print(" git tag -a v{0} -m ''".format(__init__.__version__)) # -m 'release {0}' to add tag message
print(' git push --tags')