_version.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Definition of the version number
  2. import os
  3. from io import open as io_open
  4. __all__ = ["__version__"]
  5. # major, minor, patch, -extra
  6. version_info = 4, 48, 2
  7. # Nice string for the version
  8. __version__ = '.'.join(map(str, version_info))
  9. # auto -extra based on commit hash (if not tagged as release)
  10. scriptdir = os.path.dirname(__file__)
  11. gitdir = os.path.abspath(os.path.join(scriptdir, "..", ".git"))
  12. if os.path.isdir(gitdir): # pragma: nocover
  13. try:
  14. extra = None
  15. # Open config file to check if we are in tqdm project
  16. with io_open(os.path.join(gitdir, "config"), 'r') as fh_config:
  17. if 'tqdm' in fh_config.read():
  18. # Open the HEAD file
  19. with io_open(os.path.join(gitdir, "HEAD"), 'r') as fh_head:
  20. extra = fh_head.readline().strip()
  21. # in a branch => HEAD points to file containing last commit
  22. if 'ref:' in extra:
  23. # reference file path
  24. ref_file = extra[5:]
  25. branch_name = ref_file.rsplit('/', 1)[-1]
  26. ref_file_path = os.path.abspath(os.path.join(
  27. gitdir, ref_file))
  28. # check that we are in git folder
  29. # (by stripping the git folder from the ref file path)
  30. if os.path.relpath(ref_file_path, gitdir).replace(
  31. '\\', '/') != ref_file:
  32. # out of git folder
  33. extra = None
  34. else:
  35. # open the ref file
  36. with io_open(ref_file_path, 'r') as fh_branch:
  37. commit_hash = fh_branch.readline().strip()
  38. extra = commit_hash[:8]
  39. if branch_name != "master":
  40. extra += '.' + branch_name
  41. # detached HEAD mode, already have commit hash
  42. else:
  43. extra = extra[:8]
  44. # Append commit hash (and branch) to version string if not tagged
  45. if extra is not None:
  46. with io_open(os.path.join(gitdir, "refs", "tags",
  47. 'v' + __version__)) as fdv:
  48. if fdv.readline().strip()[:8] != extra[:8]:
  49. __version__ += '-' + extra
  50. except Exception as e:
  51. if "No such file" in str(e):
  52. __version__ += "-git.UNKNOWN"
  53. else:
  54. raise