nml_git_version.diff

Return to the initial folder
  1. diff --git a/nml/version_info.py b/nml/version_info.py
  2. index 2cc8db9..870b826 100644
  3. --- a/nml/version_info.py
  4. +++ b/nml/version_info.py
  5. @@ -20,17 +20,23 @@ with NML; if not, write to the Free Software Foundation, Inc.,
  6.  
  7.  import subprocess, os
  8.  
  9. -def get_child_output(cmd):
  10. +def get_child_output(cmd, env=None, stderr=None):
  11.      """
  12.      Run a child process, and collect the generated output.
  13.  
  14.      @param cmd: Command to execute.
  15.      @type  cmd: C{list} of C{str}
  16.  
  17. +    @param env: Environment
  18. +    @type  env: C{dict}
  19. +
  20. +    @param stderr: Pipe destination for stderr
  21. +    @type  stderr: file object
  22. +
  23.      @return: Generated output of the command, split on whitespace.
  24.      @rtype:  C{list} of C{str}
  25.      """
  26. -    return subprocess.check_output(cmd, universal_newlines = True).split()
  27. +    return subprocess.check_output(cmd, universal_newlines = True, env=env, stderr=stderr).split()
  28.  
  29.  
  30.  def get_hg_version():
  31. @@ -66,6 +72,49 @@ def get_hg_version():
  32.              version = "v{}{}:{} from {}".format(cversion, modified, hash, ctimes[2].split("'", 1)[0])
  33.      return version
  34.  
  35. +def get_git_version():
  36. +    # method adopted shamelessly from OpenTTD's findversion.sh
  37. +    path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  38. +    version = ''
  39. +    env = dict(os.environ, LC_ALL='C')
  40. +    if os.path.isdir(os.path.join(path,'.git')):
  41. +        # Refresh the index to make sure file stat info is in sync
  42. +        try:
  43. +            get_child_output("git -C {} update-index --refresh".format(path).split(), env=env)
  44. +        except:
  45. +            pass
  46. +
  47. +        # Look for modifications
  48. +        try:
  49. +            modified  = (len(get_child_output("git -C {} diff-index HEAD".format(path).split(), env=env)) > 0)
  50. +            changeset = get_child_output("git -C {} rev-parse --verify HEAD".format(path).split(), env=env)[0][:8]
  51. +            isodate   = get_child_output("git -C {} show -s --pretty=%ci HEAD".format(path).split(), env=env)[0]
  52. +            branch    = get_child_output("git -C {} symbolic-ref -q HEAD".format(path).split(), env=env)[0].split('/')[-1]
  53. +        except OSError as e:
  54. +            print("Git checkout found but cannot determine its version. Error({0}): {1}".format(e.errno, e.strerror))
  55. +            return version
  56. +
  57. +        # We may fail to find a tag - but that is fine
  58. +        tag = []
  59. +        try:
  60. +            # pipe stderr to /dev/null or it will show in the console
  61. +            tag = get_child_output("git -C {} name-rev --name-only --tags --no-undefined HEAD".format(path).split(), env=env, stderr=subprocess.DEVNULL)[0]
  62. +        except (OSError, subprocess.CalledProcessError):
  63. +            pass
  64. +
  65. +        # Compose the actual version string
  66. +        if len(tag) > 0:
  67. +            version = tag[0]
  68. +        elif branch == "master":
  69. +            version = isodate + "-g" + changeset
  70. +        else:
  71. +            version = isodate + "-" + branch + "-g" + changeset
  72. +
  73. +        if modified:
  74. +            version += "M"
  75. +
  76. +    return version
  77. +
  78.  def get_lib_versions():
  79.      versions = {}
  80.      #PIL
  81. @@ -90,7 +139,7 @@ def get_lib_versions():
  82.  
  83.  def get_nml_version():
  84.      # first try whether we find an nml repository. Use that version, if available
  85. -    version = get_hg_version()
  86. +    version = get_git_version()
  87.      if version:
  88.          return version
  89.      # no repository was found. Return the version which was saved upon built
  90.  

Return to top