Skip to content

Commit 71e2cbb

Browse files
committed
freeing python version and updating versioning again
1 parent 9116de8 commit 71e2cbb

File tree

2 files changed

+72
-44
lines changed

2 files changed

+72
-44
lines changed

asciidrumming/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2
1+
#!/usr/bin/env python
22

33
import click
44
import pprint

versioning.py

Lines changed: 71 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
import os
1616
import sys
17-
from os.path import join, exists, isfile, isdir, dirname, basename
1817

1918
from pprint import pformat
20-
from distutils.core import Command
19+
from os.path import join, exists, isfile, isdir, dirname, basename
20+
21+
22+
### routines to initialize versioning
2123

2224

2325
def find_in_parents(path, name):
@@ -143,9 +145,11 @@ def setup_versioning():
143145

144146
return versionfile
145147

148+
146149
versionfile = setup_versioning()
147150

148-
def print_version_info():
151+
152+
def print_version_info(self=None):
149153
""" Testable body of a setuptools/distutils command.
150154
151155
>>> print_version_info()
@@ -161,21 +165,35 @@ def print_version_info():
161165
print('== Version-Info:\n' + pformat(versionfile.VERSION_INFO))
162166

163167

168+
169+
### versioning config/setup done. start of setuptools helpers
170+
171+
172+
from distutils.core import Command
173+
174+
def testable_do_nothing(self=None):
175+
""" A helper to increase test coverage.
176+
To be used on some classes as a method
177+
where a method override is required, but
178+
doesn't actually do anything.
179+
180+
>>> testable_do_nothing()
181+
"""
182+
164183
class cmd_version_info(Command):
165184
""" Version info command.
166185
167186
Run `./setup.py version` to get detailed info on the latest version.
168187
"""
169-
170188
description = "show versioning configuration and current project version"
171189
user_options = []
172190
boolean_options = []
173191

174-
def initialize_options(self): pass
175-
def finalize_options(self): pass
176-
177-
def run(self):
178-
print_version_info()
192+
# actually methods, but defined outside
193+
# the class for reuse- and testability.
194+
initialize_options = testable_do_nothing
195+
finalize_options = testable_do_nothing
196+
run = print_version_info
179197

180198

181199

@@ -225,26 +243,26 @@ def render_bumped(**kwd):
225243
return normalized
226244

227245

228-
def do_bump(not_really=None):
246+
def do_bump(self=None, test_data=None):
229247
""" Execute a version bump (if not testing)
230248
231-
>>> do_bump(not_really={'dirt':'','tag_version':{'release':(1,1,2)},'prefix':''})
249+
>>> do_bump(test_data={'dirt':'','tag_version':{'release':(1,1,2)},'prefix':''})
232250
== Next Version: {'release': (1, 1, 3)}
233251
== Tagging: 1.1.3
234-
>>> do_bump(not_really={'dirt':'XXX','tag_version':{'release':(1,1,2)},'prefix':''})
252+
>>> do_bump(test_data={'dirt':'XXX','tag_version':{'release':(1,1,2)},'prefix':''})
235253
Traceback (most recent call last):
236254
...
237255
SystemExit: 1
238256
"""
239-
vcs_info = versionfile.VERSION_INFO['vcs_info'] if not_really is None else not_really
257+
vcs_info = versionfile.VERSION_INFO['vcs_info'] if test_data is None else test_data
240258
tag_info = bump_version(vcs_info['tag_version'])
241259
print('== Next Version: %s' % pformat(tag_info))
242260
if vcs_info['dirt']:
243261
print("==> Auto bump aborted due to dirty git repository.")
244262
sys.exit(1)
245263
tag = vcs_info['prefix'] + render_bumped(**tag_info)
246264
print('== Tagging: %s' % tag)
247-
not_really or os.system('git tag ' + tag)
265+
test_data or os.system('git tag ' + tag)
248266

249267
class cmd_version_bump(Command):
250268
""" Version bump command.
@@ -257,9 +275,9 @@ class cmd_version_bump(Command):
257275
user_options = []
258276
boolean_options = []
259277

260-
def initialize_options(self): pass
261-
def finalize_options(self): pass
262-
def run(self): do_bump()
278+
initialize_options = testable_do_nothing
279+
finalize_options = testable_do_nothing
280+
run = do_bump
263281

