7 Author : Valentin Kuznetsov <vkuznet@gmail.com> 8 Description: AbstractGenerator class provides basic functionality 9 to generate CMSSW class from given template 19 from FWCore.Skeletons.utils
import parse_word, functor, user_info, tree
23 AbstractPkg takes care how to generate code from template/PKG 24 package area. The PKG can be any directory which may include 25 any types of files, e.g. C++ (.cc), python (.py), etc. 26 This class relies on specific logic which we outline here: 28 - each template may use tags defined with double underscores 29 enclosure, e.g. __class__, __record__, etc. 30 - each template may have example tags, such tags should 31 start with @example_. While processing template user may 32 choose to strip them off or keep the code behind those tags 33 - in addition user may specify pure python code which can 34 operate with user defined tags. This code snipped should 35 be enclosed with #python_begin and #python_end lines 36 which declares start and end of python block 44 self.
pname = self.config.get(
'pname',
None)
45 self.
tmpl = self.config.get(
'tmpl',
None)
46 self.
debug = self.config.get(
'debug', 0)
47 self.
tdir = self.config.get(
'tmpl_dir')
49 self.
date = time.strftime(
"%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
53 "Scan template files and return example tags" 55 sdir =
'%s/%s' % (self.
tdir, self.
tmpl)
56 for name
in os.listdir(sdir):
61 fname = os.path.join(sdir, name)
62 with open(fname,
'r') as stream: 63 for line
in stream.readlines():
64 if line.find(
'@example_') != -1:
65 keys += [k
for k
in line.split()
if \
66 k.find(
'@example_') != -1]
70 "Print out template example tags" 75 "Scan template files and return template tags" 77 sdir =
'%s/%s' % (self.
tdir, self.
tmpl)
78 for name
in os.listdir(sdir):
83 fname = os.path.join(sdir, name)
84 with open(fname,
'r') as stream: 85 for line
in stream.readlines():
86 if line.find(
'__') != -1:
91 "Print out template keys" 97 Determine either skip or keep given line based on class tags 101 keep_etags = self.config.get(
'tmpl_etags', [])
102 for tag
in tmpl_etags:
103 for valid_tag
in keep_etags:
104 if line.find(valid_tag) != -1:
105 line = line.replace(valid_tag,
'')
107 if line.find(tag) != -1:
112 def write(self, fname, tmpl_name, kwds):
113 "Create new file from given template name and set of arguments" 116 with open(fname,
'w')
as stream:
117 for line
in open(tmpl_name,
'r').readlines(): 121 if line.find(
'#python_begin') != -1:
124 if line.find(
'#python_end') != -1:
128 if code
and not read_code:
134 for key, val
in kwds.items():
135 if isinstance(val, basestring):
136 line = line.replace(key, val)
140 "Return keyword arguments to be used in methods" 141 kwds = {
'__pkgname__': self.config.get(
'pkgname',
'Package'),
142 '__author__': self.
author,
143 '__user__': os.getlogin(),
144 '__date__': self.
date,
145 '__class__': self.
pname,
146 '__class_lowercase__': self.pname.lower(),
147 '__name__': self.
pname,
148 '__subsys__': self.config.get(
'subsystem',
'Subsystem')}
149 args = self.config.get(
'args',
None)
152 print "Template tags:" 157 "Generate package templates in a given directory" 163 tmpl_files = self.config.get(
'tmpl_files',
'all')
170 if os.path.isdir(self.
pname):
171 msg =
"Can't create package '%s'\n" % self.
pname 172 msg +=
"Directory %s is already exists" % self.
pname 175 os.makedirs(self.
pname)
179 sdir = os.path.join(self.
tdir, self.
tmpl)
180 sources = [s
for s
in os.listdir(sdir) \
181 if s !=
'Driver.dir' and s.find(
'~') == -1]
182 driver = os.path.join(sdir,
'Driver.dir')
183 if os.path.isfile(driver):
184 sources = [s.replace(
'\n',
'')
for s
in open(driver,
'r').readlines()] 186 sources.remove(
'CVS')
190 names = set([s.split(
'.')[0]
for s
in sources])
191 if names == set([
'Skeleton']):
192 if self.pname.find(
'.') != -1:
193 _, ext = os.path.splitext(self.
pname)
194 sources = [s
for s
in sources
if s.rfind(ext) != -1]
195 self.
pname = self.pname.replace(ext,
'')
198 msg =
'Unable to find skeleton for extension "%s"' % ext
201 bdir = os.environ.get(
'CMSSW_BASE',
'')
203 ldir = os.getcwd().
split(
'/')[-1]
205 subsys = kwds[
'__subsys__']
206 pkgname = kwds[
'__pkgname__']
207 if sources == [
'Skeleton.cc',
'Skeleton.h']:
208 if ldir ==
'interface' and os.getcwd().
find(bdir) != -1:
209 idir =
'%s/%s/interface/' % (subsys, pkgname)
212 elif sources == [
'Skeleton.cc']
and \
213 len(dirs) == 5
and dirs[0] ==
'' and dirs[1] ==
'src':
214 idir =
'%s/%s/interface/' % (subsys, pkgname)
215 elif sources == [
'Skeleton.h']
and ldir ==
'interface' and \
216 len(dirs) == 5
and dirs[0] ==
'' and dirs[1] ==
'src':
217 idir =
'%s/%s/interface/' % (subsys, pkgname)
218 kwds.update({
'__incdir__': idir})
224 if tmpl_files !=
'all':
225 fname, ext = os.path.splitext(src)
226 if tmpl_files != ext:
228 src = src.split(
'/')[-1]
231 items = src.split(
'/')
235 tmpl_name = os.path.join(sdir, items[-1])
236 if os.path.isfile(tmpl_name):
241 if tname.split(
'.')[0] == self.
tmpl:
242 name2gen = name2gen.replace(self.
tmpl, self.
pname)
243 name2gen = os.path.join(os.getcwd(), name2gen)
245 print "Create", name2gen
247 if not os.path.isdir(name2gen):
248 os.makedirs(name2gen)
250 fdir = os.path.dirname(name2gen)
251 if not os.path.isdir(fdir):
253 self.
write(name2gen, tmpl_name, kwds)
254 gen_files.append(name2gen.split(
'/')[-1])
256 msg =
'New package "%s" of %s type is successfully generated' \
259 msg =
'Generated %s file' %
', '.
join(gen_files)
260 if len(gen_files) > 1:
265 if msg.find(
'New package') != -1:
def __init__(self, config=None)
def user_info(ainput=None)
def replace(string, replacements)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
def parse_etags(self, line)
static std::string join(char **cmd)
def write(self, fname, tmpl_name, kwds)
def functor(code, kwds, debug=0)