Support of WG4 OpenFronthaul Management-Plane VES
[scp/oam/modeling.git] / data-model / tools / rename-by-revision.py
1 #!/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 #
24 from pyang import repository, context
25 import argparse
26 import os
27
28 parser = argparse.ArgumentParser(description='rename a list of yang model files by the revision information')
29 parser.add_argument('yang_files', type=str, nargs='+',
30                     help='list of yang files')
31 args = parser.parse_args()
32
33 repos = repository.FileRepository(".")
34 for 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:
48                 os.rename(fname,targ_dir+'/'+targ_module+'@'+r+'.yang')
49             break