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