4 from subprocess
import Popen, PIPE
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)
24 args = argparser.parse_args()
30 if not os.path.exists(args.infile):
31 print(
'Error: input file %s not found. Exiting.' % args.infile)
35 if os.path.exists(args.input_json):
37 injson=
open(args.input_json)
38 outmd = json.load(injson)
40 print(
'Error loading input json file.')
43 print(
'Error, specified input file does not exist.')
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)
56 for ikey
in [
'crc',
'checksum',
'file_id' ]:
57 if ikey
in list(outmd.keys()):
62 outmd[
'DUNE.campaign'] = args.campaign
64 outmd[
'data_stream'] = args.data_stream
65 if args.strip_parents
and 'parents' in list(outmd.keys()):
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.')
74 if args.requestid !=
None:
75 outmd[
'DUNE.requestid'] = args.requestid
77 mdtext = json.dumps(outmd, indent=2, sort_keys=
True)
83 ih.declareFile(mdtext)
85 print(
'Error declaring file. Please check!')
int open(const char *, int)
Opens a file descriptor.