pandora_metadata.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys, getopt
3 import os
4 from subprocess import Popen, PIPE
5 import json
6 import argparse
7 
8 argparser = argparse.ArgumentParser('Parse arguments')
9 argparser.add_argument('--infile',help='path to input file',required=True,type=str)
10 argparser.add_argument('--declare',help='validate and declare the metadata for the file specified in --infile to SAM',action='store_true')
11 argparser.add_argument('--appname',help='Optional override of application name for SAM metadata',type=str)
12 argparser.add_argument('--appversion',help='Optional override of application version for SAM metadata',type=str)
13 argparser.add_argument('--appfamily',help='Optional override of application family for SAM metadata',type=str)
14 argparser.add_argument('--campaign',help='Optional override for DUNE.campaign for SAM metadata',type=str)
15 argparser.add_argument('--data_stream',help='Optional override for data_stream for SAM metadata',type=str)
16 argparser.add_argument('--strip_parents',help='Do not include the file\'s parents in SAM metadata for declaration',action="store_true")
17 argparser.add_argument('--no_crc',help='Leave the crc out of the generated json',action="store_true")
18 argparser.add_argument('--input_json',help='Input json file containing metadata to be added to output (can contain ANY valid SAM metadata parameters)',type=str)
19 argparser.add_argument('--file_format',help='Value of file_format set in output md, default is binary',type=str,default='binary')
20 argparser.add_argument('--data_tier',help='Value of data_tier set in output md, default is pandora_info',type=str,default='pandora_info')
21 argparser.add_argument('--requestid',help='Value of DUNE.requestid set in output md',type=str)
22 
23 global args
24 args = argparser.parse_args()
25 
26 #make some skeleton output
27 outmd = {}
28 
29 #check that the input file actually exists and bail out if it doesn't
30 if not os.path.exists(args.infile):
31  print('Error: input file %s not found. Exiting.' % args.infile)
32  sys.exit(1)
33 
34 if args.input_json:
35  if os.path.exists(args.input_json):
36  try:
37  injson=open(args.input_json)
38  outmd = json.load(injson)
39  except:
40  print('Error loading input json file.')
41  raise
42  else:
43  print('Error, specified input file does not exist.')
44  sys.exit(1)
45 
46 # now everything is the same as whatever is in the input jsooon file. Obviously we need to replace some thingss like file name, size, and checkssum at a minumum. We can also strip parents if that is preferred.
47 
48 outmd['file_name'] = os.path.basename(args.infile)
49 outmd['file_format'] = args.file_format
50 outmd['data_tier'] = args.data_tier
51 outmd['file_size'] = os.path.getsize(args.infile)
52 
53 #kill the existing checksum and file_id values, if set, because they would correspond to
54 # the value from the input json, which is from another file.
55 
56 for ikey in [ 'crc', 'checksum', 'file_id' ]:
57  if ikey in list(outmd.keys()):
58  del outmd[ikey]
59 
60 # Check optional overrides
61 if args.campaign:
62  outmd['DUNE.campaign'] = args.campaign
63 if args.data_stream:
64  outmd['data_stream'] = args.data_stream
65 if args.strip_parents and 'parents' in list(outmd.keys()):
66  del outmd['parents']
67 
68 if args.appname and args.appfamily and args.appversion:
69  outmd['application'] = { 'family' : args.appfamily, 'name' : args.appname, 'version' : args.appversion }
70 elif args.appname or args.appfamily or args.appversion:
71  print('Error: you specified at least one of --appfamily, --appname, or --appversion, but not all three. You must specify all or none of them (if none, you will get the existing values in the input json file, if you supplied one.')
72  sys.exit(1)
73 
74 if args.requestid != None:
75  outmd['DUNE.requestid'] = args.requestid
76 
77 mdtext = json.dumps(outmd, indent=2, sort_keys=True)
78 
79 if args.declare:
80  import ifdh
81  ih = ifdh.ifdh()
82  try:
83  ih.declareFile(mdtext)
84  except:
85  print('Error declaring file. Please check!')
86 
87 print(mdtext)
88 sys.exit(0)
int open(const char *, int)
Opens a file descriptor.