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