OpenVPN
parse-version.m4.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-2024 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: ./parse-version.m4.py m4file [directory]
26 # Read <m4file>, extract all lines looking like M4 define(), and translate
27 # them into CMake style set(). Those are then written out to file
28 # <directory>/version.cmake.
29 # Intended to be used on top-level version.m4 file.
30 
31 import os
32 import re
33 import sys
34 
35 def main():
36  assert len(sys.argv) > 1
37  version_path = sys.argv[1]
38  output = []
39  with open(version_path, 'r') as version_file:
40  for line in version_file:
41  match = re.match(r'[ \t]*define\(\[(.*)\],[ \t]*\[(.*)\]\)[ \t]*', line)
42  if match is not None:
43  output.append(match.expand(r'set(\1 \2)'))
44  out_path = os.path.join("%s" % (sys.argv[2] if len(sys.argv) > 2 else "."), "version.cmake")
45 
46  prev_content = ""
47  try:
48  with open(out_path, "r") as out_file:
49  prev_content = out_file.read()
50  except:
51  # file doesn't exist
52  pass
53 
54  content = "\n".join(output) + "\n"
55  if prev_content != content:
56  print("Writing %s" % out_path)
57  with open(out_path, "w") as out_file:
58  out_file.write(content)
59  else:
60  print("Content of %s hasn't changed" % out_path)
61 
62 if __name__ == "__main__":
63  main()
parse-version.main
def main()
Definition: parse-version.m4.py:35