Skip to content

Commit

Permalink
1.3.1dev: Replace optparse with argparse
Browse files Browse the repository at this point in the history
Refs #12148.


git-svn-id: http://trac.edgewall.org/intertrac/log:/trunk@15064 af82e41b-90c4-0310-8c96-b1721e28e2e2
  • Loading branch information
rjollos committed Aug 19, 2016
1 parent 4f6876f commit 4d0afa3
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 311 deletions.
51 changes: 30 additions & 21 deletions contrib/checkwiki.py
Expand Up @@ -12,6 +12,7 @@
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/.

import argparse
import os
import sys
from pkg_resources import resource_listdir, resource_string
Expand Down Expand Up @@ -114,16 +115,24 @@ def write(self, data):
pass


def parse_args():
from optparse import OptionParser
parser = OptionParser(usage='Usage: %prog [options] [PAGES...]')
parser.add_option('-d', '--download', dest='download', default=False,
action='store_true',
help='Download default pages from trac.edgewall.org '
'before checking')
parser.add_option('-p', '--prefix', dest='prefix', default='',
help='Prepend "prefix/" to the page when downloading')
return parser.parse_args()
def parse_args(all_pages):
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--download', action='store_true',
help="download default pages from trac.edgewall.org "
"before checking")
parser.add_argument('-p', '--prefix', default='',
help="prepend PREFIX/ to the page name when "
"downloading")
parser.add_argument('pages', metavar='page', nargs='*',
help="the wiki page(s) to download and/or check")

args = parser.parse_args()
if args.pages:
for page in args.pages:
if page not in all_pages:
parser.error("%s is not one of the default pages." % page)

return args


def download_default_pages(names, prefix):
Expand Down Expand Up @@ -156,22 +165,22 @@ def download_default_pages(names, prefix):


def main():
options, args = parse_args()
names = sorted(name for name in resource_listdir('trac.wiki',
'default-pages')
if not name.startswith('.'))
if args:
args = sorted(set(names) & set(map(os.path.basename, args)))
all_pages = sorted(name for name
in resource_listdir('trac.wiki', 'default-pages')
if not name.startswith('.'))
args = parse_args(all_pages)
if args.pages:
pages = sorted(args.pages)
else:
args = names
pages = all_pages

if options.download:
download_default_pages(args, options.prefix)
if args.download:
download_default_pages(pages, args.prefix)

env = EnvironmentStub(disable=['trac.mimeview.pygments.*'])
load_components(env)
with env.db_transaction:
for name in names:
for name in all_pages:
wiki = WikiPage(env, name)
wiki.text = resource_string('trac.wiki', 'default-pages/' +
name).decode('utf-8')
Expand All @@ -182,7 +191,7 @@ def main():

req = Mock(href=Href('/'), abs_href=Href('http://localhost/'),
perm=MockPerm())
for name in args:
for name in pages:
wiki = WikiPage(env, name)
if not wiki.exists:
continue
Expand Down
129 changes: 66 additions & 63 deletions contrib/htdigest.py
Expand Up @@ -17,19 +17,21 @@

from __future__ import print_function

import argparse
import errno
import fileinput
import getpass
import hashlib
import sys
from getpass import getpass
from hashlib import md5
from optparse import OptionParser

from trac.util.text import printerr


def ask_pass():
pass1 = getpass('New password: ')
pass2 = getpass('Re-type new password: ')
pass1 = getpass.getpass('New password: ')
pass2 = getpass.getpass('Re-type new password: ')
if pass1 != pass2:
print("They don't match, sorry", file=sys.stderr)
printerr("htdigest: password verification error")
sys.exit(1)
return pass1

Expand All @@ -41,67 +43,68 @@ def get_digest(userprefix, password=None):


def make_digest(userprefix, password):
return userprefix + md5(userprefix + password).hexdigest()

return userprefix + hashlib.md5(userprefix + password).hexdigest()

usage = "%prog [-c] [-b] passwordfile realm username"
parser = OptionParser(usage=usage)
parser.add_option('-c', action='store_true', dest='create', default=False,
help="Create a new file")
parser.add_option('-b', action='store_true', dest='batch', default=False,
help="Batch mode, password on the commandline.")

if len(sys.argv) <= 1:
parser.print_help()
sys.exit(1)
def main():
"""
%(prog)s [-c] passwordfile realm username
%(prog)s -b[c] passwordfile realm username password\
"""
parser = argparse.ArgumentParser(usage=main.__doc__)
parser.add_argument('-b', action='store_true', dest='batch',
help="batch mode; password is passed on the command "
"line IN THE CLEAR")
parser.add_argument('-c', action='store_true', dest='create',
help="create a new htdigest file, overwriting any "
"existing file")
parser.add_argument('passwordfile', help=argparse.SUPPRESS)
parser.add_argument('realm', help=argparse.SUPPRESS)
parser.add_argument('username', help=argparse.SUPPRESS)
parser.add_argument('password', nargs='?', help=argparse.SUPPRESS)

opts, args = parser.parse_args()
args = parser.parse_args()
if args.batch and args.password is None:
parser.error("too few arguments")
elif not args.batch and args.password is not None:
parser.error("too many arguments")

try:
if opts.batch:
filename, realm, username, password = args
prefix = '%s:%s:' % (args.username, args.realm)
if args.create:
try:
with open(args.passwordfile, 'w') as f:
print(get_digest(prefix, args.password), file=f)
except EnvironmentError as e:
if e.errno == errno.EACCES:
printerr("Unable to update file %s" % args.passwordfile)
sys.exit(1)
else:
raise
else:
filename, realm, username = args
password = None
except ValueError:
parser.error("Wrong number of arguments")

