blob: b0836c7c0de84eddaf5ee5a3f1bcad3812ab2095 [file] [log] [blame]
demx8as63bcfd682021-02-27 08:53:05 +01001#!/usr/bin/env python
2#
3# Copyright 2016 Einar Nilsen-Nygaard [https://gist.github.com/einarnn]
4# Update Copyright 2021 highstreet technologies GmbH
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18# The script renames yang modules with filename with schema "<module-name>.yang"
19# to filenames with schema name "<module-name>@<revision>.yang" as used by
20# netconf-server and netconf-client impelementstions to distingligh the
21# yang modules already by its filename avoiding parsing the yang module getting
22# its revision.
23#
24from pyang import repository, context
25import argparse
26import os
27
28parser = argparse.ArgumentParser(description='rename a list of yang model files by the revision information')
29parser.add_argument('yang_files', type=str, nargs='+',
30 help='list of yang files')
31args = parser.parse_args()
32
33repos = repository.FileRepository(".")
34for fname in args.yang_files:
35 targ_module = os.path.basename(fname).split(".")[0]
36 if '@' in targ_module:
37 targ_module = targ_module.split('@')[0]
38 ctx = context.Context(repos)
39 fd = open(fname, 'r')
40 text = fd.read()
41 ctx.add_module(fname, text)
42 for ((m,r), v) in ctx.modules.items():
43 if m==targ_module:
44 targ_dir = os.path.dirname(fname)
45 if len(targ_dir)==0:
46 os.rename(fname,targ_module+'@'+r+'.yang')
47 else:
demx8as6203a1a12021-02-28 13:47:37 +010048 os.rename(fname,targ_dir+'/'+targ_module+'@'+r+'.yang')
demx8as63bcfd682021-02-27 08:53:05 +010049 break