33"""Development related tasks to be run with 'invoke'"""
44
55import os
6+ import re
67import shutil
8+ import sys
79
810import invoke
911
@@ -135,6 +137,33 @@ def clean_all(context):
135137 pass
136138namespace_clean .add_task (clean_all , 'all' )
137139
140+
141+ @invoke .task
142+ def tag (context , name , message = '' ):
143+ "Add a Git tag and push it to origin"
144+ # If a tag was provided on the command-line, then add a Git tag and push it to origin
145+ if name :
146+ context .run ('git tag -a {} -m {!r}' .format (name , message ))
147+ context .run ('git push origin {}' .format (name ))
148+ namespace .add_task (tag )
149+
150+ @invoke .task ()
151+ def validatetag (context ):
152+ "Check to make sure that a tag exists for the current HEAD and it looks like a valid version number"
153+ # Validate that a Git tag exists for the current commit HEAD
154+ result = context .run ("git describe --exact-match --tags $(git log -n1 --pretty='%h')" )
155+ tag = result .stdout .rstrip ()
156+
157+ # Validate that the Git tag appears to be a valid version number
158+ ver_regex = re .compile ('(\d+)\.(\d+)\.(\d+)' )
159+ match = ver_regex .fullmatch (tag )
160+ if match is None :
161+ print ('Tag {!r} does not appear to be a valid version number' .format (tag ))
162+ sys .exit (- 1 )
163+ else :
164+ print ('Tag {!r} appears to be a valid version number' .format (tag ))
165+ namespace .add_task (validatetag )
166+
138167@invoke .task (pre = [clean_all ])
139168def sdist (context ):
140169 "Create a source distribution"
@@ -147,13 +176,13 @@ def wheel(context):
147176 context .run ('python setup.py bdist_wheel' )
148177namespace .add_task (wheel )
149178
150- @invoke .task (pre = [sdist , wheel ])
179+ @invoke .task (pre = [validatetag , sdist , wheel ])
151180def pypi (context ):
152181 "Build and upload a distribution to pypi"
153182 context .run ('twine upload dist/*' )
154183namespace .add_task (pypi )
155184
156- @invoke .task (pre = [sdist , wheel ])
185+ @invoke .task (pre = [validatetag , sdist , wheel ])
157186def pypi_test (context ):
158187 "Build and upload a distribution to https://test.pypi.org"
159188 context .run ('twine upload --repository-url https://test.pypi.org/legacy/ dist/*' )
0 commit comments