prefix = '%s:%s:' % (username, realm)

if opts.create:
try:
f = open(filename, 'w')
except EnvironmentError as e:
if e.errno == errno.EACCES:
print("Unable to update file", filename, file=sys.stderr)
sys.exit(1)
else:
raise
try:
print(get_digest(prefix, password), file=f)
finally:
f.close()
else:
try:
matched = False
for line in fileinput.input(filename, inplace=True):
if line.startswith(prefix):
if not matched:
print(get_digest(prefix, password))
matched = True
try:
for line in fileinput.input(args.passwordfile, inplace=True):
if line.startswith(prefix):
if not matched:
print(get_digest(prefix, args.password))
matched = True
else:
print(line.rstrip())
if not matched:
with open(args.passwordfile, 'a') as f:
print(get_digest(prefix, args.password), file=f)
except EnvironmentError as e:
if e.errno == errno.ENOENT:
printerr("Could not open password file %s for reading. "
"Use -c option to create a new one."
% args.passwordfile)
sys.exit(1)
elif e.errno == errno.EACCES:
printerr("Unable to update file %s" % args.passwordfile)
sys.exit(1)
else:
print(line.rstrip())
if not matched:
with open(filename, 'a') as f:
print(get_digest(prefix, password), file=f)
except EnvironmentError as e:
if e.errno == errno.ENOENT:
print("Could not open passwd file %s for reading." % filename,
file=sys.stderr)
print("Use -c option to create a new one.", file=sys.stderr)
sys.exit(1)
elif e.errno == errno.EACCES:
print("Unable to update file", filename, file=sys.stderr)
sys.exit(1)
else:
raise
raise


if __name__ == '__main__':
main()
101 changes: 47 additions & 54 deletions contrib/htpasswd.py
Expand Up @@ -13,20 +13,29 @@
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/.

import argparse
import getpass
import optparse
import sys

from trac.util import salt
from trac.util.compat import crypt, wait_for_file_mtime_change
from trac.util.text import printerr, printout
from trac.util.text import printerr

if crypt is None:
printerr("The crypt module is not found. Install the passlib package "
"from PyPI.", newline=True)
sys.exit(1)


def ask_pass():
pass1 = getpass.getpass('New password: ')
pass2 = getpass.getpass('Re-type new password: ')
if pass1 != pass2:
printerr("htpasswd: password verification error")
sys.exit(1)
return pass1


class HtpasswdFile(object):
"""A class for manipulating htpasswd files."""

Expand Down Expand Up @@ -70,65 +79,49 @@ def delete(self, username):

def main():
"""
%prog [-c] filename username
%prog -b[c] filename username password
%prog -D filename username
%(prog)s [-c] passwordfile username
%(prog)s -b[c] passwordfile username password
%(prog)s -D passwordfile username\
"""
# For now, we only care about the use cases that affect tests/functional.py
parser = optparse.OptionParser(usage=main.__doc__)
parser.add_option('-b', action='store_true', dest='batch', default=False,
help='Batch mode; password is passed on the command line IN THE CLEAR.'
)
parser.add_option('-c', action='store_true', dest='create', default=False,
help='Create a new htpasswd file, overwriting any existing file.')
parser.add_option('-D', action='store_true', dest='delete_user',
default=False, help='Remove the given user from the password file.')

if len(sys.argv) <= 1:
parser.print_help()
sys.exit(1)

options, args = parser.parse_args()

def syntax_error(msg):
"""Utility function for displaying fatal error messages with usage
help.
"""
printerr("Syntax error: " + msg, newline=True)
printerr(parser.format_help(), newline=True)
sys.exit(1)

# Non-option arguments
if len(args) < 2:
syntax_error("Insufficient number of arguments.\n")
filename, username = args[:2]
password = None
if options.delete_user:
if len(args) != 2:
syntax_error("Incorrect number of arguments.\n")
parser = argparse.ArgumentParser(usage=main.__doc__)
parser.add_argument('-b', action='store_true', dest='batch',
help="batch mode; password is passed on the command "
"line IN THE CLEAR")
parser_group = parser.add_mutually_exclusive_group()
parser_group.add_argument('-c', action='store_true', dest='create',
help="create a new htpasswd file, overwriting "
"any existing file")
parser_group.add_argument('-D', action='store_true', dest='delete_user',
help="remove the given user from the password "
"file")
parser.add_argument('passwordfile', help=argparse.SUPPRESS)
parser.add_argument('username', help=argparse.SUPPRESS)
parser.add_argument('password', nargs='?', help=argparse.SUPPRESS)

args = parser.parse_args()
password = args.password
if args.delete_user:
if password is not None:
parser.error("too many arguments")
else:
if len(args) == 3 and options.batch:
password = args[2]
elif len(args) == 2 and not options.batch:
first = getpass.getpass("New password:")
second = getpass.getpass("Re-type new password:")
if first == second:
password = first
else:
printout("htpasswd: password verification error")
return
else:
syntax_error("Incorrect number of arguments.\n")
if args.batch and password is None:
parser.error("too few arguments")
elif not args.batch and password is not None:
parser.error("too many arguments")

try:
passwdfile = HtpasswdFile(filename, create=options.create)
except IOError:
syntax_error("File not found.\n")
passwdfile = HtpasswdFile(args.passwordfile, create=args.create)
except EnvironmentError:
printerr("File not found.")
sys.exit(1)
else:
if options.delete_user:
passwdfile.delete(username)
if args.delete_user:
passwdfile.delete(args.username)
else:
passwdfile.update(username, password)
if password is None:
password = ask_pass()
passwdfile.update(args.username, password)
passwdfile.save()


Expand Down

0 comments on commit 4d0afa3

Please sign in to comment.