21
21
22
22
23
23
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
+
24
40
try :
25
41
# Test if wheel is installed, otherwise the user will only see:
26
42
# error: invalid command 'bdist_wheel'
33
49
print (" ~/your/env/$ pip install wheel" )
34
50
sys .exit (- 1 )
35
51
36
- import subprocess
52
+ try :
53
+ import twine
54
+ except ImportError as err :
55
+ print ("\n Error: %s" % err )
56
+ print ("\n Maybe 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 )
37
61
38
62
def verbose_check_output (* args ):
39
- print ("\n Call: %r\n " % " " .join (args ))
63
+ """ 'verbose' version of subprocess.check_output() """
64
+ call_info = "Call: %r" % " " .join (args )
40
65
try :
41
- return subprocess .check_output (args , universal_newlines = True )
66
+ output = subprocess .check_output (args , universal_newlines = True , stderr = subprocess . STDOUT )
42
67
except subprocess .CalledProcessError as err :
43
68
print ("\n ***ERROR:" )
44
69
print (err .output )
45
70
raise
71
+ return call_info , output
46
72
47
73
def verbose_check_call (* args ):
48
- print ("\n Call: %r\n " % " " .join (args ))
74
+ """ 'verbose' version of subprocess.check_call() """
75
+ print ("\t Call: %r\n " % " " .join (args ))
49
76
subprocess .check_call (args , universal_newlines = True )
50
77
51
- # Check if we are on 'master' branch:
52
- output = verbose_check_output ("git" , "branch" , "--no-color" )
78
+ if "dev" in __version__ :
79
+ print ("\n ERROR: Version contains 'dev': v%s\n " % __version__ )
80
+ sys .exit (- 1 )
81
+
82
+ print ("\n Check if we are on 'master' branch:" )
83
+ call_info , output = verbose_check_output ("git" , "branch" , "--no-color" )
84
+ print ("\t %s" % call_info )
53
85
if "* master" in output :
54
86
print ("OK" )
55
87
else :
@@ -59,22 +91,48 @@ def verbose_check_call(*args):
59
91
print ("Bye." )
60
92
sys .exit (- 1 )
61
93
62
- # publish only if git repro is clean:
63
- output = verbose_check_output ("git" , "status" , "--porcelain" )
94
+ print ("\n check if if git repro is clean:" )
95
+ call_info , output = verbose_check_output ("git" , "status" , "--porcelain" )
96
+ print ("\t %s" % call_info )
64
97
if output == "" :
65
98
print ("OK" )
66
99
else :
67
- print ("\n ***ERROR: git repro not clean:" )
100
+ print ("\n *** ERROR: git repro not clean:" )
68
101
print (output )
69
102
sys .exit (- 1 )
70
103
71
- # tag first (will raise a error of tag already exists)
104
+ print ( " \n git tag version (will raise a error of tag already exists)" )
72
105
verbose_check_call ("git" , "tag" , "v%s" % __version__ )
73
106
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 ("\n Cleanup old builds:" )
108
+ def rmtree (path ):
109
+ path = os .path .abspath (path )
110
+ print ("\t remove tree:" , path )
111
+ shutil .rmtree (path )
112
+ rmtree ("./dist" )
113
+ rmtree ("./build" )
114
+
115
+ print ("\n build 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 ("\n Upload with twine:" )
128
+ twine_args = sys .argv [1 :]
129
+ twine_args .remove ("publish" )
130
+ twine_args .insert (1 , "dist/*" )
131
+ print ("\t twine 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 ("\n git push to server" )
78
136
verbose_check_call ("git" , "push" )
79
137
verbose_check_call ("git" , "push" , "--tags" )
80
138
0 commit comments