Line Code
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#!/usr/bin/env python3
import os
import json
import argparse
import subprocess

import FWCore.ParameterSet.Config as cms

import HLTrigger.Configuration.Tools.options as options
from HLTrigger.Configuration.extend_argparse import *

def getHLTProcess(args):
    if args.menu.run:
        configline = f'--runNumber {args.menu.run}'
    else:
        configline = f'--{args.menu.database} --{args.menu.version} --configName {args.menu.name}'

    # cmd to download HLT configuration
    cmdline = f'hltConfigFromDB {configline} --noedsources --noes --nooutput'
    if args.proxy:
        cmdline += f' --dbproxy --dbproxyhost {args.proxy_host} --dbproxyport {args.proxy_port}'

    # download HLT configuration
    proc = subprocess.Popen(cmdline, shell = True, stdin = None, stdout = subprocess.PIPE, stderr = None)
    (out, err) = proc.communicate()

    # load HLT configuration
    try:
        foo = {'process': None}
        exec(out, foo)
        process = foo['process']
    except:
        raise Exception(f'query did not return a valid python file:\n query="{cmdline}"')

    if not isinstance(process, cms.Process):
        raise Exception(f'query did not return a valid HLT menu:\n query="{cmdline}"')

    return process

def main():
    # define an argparse parser to parse our options
    textwidth = int( 80 )
    try:
        textwidth = int( os.popen("stty size", "r").read().split()[1] )
    except:
        pass
    formatter = FixedWidthFormatter( HelpFormatterRespectNewlines, width = textwidth )

    # read defaults
    defaults = options.HLTProcessOptions()

    parser = argparse.ArgumentParser(
        description = 'Create outputs to announce the release of a new HLT menu.',
        argument_default = argparse.SUPPRESS,
        formatter_class = formatter,
        add_help = False
    )

    # required argument
    parser.add_argument('menu',
                        action  = 'store',
                        type    = options.ConnectionHLTMenu,
                        metavar = 'MENU',
                        help    = 'HLT menu to dump from the database. Supported formats are:\n  - /path/to/configuration[/Vn]\n  - [[{v1|v2|v3}/]{run3|run2|online|adg}:]/path/to/configuration[/Vn]\n  - run:runnumber\nThe possible converters are "v1", "v2, and "v3" (default).\nThe possible databases are "run3" (default, used for offline development), "run2" (used for accessing run2 offline development menus), "online" (used to extract online menus within Point 5) and "adg" (used to extract the online menus outside Point 5).\nIf no menu version is specified, the latest one is automatically used.\nIf "run:" is used instead, the HLT menu used for the given run number is looked up and used.\nNote other converters and databases exist as options but they are only for expert/special use.' )

    # options
    parser.add_argument('--dbproxy',
                        dest    = 'proxy',
                        action  = 'store_true',
                        default = defaults.proxy,
                        help    = 'Use a socks proxy to connect outside CERN network (default: False)' )
    parser.add_argument('--dbproxyport',
                        dest    = 'proxy_port',
                        action  = 'store',
                        metavar = 'PROXYPORT',
                        default = defaults.proxy_port,
                        help    = 'Port of the socks proxy (default: 8080)' )
    parser.add_argument('--dbproxyhost',
                        dest    = 'proxy_host',
                        action  = 'store',
                        metavar = 'PROXYHOST',
                        default = defaults.proxy_host,
                        help    = 'Host of the socks proxy (default: "localhost")' )

    parser.add_argument('--metadata-json',
                        dest    = 'metadata_json',
                        action  = 'store',
                        default = 'hltPathOwners.json',
                        help    = 'Path to .json file with metadata on HLT Paths (online?, group-owners)' )

    parser.add_argument('-o', '--output-json',
                        dest    = 'output_json',
                        action  = 'store',
                        default = None,
                        help    = 'Path to name of new .json file (without triggers missing in MENU)' )

    # redefine "--help" to be the last option, and use a customized message 
    parser.add_argument('-h', '--help', 
                        action  = 'help', 
                        help    = 'Show this help message and exit' )

    # parse command line arguments and options
    args = parser.parse_args()

    if not os.path.isfile(args.metadata_json):
        raise RuntimeError(f'invalid path to metadata JSON file [--metadata-json]: {args.metadata_json}')

    metadataDict = json.load(open(args.metadata_json))

    process = getHLTProcess(args)

    pathNames = sorted([pathName if '_v' not in pathName else pathName[:pathName.rfind('_v')]+'_v' for pathName, path in process.paths_().items() if not pathName.startswith('Dataset_')])

    for pathName in pathNames:
        if pathName not in metadataDict:
            print(pathName)

    if args.output_json != None:
        metadataDict_out = {}
        for pathName in metadataDict:
            if pathName in pathNames:
                metadataDict_out[pathName] = metadataDict[pathName]
        json.dump(metadataDict_out, open(args.output_json, 'w'), sort_keys = True, indent = 2)

###
### main
###
if __name__ == '__main__':
    main()