def get_options(): '''parse command-line options''' global MASTER_OPTS, PASS_ARGS, OPT_AGGREGATE if len(sys.argv) <= 1: usage() sys.exit(1) # getopt() assumes that all options given after the first non-option # argument are all arguments (this is standard UNIX behavior, not GNU) # but in this case, I like the GNU way better, so re-arrange the options # This has odd consequences when someone gives a 'stale' --install or # --remove option without any argument, but hey ... arglist = rearrange_options() try: opts, args = getopt.getopt(arglist, 'hc:n:g:x:X:iRluUCm:fN:z:vqa', ['help', 'conf=', 'node=', 'group=', 'exclude=', 'exclude-group=', 'list', 'install', 'remove', 'update', 'upgrade', 'clean', 'cleanup', 'manager=', 'numproc=', 'zzz=', 'fix', 'verbose', 'quiet', 'unix', 'aggregate']) except getopt.GetoptError as reason: print '%s: %s' % (PROGNAME, reason) # usage() sys.exit(1) PASS_ARGS = [] MASTER_OPTS = [sys.argv[0],] # first read the config file for opt, arg in opts: if opt in ('-h', '--help', '-?'): usage() sys.exit(1) if opt in ('-c', '--conf'): param.CONF_FILE = arg PASS_ARGS.append(opt) PASS_ARGS.append(arg) continue # these options influence program output, so process them # as soon as possible, even before reading the config file if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue config.read_config() synctool.nodeset.make_default_nodeset() check_cmd_config() # then process all the other options # # Note: some options are passed on to synctool-pkg on the node, while # others are not. Therefore some 'continue', while others don't action = 0 needs_package_list = False for opt, arg in opts: if opt: MASTER_OPTS.append(opt) if arg: MASTER_OPTS.append(arg) if opt in ('-h', '--help', '-?', '-c', '--conf'): # already done continue if opt in ('-n', '--node'): NODESET.add_node(arg) continue if opt in ('-g', '--group'): NODESET.add_group(arg) continue if opt in ('-x', '--exclude'): NODESET.exclude_node(arg) continue if opt in ('-X', '--exclude-group'): NODESET.exclude_group(arg) continue if opt in ('-i', '--install'): action += 1 needs_package_list = True if opt in ('-R', '--remove'): action += 1 needs_package_list = True if opt in ('-l', '--list'): action += 1 if opt in ('-u', '--update'): action += 1 if opt in ('-U', '--upgrade'): action += 1 if opt in ('-C', '--clean', '--cleanup'): action += 1 if opt in ('-m', '--manager'): if arg not in param.KNOWN_PACKAGE_MANAGERS: error("unknown or unsupported package manager '%s'" % arg) sys.exit(1) param.PACKAGE_MANAGER = arg if opt in ('-f', '--fix'): synctool.lib.DRY_RUN = False if opt in ('-N', '--numproc'): try: param.NUM_PROC = int(arg) except ValueError: print ("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.NUM_PROC < 1: print '%s: invalid value for numproc' % PROGNAME sys.exit(1) continue if opt in ('-z', '--zzz'): try: param.SLEEP_TIME = int(arg) except ValueError: print ("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.SLEEP_TIME < 0: print '%s: invalid value for sleep time' % PROGNAME sys.exit(1) if not param.SLEEP_TIME: # (temporarily) set to -1 to indicate we want # to run serialized # synctool.lib.multiprocess() will use this param.SLEEP_TIME = -1 continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True if opt == '--unix': synctool.lib.UNIX_CMD = True if opt in ('-a', '--aggregate'): OPT_AGGREGATE = True continue if opt: PASS_ARGS.append(opt) if arg: PASS_ARGS.append(arg) # enable logging at the master node PASS_ARGS.append('--masterlog') if args != None: MASTER_OPTS.extend(args) PASS_ARGS.extend(args) else: if needs_package_list: error('options --install and --remove require a package name') sys.exit(1) if not action: usage() sys.exit(1) if action > 1: there_can_be_only_one()
def get_options(): # type: () -> int '''parse command-line options''' global SINGLE_FILES try: opts, args = getopt.getopt(sys.argv[1:], 'hc:d:1:r:efNFTvq', ['help', 'conf=', 'diff=', 'single=', 'ref=', 'erase-saved', 'fix', 'no-post', 'fullpath', 'terse', 'color', 'no-color', 'masterlog', 'node=', 'nodename=', 'verbose', 'quiet', 'unix', 'version']) except getopt.GetoptError as reason: print '%s: %s' % (PROGNAME, reason) usage() sys.exit(1) if args: error('excessive arguments on command line') sys.exit(1) for opt, arg in opts: if opt in ('-h', '--help', '-?'): usage() sys.exit(1) if opt in ('-c', '--conf'): param.CONF_FILE = arg continue # these options influence program output, so process them # as soon as possible, even before reading the config file if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue if opt in ('-T', '--terse'): param.TERSE = True param.FULL_PATH = False continue if opt in ('-F', '--fullpath'): param.FULL_PATH = True continue if opt == '--version': print param.VERSION sys.exit(0) # first read the config file config.read_config() # synctool.nodeset.make_default_nodeset() check_cmd_config() errors = 0 action = ACTION_DEFAULT SINGLE_FILES = [] # these are only used for checking the validity of option combinations opt_diff = False opt_single = False opt_reference = False opt_erase_saved = False opt_upload = False opt_suffix = False opt_fix = False for opt, arg in opts: if opt in ('-h', '--help', '-?', '-c', '--conf', '-T', '--terse', '-F', '--fullpath', '--version'): # already done continue if opt in ('-f', '--fix'): opt_fix = True synctool.lib.DRY_RUN = False continue if opt == '--no-post': synctool.lib.NO_POST = True continue if opt == '--color': param.COLORIZE = True continue if opt == '--no-color': param.COLORIZE = False continue if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue if opt == '--masterlog': # used by the master for message logging purposes synctool.lib.MASTERLOG = True continue if opt in ('-N', '--node', '--nodename'): # used by the master to set the client's nodename # or to force the nodename when running in stand-alone mode param.NODENAME = arg continue if opt in ('-d', '--diff'): opt_diff = True action = ACTION_DIFF filename = synctool.lib.strip_path(arg) if not filename: error('missing filename') sys.exit(1) if filename[0] != '/': error('filename must be a full path, starting with a slash') sys.exit(1) if filename not in SINGLE_FILES: SINGLE_FILES.append(filename) continue if opt in ('-1', '--single'): opt_single = True filename = synctool.lib.strip_path(arg) if not filename: error('missing filename') sys.exit(1) if filename[0] != '/': error('filename must be a full path, starting with a slash') sys.exit(1) if filename not in SINGLE_FILES: SINGLE_FILES.append(filename) continue if opt in ('-r', '--ref', '--reference'): opt_reference = True action = ACTION_REFERENCE filename = synctool.lib.strip_path(arg) if not filename: error('missing filename') sys.exit(1) if filename[0] != '/': error('filename must be a full path, starting with a slash') sys.exit(1) if filename not in SINGLE_FILES: SINGLE_FILES.append(filename) continue if opt in ('-e', '--erase-saved'): opt_erase_saved = True action = ACTION_ERASE_SAVED continue error("unknown command line option '%s'" % opt) errors += 1 if errors: usage() sys.exit(1) # diff with fix works like single if opt_diff and opt_fix: opt_diff = False opt_single = True action = ACTION_DEFAULT option_combinations(opt_diff, opt_single, opt_reference, opt_erase_saved, opt_upload, opt_suffix, opt_fix) return action
def get_options(): '''parse command-line options''' global DESTDIR, MASTER_OPTS, OPT_AGGREGATE, DSH_CP_OPTIONS, OPT_PURGE if len(sys.argv) <= 1: usage() sys.exit(1) DESTDIR = None DSH_CP_OPTIONS = None try: opts, args = getopt.getopt(sys.argv[1:], 'hc:n:g:x:X:o:pN:z:vqaf', ['help', 'conf=', 'node=', 'group=', 'exclude=', 'exclude-group=', 'options=', 'purge', 'no-nodename', 'numproc=', 'zzz=', 'unix', 'verbose', 'quiet', 'aggregate', 'fix']) except getopt.GetoptError as reason: print '%s: %s' % (PROGNAME, reason) # usage() sys.exit(1) # first read the config file for opt, arg in opts: if opt in ('-h', '--help', '-?'): usage() sys.exit(1) if opt in ('-c', '--conf'): param.CONF_FILE = arg continue # these options influence program output, so process them # as soon as possible, even before reading the config file if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue config.read_config() synctool.nodeset.make_default_nodeset() check_cmd_config() # then process the other options MASTER_OPTS = [sys.argv[0],] for opt, arg in opts: if opt: MASTER_OPTS.append(opt) if arg: MASTER_OPTS.append(arg) if opt in ('-h', '--help', '-?', '-c', '--conf'): # already done continue if opt in ('-n', '--node'): NODESET.add_node(arg) continue if opt in ('-g', '--group'): NODESET.add_group(arg) continue if opt in ('-x', '--exclude'): NODESET.exclude_node(arg) continue if opt in ('-X', '--exclude-group'): NODESET.exclude_group(arg) continue if opt in ('-o', '--options'): DSH_CP_OPTIONS = arg continue if opt in ('-p', '--purge'): OPT_PURGE = True continue if opt == '--no-nodename': synctool.lib.OPT_NODENAME = False continue if opt in ('-N', '--numproc'): try: param.NUM_PROC = int(arg) except ValueError: print ("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.NUM_PROC < 1: print '%s: invalid value for numproc' % PROGNAME sys.exit(1) continue if opt in ('-z', '--zzz'): try: param.SLEEP_TIME = int(arg) except ValueError: print ("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.SLEEP_TIME < 0: print '%s: invalid value for sleep time' % PROGNAME sys.exit(1) if not param.SLEEP_TIME: # (temporarily) set to -1 to indicate we want # to run serialized # synctool.lib.multiprocess() will use this param.SLEEP_TIME = -1 continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt in ('-a', '--aggregate'): OPT_AGGREGATE = True continue if opt in ('-f', '--fix'): synctool.lib.DRY_RUN = False continue if not args: print '%s: missing file to copy' % PROGNAME sys.exit(1) if len(args) < 2: print '%s: missing destination' % PROGNAME sys.exit(1) MASTER_OPTS.extend(args) DESTDIR = args.pop(-1) # dest may be ':' meaning that we want to copy the source dirname if DESTDIR == ':': if os.path.isdir(args[0]): DESTDIR = args[0] else: DESTDIR = os.path.dirname(args[0]) # DESTDIR[0] == ':' would create "rsync to node::module" # which is something we don't want if not DESTDIR or DESTDIR[0] == ':': print '%s: invalid destination' % PROGNAME sys.exit(1) # ensure trailing slash if DESTDIR[-1] != os.sep: DESTDIR += os.sep return args
def get_options(): '''parse command-line options''' global MASTER_OPTS, OPT_AGGREGATE try: opts, args = getopt.getopt(sys.argv[1:], 'hc:vn:g:x:X:aN:qp:z:', [ 'help', 'conf=', 'verbose', 'node=', 'group=', 'exclude=', 'exclude-group=', 'aggregate', 'unix', 'quiet', 'numproc=', 'zzz=' ]) except getopt.GetoptError as reason: print '%s: %s' % (PROGNAME, reason) # usage() sys.exit(1) # first read the config file for opt, arg in opts: if opt in ('-h', '--help', '-?'): usage() sys.exit(1) if opt in ('-c', '--conf'): param.CONF_FILE = arg continue # these options influence program output, so process them # as soon as possible, even before reading the config file if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue config.read_config() synctool.nodeset.make_default_nodeset() check_cmd_config() # then process the other options MASTER_OPTS = [ sys.argv[0], ] for opt, arg in opts: if opt: MASTER_OPTS.append(opt) if arg: MASTER_OPTS.append(arg) if opt in ('-h', '--help', '-?', '-c', '--conf'): # already done continue if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-n', '--node'): NODESET.add_node(arg) continue if opt in ('-g', '--group'): NODESET.add_group(arg) continue if opt in ('-x', '--exclude'): NODESET.exclude_node(arg) continue if opt in ('-X', '--exclude-group'): NODESET.exclude_group(arg) continue if opt in ('-a', '--aggregate'): OPT_AGGREGATE = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt in ('-N', '--numproc'): try: param.NUM_PROC = int(arg) except ValueError: print("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.NUM_PROC < 1: print '%s: invalid value for numproc' % PROGNAME sys.exit(1) continue if opt in ('-z', '--zzz'): try: param.SLEEP_TIME = int(arg) except ValueError: print("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.SLEEP_TIME < 0: print '%s: invalid value for sleep time' % PROGNAME sys.exit(1) if not param.SLEEP_TIME: # (temporarily) set to -1 to indicate we want # to run serialized # synctool.parallel.do() will use this param.SLEEP_TIME = -1 continue if args != None and len(args) > 0: print '%s: too many arguments' % PROGNAME sys.exit(1)
def get_options(): '''parse command-line options''' global MASTER_OPTS, OPT_AGGREGATE try: opts, args = getopt.getopt(sys.argv[1:], 'hc:vn:g:x:X:aN:qp:z:', ['help', 'conf=', 'verbose', 'node=', 'group=', 'exclude=', 'exclude-group=', 'aggregate', 'unix', 'quiet', 'numproc=', 'zzz=']) except getopt.GetoptError as reason: print '%s: %s' % (PROGNAME, reason) # usage() sys.exit(1) # first read the config file for opt, arg in opts: if opt in ('-h', '--help', '-?'): usage() sys.exit(1) if opt in ('-c', '--conf'): param.CONF_FILE = arg continue # these options influence program output, so process them # as soon as possible, even before reading the config file if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue config.read_config() synctool.nodeset.make_default_nodeset() check_cmd_config() # then process the other options MASTER_OPTS = [sys.argv[0],] for opt, arg in opts: if opt: MASTER_OPTS.append(opt) if arg: MASTER_OPTS.append(arg) if opt in ('-h', '--help', '-?', '-c', '--conf'): # already done continue if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-n', '--node'): NODESET.add_node(arg) continue if opt in ('-g', '--group'): NODESET.add_group(arg) continue if opt in ('-x', '--exclude'): NODESET.exclude_node(arg) continue if opt in ('-X', '--exclude-group'): NODESET.exclude_group(arg) continue if opt in ('-a', '--aggregate'): OPT_AGGREGATE = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt in ('-N', '--numproc'): try: param.NUM_PROC = int(arg) except ValueError: print ("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.NUM_PROC < 1: print '%s: invalid value for numproc' % PROGNAME sys.exit(1) continue if opt in ('-z', '--zzz'): try: param.SLEEP_TIME = int(arg) except ValueError: print ("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.SLEEP_TIME < 0: print '%s: invalid value for sleep time' % PROGNAME sys.exit(1) if not param.SLEEP_TIME: # (temporarily) set to -1 to indicate we want # to run serialized # synctool.parallel.do() will use this param.SLEEP_TIME = -1 continue if args != None and len(args) > 0: print '%s: too many arguments' % PROGNAME sys.exit(1)
def main(): # type: () -> None '''do your thing''' param.init() get_options() if ACTION == ACTION_VERSION: print param.VERSION sys.exit(0) if ACTION == ACTION_FQDN: print socket.getfqdn() sys.exit(0) config.read_config() # synctool.nodeset.make_default_nodeset() if ACTION == ACTION_LIST_NODES: list_all_nodes() elif ACTION == ACTION_LIST_GROUPS: list_all_groups() elif ACTION == ACTION_NODES: if not ARG_NODENAMES: error("option '--node' requires an argument; the node name") sys.exit(1) list_nodes(ARG_NODENAMES) elif ACTION == ACTION_GROUPS: if not ARG_GROUPS: error("option '--node-group' requires an argument; " "the node group name") sys.exit(1) list_nodegroups(ARG_GROUPS) elif ACTION == ACTION_CMDS: list_commands(ARG_CMDS) elif ACTION == ACTION_PKGMGR: print param.PACKAGE_MANAGER elif ACTION == ACTION_NUMPROC: print param.NUM_PROC elif ACTION == ACTION_LIST_DIRS: list_dirs() elif ACTION == ACTION_PREFIX: print param.ROOTDIR elif ACTION == ACTION_NODENAME: config.init_mynodename() if not param.NODENAME: error('unable to determine my nodename (%s)' % param.HOSTNAME) stderr('please check %s' % param.CONF_FILE) sys.exit(1) print param.NODENAME elif ACTION == ACTION_MASTER: print param.MASTER elif ACTION == ACTION_SLAVE: if not param.SLAVES: print '(none)' else: for node in param.SLAVES: print node, print elif ACTION == ACTION_EXPAND: if not ARG_EXPAND: print 'none' else: expand(ARG_EXPAND) else: raise RuntimeError('bug: unknown ACTION code %d' % ACTION)
def get_options(): '''parse command-line options''' global MASTER_OPTS, OPT_SKIP_RSYNC, OPT_AGGREGATE, SSH_OPTIONS global OPT_MULTIPLEX, CTL_CMD, PERSIST if len(sys.argv) <= 1: usage() sys.exit(1) try: opts, args = getopt.getopt(sys.argv[1:], 'hc:n:g:x:X:o:MP:O:N:z:vqa', ['help', 'conf=', 'node=', 'group=', 'exclude=', 'exclude-group=', 'aggregate', 'options=', 'master', 'multiplex', 'persist=', 'numproc=', 'zzz=', 'no-nodename', 'unix', 'verbose', 'aggregate', 'skip-rsync', 'quiet']) except getopt.GetoptError as reason: print '%s: %s' % (PROGNAME, reason) # usage() sys.exit(1) # first read the config file for opt, arg in opts: if opt in ('-h', '--help', '-?'): usage() sys.exit(1) if opt in ('-c', '--conf'): param.CONF_FILE = arg continue # these options influence program output, so process them # as soon as possible, even before reading the config file if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue config.read_config() synctool.nodeset.make_default_nodeset() check_cmd_config() # then process the other options MASTER_OPTS = [sys.argv[0],] for opt, arg in opts: if opt: MASTER_OPTS.append(opt) if arg: MASTER_OPTS.append(arg) if opt in ('-h', '--help', '-?', '-c', '--conf'): continue if opt in ('-n', '--node'): NODESET.add_node(arg) continue if opt in ('-g', '--group'): NODESET.add_group(arg) continue if opt in ('-x', '--exclude'): NODESET.exclude_node(arg) continue if opt in ('-X', '--exclude-group'): NODESET.exclude_group(arg) continue if opt in ('-o', '--options'): SSH_OPTIONS = arg continue if opt in ('-M', '--master', '--multiplex'): OPT_MULTIPLEX = True continue if opt in ('-P', '--persist'): PERSIST = arg # spellcheck it later continue if opt == '-O': if CTL_CMD != None: print "%s: only a single '-O' option can be given" % PROGNAME sys.exit(1) if arg not in ('check', 'stop', 'exit'): print "%s: unknown control command '%s'" % (PROGNAME, arg) sys.exit(1) CTL_CMD = arg continue if opt in ('-N', '--numproc'): try: param.NUM_PROC = int(arg) except ValueError: print ("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.NUM_PROC < 1: print '%s: invalid value for numproc' % PROGNAME sys.exit(1) continue if opt in ('-z', '--zzz'): try: param.SLEEP_TIME = int(arg) except ValueError: print ("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.SLEEP_TIME < 0: print '%s: invalid value for sleep time' % PROGNAME sys.exit(1) if not param.SLEEP_TIME: # (temporarily) set to -1 to indicate we want # to run serialized # synctool.parallel.do() will use this param.SLEEP_TIME = -1 continue if opt in ('-a', '--aggregate'): OPT_AGGREGATE = True continue if opt == '--no-nodename': synctool.lib.OPT_NODENAME = False continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue if opt == '--skip-rsync': OPT_SKIP_RSYNC = True continue if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if not OPT_MULTIPLEX and PERSIST is not None: print '%s: option --persist requires option --master' % PROGNAME sys.exit(1) if OPT_MULTIPLEX and CTL_CMD != None: print '%s: options --master and -O can not be combined' % PROGNAME sys.exit(1) if OPT_MULTIPLEX or CTL_CMD != None: if len(args) > 0: print '%s: excessive arguments on command-line' % PROGNAME sys.exit(1) elif not args: print '%s: missing remote command' % PROGNAME sys.exit(1) if len(args) > 0: MASTER_OPTS.extend(args) return args
def get_options(): '''parse command-line options''' global MASTER_OPTS, OPT_SKIP_RSYNC, OPT_AGGREGATE, SSH_OPTIONS global OPT_MULTIPLEX, CTL_CMD, PERSIST if len(sys.argv) <= 1: usage() sys.exit(1) try: opts, args = getopt.getopt(sys.argv[1:], 'hc:n:g:x:X:o:MP:O:N:z:vqa', [ 'help', 'conf=', 'node=', 'group=', 'exclude=', 'exclude-group=', 'aggregate', 'options=', 'master', 'multiplex', 'persist=', 'numproc=', 'zzz=', 'no-nodename', 'unix', 'verbose', 'aggregate', 'skip-rsync', 'quiet' ]) except getopt.GetoptError as reason: print '%s: %s' % (PROGNAME, reason) # usage() sys.exit(1) # first read the config file for opt, arg in opts: if opt in ('-h', '--help', '-?'): usage() sys.exit(1) if opt in ('-c', '--conf'): param.CONF_FILE = arg continue # these options influence program output, so process them # as soon as possible, even before reading the config file if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue config.read_config() synctool.nodeset.make_default_nodeset() check_cmd_config() # then process the other options MASTER_OPTS = [ sys.argv[0], ] for opt, arg in opts: if opt: MASTER_OPTS.append(opt) if arg: MASTER_OPTS.append(arg) if opt in ('-h', '--help', '-?', '-c', '--conf'): continue if opt in ('-n', '--node'): NODESET.add_node(arg) continue if opt in ('-g', '--group'): NODESET.add_group(arg) continue if opt in ('-x', '--exclude'): NODESET.exclude_node(arg) continue if opt in ('-X', '--exclude-group'): NODESET.exclude_group(arg) continue if opt in ('-o', '--options'): SSH_OPTIONS = arg continue if opt in ('-M', '--master', '--multiplex'): OPT_MULTIPLEX = True continue if opt in ('-P', '--persist'): PERSIST = arg # spellcheck it later continue if opt == '-O': if CTL_CMD != None: print "%s: only a single '-O' option can be given" % PROGNAME sys.exit(1) if arg not in ('check', 'stop', 'exit'): print "%s: unknown control command '%s'" % (PROGNAME, arg) sys.exit(1) CTL_CMD = arg continue if opt in ('-N', '--numproc'): try: param.NUM_PROC = int(arg) except ValueError: print("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.NUM_PROC < 1: print '%s: invalid value for numproc' % PROGNAME sys.exit(1) continue if opt in ('-z', '--zzz'): try: param.SLEEP_TIME = int(arg) except ValueError: print("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.SLEEP_TIME < 0: print '%s: invalid value for sleep time' % PROGNAME sys.exit(1) if not param.SLEEP_TIME: # (temporarily) set to -1 to indicate we want # to run serialized # synctool.parallel.do() will use this param.SLEEP_TIME = -1 continue if opt in ('-a', '--aggregate'): OPT_AGGREGATE = True continue if opt == '--no-nodename': synctool.lib.OPT_NODENAME = False continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue if opt == '--skip-rsync': OPT_SKIP_RSYNC = True continue if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if not OPT_MULTIPLEX and PERSIST is not None: print '%s: option --persist requires option --master' % PROGNAME sys.exit(1) if OPT_MULTIPLEX and CTL_CMD != None: print '%s: options --master and -O can not be combined' % PROGNAME sys.exit(1) if OPT_MULTIPLEX or CTL_CMD != None: if len(args) > 0: print '%s: excessive arguments on command-line' % PROGNAME sys.exit(1) elif not args: print '%s: missing remote command' % PROGNAME sys.exit(1) if len(args) > 0: MASTER_OPTS.extend(args) return args
def main(): '''do your thing''' param.init() get_options() if ACTION == ACTION_VERSION: print param.VERSION sys.exit(0) if ACTION == ACTION_FQDN: print socket.getfqdn() sys.exit(0) config.read_config() # synctool.nodeset.make_default_nodeset() if ACTION == ACTION_LIST_NODES: list_all_nodes() elif ACTION == ACTION_LIST_GROUPS: list_all_groups() elif ACTION == ACTION_NODES: if not ARG_NODENAMES: error("option '--node' requires an argument; the node name") sys.exit(1) list_nodes(ARG_NODENAMES) elif ACTION == ACTION_GROUPS: if not ARG_GROUPS: error("option '--node-group' requires an argument; " "the node group name") sys.exit(1) list_nodegroups(ARG_GROUPS) elif ACTION == ACTION_CMDS: list_commands(ARG_CMDS) elif ACTION == ACTION_PKGMGR: print param.PACKAGE_MANAGER elif ACTION == ACTION_NUMPROC: print param.NUM_PROC elif ACTION == ACTION_LIST_DIRS: list_dirs() elif ACTION == ACTION_PREFIX: print param.ROOTDIR elif ACTION == ACTION_NODENAME: config.init_mynodename() if not param.NODENAME: error('unable to determine my nodename (%s)' % param.HOSTNAME) stderr('please check %s' % param.CONF_FILE) sys.exit(1) print param.NODENAME elif ACTION == ACTION_MASTER: print param.MASTER elif ACTION == ACTION_SLAVE: if not len(param.SLAVES): print '(none)' else: for node in param.SLAVES: print node, print elif ACTION == ACTION_EXPAND: if not ARG_EXPAND: print 'none' else: expand(ARG_EXPAND) else: raise RuntimeError('bug: unknown ACTION code %d' % ACTION)
def get_options(): # type: () -> None '''parse command-line options''' global PASS_ARGS, OPT_SKIP_RSYNC, OPT_AGGREGATE global OPT_CHECK_UPDATE, OPT_DOWNLOAD, MASTER_OPTS global UPLOAD_FILE # check for typo's on the command-line; # things like "-diff" will trigger "-f" => "--fix" be_careful_with_getopt() try: opts, args = getopt.getopt(sys.argv[1:], 'hc:vn:g:x:X:d:1:r:u:s:o:p:efN:FTqaS', ['help', 'conf=', 'verbose', 'node=', 'group=', 'exclude=', 'exclude-group=', 'diff=', 'single=', 'ref=', 'upload=', 'suffix=', 'overlay=', 'purge=', 'erase-saved', 'fix', 'no-post', 'numproc=', 'fullpath', 'terse', 'color', 'no-color', 'quiet', 'aggregate', 'unix', 'skip-rsync', 'version', 'check-update', 'download']) except getopt.GetoptError as reason: print '%s: %s' % (PROGNAME, reason) # usage() sys.exit(1) if args: error('excessive arguments on command line') sys.exit(1) UPLOAD_FILE = synctool.upload.UploadFile() # these are only used for checking the validity of option combinations opt_diff = False opt_single = False opt_reference = False opt_erase_saved = False opt_upload = False opt_suffix = False opt_overlay = False opt_purge = False opt_fix = False opt_group = False PASS_ARGS = [] MASTER_OPTS = [sys.argv[0],] # first read the config file for opt, arg in opts: if opt in ('-h', '--help', '-?'): usage() sys.exit(1) if opt in ('-c', '--conf'): param.CONF_FILE = arg PASS_ARGS.append(opt) PASS_ARGS.append(arg) continue # these options influence program output, so process them # as soon as possible, even before reading the config file if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue if opt in ('-T', '--terse'): param.TERSE = True param.FULL_PATH = False continue if opt in ('-F', '--fullpath'): param.FULL_PATH = True continue if opt == '--version': print param.VERSION sys.exit(0) config.read_config() synctool.nodeset.make_default_nodeset() check_cmd_config() # then process all the other options # # Note: some options are passed on to synctool on the node, while # others are not. Therefore some 'continue', while others don't for opt, arg in opts: if opt: MASTER_OPTS.append(opt) if arg: MASTER_OPTS.append(arg) if opt in ('-h', '--help', '-?', '-c', '--conf', '--version'): # already done continue if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True if opt in ('-n', '--node'): NODESET.add_node(arg) if not UPLOAD_FILE.node: UPLOAD_FILE.node = arg continue if opt in ('-g', '--group'): NODESET.add_group(arg) opt_group = True continue if opt in ('-x', '--exclude'): NODESET.exclude_node(arg) continue if opt in ('-X', '--exclude-group'): NODESET.exclude_group(arg) continue if opt in ('-d', '--diff'): opt_diff = True if opt in ('-1', '--single'): opt_single = True if opt in ('-r', '--ref'): opt_reference = True if opt in ('-u', '--upload'): opt_upload = True UPLOAD_FILE.filename = arg continue if opt in ('-s', '--suffix'): opt_suffix = True UPLOAD_FILE.suffix = arg continue if opt in ('-o', '--overlay'): opt_overlay = True UPLOAD_FILE.overlay = arg continue if opt in ('-p', '--purge'): opt_purge = True UPLOAD_FILE.purge = arg continue if opt in ('-e', '--erase-saved'): opt_erase_saved = True if opt in ('-q', '--quiet'): synctool.lib.QUIET = True if opt in ('-f', '--fix'): opt_fix = True synctool.lib.DRY_RUN = False if opt == '--no-post': synctool.lib.NO_POST = True if opt in ('-N', '--numproc'): try: param.NUM_PROC = int(arg) except ValueError: print "option '%s' requires a numeric value" % opt sys.exit(1) if param.NUM_PROC < 1: print 'invalid value for numproc' sys.exit(1) continue if opt in ('-F', '--fullpath'): param.FULL_PATH = True param.TERSE = False if opt in ('-T', '--terse'): param.TERSE = True param.FULL_PATH = False if opt == '--color': param.COLORIZE = True if opt == '--no-color': param.COLORIZE = False if opt in ('-a', '--aggregate'): OPT_AGGREGATE = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True if opt in ('-S', '--skip-rsync'): OPT_SKIP_RSYNC = True continue if opt == '--check-update': OPT_CHECK_UPDATE = True continue if opt == '--download': OPT_DOWNLOAD = True continue if opt: PASS_ARGS.append(opt) if arg: PASS_ARGS.append(arg) # diff with fix works like single if opt_diff and opt_fix: opt_diff = False opt_single = True # do basic checks for uploading and sub options if opt_suffix and not opt_upload: print 'option --suffix must be used in conjunction with --upload' sys.exit(1) if opt_overlay and not opt_upload: print 'option --overlay must be used in conjunction with --upload' sys.exit(1) if opt_purge: if not opt_upload: print 'option --purge must be used in conjunction with --upload' sys.exit(1) if opt_overlay: print 'option --overlay and --purge can not be combined' sys.exit(1) if opt_suffix: print 'option --suffix and --purge can not be combined' sys.exit(1) # enable logging at the master node PASS_ARGS.append('--masterlog') if args != None: MASTER_OPTS.extend(args) PASS_ARGS.extend(args) option_combinations(opt_diff, opt_single, opt_reference, opt_erase_saved, opt_upload, opt_fix, opt_group)
def get_options(): '''parse command-line options''' global PASS_ARGS, OPT_SKIP_RSYNC, OPT_AGGREGATE global OPT_CHECK_UPDATE, OPT_DOWNLOAD, MASTER_OPTS global UPLOAD_FILE # check for typo's on the command-line; # things like "-diff" will trigger "-f" => "--fix" be_careful_with_getopt() try: opts, args = getopt.getopt(sys.argv[1:], 'hc:vn:g:x:X:d:1:r:u:s:o:p:efN:FTqaS', ['help', 'conf=', 'verbose', 'node=', 'group=', 'exclude=', 'exclude-group=', 'diff=', 'single=', 'ref=', 'upload=', 'suffix=', 'overlay=', 'purge=', 'erase-saved', 'fix', 'no-post', 'numproc=', 'fullpath', 'terse', 'color', 'no-color', 'quiet', 'aggregate', 'unix', 'skip-rsync', 'version', 'check-update', 'download']) except getopt.GetoptError as reason: print '%s: %s' % (PROGNAME, reason) # usage() sys.exit(1) if args != None and len(args) > 0: error('excessive arguments on command line') sys.exit(1) UPLOAD_FILE = synctool.upload.UploadFile() # these are only used for checking the validity of option combinations opt_diff = False opt_single = False opt_reference = False opt_erase_saved = False opt_upload = False opt_suffix = False opt_overlay = False opt_purge = False opt_fix = False opt_group = False PASS_ARGS = [] MASTER_OPTS = [sys.argv[0],] # first read the config file for opt, arg in opts: if opt in ('-h', '--help', '-?'): usage() sys.exit(1) if opt in ('-c', '--conf'): param.CONF_FILE = arg PASS_ARGS.append(opt) PASS_ARGS.append(arg) continue # these options influence program output, so process them # as soon as possible, even before reading the config file if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue if opt in ('-T', '--terse'): param.TERSE = True param.FULL_PATH = False continue if opt in ('-F', '--fullpath'): param.FULL_PATH = True continue if opt == '--version': print param.VERSION sys.exit(0) config.read_config() synctool.nodeset.make_default_nodeset() check_cmd_config() # then process all the other options # # Note: some options are passed on to synctool on the node, while # others are not. Therefore some 'continue', while others don't for opt, arg in opts: if opt: MASTER_OPTS.append(opt) if arg: MASTER_OPTS.append(arg) if opt in ('-h', '--help', '-?', '-c', '--conf', '--version'): # already done continue if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True if opt in ('-n', '--node'): NODESET.add_node(arg) if not UPLOAD_FILE.node: UPLOAD_FILE.node = arg continue if opt in ('-g', '--group'): NODESET.add_group(arg) opt_group = True continue if opt in ('-x', '--exclude'): NODESET.exclude_node(arg) continue if opt in ('-X', '--exclude-group'): NODESET.exclude_group(arg) continue if opt in ('-d', '--diff'): opt_diff = True if opt in ('-1', '--single'): opt_single = True if opt in ('-r', '--ref'): opt_reference = True if opt in ('-u', '--upload'): opt_upload = True UPLOAD_FILE.filename = arg continue if opt in ('-s', '--suffix'): opt_suffix = True UPLOAD_FILE.suffix = arg continue if opt in ('-o', '--overlay'): opt_overlay = True UPLOAD_FILE.overlay = arg continue if opt in ('-p', '--purge'): opt_purge = True UPLOAD_FILE.purge = arg continue if opt in ('-e', '--erase-saved'): opt_erase_saved = True if opt in ('-q', '--quiet'): synctool.lib.QUIET = True if opt in ('-f', '--fix'): opt_fix = True synctool.lib.DRY_RUN = False if opt == '--no-post': synctool.lib.NO_POST = True if opt in ('-N', '--numproc'): try: param.NUM_PROC = int(arg) except ValueError: print "option '%s' requires a numeric value" % opt sys.exit(1) if param.NUM_PROC < 1: print 'invalid value for numproc' sys.exit(1) continue if opt in ('-F', '--fullpath'): param.FULL_PATH = True param.TERSE = False if opt in ('-T', '--terse'): param.TERSE = True param.FULL_PATH = False if opt == '--color': param.COLORIZE = True if opt == '--no-color': param.COLORIZE = False if opt in ('-a', '--aggregate'): OPT_AGGREGATE = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True if opt in ('-S', '--skip-rsync'): OPT_SKIP_RSYNC = True continue if opt == '--check-update': OPT_CHECK_UPDATE = True continue if opt == '--download': OPT_DOWNLOAD = True continue if opt: PASS_ARGS.append(opt) if arg: PASS_ARGS.append(arg) # diff with fix works like single if opt_diff and opt_fix: opt_diff = False opt_single = True # do basic checks for uploading and sub options if opt_suffix and not opt_upload: print 'option --suffix must be used in conjunction with --upload' sys.exit(1) if opt_overlay and not opt_upload: print 'option --overlay must be used in conjunction with --upload' sys.exit(1) if opt_purge: if not opt_upload: print 'option --purge must be used in conjunction with --upload' sys.exit(1) if opt_overlay: print 'option --overlay and --purge can not be combined' sys.exit(1) if opt_suffix: print 'option --suffix and --purge can not be combined' sys.exit(1) # enable logging at the master node PASS_ARGS.append('--masterlog') if args != None: MASTER_OPTS.extend(args) PASS_ARGS.extend(args) option_combinations(opt_diff, opt_single, opt_reference, opt_erase_saved, opt_upload, opt_fix, opt_group)
def get_options(): # type: () -> List[str] '''parse command-line options''' global DESTDIR, MASTER_OPTS, OPT_AGGREGATE, DSH_CP_OPTIONS, OPT_PURGE if len(sys.argv) <= 1: usage() sys.exit(1) DESTDIR = None DSH_CP_OPTIONS = None try: opts, args = getopt.getopt(sys.argv[1:], 'hc:n:g:x:X:o:pN:z:vqaf', [ 'help', 'conf=', 'node=', 'group=', 'exclude=', 'exclude-group=', 'options=', 'purge', 'no-nodename', 'numproc=', 'zzz=', 'unix', 'verbose', 'quiet', 'aggregate', 'fix' ]) except getopt.GetoptError as reason: print '%s: %s' % (PROGNAME, reason) # usage() sys.exit(1) # first read the config file for opt, arg in opts: if opt in ('-h', '--help', '-?'): usage() sys.exit(1) if opt in ('-c', '--conf'): param.CONF_FILE = arg continue # these options influence program output, so process them # as soon as possible, even before reading the config file if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue config.read_config() synctool.nodeset.make_default_nodeset() check_cmd_config() # then process the other options MASTER_OPTS = [ sys.argv[0], ] for opt, arg in opts: if opt: MASTER_OPTS.append(opt) if arg: MASTER_OPTS.append(arg) if opt in ('-h', '--help', '-?', '-c', '--conf'): # already done continue if opt in ('-n', '--node'): NODESET.add_node(arg) continue if opt in ('-g', '--group'): NODESET.add_group(arg) continue if opt in ('-x', '--exclude'): NODESET.exclude_node(arg) continue if opt in ('-X', '--exclude-group'): NODESET.exclude_group(arg) continue if opt in ('-o', '--options'): DSH_CP_OPTIONS = arg continue if opt in ('-p', '--purge'): OPT_PURGE = True continue if opt == '--no-nodename': synctool.lib.OPT_NODENAME = False continue if opt in ('-N', '--numproc'): try: param.NUM_PROC = int(arg) except ValueError: print("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.NUM_PROC < 1: print '%s: invalid value for numproc' % PROGNAME sys.exit(1) continue if opt in ('-z', '--zzz'): try: param.SLEEP_TIME = int(arg) except ValueError: print("%s: option '%s' requires a numeric value" % (PROGNAME, opt)) sys.exit(1) if param.SLEEP_TIME < 0: print '%s: invalid value for sleep time' % PROGNAME sys.exit(1) if not param.SLEEP_TIME: # (temporarily) set to -1 to indicate we want # to run serialized # synctool.lib.multiprocess() will use this param.SLEEP_TIME = -1 continue if opt == '--unix': synctool.lib.UNIX_CMD = True continue if opt in ('-v', '--verbose'): synctool.lib.VERBOSE = True continue if opt in ('-q', '--quiet'): synctool.lib.QUIET = True continue if opt in ('-a', '--aggregate'): OPT_AGGREGATE = True continue if opt in ('-f', '--fix'): synctool.lib.DRY_RUN = False continue if not args: print '%s: missing file to copy' % PROGNAME sys.exit(1) if len(args) < 2: print '%s: missing destination' % PROGNAME sys.exit(1) MASTER_OPTS.extend(args) DESTDIR = args.pop(-1) # dest may be ':' meaning that we want to copy the source dirname if DESTDIR == ':': if os.path.isdir(args[0]): DESTDIR = args[0] else: DESTDIR = os.path.dirname(args[0]) # DESTDIR[0] == ':' would create "rsync to node::module" # which is something we don't want if not DESTDIR or DESTDIR[0] == ':': print '%s: invalid destination' % PROGNAME sys.exit(1) # ensure trailing slash if DESTDIR[-1] != os.sep: DESTDIR += os.sep return args