/
build_sdist
executable file
·101 lines (89 loc) · 3.91 KB
/
build_sdist
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
#!/usr/bin/env python3
# jbin - Joe's miscellaneous scripts, tools and configs
# build_sdist: Build a Python package from the current working directory
# Copyright (C) 2021-2023 Johannes Bauer
#
# This file is part of jbin.
#
# jbin is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; this program is ONLY licensed under
# version 3 of the License, later versions are explicitly excluded.
#
# jbin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with jbin. If not, see <http://www.gnu.org/licenses/>.
#
# Johannes Bauer <JohannesBauer@gmx.de>
import os
import sys
import shutil
import subprocess
import contextlib
from FriendlyArgumentParser import FriendlyArgumentParser
parser = FriendlyArgumentParser(description = "Build a Python package from the current working directory.")
parser.add_argument("-n", "--name", metavar = "pkgname", help = "Specifies the module name that is used to generate the package. By default, the basename of the current working directory is used for this.")
parser.add_argument("-l", "--local-install", action = "store_true", help = "Locally install the package after building it.")
parser.add_argument("-u", "--twine-upload", action = "store_true", help = "Upload the package to twine after building it.")
parser.add_argument("-v", "--verbose", action = "count", default = 0, help = "Increases verbosity. Can be specified multiple times to increase.")
args = parser.parse_args(sys.argv[1:])
if args.name is None:
pkgname = os.path.basename(os.getcwd())
else:
pkgname = args.name
try:
sys.path.insert(0, ".")
module = __import__(pkgname)
except ModuleNotFoundError as e:
print("Fatal: Could not find module '%s' (%s)." % (pkgname, str(e)))
sys.exit(1)
imported_path = os.path.realpath(module.__file__)
wanted_path = os.path.realpath(pkgname + "/__init__.py")
if imported_path != wanted_path:
print("Fatal: Did not import the correct module. Wanted %s but imported %s." % (wanted_path, imported_path))
sys.exit(1)
version = getattr(module, "VERSION", None)
if version is None:
print("Fatal: Could not get VERSION information from module %s." % (str(module)))
sys.exit(1)
if len(version) == 0:
print("Fatal: Could not get VERSION information from module %s, retrieved empty string." % (str(module)))
sys.exit(1)
if not version[0].isdigit():
print("Fatal: Could not get VERSION information from module %s, does not start with a digit." % (str(module)))
sys.exit(1)
if ("rc" in version) and (args.twine_upload):
print("Error: Refusing to upload release candidate '%s' to PyPi." % (version))
sys.exit(1)
setup_file = os.path.realpath(imported_path + "/../../setup.py")
setup_template_file = os.path.realpath(imported_path + "/../../setup_template.py")
if os.path.isfile(setup_template_file):
with open(setup_template_file) as f:
setup_py = f.read()
setup_py = setup_py.replace("${PACKAGE_VERSION}", version)
with open("setup.py", "w") as f:
f.write(setup_py)
with contextlib.suppress(FileNotFoundError):
shutil.rmtree("build")
with contextlib.suppress(FileNotFoundError):
shutil.rmtree("dist")
with contextlib.suppress(FileNotFoundError):
shutil.rmtree("%s.egg-info" % (pkgname))
cmd = [ "python3", "setup.py", "sdist" ]
if args.verbose >= 1:
print("Running: %s" % (" ".join(cmd)))
subprocess.run(cmd)
if args.local_install:
cmd = [ "pip3", "install", "--force-reinstall", "dist/%s-%s.tar.gz" % (pkgname, version) ]
if args.verbose >= 1:
print("Running: %s" % (" ".join(cmd)))
subprocess.run(cmd)
if args.twine_upload:
cmd = [ "twine", "upload", "dist/%s-%s.tar.gz" % (pkgname, version) ]
if args.verbose >= 1:
print("Running: %s" % (" ".join(cmd)))
subprocess.run(cmd)