def generate(self): "Generate package templates in a given directory" # keep current location, since generate will switch directories cdir = os.getcwd() # read from configutation which template files to create tmpl_files = self.config.get('tmpl_files', 'all') # setup keyword arguments which we'll pass to write method kwds = self.get_kwds() # create template package dir and cd into it if tmpl_files == 'all' and self.tmpl not in self.not_in_dir: if os.path.isdir(self.pname): msg = "Can't create package '%s'\n" % self.pname msg += "Directory %s is already exists" % self.pname print(msg) sys.exit(1) os.makedirs(self.pname) os.chdir(self.pname) # read directory driver information and create file list to generate sdir = os.path.join(self.tdir, self.tmpl) sources = [s for s in os.listdir(sdir) \ if s != 'Driver.dir' and s.find('~') == -1] driver = os.path.join(sdir, 'Driver.dir') if os.path.isfile(driver): sources = [ s.replace('\n', '') for s in open(driver, 'r').readlines() ] if 'CVS' in sources: sources.remove('CVS') # special case of Skeleton, which requires to generate only given # file type if self.pname has extension of that type names = set([s.split('.')[0] for s in sources]) if names == set(['Skeleton']): if self.pname.find('.') != -1: _, ext = os.path.splitext(self.pname) sources = [s for s in sources if s.rfind(ext) != -1] self.pname = self.pname.replace(ext, '') kwds = self.get_kwds() if not sources: msg = 'Unable to find skeleton for extension "%s"' % ext print(msg) sys.exit(1) bdir = os.environ.get('CMSSW_BASE', '') dirs = os.getcwd().replace(bdir, '').split('/') ldir = os.getcwd().split('/')[-1] idir = '' subsys = kwds['__subsys__'] pkgname = kwds['__pkgname__'] if sources == ['Skeleton.cc', 'Skeleton.h']: if ldir == 'interface' and os.getcwd().find(bdir) != -1: idir = '%s/%s/interface/' % (subsys, pkgname) # run within some directory of the Sybsystem/Pkg area # and only for mkskel <file>.cc elif sources == ['Skeleton.cc'] and \ len(dirs) == 5 and dirs[0] == '' and dirs[1] == 'src': idir = '%s/%s/interface/' % (subsys, pkgname) elif sources == ['Skeleton.h'] and ldir == 'interface' and \ len(dirs) == 5 and dirs[0] == '' and dirs[1] == 'src': idir = '%s/%s/interface/' % (subsys, pkgname) kwds.update({'__incdir__': idir}) # loop over source files, create dirs as necessary and generate files # names for writing templates gen_files = [] for src in sources: if tmpl_files != 'all': fname, ext = os.path.splitext(src) if tmpl_files != ext: continue #also reject if this is the wrong directory if self.working_dir and src.split('/')[-2] != self.working_dir: continue src = src.split('/')[-1] if self.debug: print("Read", src) items = src.split('/') if items[-1] == '/': items = items[:-1] tname = items[-1] # template file name tmpl_name = os.path.join(sdir, items[-1]) # full tmpl file name if os.path.isfile(tmpl_name): ftype = 'file' else: ftype = 'dir' name2gen = src # new file we'll create if items[-1] == 'testBuildFile.xml': name2gen = '/'.join(src.split('/')[:-1]) + '/BuildFile.xml' if -1 != tname.split('.')[0].find(self.tmpl): # need to substitute name2gen = name2gen.replace(self.tmpl, self.pname) name2gen = os.path.join(os.getcwd(), name2gen) if self.debug: print("Create", name2gen) if ftype == 'dir': if not os.path.isdir(name2gen): os.makedirs(name2gen) continue # we're done with dir fdir = os.path.dirname(name2gen) if not os.path.isdir(fdir): os.makedirs(fdir) self.write(name2gen, tmpl_name, kwds) gen_files.append(name2gen.split('/')[-1]) if tmpl_files == 'all' and self.tmpl not in self.not_in_dir: msg = 'New package "%s" of %s type is successfully generated' \ % (self.pname, self.tmpl) else: msg = 'Generated %s file' % ', '.join(gen_files) if len(gen_files) > 1: msg += 's' print(msg) # return back where we started os.chdir(cdir) if msg.find('New package') != -1: tree(self.pname)
def generate(self): "Generate package templates in a given directory" # keep current location, since generate will switch directories cdir = os.getcwd() # read from configutation which template files to create tmpl_files = self.config.get('tmpl_files', 'all') # setup keyword arguments which we'll pass to write method kwds = self.get_kwds() # create template package dir and cd into it if tmpl_files == 'all' and self.tmpl not in self.not_in_dir: if os.path.isdir(self.pname): msg = "Can't create package '%s'\n" % self.pname msg += "Directory %s is already exists" % self.pname print msg sys.exit(1) os.makedirs(self.pname) os.chdir(self.pname) # read directory driver information and create file list to generate sdir = os.path.join(self.tdir, self.tmpl) sources = [s for s in os.listdir(sdir) \ if s != 'Driver.dir' and s.find('~') == -1] driver = os.path.join(sdir, 'Driver.dir') if os.path.isfile(driver): sources = [s.replace('\n', '') for s in open(driver, 'r').readlines()] if 'CVS' in sources: sources.remove('CVS') # special case of Skeleton, which requires to generate only given # file type if self.pname has extension of that type names = set([s.split('.')[0] for s in sources]) if names == set(['Skeleton']): if self.pname.find('.') != -1: _, ext = os.path.splitext(self.pname) sources = [s for s in sources if s.rfind(ext) != -1] self.pname = self.pname.replace(ext, '') kwds = self.get_kwds() if not sources: msg = 'Unable to find skeleton for extension "%s"' % ext print msg sys.exit(1) bdir = os.environ.get('CMSSW_BASE', '') dirs = os.getcwd().replace(bdir, '').split('/') ldir = os.getcwd().split('/')[-1] idir = '' subsys = kwds['__subsys__'] pkgname = kwds['__pkgname__'] if sources == ['Skeleton.cc', 'Skeleton.h']: if ldir == 'interface' and os.getcwd().find(bdir) != -1: idir = '%s/%s/interface/' % (subsys, pkgname) # run within some directory of the Sybsystem/Pkg area # and only for mkskel <file>.cc elif sources == ['Skeleton.cc'] and \ len(dirs) == 5 and dirs[0] == '' and dirs[1] == 'src': idir = '%s/%s/interface/' % (subsys, pkgname) elif sources == ['Skeleton.h'] and ldir == 'interface' and \ len(dirs) == 5 and dirs[0] == '' and dirs[1] == 'src': idir = '%s/%s/interface/' % (subsys, pkgname) kwds.update({'__incdir__': idir}) # loop over source files, create dirs as necessary and generate files # names for writing templates gen_files = [] for src in sources: if tmpl_files != 'all': fname, ext = os.path.splitext(src) if tmpl_files != ext: continue src = src.split('/')[-1] if self.debug: print "Read", src items = src.split('/') if items[-1] == '/': items = items[:-1] tname = items[-1] # template file name tmpl_name = os.path.join(sdir, items[-1]) # full tmpl file name if os.path.isfile(tmpl_name): ftype = 'file' else: ftype = 'dir' name2gen = src # new file we'll create if tname.split('.')[0] == self.tmpl: # need to substitute name2gen = name2gen.replace(self.tmpl, self.pname) name2gen = os.path.join(os.getcwd(), name2gen) if self.debug: print "Create", name2gen if ftype == 'dir': if not os.path.isdir(name2gen): os.makedirs(name2gen) continue # we're done with dir fdir = os.path.dirname(name2gen) if not os.path.isdir(fdir): os.makedirs(fdir) self.write(name2gen, tmpl_name, kwds) gen_files.append(name2gen.split('/')[-1]) if tmpl_files == 'all' and self.tmpl not in self.not_in_dir: msg = 'New package "%s" of %s type is successfully generated' \ % (self.pname, self.tmpl) else: msg = 'Generated %s file' % ', '.join(gen_files) if len(gen_files) > 1: msg += 's' print msg # return back where we started os.chdir(cdir) if msg.find('New package') != -1: tree(self.pname)