log_stdout=open("{}/{}".format(sdconfig.log_folder,sdconst.LOGFILE_CONSUMER), "a+") log_stderr=open("{}/{}".format(sdconfig.log_folder,sdconst.LOGFILE_CONSUMER), "a+") context=daemon.DaemonContext(working_directory=sdconfig.tmp_folder, pidfile=pidfile,stdout=log_stdout,stderr=log_stderr) context.signal_map={ signal.SIGTERM: terminate, } # retrieve unprivileged user from configuration file if any user=sdconfig.config.get('daemon','user') group=sdconfig.config.get('daemon','group') if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('action') args = parser.parse_args() if args.action in ['start','stop']: if not sdpermission.is_admin(): sdtools.print_stderr() # this is to prevent having all on the same line when using "synda service" command e.g. "Shutting down synda daemon (sdt): You need to be root to perform this command." sdtools.print_stderr(sdi18n.m0027) sys.exit(1) if args.action == 'start': start() #time.sleep(18) # can take some time to start # #if not is_running(): # import sdlog # sdlog.error("SDDAEMON-222" "Error occurs during transfer daemon startup, see log files for details",stderr=True) # sys.exit(2) elif args.action == 'stop':
# create parser for sub-commands sdsubparser.run(subparsers) args = parser.parse_args() # check type mutex # # There is no way to check mutex as 'dest' argparse feature is used. Maybe # use add_mutually_exclusive_group(), but currently, doing so makes the # help looks ugly. So better leave it as is until argparse handle this case # smoothly. # -- permission check -- # if args.subcommand in (sdconst.ADMIN_SUBCOMMANDS): if not sdpermission.is_admin(): sdtools.print_stderr(sdi18n.m0027) sys.exit(1) # -- subcommand routing -- # if args.subcommand == 'help': if args.topic is None: parser.print_help() else: if args.topic in subparsers.choices: subparsers.choices[args.topic].print_help() else: sdtools.print_stderr('Help topic not found (%s)' % args.topic)
def run(): # create the top-level parser parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter) #parser = sdtools.DefaultHelpParser(formatter_class=argparse.RawDescriptionHelpFormatter,description=sdi18n.m0016) subparsers = parser.add_subparsers(dest='subcommand',metavar='subcommand') # ,help=sdi18n.m0015 parser.add_argument('-V','--version',action='version',version=sdconst.SYNDA_VERSION) # beware: version exist both as option and as subcommand # create parser for sub-commands sdsubparser.run(subparsers) args = parser.parse_args() # check type mutex # # There is no way to check mutex as 'dest' argparse feature is used. Maybe # use add_mutually_exclusive_group(), but currently, doing so makes the # help looks ugly. So better leave it as is until argparse handle this case # smoothly. if args.subcommand=='setup': print('Setting up environment...') # -- permission check -- # if args.subcommand in (sdconst.ADMIN_SUBCOMMANDS): if not sdpermission.is_admin(): sdtools.print_stderr(sdi18n.m0028) sys.exit(1) # -- subcommand routing -- # if args.subcommand=='help': if args.topic is None: parser.print_help() else: if args.topic in subparsers.choices: subparsers.choices[args.topic].print_help() else: sdtools.print_stderr('Help topic not found (%s)'%args.topic) sys.exit(0) import sdtsaction if args.subcommand in sdtsaction.actions.keys(): import syndautils # hack to explode id in individual facets (experimental) if args.subcommand=='search': if args.explode: if len(args.parameter)>0: id_=args.parameter[0] id_=id_.split('=')[1] if '=' in id_ else id_ # if id_ is in the form 'k=v', we strip 'k='. We assume here that '=' character doesn't appear in key nor value. delim='/' if '/' in id_ else '.' li=id_.split(delim)+args.parameter[1:] # this allow to add other parameter after id e.g. 'synda search <master_id> <version>' args.parameter=li stream=syndautils.get_stream(subcommand=args.subcommand,parameter=args.parameter,selection_file=args.selection_file,no_default=args.no_default) # hack for 'show' and 'version' subcommands. # # description # this hack normalize 'show' and 'version' subcommands 'type_' # attribute with other type_ sensitive subcommands. Without this # hack, the next statement (i.e. "if args.type_ is None:") fails with # "AttributeError: 'Namespace' object has no attribute 'type_'". # # notes # - show and version subcommands type_ attribute is already strictly # defined by the parameter argument (e.g. dataset identifier, file # identifier, etc..), so we dont want the user to also be able to # set type_ attribute using options. This is why type_ group is not # present for show and version subcommands (see subparser module # for details). # - another way to normalize is to use "parser.set_defaults(type_=None)" # if args.subcommand in ('show','version'): args.type_=None # infer type if not set by user if args.type_ is None: import sdtype args.type_=sdtype.infer_display_type(stream) args.stream=stream # TODO: pass 'stream' object downstream as a standalone argument (not inside args) set_stream_type(args) status=sdtsaction.actions[args.subcommand](args) # hack # TODO: review all return code in sdtsaction module if not isinstance(status,int): status=0 # arbitrary sys.exit(status) import sdtiaction if args.subcommand in sdtiaction.actions.keys(): status=sdtiaction.actions[args.subcommand](args) # hack # TODO: review all return code in sdtiaction module if not isinstance(status,int): status=0 # arbitrary sys.exit(status) sdtools.print_stderr('Invalid operation %s'%args.subcommand) sdtools.print_stderr("Use '--help' option for more info") #parser.print_help() sys.exit(2)