commodconverter
 All Classes Namespaces Files Functions Variables Macros
rename.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 import os
3 import sys
4 import shutil
5 import subprocess
6 from glob import glob
7 import argparse as ap
8 
9 absexpanduser = lambda x: os.path.abspath(os.path.expanduser(x))
10 
11 def main():
12  description = ("This script renames all instances of stub, Stub, and STUB "
13  "to the value given on the command line.")
14  parser = ap.ArgumentParser(description=description)
15  parser.add_argument('name', help="replacement for stub", default='name')
16  ns = parser.parse_args()
17 
18  low, cap, upp = ns.name.lower(), ns.name.capitalize(), ns.name.upper()
19  stublow, stubcap, stubupp = 'stub', 'Stub', 'STUB'
20  mvfiles = glob('src/stub*')
21  for f in mvfiles:
22  d = os.path.dirname(f)
23  name = os.path.basename(f)
24  newname = os.path.join(d, name.replace('stub', low))
25  subprocess.call(['git','mv',f,newname])
26  subprocess.call(['git','commit','-m',
27  '"moves stubfile '.join(name).join('->').join(newname).join('"')])
28 
29  files = ['CMakeLists.txt', 'input/example.xml'] + glob('src/*')
30  for f in files:
31  with open(f, 'r') as inp:
32  s = inp.read()
33  s = s.replace('stubs', low)
34  s = s.replace(stublow, low)
35  s = s.replace(stubcap, cap)
36  s = s.replace(stubupp, upp)
37  os.remove(f)
38  d = os.path.dirname(f)
39  name = os.path.basename(f)
40  with open(os.path.join(d, name.replace('stub', low)), 'w') as out:
41  out.write(s)
42 
43 if __name__ == "__main__":
44  main()
def main
Definition: rename.py:11