diff options
author | Konstantin Ryabitsev <konstantin@linuxfoundation.org> | 2020-09-04 16:16:06 -0400 |
---|---|---|
committer | Konstantin Ryabitsev <konstantin@linuxfoundation.org> | 2020-09-04 16:16:06 -0400 |
commit | c63d2e96efdf2a413b3cea37f85a6bb8186c0bc7 (patch) | |
tree | 1df764f68759a18e455eec97b5f2a6fb495c7707 | |
parent | 2b077c319b2796b5fc9c17deec38966b462c511e (diff) | |
download | grokmirror-c63d2e96efdf2a413b3cea37f85a6bb8186c0bc7.tar.gz |
Use argparse instead of deprecated optparse
This was long overdue but is now done.
Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
-rw-r--r-- | grokmirror/bundle.py | 1 | ||||
-rwxr-xr-x | grokmirror/dumb_pull.py | 65 | ||||
-rwxr-xr-x | grokmirror/fsck.py | 66 | ||||
-rwxr-xr-x | grokmirror/manifest.py | 120 | ||||
-rwxr-xr-x | grokmirror/pull.py | 60 |
5 files changed, 153 insertions, 159 deletions
diff --git a/grokmirror/bundle.py b/grokmirror/bundle.py index b72f3c0..5d69536 100644 --- a/grokmirror/bundle.py +++ b/grokmirror/bundle.py @@ -132,6 +132,7 @@ def parse_args(): op.add_argument('-i', '--include', nargs='*', default='*', help='List repositories to bundle (accepts shell globbing)') + op.add_argument('--version', action='version', version=grokmirror.VERSION) opts = op.parse_args() diff --git a/grokmirror/dumb_pull.py b/grokmirror/dumb_pull.py index fed2bd0..8bef771 100755 --- a/grokmirror/dumb_pull.py +++ b/grokmirror/dumb_pull.py @@ -147,40 +147,39 @@ def run_post_update_hook(hookscript, gitdir): def parse_args(): - from optparse import OptionParser - - usage = '''usage: %prog [options] /path/to/repos - Bluntly fetch remotes in specified git repositories. - ''' - - op = OptionParser(usage=usage, version=grokmirror.VERSION) - op.add_option('-v', '--verbose', dest='verbose', action='store_true', - default=False, - help='Be verbose and tell us what you are doing') - op.add_option('-s', '--svn', dest='svn', action='store_true', - default=False, - help='The remotes for these repositories are Subversion') - op.add_option('-r', '--remote-names', dest='remotes', action='append', - default=[], - help='Only fetch remotes matching this name (accepts ' - 'shell globbing, can be passed multiple times)') - op.add_option('-u', '--post-update-hook', dest='posthook', - default='', - help='Run this hook after each repository is updated. Passes ' - 'full path to the repository as the sole argument') - op.add_option('-l', '--logfile', dest='logfile', - default=None, - help='Put debug logs into this file') - - opts, args = op.parse_args() - - if not len(args): + import argparse + # noinspection PyTypeChecker + op = argparse.ArgumentParser(prog='grok-dumb-pull', + description='Fetch remotes in repositories not managed by grokmirror', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + op.add_argument('-v', '--verbose', dest='verbose', action='store_true', + default=False, + help='Be verbose and tell us what you are doing') + op.add_argument('-s', '--svn', dest='svn', action='store_true', + default=False, + help='The remotes for these repositories are Subversion') + op.add_argument('-r', '--remote-names', dest='remotes', action='append', + default=None, + help='Only fetch remotes matching this name (accepts shell globbing)') + op.add_argument('-u', '--post-update-hook', dest='posthook', + default='', + help='Run this hook after each repository is updated.') + op.add_argument('-l', '--logfile', dest='logfile', + default=None, + help='Put debug logs into this file') + op.add_argument('--version', action='version', version=grokmirror.VERSION) + op.add_argument('paths', nargs='+', help='Full path(s) of the repos to pull') + + opts = op.parse_args() + + if not len(opts.paths): op.error('You must provide at least a path to the repos to pull') - return opts, args + return opts -def dumb_pull(args, verbose=False, svn=False, remotes=None, posthook='', logfile=None): +def dumb_pull(paths, verbose=False, svn=False, remotes=None, posthook='', logfile=None): global logger loglevel = logging.INFO @@ -190,7 +189,7 @@ def dumb_pull(args, verbose=False, svn=False, remotes=None, posthook='', logfile remotes = ['*'] # Find all repositories we are to pull - for entry in args: + for entry in paths: if entry[-4:] == '.git': if not os.path.exists(entry): logger.critical('%s does not exist', entry) @@ -210,10 +209,10 @@ def dumb_pull(args, verbose=False, svn=False, remotes=None, posthook='', logfile def command(): - opts, args = parse_args() + opts = parse_args() return dumb_pull( - args, verbose=opts.verbose, svn=opts.svn, remotes=opts.remotes, + opts.paths, verbose=opts.verbose, svn=opts.svn, remotes=opts.remotes, posthook=opts.posthook, logfile=opts.logfile) diff --git a/grokmirror/fsck.py b/grokmirror/fsck.py index 7d98ab6..37dfe6c 100755 --- a/grokmirror/fsck.py +++ b/grokmirror/fsck.py @@ -1075,43 +1075,41 @@ def fsck_mirror(config, force=False, repack_only=False, conn_only=False, def parse_args(): - from optparse import OptionParser - - usage = '''usage: %prog -c fsck.conf - Run a git-fsck check on grokmirror-managed repositories. - ''' - - op = OptionParser(usage=usage, version=grokmirror.VERSION) - op.add_option('-v', '--verbose', dest='verbose', action='store_true', - default=False, - help='Be verbose and tell us what you are doing') - op.add_option('-f', '--force', dest='force', - action='store_true', default=False, - help='Force immediate run on all repositories') - op.add_option('-c', '--config', dest='config', - help='Location of the configuration file') - op.add_option('--repack-only', dest='repack_only', - action='store_true', default=False, - help='Only find and repack repositories that need optimizing') - op.add_option('--connectivity-only', dest='conn_only', - action='store_true', default=False, - help='Only check connectivity when running fsck checks') - op.add_option('--repack-all-quick', dest='repack_all_quick', - action='store_true', default=False, - help='(Assumes --force): Do a quick repack of all repos') - op.add_option('--repack-all-full', dest='repack_all_full', - action='store_true', default=False, - help='(Assumes --force): Do a full repack of all repos') - - opts, args = op.parse_args() + import argparse + # noinspection PyTypeChecker + op = argparse.ArgumentParser(prog='grok-fsck', + description='Optimize and check mirrored repositories', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + op.add_argument('-v', '--verbose', dest='verbose', action='store_true', + default=False, + help='Be verbose and tell us what you are doing') + op.add_argument('-f', '--force', dest='force', + action='store_true', default=False, + help='Force immediate run on all repositories') + op.add_argument('-c', '--config', dest='config', + required=True, + help='Location of the configuration file') + op.add_argument('--repack-only', dest='repack_only', + action='store_true', default=False, + help='Only find and repack repositories that need optimizing') + op.add_argument('--connectivity-only', dest='conn_only', + action='store_true', default=False, + help='Only check connectivity when running fsck checks') + op.add_argument('--repack-all-quick', dest='repack_all_quick', + action='store_true', default=False, + help='(Assumes --force): Do a quick repack of all repos') + op.add_argument('--repack-all-full', dest='repack_all_full', + action='store_true', default=False, + help='(Assumes --force): Do a full repack of all repos') + op.add_argument('--version', action='version', version=grokmirror.VERSION) + + opts = op.parse_args() if opts.repack_all_quick and opts.repack_all_full: op.error('Pick either --repack-all-full or --repack-all-quick') - if not opts.config: - op.error('You must provide the path to the config file') - - return opts, args + return opts def grok_fsck(cfgfile, verbose=False, force=False, repack_only=False, conn_only=False, @@ -1162,7 +1160,7 @@ def grok_fsck(cfgfile, verbose=False, force=False, repack_only=False, conn_only= def command(): - opts, args = parse_args() + opts = parse_args() return grok_fsck(opts.config, opts.verbose, opts.force, opts.repack_only, opts.conn_only, opts.repack_all_quick, opts.repack_all_full) diff --git a/grokmirror/manifest.py b/grokmirror/manifest.py index c9df146..e11ef78 100755 --- a/grokmirror/manifest.py +++ b/grokmirror/manifest.py @@ -109,54 +109,54 @@ def purge_manifest(manifest, toplevel, gitdirs): def parse_args(): - from optparse import OptionParser - - usage = '''usage: %prog -m manifest.js[.gz] -t /path [/path/to/bare.git] - Create or update manifest.js with the latest repository information. - ''' - - op = OptionParser(usage=usage, version=grokmirror.VERSION) - op.add_option('', '--cfgfile', dest='cfgfile', - default=None, - help='Path to grokmirror.conf containing a [manifest] section') - op.add_option('-m', '--manifest', dest='manifile', - help='Location of manifest.js or manifest.js.gz') - op.add_option('-t', '--toplevel', dest='toplevel', - help='Top dir where all repositories reside') - op.add_option('-l', '--logfile', dest='logfile', - default=None, - help='When specified, will put debug logs in this location') - op.add_option('-n', '--use-now', dest='usenow', action='store_true', - default=False, - help='Use current timestamp instead of parsing commits') - op.add_option('-c', '--check-export-ok', dest='check_export_ok', - action='store_true', default=False, - help='Export only repositories marked as git-daemon-export-ok') - op.add_option('-p', '--purge', dest='purge', action='store_true', - default=False, - help='Purge deleted git repositories from manifest') - op.add_option('-x', '--remove', dest='remove', action='store_true', - default=False, - help='Remove repositories passed as arguments from manifest') - op.add_option('-y', '--pretty', dest='pretty', action='store_true', - default=False, - help='Pretty-print manifest (sort keys and add indentation)') - op.add_option('-i', '--ignore-paths', dest='ignore', action='append', - default=None, - help='When finding git dirs, ignore these paths ' - '(can be used multiple times, accepts shell-style globbing wildcards)') - op.add_option('-w', '--wait-for-manifest', dest='wait', - action='store_true', default=False, - help='When running with arguments, wait if manifest is not there ' - '(can be useful when multiple writers are writing the manifest)') - op.add_option('-o', '--fetch-objstore', dest='fetchobst', - action='store_true', default=False, - help='Fetch updates into objstore repo (if used)') - op.add_option('-v', '--verbose', dest='verbose', action='store_true', - default=False, - help='Be verbose and tell us what you are doing') - - opts, args = op.parse_args() + import argparse + # noinspection PyTypeChecker + op = argparse.ArgumentParser(prog='grok-manifest', + description='Create or update a manifest file', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + op.add_argument('--cfgfile', dest='cfgfile', + default=None, + help='Path to grokmirror.conf containing a [manifest] section') + op.add_argument('-m', '--manifest', dest='manifile', + help='Location of manifest.js or manifest.js.gz') + op.add_argument('-t', '--toplevel', dest='toplevel', + help='Top dir where all repositories reside') + op.add_argument('-l', '--logfile', dest='logfile', + default=None, + help='When specified, will put debug logs in this location') + op.add_argument('-n', '--use-now', dest='usenow', action='store_true', + default=False, + help='Use current timestamp instead of parsing commits') + op.add_argument('-c', '--check-export-ok', dest='check_export_ok', + action='store_true', default=False, + help='Export only repositories marked as git-daemon-export-ok') + op.add_argument('-p', '--purge', dest='purge', action='store_true', + default=False, + help='Purge deleted git repositories from manifest') + op.add_argument('-x', '--remove', dest='remove', action='store_true', + default=False, + help='Remove repositories passed as arguments from manifest') + op.add_argument('-y', '--pretty', dest='pretty', action='store_true', + default=False, + help='Pretty-print manifest (sort keys and add indentation)') + op.add_argument('-i', '--ignore-paths', dest='ignore', action='append', + default=None, + help='When finding git dirs, ignore these paths (accepts shell-style globbing)') + op.add_argument('-w', '--wait-for-manifest', dest='wait', + action='store_true', default=False, + help='When running with arguments, wait if manifest is not there ' + '(can be useful when multiple writers are writing the manifest)') + op.add_argument('-o', '--fetch-objstore', dest='fetchobst', + action='store_true', default=False, + help='Fetch updates into objstore repo (if used)') + op.add_argument('-v', '--verbose', dest='verbose', action='store_true', + default=False, + help='Be verbose and tell us what you are doing') + op.add_argument('--version', action='version', version=grokmirror.VERSION) + op.add_argument('paths', nargs='*', help='Full path(s) to process') + + opts = op.parse_args() if opts.cfgfile: config = grokmirror.load_config_file(opts.cfgfile) @@ -184,13 +184,13 @@ def parse_args(): if opts.ignore is None: opts.ignore = list() - if not len(args) and opts.wait: + if not len(opts.paths) and opts.wait: op.error('--wait option only makes sense when dirs are passed') - return opts, args + return opts -def grok_manifest(manifile, toplevel, args=None, logfile=None, usenow=False, +def grok_manifest(manifile, toplevel, paths=None, logfile=None, usenow=False, check_export_ok=False, purge=False, remove=False, pretty=False, ignore=None, wait=False, verbose=False, fetchobst=False): global logger @@ -198,8 +198,8 @@ def grok_manifest(manifile, toplevel, args=None, logfile=None, usenow=False, logger = grokmirror.init_logger('manifest', logfile, loglevel, verbose) startt = datetime.datetime.now() - if args is None: - args = list() + if paths is None: + paths = list() if ignore is None: ignore = list() @@ -212,9 +212,9 @@ def grok_manifest(manifile, toplevel, args=None, logfile=None, usenow=False, if not len(manifest.keys()): usenow = False - if remove and len(args): + if remove and len(paths): # Remove the repos as required, write new manfiest and exit - for fullpath in args: + for fullpath in paths: repo = '/' + os.path.relpath(fullpath, toplevel) if repo in manifest: manifest.pop(repo) @@ -232,17 +232,17 @@ def grok_manifest(manifile, toplevel, args=None, logfile=None, usenow=False, gitdirs = list() - if purge or not len(args) or not len(manifest): + if purge or not len(paths) or not len(manifest): # We automatically purge when we do a full tree walk for gitdir in grokmirror.find_all_gitdirs(toplevel, ignore=ignore, exclude_objstore=True): gitdirs.append(gitdir) purge_manifest(manifest, toplevel, gitdirs) - if len(manifest) and len(args): + if len(manifest) and len(paths): # limit ourselves to passed dirs only when there is something # in the manifest. This precaution makes sure we regenerate the # whole file when there is nothing in it or it can't be parsed. - gitdirs = [os.path.realpath(x) for x in args] + gitdirs = [os.path.realpath(x) for x in paths] # Don't draw a progress bar for a single repo symlinks = list() @@ -293,10 +293,10 @@ def grok_manifest(manifile, toplevel, args=None, logfile=None, usenow=False, def command(): - opts, args = parse_args() + opts = parse_args() return grok_manifest( - opts.manifile, opts.toplevel, args=args, logfile=opts.logfile, + opts.manifile, opts.toplevel, paths=opts.paths, logfile=opts.logfile, usenow=opts.usenow, check_export_ok=opts.check_export_ok, purge=opts.purge, remove=opts.remove, pretty=opts.pretty, ignore=opts.ignore, wait=opts.wait, verbose=opts.verbose, diff --git a/grokmirror/pull.py b/grokmirror/pull.py index 6ed8a1c..ca86570 100755 --- a/grokmirror/pull.py +++ b/grokmirror/pull.py @@ -1250,37 +1250,33 @@ def pull_mirror(config, nomtime=False, forcepurge=False, runonce=False): def parse_args(): - from optparse import OptionParser - - usage = '''usage: %prog -c repos.conf - Create a grok mirror using the repository configuration found in repos.conf - ''' - - op = OptionParser(usage=usage, version=grokmirror.VERSION) - op.add_option('-v', '--verbose', dest='verbose', action='store_true', - default=False, - help='Be verbose and tell us what you are doing') - op.add_option('-n', '--no-mtime-check', dest='nomtime', - action='store_true', default=False, - help='Run without checking manifest mtime') - op.add_option('-p', '--purge', dest='purge', - action='store_true', default=False, - help='Remove any git trees that are no longer in manifest') - op.add_option('--force-purge', dest='forcepurge', - action='store_true', default=False, - help='Force purge despite significant repo deletions') - op.add_option('-o', '--continuous', dest='runonce', - action='store_false', default=True, - help='Run continuously (no effect if refresh is not set in config)') - op.add_option('-c', '--config', dest='config', - help='Location of the configuration file') - - opts, args = op.parse_args() - - if not opts.config: - op.error('You must provide the path to the config file') - - return opts, args + import argparse + # noinspection PyTypeChecker + op = argparse.ArgumentParser(prog='grok-pull', + description='Create or update a git repository collection mirror', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + op.add_argument('-v', '--verbose', dest='verbose', action='store_true', + default=False, + help='Be verbose and tell us what you are doing') + op.add_argument('-n', '--no-mtime-check', dest='nomtime', + action='store_true', default=False, + help='Run without checking manifest mtime') + op.add_argument('-p', '--purge', dest='purge', + action='store_true', default=False, + help='Remove any git trees that are no longer in manifest') + op.add_argument('--force-purge', dest='forcepurge', + action='store_true', default=False, + help='Force purge despite significant repo deletions') + op.add_argument('-o', '--continuous', dest='runonce', + action='store_false', default=True, + help='Run continuously (no effect if refresh is not set in config)') + op.add_argument('-c', '--config', dest='config', + required=True, + help='Location of the configuration file') + op.add_argument('--version', action='version', version=grokmirror.VERSION) + + return op.parse_args() def grok_pull(cfgfile, verbose=False, nomtime=False, purge=False, forcepurge=False, runonce=False): @@ -1306,7 +1302,7 @@ def grok_pull(cfgfile, verbose=False, nomtime=False, purge=False, forcepurge=Fal def command(): - opts, args = parse_args() + opts = parse_args() retval = grok_pull( opts.config, opts.verbose, opts.nomtime, opts.purge, opts.forcepurge, opts.runonce) |