264282

265283
#class cmd_update_versionfile(Command):
@@ -332,20 +350,22 @@ class cmd_build_py(_build_py):
332350
else:
333351
from distutils.command.sdist import sdist as _sdist
334352

335-
def add_to_sdist(base_dir):
353+
def add_to_sdist(self=None, base_dir=os.curdir, files=()):
336354
""" The custom part of the sdist command.
337355
338-
>>> add_to_sdist('/tmp')
356+
>>> add_to_sdist(base_dir='/tmp')
339357
== Rendering:
340358
...
341-
>>> add_to_sdist('/tmp')
359+
>>> add_to_sdist(base_dir='/tmp')
342360
== Rendering:
343361
...
344362
"""
345363
# now locate _version.py in the new base_dir directory
346364
# (remembering that it may be a hardlink) and replace it with an
347365
# updated value
348366

367+
self and _sdist.make_release_tree(self, base_dir, files)
368+
349369
source_versionfile, build_versionfile = read_setup_cfg()
350370
target_versionfile = os.path.join(base_dir, build_versionfile)
351371
static_versionfile = versionfile.render_static_file() if versionfile else 'test_content'
@@ -368,52 +388,60 @@ def add_to_sdist(base_dir):
368388
except OSError:
369389
print("=== Could not add %s to sdist!" % basename(__file__))
370390

391+
def sdist_run(self=None):
392+
""" A mere fake when run as a test... but 199% covered!
371393
372-
class cmd_sdist(_sdist):
394+
>>> sdist_run()
395+
"""
396+
if self: self.distribution.metadata.version = setup_versioning().get_version()
397+
if self: return _sdist.run(self)
373398

374-
def run(self):
375-
self.distribution.metadata.version = setup_versioning().get_version()
376-
return _sdist.run(self)
399+
class cmd_sdist(_sdist):
377400

378-
def make_release_tree(self, base_dir, files):
379-
_sdist.make_release_tree(self, base_dir, files)
380-
add_to_sdist(base_dir)
401+
run = sdist_run
402+
make_release_tree = add_to_sdist
381403

382404

383405
from distutils.command.upload import upload as _upload
384406

407+
def protected_upload(self=None):
408+
""" Allow only uploads with Python 3.
409+
I experienced problems earlier, when i uploaded packages built
410+
with python2. They containded .pyc files that made the distribution
411+
fail on python3 because of a bad magic number error. Since only I use
412+
this by now, i protect myself from this to happen again by this little
413+
mechanism. It may be removed or changed in the future.
385414
386-
class cmd_upload(_upload):
415+
>>> protected_upload()
416+
==> For backwards compatibility you should only upload packages built with Python 3 to PyPI.
417+
"""
418+
if self and sys.version_info.major == 3: return _upload.run(self)
419+
print('==> For backwards compatibility you should only upload packages built with Python 3 to PyPI.')
387420

421+
class cmd_upload(_upload):
388422
description="Do the normal upload, but prevent pushing with Python2."
389-
390-
def run(self):
391-
if sys.version_info.major == 3:
392-
return _upload.run(self)
393-
print('==> For backwards compatibility you should only upload packages built with Python 3 to PyPI.')
394-
sys.exit(1)
423+
run = protected_upload
395424

396425

397-
def publish_code(not_really=None):
426+
def publish_code(self=None):
398427
""" push git and its tags
399428
400-
>>> publish_code(not_really=True)
429+
>>> publish_code()
401430
=== git push
402431
=== pushing tags also
432+
433+
TODO: do not use os.system, there are better ways...
403434
"""
404435
print("=== git push")
405-
not_really or os.system('git push')
436+
self and os.system('git push')
406437
print("=== pushing tags also")
407-
not_really or os.system('git push --tags')
438+
self and os.system('git push --tags')
439+
if self: return cmd_upload.run(self)
408440

409441

410442
class cmd_release(cmd_upload):
411-
412443
description="Do the protected upload, but push git things first"
413-
414-
def run(self):
415-
publish_code()
416-
return cmd_upload.run(self)
444+
run = publish_code
417445

418446

419447

0 commit comments

Comments
 (0)