-
Notifications
You must be signed in to change notification settings - Fork 44
/
build_package.py
128 lines (95 loc) · 3.74 KB
/
build_package.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
127
128
"""
Script to build the package.
The script removes the previous built binaries and generated documentation
before it generate the documentation and build the binaries and finally
check the built binaries.
It assumes that the library is installed in so called develop mode.
Created on 7. des. 2018
@author: pab
"""
import os
import re
import shutil
import subprocess
import importlib
ROOT = os.path.abspath(os.path.dirname(__file__))
PACKAGE_NAME = 'numdifftools'
INFO = importlib.import_module(PACKAGE_NAME+'.info', './src')
LICENSE = importlib.import_module(PACKAGE_NAME+'.license', './src')
def remove_previous_build():
"""Removes ./dist, ./build, ./docs/_build, and ./src/{}.egg-info directories.
""".format(PACKAGE_NAME)
egginfo_path = os.path.join('src', '{}.egg-info'.format(PACKAGE_NAME))
docs_folder = os.path.join('docs', '_build')
for dirname in ['dist', 'build', egginfo_path, docs_folder]:
path = os.path.join(ROOT, dirname)
if os.path.exists(path) and os.path.isdir(path):
shutil.rmtree(path)
def update_readme():
readme_txt = INFO.__doc__.replace(
"""Introduction to {}
================{}
""".format(PACKAGE_NAME, '='*len(PACKAGE_NAME)), """{1}
{0}
{1}
""".format(PACKAGE_NAME, '='*len(PACKAGE_NAME)))
readme_txt = readme_txt.replace('.. only:: html', '')
filename = os.path.join(ROOT, "README.rst")
with open(filename, "w") as fid:
fid.write(readme_txt)
def set_package(version):
"""Set version of {} package""".format(PACKAGE_NAME)
if version:
filename = os.path.join(ROOT, "src", PACKAGE_NAME, "__init__.py")
print("Version: {}".format(version))
with open(filename, "r") as fid:
text = fid.read()
new_text = re.sub(r"__version__ = ['\"]([^'\"]*)['\"]",
'__version__ = "{}"'.format(version),
text, re.M) # @UndefinedVariable
with open(filename, "w") as fid:
fid.write(new_text)
def update_license():
filename = os.path.join(ROOT, "LICENSE.txt")
with open(filename, "w") as fid:
fid.write(LICENSE.__doc__)
class ChangeDir:
"""Context manager for changing the current working directory"""
def __init__(self, new_path):
self.new_path = new_path
def __enter__(self):
self.saved_path = os.getcwd()
os.chdir(self.new_path)
def __exit__(self, etype, value, traceback):
os.chdir(self.saved_path)
def call_subprocess(cmd_opts):
"""Safe call to subprocess"""
print("\n\n***********************************************")
print("Running {}".format(' '.join(cmd_opts)))
try:
subprocess.call(cmd_opts)
except Exception as error: # subprocess.CalledProcessError:
print(str(error))
print("***********************************************\n")
if __name__ == "__main__":
import click
@click.command(context_settings=dict(help_option_names=['-h', '--help']))
@click.argument("version")
def build_main(version):
"""Build and update {} version, documentation and package.
The script remove the previous built binaries and generated documentation
before it generate the documentation and build the binaries
and finally check the built binaries.
""".format(PACKAGE_NAME)
remove_previous_build()
set_package(version)
update_license()
update_readme()
for cmd in ['docs', 'latexpdf', 'egg_info', 'sdist', 'bdist_wheel']:
if cmd == 'latexpdf':
with ChangeDir('./docs'):
call_subprocess(["make.bat", cmd])
else:
call_subprocess(["python", "setup.py", cmd])
call_subprocess(["twine", "check", "dist/*"])
build_main()