Last active
January 12, 2016 15:35
-
-
Save einarnn/6b181636f8971c7411f8 to your computer and use it in GitHub Desktop.
Rename a list of yang files with revision numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pyang | |
import argparse | |
import os | |
parser = argparse.ArgumentParser(description='rename a list of yang model files by the revision information') | |
parser.add_argument('yang_files', type=str, nargs='+', | |
help='list of yang files') | |
args = parser.parse_args() | |
repos = pyang.FileRepository(".") | |
for fname in args.yang_files: | |
targ_module = os.path.basename(fname).split(".")[0] | |
if '@' in targ_module: | |
targ_module = targ_module.split('@')[0] | |
ctx = pyang.Context(repos) | |
fd = open(fname, 'r') | |
text = fd.read() | |
ctx.add_module(fname, text) | |
for ((m,r), v) in ctx.modules.iteritems(): | |
if m==targ_module: | |
targ_dir = os.path.dirname(fname) | |
if len(targ_dir)==0: | |
os.rename(fname,targ_module+'@'+r+'.yang') | |
else: | |
os.rename(fname,targ_dir+'/'+targ_module+'@'+r+'.yang') | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment