submit_hadd.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import argparse
4 import subprocess
5 import datetime
6 import os
7 import ifdh
8 import sys
9 
10 def split_list(flist):
11  return
12 
13 if __name__ == "__main__":
14 
15  if "-f" in sys.argv or "--file" in sys.argv:
16  prelim_parser = argparse.ArgumentParser(prog='submit_hadd.py',
17  description="""Submit grid jobs to hadd CAF trees or ROOT ntuples. """)
18 
19  prelim_parser.add_argument('-f', '--file', type=str, action='append')
20  pre_args, unknown = prelim_parser.parse_known_args()
21 
22  if pre_args.file:
23  for filepath in pre_args.file:
24  if not os.path.exists(filepath):
25  print("Config file is not found, exit now.")
26  os.exit(1)
27  for line in open(filepath, 'r'):
28  sys.argv += remove_comments(line).split()
29 
30  parser = argparse.ArgumentParser(
31  prog='sub_hadd',
32  description="""Submit grid jobs to hadd CAF trees or ROOT ntuples. """,
33  epilog="Questions and Comments to dingpf@fnal.gov")
34 
35  # required arguments
36  parser.add_argument('--job-name', help="job name", required=True)
37  parser.add_argument('--njobs', help='njobs', required=True)
38  parser.add_argument('--outdir', help='output directory', required=True)
39  parser.add_argument('--flist', help='list of files to add')
40  parser.add_argument('--indir', help='input directory')
41  parser.add_argument('--pattern', help='find file pattern')
42  parser.add_argument('--release', help='release version', required=True)
43  parser.add_argument('--method', default='hadd_cafana',
44  choices=['hadd','hadd_caf', 'hadd_cafana'],
45  help='sample type')
46  parser.add_argument('--job-script', default='NONE',
47  help='worker node job script')
48 
49  # Optional arguments
50  parser.add_argument('--offsite', action='store_true',
51  help='run jobs on offsite locations')
52  parser.add_argument('--just-say', action='store_true',
53  help="do not run command, but just print out what will be done.")
54  parser.add_argument('--user', default='NONE', help="user name")
55  args = parser.parse_args()
56 
57  if args.user == "NONE":
58  args.user = os.environ["USER"]
59  if args.job_script == "NONE":
60  args.job_script = os.environ["NOVAGRIDUTILS_DIR"] + \
61  '/bin/hadd_grid_script.sh'
62 
63  # determine job name
64  ct_stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
65  job_name = "{}_{}_{}".format(args.user, args.job_name, ct_stamp)
66 
67  # make sure paths get translated to absolute path
68  outdir = os.path.abspath(args.outdir)
69  job_outdir = '{}/{}'.format(outdir, job_name)
70  job_script = os.path.abspath(args.job_script)
71 
72  # split filelist into small file lists
73  list_file_name = "{}.list".format(ct_stamp)
74  list_file_url = "http://home.fnal.gov/~{}/".format(args.user) + \
75  list_file_name
76 
77  if args.indir and args.pattern:
78  # make flist on the fly
79  cmd = 'find {} -name {}>{};'.format(
80  args.indir, args.pattern, list_file_name)
81  cmd += ' wc -l {i}; '
82  cmd += 'scp {i} {u}@fnalu.fnal.gov:/publicweb/{c}/{u}/;'.format(
83  u=args.user, i=list_file_name, c=args.user[0])
84  cmd += ' rm -f {}'.format(list_file_name)
85  print cmd
86  print("using find to make flist, and scp file to sever")
87  elif args.flist:
88  cmd = 'scp {f} {u}@fnalu.fnal.gov:/publicweb/{c}/{u}/{l}'.format(
89  f=args.flist, u=args.user, l=list_file_name, c=args.user[0])
90  print("scp list file to fermilab central web sever")
91  else:
92  print("either file list option or a combination of indir and")
93  print("patter should be specified!")
94  exit(1)
95 
96  if not args.just_say:
97  subprocess.call(cmd, shell=True)
98 
99  # setup ifdh
100  if "IFDH_BASE_URI" not in os.environ:
101  os.environ["IFDH_BASE_URI"] = "http://samweb.fnal.gov:8480/sam/nova/api"
102  i = ifdh.ifdh(os.environ["IFDH_BASE_URI"])
103 
104  # setup envs used by jobs script
105  envs = ["OUT_DIR", "RELEASE_NAME", "NJOBS", "LIST_FILE_URL", \
106  "HADD_METHOD", "GRIDUSER"]
107  os.environ["RELEASE_NAME"] = args.release
108  os.environ["OUT_DIR"] = job_outdir
109  os.environ["NJOBS"] = args.njobs
110  os.environ["HADD_METHOD"] = args.method
111  os.environ["LIST_FILE_URL"] = list_file_url
112  os.environ["GRIDUSER"] = args.user
113 
114  env_pass = ""
115  for i in envs:
116  print "INFO: {:<15} = {}".format(i, os.environ[i])
117  env_pass += " -e {}".format(i)
118 
119  # job submission command
120  cmd = 'jobsub_submit -N {}'.format(args.njobs)
121  if not args.offsite:
122  cmd += ' --resource-provides=usage_model=DEDICATED,OPPORTUNISTIC'
123  else:
124  cmd += ' --resource-provides=usage_model=DEDICATED,OFFSITE,'
125  cmd += 'OPPORTUNISTIC --OS=SL6 --site=BNL,Caltech,SMU_HPC,UCSD,'
126  cmd += 'Wisconsin,Cornell -M'
127  # cmd += ' -M -f {}'.format(input_tar)
128  cmd += env_pass
129  cmd += ' -G nova file://{}'.format(job_script)
130  print 80*'*'
131  print "EXECUTING: {}".format(cmd)
132 
133  # run jobs submission command
134  if not args.just_say:
135  subprocess.call(cmd, shell=True, env=os.environ)
void split(double tt, double *fr)
bool print
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
procfile open("FD_BRL_v0.txt")
exit(0)
def remove_comments(src)
def split_list(flist)
Definition: submit_hadd.py:10