DylanB95EST | 63132ce | 2021-12-14 16:34:38 +0000 | [diff] [blame] | 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 |
lukegleeson | 0cbc448 | 2022-02-10 10:58:29 +0000 | [diff] [blame] | 21 | import datetime |
DylanB95EST | 63132ce | 2021-12-14 16:34:38 +0000 | [diff] [blame] | 22 | import hashlib |
| 23 | import sys |
lukegleeson | 0cbc448 | 2022-02-10 10:58:29 +0000 | [diff] [blame] | 24 | import re |
DylanB95EST | 63132ce | 2021-12-14 16:34:38 +0000 | [diff] [blame] | 25 | |
| 26 | yang_source = '' |
| 27 | checksum = '' |
lukegleeson | 0cbc448 | 2022-02-10 10:58:29 +0000 | [diff] [blame] | 28 | regexForModuleName = '(?<=module)(.*)(?={)' |
| 29 | regexForRevision = '(?<=revision)(.*)(?={)' |
DylanB95EST | 63132ce | 2021-12-14 16:34:38 +0000 | [diff] [blame] | 30 | |
lukegleeson | 0cbc448 | 2022-02-10 10:58:29 +0000 | [diff] [blame] | 31 | def main(): |
| 32 | for yang_source in sys.argv[1:]: |
| 33 | checksum = hashlib.sha256(str(yang_source).encode()).hexdigest() |
DylanB95EST | 63132ce | 2021-12-14 16:34:38 +0000 | [diff] [blame] | 34 | |
lukegleeson | 0cbc448 | 2022-02-10 10:58:29 +0000 | [diff] [blame] | 35 | with open('changelog/db/changes/data/yang-models/' + yang_source + '.yang', 'r') as content: |
| 36 | dmiRegistry = content.read() |
DylanB95EST | 63132ce | 2021-12-14 16:34:38 +0000 | [diff] [blame] | 37 | |
lukegleeson | 0cbc448 | 2022-02-10 10:58:29 +0000 | [diff] [blame] | 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() |