version.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. Current Fabric version constant plus version pretty-print method.
  3. This functionality is contained in its own module to prevent circular import
  4. problems with ``__init__.py`` (which is loaded by setup.py during installation,
  5. which in turn needs access to this version information.)
  6. """
  7. from subprocess import Popen, PIPE
  8. from os.path import abspath, dirname
  9. VERSION = (1, 13, 2, 'final', 0)
  10. def git_sha():
  11. loc = abspath(dirname(__file__))
  12. try:
  13. p = Popen(
  14. "cd \"%s\" && git log -1 --format=format:%%h" % loc,
  15. shell=True,
  16. stdout=PIPE,
  17. stderr=PIPE
  18. )
  19. return p.communicate()[0]
  20. # OSError occurs on Unix-derived platforms lacking Popen's configured shell
  21. # default, /bin/sh. E.g. Android.
  22. except OSError:
  23. return None
  24. def get_version(form='short'):
  25. """
  26. Return a version string for this package, based on `VERSION`.
  27. Takes a single argument, ``form``, which should be one of the following
  28. strings:
  29. * ``branch``: just the major + minor, e.g. "0.9", "1.0".
  30. * ``short`` (default): compact, e.g. "0.9rc1", "0.9.0". For package
  31. filenames or SCM tag identifiers.
  32. * ``normal``: human readable, e.g. "0.9", "0.9.1", "0.9 beta 1". For e.g.
  33. documentation site headers.
  34. * ``verbose``: like ``normal`` but fully explicit, e.g. "0.9 final". For
  35. tag commit messages, or anywhere that it's important to remove ambiguity
  36. between a branch and the first final release within that branch.
  37. * ``all``: Returns all of the above, as a dict.
  38. """
  39. # Setup
  40. versions = {}
  41. branch = "%s.%s" % (VERSION[0], VERSION[1])
  42. tertiary = VERSION[2]
  43. type_ = VERSION[3]
  44. final = (type_ == "final")
  45. type_num = VERSION[4]
  46. firsts = "".join([x[0] for x in type_.split()])
  47. # Branch
  48. versions['branch'] = branch
  49. # Short
  50. v = branch
  51. if (tertiary or final):
  52. v += "." + str(tertiary)
  53. if not final:
  54. v += firsts
  55. if type_num:
  56. v += str(type_num)
  57. versions['short'] = v
  58. # Normal
  59. v = branch
  60. if tertiary:
  61. v += "." + str(tertiary)
  62. if not final:
  63. if type_num:
  64. v += " " + type_ + " " + str(type_num)
  65. else:
  66. v += " pre-" + type_
  67. versions['normal'] = v
  68. # Verbose
  69. v = branch
  70. if tertiary:
  71. v += "." + str(tertiary)
  72. if not final:
  73. if type_num:
  74. v += " " + type_ + " " + str(type_num)
  75. else:
  76. v += " pre-" + type_
  77. else:
  78. v += " final"
  79. versions['verbose'] = v
  80. try:
  81. return versions[form]
  82. except KeyError:
  83. if form == 'all':
  84. return versions
  85. raise TypeError('"%s" is not a valid form specifier.' % form)
  86. __version__ = get_version('short')
  87. if __name__ == "__main__":
  88. print(get_version('all'))