Skip to content

Commit 8ee7edb

Browse files
committed
1 parent 1d0e1a4 commit 8ee7edb

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

setup.py

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@
2121

2222

2323
if "publish" in sys.argv:
24+
"""
25+
Build and upload to PyPi, if...
26+
... __version__ doesn't contains "dev"
27+
... we are on git 'master' branch
28+
... git repository is 'clean' (no changed files)
29+
30+
Upload with "twine", git tag the current version and git push --tag
31+
32+
The cli arguments will be pass to 'twine'. So this is possible:
33+
* Display 'twine' help page...: ./setup.py publish --help
34+
* use testpypi................: ./setup.py publish --repository=test
35+
"""
36+
# Imports here, so it's easier to copy&paste this complete code block ;)
37+
import subprocess
38+
import shutil
39+
2440
try:
2541
# Test if wheel is installed, otherwise the user will only see:
2642
# error: invalid command 'bdist_wheel'
@@ -33,23 +49,39 @@
3349
print(" ~/your/env/$ pip install wheel")
3450
sys.exit(-1)
3551

36-
import subprocess
52+
try:
53+
import twine
54+
except ImportError as err:
55+
print("\nError: %s" % err)
56+
print("\nMaybe https://pypi.python.org/pypi/twine is not installed or virtualenv not activated?!?")
57+
print("e.g.:")
58+
print(" ~/your/env/$ source bin/activate")
59+
print(" ~/your/env/$ pip install twine")
60+
sys.exit(-1)
3761

3862
def verbose_check_output(*args):
39-
print("\nCall: %r\n" % " ".join(args))
63+
""" 'verbose' version of subprocess.check_output() """
64+
call_info = "Call: %r" % " ".join(args)
4065
try:
41-
return subprocess.check_output(args, universal_newlines=True)
66+
output = subprocess.check_output(args, universal_newlines=True, stderr=subprocess.STDOUT)
4267
except subprocess.CalledProcessError as err:
4368
print("\n***ERROR:")
4469
print(err.output)
4570
raise
71+
return call_info, output
4672

4773
def verbose_check_call(*args):
48-
print("\nCall: %r\n" % " ".join(args))
74+
""" 'verbose' version of subprocess.check_call() """
75+
print("\tCall: %r\n" % " ".join(args))
4976
subprocess.check_call(args, universal_newlines=True)
5077

51-
# Check if we are on 'master' branch:
52-
output = verbose_check_output("git", "branch", "--no-color")
78+
if "dev" in __version__:
79+
print("\nERROR: Version contains 'dev': v%s\n" % __version__)
80+
sys.exit(-1)
81+
82+
print("\nCheck if we are on 'master' branch:")
83+
call_info, output = verbose_check_output("git", "branch", "--no-color")
84+
print("\t%s" % call_info)
5385
if "* master" in output:
5486
print("OK")
5587
else:
@@ -59,22 +91,48 @@ def verbose_check_call(*args):
5991
print("Bye.")
6092
sys.exit(-1)
6193

62-
# publish only if git repro is clean:
63-
output = verbose_check_output("git", "status", "--porcelain")
94+
print("\ncheck if if git repro is clean:")
95+
call_info, output = verbose_check_output("git", "status", "--porcelain")
96+
print("\t%s" % call_info)
6497
if output == "":
6598
print("OK")
6699
else:
67-
print("\n***ERROR: git repro not clean:")
100+
print("\n *** ERROR: git repro not clean:")
68101
print(output)
69102
sys.exit(-1)
70103

71-
# tag first (will raise a error of tag already exists)
104+
print("\ngit tag version (will raise a error of tag already exists)")
72105
verbose_check_call("git", "tag", "v%s" % __version__)
73106

74-
# build and upload to PyPi:
75-
verbose_check_call(sys.executable or "python", "setup.py", "sdist", "bdist_wheel", "upload")
76-
77-
# push
107+
print("\nCleanup old builds:")
108+
def rmtree(path):
109+
path = os.path.abspath(path)
110+
print("\tremove tree:", path)
111+
shutil.rmtree(path)
112+
rmtree("./dist")
113+
rmtree("./build")
114+
115+
print("\nbuild but don't upload...")
116+
log_filename="build.log"
117+
with open(log_filename, "a") as log:
118+
call_info, output = verbose_check_output(
119+
sys.executable or "python",
120+
"setup.py", "sdist", "bdist_wheel", "bdist_egg"
121+
)
122+
print("\t%s" % call_info)
123+
log.write(call_info)
124+
log.write(output)
125+
print("Build output is in log file: %r" % log_filename)
126+
127+
print("\nUpload with twine:")
128+
twine_args = sys.argv[1:]
129+
twine_args.remove("publish")
130+
twine_args.insert(1, "dist/*")
131+
print("\ttwine upload command args: %r" % " ".join(twine_args))
132+
from twine.commands.upload import main as twine_upload
133+
twine_upload(twine_args)
134+
135+
print("\ngit push to server")
78136
verbose_check_call("git", "push")
79137
verbose_check_call("git", "push", "--tags")
80138

0 commit comments

Comments
 (0)