/tortoisehg/util/version.py

https://bitbucket.org/tortoisehg/hgtk/ · Python · 73 lines · 57 code · 10 blank · 6 comment · 17 complexity · 36c36535ec649a2370ee2a07e2291b35 MD5 · raw file

  1. # version.py - TortoiseHg version
  2. #
  3. # Copyright 2009 Steve Borho <steve@borho.org>
  4. #
  5. # This software may be used and distributed according to the terms of the
  6. # GNU General Public License version 2, incorporated herein by reference.
  7. import os
  8. from mercurial import ui, hg, commands, error
  9. from tortoisehg.util.i18n import _
  10. def liveversion():
  11. 'Attempt to read the version from the live repository'
  12. utilpath = os.path.dirname(os.path.realpath(__file__))
  13. thgpath = os.path.dirname(os.path.dirname(utilpath))
  14. if not os.path.isdir(os.path.join(thgpath, '.hg')):
  15. raise error.RepoError(_('repository %s not found') % thgpath)
  16. u = ui.ui()
  17. repo = hg.repository(u, path=thgpath)
  18. u.pushbuffer()
  19. commands.identify(u, repo, id=True, tags=True)
  20. l = u.popbuffer().split()
  21. while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
  22. l.pop()
  23. if len(l) > 1: # tag found
  24. version = l[-1]
  25. if l[0].endswith('+'): # propagate the dirty status to the tag
  26. version += '+'
  27. elif len(l) == 1: # no tag found
  28. u.pushbuffer()
  29. commands.parents(u, repo, template='{latesttag}+{latesttagdistance}-')
  30. version = u.popbuffer() + l[0]
  31. return repo[None].branch(), version
  32. def version():
  33. try:
  34. branch, version = liveversion()
  35. return version
  36. except:
  37. pass
  38. try:
  39. import __version__
  40. return __version__.version
  41. except ImportError:
  42. return _('unknown')
  43. def package_version():
  44. try:
  45. branch, version = liveversion()
  46. extra = None
  47. if '+' in version:
  48. version, extra = version.split('+', 1)
  49. v = [int(x) for x in version.split('.')]
  50. while len(v) < 3:
  51. v.append(0)
  52. major, minor, periodic = v
  53. if extra != None:
  54. tagdistance = int(extra.split('-', 1)[0])
  55. periodic *= 10000
  56. if branch == 'default':
  57. periodic += tagdistance + 5000
  58. else:
  59. periodic += tagdistance + 1000
  60. return '.'.join([str(x) for x in (major, minor, periodic)])
  61. except:
  62. pass
  63. return _('unknown')