-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_db.py
67 lines (58 loc) · 2.66 KB
/
insert_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import subprocess
import sys
def get_data_dir(data_label, run):
if data_label in [ '0', '1' ]:
return 'pr{0}r{1}'.format(data_label, run)
else:
return 'sp{0}r{1}'.format(data_label, run)
def execute_insert(bin_path, ntp_dir, dbname, logfile, data_label, run):
command = [ args.insert_postgres_path, args.ntp_dir, args.dbname, d, r ]
subprocess.check_call(command, stderr=logfile)
return
if __name__ == '__main__':
import argparse
import time
parser = argparse.ArgumentParser()
parser.add_argument("--insert_postgres_path", default="bin/insert_postgres",
help="Path to the insert_postgres executable.")
parser.add_argument("--ntp_dir", default="ntp",
help="Path to the root directory of the ntuples.")
parser.add_argument("--log_fname", default="log/insert_db.log",
help="Log file associated to the launching of this script.")
parser.add_argument("dbname",
help="Database name to connect to.")
parser.add_argument("data_labels", type=str,
help="Comma separated string of data labels to process.")
parser.add_argument("runs", type=str,
help="Comma separated string of runs to process.")
args = parser.parse_args()
logfile = open(args.log_fname, 'w')
print
print 'Root directory of the input records: {0}'.format(args.ntp_dir)
print 'Destination database for insertions: {0}'.format(args.dbname)
print 'Log file output: {0}'.format(args.log_fname)
print
print 'Start time: {0}.'.format(time.asctime( time.localtime(time.time()) ))
print
data_labels = args.data_labels.split(',')
runs = args.runs.split(',')
for d in data_labels:
for r in runs:
sys.stdout.write('Inserting records from {0}/{1}/root/*.root'.format(args.ntp_dir, get_data_dir(d, r)))
sys.stdout.flush()
try:
execute_insert(args.insert_postgres_path,
args.ntp_dir, args.dbname,
logfile,
d, r)
sys.stdout.write(' OK. ')
sys.stdout.write('Finished at {0}.\n'.format(time.asctime( time.localtime(time.time()) )))
sys.stdout.flush()
except subprocess.CalledProcessError:
print
print ('Error: Problem when inserting {0}. '
'See {1} for details.').format(get_data_dir(d, r), args.log_fname)
sys.exit(1)
logfile.close()
print
print 'Complete! Use \'psql -d {0}\' to see the results.'.format(args.dbname)