1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# Copyright 2019 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import re
import fileinput
def read_vyatta_versions(config_file):
config_file_versions = {}
with open(config_file, 'r') as config_file_handle:
for config_line in config_file_handle:
if re.match(r'/\* === vyatta-config-version:.+=== \*/$', config_line):
if not re.match(r'/\* === vyatta-config-version:\s+"([\w,-]+@\d+:)+([\w,-]+@\d+)"\s+=== \*/$', config_line):
raise ValueError("malformed configuration string: "
"{}".format(config_line))
for pair in re.findall(r'([\w,-]+)@(\d+)', config_line):
config_file_versions[pair[0]] = int(pair[1])
return config_file_versions
def read_vyos_versions(config_file):
config_file_versions = {}
with open(config_file, 'r') as config_file_handle:
for config_line in config_file_handle:
if re.match(r'// vyos-config-version:.+', config_line):
if not re.match(r'// vyos-config-version:\s+"([\w,-]+@\d+:)+([\w,-]+@\d+)"\s*', config_line):
raise ValueError("malformed configuration string: "
"{}".format(config_line))
for pair in re.findall(r'([\w,-]+)@(\d+)', config_line):
config_file_versions[pair[0]] = int(pair[1])
return config_file_versions
def remove_versions(config_file):
"""
Remove old version string.
"""
for line in fileinput.input(config_file, inplace=True):
if re.match(r'/\* Warning:.+ \*/$', line):
continue
if re.match(r'/\* === vyatta-config-version:.+=== \*/$', line):
continue
if re.match(r'/\* Release version:.+ \*/$', line):
continue
if re.match('// vyos-config-version:.+', line):
continue
if re.match('// Warning:.+', line):
continue
if re.match('// Release version:.+', line):
continue
sys.stdout.write(line)
def format_versions_string(config_versions):
cfg_keys = list(config_versions.keys())
cfg_keys.sort()
component_version_strings = []
for key in cfg_keys:
cfg_vers = config_versions[key]
component_version_strings.append('{}@{}'.format(key, cfg_vers))
separator = ":"
component_version_string = separator.join(component_version_strings)
return component_version_string
def write_vyatta_versions_foot(config_file, component_version_string,
os_version_string):
if config_file:
with open(config_file, 'a') as config_file_handle:
config_file_handle.write('/* Warning: Do not remove the following line. */\n')
config_file_handle.write('/* === vyatta-config-version: "{}" === */\n'.format(component_version_string))
config_file_handle.write('/* Release version: {} */\n'.format(os_version_string))
else:
sys.stdout.write('/* Warning: Do not remove the following line. */\n')
sys.stdout.write('/* === vyatta-config-version: "{}" === */\n'.format(component_version_string))
sys.stdout.write('/* Release version: {} */\n'.format(os_version_string))
def write_vyos_versions_foot(config_file, component_version_string,
os_version_string):
if config_file:
with open(config_file, 'a') as config_file_handle:
config_file_handle.write('// Warning: Do not remove the following line.\n')
config_file_handle.write('// vyos-config-version: "{}"\n'.format(component_version_string))
config_file_handle.write('// Release version: {}\n'.format(os_version_string))
else:
sys.stdout.write('// Warning: Do not remove the following line.\n')
sys.stdout.write('// vyos-config-version: "{}"\n'.format(component_version_string))
sys.stdout.write('// Release version: {}\n'.format(os_version_string))
|