git_version_detection.diff

Return to the initial folder
  1. commit 6461c4548e2fa8992fa3b55738239f29df3f2d05
  2. Author: planetmaker <planetmaker@openttd.org>
  3. Date:   Mon Oct 15 17:47:20 2018 +0200
  4.  
  5.     Add: Version detection with git
  6.  
  7. diff --git a/.gitignore b/.gitignore
  8. new file mode 100644
  9. index 0000000..596f61f
  10. --- /dev/null
  11. +++ b/.gitignore
  12. @@ -0,0 +1,28 @@
  13. +nml/__pycache__/
  14. +nml/actions/__pycache__/
  15. +nml/ast/__pycache__/
  16. +nml/expression/__pycache__/
  17. +
  18. +^build/
  19. +.*buildout.*
  20. +\.install
  21. +egg
  22. +~$
  23. +\.orig$
  24. +
  25. +syntax: glob
  26. +*.pyc
  27. +*.pyd
  28. +*.so
  29. +regression/.nmlcache
  30. +regression/output/
  31. +regression/output2/
  32. +regression/nml_output/
  33. +
  34. +parser.out
  35. +parsetab.py
  36. +
  37. +nml/__version__.py
  38. +
  39. +MANIFEST
  40. +dist/*
  41. diff --git a/nml/version_info.py b/nml/version_info.py
  42. index 2cc8db9..590e8da 100644
  43. --- a/nml/version_info.py
  44. +++ b/nml/version_info.py
  45. @@ -66,6 +66,41 @@ def get_hg_version():
  46.              version = "v{}{}:{} from {}".format(cversion, modified, hash, ctimes[2].split("'", 1)[0])
  47.      return version
  48.  
  49. +
  50. +def get_git_version():
  51. +    path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  52. +    version = ''
  53. +    if os.path.isdir(os.path.join(path,'.git')):
  54. +        # we want to return to where we were. So save the old path
  55. +        try:
  56. +            version_list = get_child_output(['git', '-C', path, 'describe', '--tags', '--abbrev=8'])
  57. +        except OSError as e:
  58. +            print("Git checkout found but cannot determine its version. Error({0}): {1}".format(e.errno, e.strerror))
  59. +            return version
  60. +
  61. +        # We have a modified repo state, if the status returns some modified files
  62. +        modified = (subprocess.check_output("git status --short | wc -l",
  63. +                                            shell = True,
  64. +                                            universal_newlines = True
  65. +                                            ).split() != '0')
  66. +
  67. +        # We have a release, if only the tag is reported back
  68. +        release = len(version.split('-')) == 1
  69. +
  70. +        if modified or not release:
  71. +            # Get the date of the commit of the current NML version in days since January 1st 2000
  72. +            ctimes = get_child_output(["git", "-C", path, "show", "--pretty=%cI", "HEAD"])[0].split('T')
  73. +            if modified:
  74. +                mod_str = 'M'
  75. +            else:
  76. +                mod_str = ''
  77. +            # Combine the version string
  78. +            version = "{}{} from {}".format(version_list[0], mod_str, ctimes[0])
  79. +        else:
  80. +            version = version_list[0]
  81. +    return version
  82. +
  83. +
  84.  def get_lib_versions():
  85.      versions = {}
  86.      #PIL
  87. @@ -90,7 +125,7 @@ def get_lib_versions():
  88.  
  89.  def get_nml_version():
  90.      # first try whether we find an nml repository. Use that version, if available
  91. -    version = get_hg_version()
  92. +    version = get_git_version()
  93.      if version:
  94.          return version
  95.      # no repository was found. Return the version which was saved upon built
  96.  

Return to top