CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
mps_splice.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import re
3 import argparse
4 import math
5 import fileinput
6 
7 # Set up argrument parser
8 helpEpilog = ''''''
9 
10 parser = argparse.ArgumentParser(description='Take card file, blank all INFI directives and insert the INFI directives from the modifier file instead.',
11  epilog=helpEpilog,
13 
14 parser.add_argument('inCfg', action='store',
15  help='name of the config-template')
16 parser.add_argument('modCfg', action='store',
17  help='name of the modifier file from mps_split')
18 parser.add_argument('outCfg', action='store',
19  help='name of modified output file')
20 parser.add_argument('isn', action='store',
21  help='number of the job (three digit number with leading zeros)')
22 
23 # Parse arguments
24 args = parser.parse_args()
25 inCfg = args.inCfg
26 modCfg = args.modCfg
27 outCfg = args.outCfg
28 isn = args.isn
29 
30 
31 # open input file
32 with open(inCfg, 'r') as INFILE:
33  body = INFILE.read()
34 
35 # read modifier file
36 with open(modCfg, 'r') as MODFILE:
37  mods = MODFILE.read()
38 mods = mods.strip()
39 
40 # prepare the new fileNames directive. Delete first line if necessary.
41 fileNames = mods.split('\n')
42 if 'CastorPool=' in fileNames[0]:
43  del fileNames[0]
44 
45 # replace ISN number (input is a string of three digit number with leading zeros though)
46 body = re.sub(re.compile('ISN',re.M), isn, body)
47 
48 # print to outCfg
49 with open(outCfg, 'w') as OUTFILE:
50  OUTFILE.write(body)
51 
52 # Number of total files and number of extends for cms.vstring are needed
53 numberOfFiles = len(fileNames)
54 numberOfExtends = int(math.ceil(numberOfFiles/255.))
55 
56 # Create and insert the readFile.extend lines
57 for j in xrange(numberOfExtends):
58  insertBlock = "readFiles.extend([\n "
59  i=0
60  currentStart = j*255
61  while (i<255) and ((currentStart+i)<numberOfFiles):
62  entry = fileNames[currentStart+i].strip()
63  if (i==254) or ((currentStart+i+1)==numberOfFiles):
64  insertBlock += "\'"+entry+"\'])\n"
65  else:
66  insertBlock += "\'"+entry+"\',\n "
67  i+=1
68 
69  for line in fileinput.input(outCfg, inplace=1):
70  if re.match('readFiles\s*=\s*cms.untracked.vstring()',line):
71  print line,
72  print insertBlock,
73  else:
74  print line,
75 
76 
77 
78 
79 
80 
81 
82