blob: 3a076d4378422c82bfdcd2bec278000a066446ed [file] [log] [blame]
DylanB95EST63132ce2021-12-14 16:34:38 +00001# ============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
20import csv
lukegleeson0cbc4482022-02-10 10:58:29 +000021import datetime
DylanB95EST63132ce2021-12-14 16:34:38 +000022import hashlib
23import sys
lukegleeson0cbc4482022-02-10 10:58:29 +000024import re
DylanB95EST63132ce2021-12-14 16:34:38 +000025
26yang_source = ''
27checksum = ''
lukegleeson0cbc4482022-02-10 10:58:29 +000028regexForModuleName = '(?<=module)(.*)(?={)'
29regexForRevision = '(?<=revision)(.*)(?={)'
DylanB95EST63132ce2021-12-14 16:34:38 +000030
lukegleeson0cbc4482022-02-10 10:58:29 +000031def main():
32 for yang_source in sys.argv[1:]:
33 checksum = hashlib.sha256(str(yang_source).encode()).hexdigest()
DylanB95EST63132ce2021-12-14 16:34:38 +000034
lukegleeson0cbc4482022-02-10 10:58:29 +000035 with open('changelog/db/changes/data/yang-models/' + yang_source + '.yang', 'r') as content:
36 dmiRegistry = content.read()
DylanB95EST63132ce2021-12-14 16:34:38 +000037
lukegleeson0cbc4482022-02-10 10:58:29 +000038 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
66main()