OpenVPN
git-version.py
Go to the documentation of this file.
1 #
2 # OpenVPN -- An application to securely tunnel IP networks
3 # over a single UDP port, with support for SSL/TLS-based
4 # session authentication and key exchange,
5 # packet encryption, packet authentication, and
6 # packet compression.
7 #
8 # Copyright (C) 2022-2023 OpenVPN Inc <sales@openvpn.net>
9 # Copyright (C) 2022-2022 Lev Stipakov <lev@lestisoftware.fi>
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License version 2
13 # as published by the Free Software Foundation.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #
24 
25 # Usage: ./git-version.py [directory]
26 # Find a good textual representation of the git commit currently checked out.
27 # Make that representation available as CONFIGURE_GIT_REVISION in
28 # <directory>/config-version.h.
29 # It will prefer a tag name if it is checked out exactly, otherwise will use
30 # the branch name. 'none' if no branch is checked out (detached HEAD).
31 # This is used to enhance the output of openvpn --version with Git information.
32 
33 import os
34 import sys
35 import subprocess
36 
37 def run_command(args):
38  sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
39  o, _ = sp.communicate()
40  return o.decode("utf-8")[:-1]
41 
43  commit_id = run_command(["git", "rev-parse", "--short=16", "HEAD"])
44  if not commit_id:
45  raise
46  branch = run_command(["git", "describe", "--exact-match"])
47  if not branch:
48  # this returns an array like ["master"] or ["release", "2.6"]
49  branch = run_command(["git", "rev-parse", "--symbolic-full-name", "HEAD"]).split("/")[2:]
50  if not branch:
51  branch = ["none"]
52  branch = "/" .join(branch) # handle cases like release/2.6
53 
54  return branch, commit_id
55 
56 def main():
57  try:
58  branch, commit_id = get_branch_commit_id()
59  except:
60  branch, commit_id = "unknown", "unknown"
61 
62  prev_content = ""
63 
64  name = os.path.join("%s" % (sys.argv[1] if len(sys.argv) > 1 else "."), "config-version.h")
65  try:
66  with open(name, "r") as f:
67  prev_content = f.read()
68  except:
69  # file doesn't exist
70  pass
71 
72  content = "#define CONFIGURE_GIT_REVISION \"%s/%s\"\n" % (branch, commit_id)
73  content += "#define CONFIGURE_GIT_FLAGS \"\"\n"
74 
75  if prev_content != content:
76  print("Writing %s" % name)
77  with open(name, "w") as f:
78  f.write(content)
79  else:
80  print("Content of %s hasn't changed" % name)
81 
82 if __name__ == "__main__":
83  main()
git-version.main
def main()
Definition: git-version.py:56
git-version.run_command
def run_command(args)
Definition: git-version.py:37
git-version.get_branch_commit_id
def get_branch_commit_id()
Definition: git-version.py:42