-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdbwalk.py
84 lines (76 loc) · 2.7 KB
/
cdbwalk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''Script that walks over a CDB Pan checkout, performs actions on
templates that declare them, and commits any changes, if needed.
TODO:
- Configure which actions map to which headers without modifying the
code.
- Make it generic enough to work with CDB and SCDB
Author: Luis Fernando Muñoz Mejías
'''
import sys
import os
from pan_file import pan_file, pan_exception
from optparse import OptionParser
import subprocess
from egroup2pan import egroup_string_to_pan_variable as egroup2pan
from cdb import cdb_error, do_commit
import yaml
def actions(filename):
y = yaml.load(file(filename))
d = dict()
for header, action in y.items():
try:
m = sys.modules[action['module']]
except KeyError:
m = __import__(action['module'])
d[header] = m.__dict__[action['callable']]
return d
class log(object):
'''Log object for displaying information'''
def __init__(self, verbose=False):
self.verbose = verbose
def __call__(self, *kwargs):
if self.verbose:
print ' '.join(kwargs)
def parse_opts():
'''Parses the options from the command line'''
o = OptionParser()
o.add_option("-d", "--dir", dest="top",
help="Top level of the CDB checkout")
o.add_option("-e", "--extension", dest="extension",
help="Extension of the Pan templates (defaults to %default)",
default=".tpl")
o.add_option("-c", "--commit", dest="commit",
help="Commit to CDB any changes",
action="store_true")
o.add_option("-v", "--verbose", dest="verbose",
help="Verbose",
action="store_true")
o.add_option("--cfg", "--config-file", dest="cfg",
help="Configuration file mapping headers with code to be executed")
(opts, args) = o.parse_args()
return opts
if __name__ == '__main__':
opts = parse_opts()
lg = log(opts.verbose)
changed = []
actions = init_actions()
for root, dirs, files in os.walk(opts.top):
for rm in 'CVS', '.svn', '.git':
if rm in dirs:
dirs.remove(rm)
for f in files:
if f.endswith(opts.extension):
try:
fullpath = os.path.join(root, f)
lg("Processing template:", fullpath)
p = pan_file(fullpath, **actions)
p.process()
if p.write_file():
lg("Modified:", fullpath)
changed.append(fullpath)
except pan_exception, e:
print e
if changed and opts.commit:
do_commit(*changed)