Update dmi-registry yang to support cmHandle State
[cps.git] / cps-ri / src / main / resources / yangResourceCsvGenerator.py
1 #  ============LICENSE_START=======================================================
2 #  Copyright (C) 2022 Nordix Foundation
3 #  ================================================================================
4 #  Licensed under the Apache License, Version 2.0 (the "License");
5 #  you may not use this file except in compliance with the License.
6 #  You may obtain a copy of the License at
7 #
8 #        http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #  Unless required by applicable law or agreed to in writing, software
11 #  distributed under the License is distributed on an "AS IS" BASIS,
12 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #  See the License for the specific language governing permissions and
14 #  limitations under the License.
15 #
16 #  SPDX-License-Identifier: Apache-2.0
17 #  ============LICENSE_END=========================================================
18
19
20 import csv
21 import datetime
22 import hashlib
23 import sys
24 import re
25
26 yang_source = ''
27 checksum = ''
28 regexForModuleName = '(?<=module)(.*)(?={)'
29 regexForRevision = '(?<=revision)(.*)(?={)'
30
31 def main():
32     for yang_source in sys.argv[1:]:
33         checksum = hashlib.sha256(str(yang_source).encode()).hexdigest()
34
35         with open('changelog/db/changes/data/yang-models/' + yang_source + '.yang', 'r') as content:
36             dmiRegistry = content.read()
37
38         try:
39             latestRevision = re.search(regexForRevision, dmiRegistry).group(0).replace('\"','').strip()
40         except:
41             print("ERROR IN in yangResourceCsvGenerator.py: Unable to find revision for " + yang_source + '.yang')
42
43         try:
44             module_name = re.search(regexForModuleName, dmiRegistry).group(0).strip()
45         except:
46             print("ERROR IN in yangResourceCsvGenerator.py: Unable to find module name for " + yang_source + '.yang')
47
48         #If true, file was created after module_name and revision columns were added to yang-resources table
49         writeWithModuleNameAndRevision = yang_source != 'dmi-registry@2021-12-13'
50
51         try:
52             # open the file in the write mode
53             with open('changelog/db/changes/data/dmi/generated-csv/generated_yang_resource_' + yang_source + '.csv', 'w', newline='') \
54                     as file:
55                 writer = csv.writer(file, delimiter='|')
56                 if(writeWithModuleNameAndRevision):
57                     writer.writerow(["name", "content", "checksum", "module_name", "revision"])
58                     writer.writerow([yang_source + '.yang', dmiRegistry, checksum, module_name, latestRevision])
59                 else:
60                     writer.writerow(["name", "content", "checksum"])
61                     writer.writerow([yang_source + '.yang', dmiRegistry, checksum])
62         except:
63             print("ERROR IN in yangResourceCsvGenerator.py: Unable to write to changelog/db/changes/data/dmi/generated-csv/generated_yang_resource_" + yang_source + ".csv")
64
65
66 main()