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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=========================================================
28 regexForModuleName = '(?<=module)(.*)(?={)'
29 regexForRevision = '(?<=revision)(.*)(?={)'
32 for yang_source in sys.argv[1:]:
33 checksum = hashlib.sha256(str(yang_source).encode()).hexdigest()
35 with open('changelog/db/changes/data/yang-models/' + yang_source + '.yang', 'r') as content:
36 dmiRegistry = content.read()
39 latestRevision = re.search(regexForRevision, dmiRegistry).group(0).replace('\"','').strip()
41 print("ERROR IN in yangResourceCsvGenerator.py: Unable to find revision for " + yang_source + '.yang')
44 module_name = re.search(regexForModuleName, dmiRegistry).group(0).strip()
46 print("ERROR IN in yangResourceCsvGenerator.py: Unable to find module name for " + yang_source + '.yang')
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'
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='') \
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])
60 writer.writerow(["name", "content", "checksum"])
61 writer.writerow([yang_source + '.yang', dmiRegistry, checksum])
63 print("ERROR IN in yangResourceCsvGenerator.py: Unable to write to changelog/db/changes/data/dmi/generated-csv/generated_yang_resource_" + yang_source + ".csv")