daca2: Use argparse instead of manual argument parsing

This commit is contained in:
Boris Egorov 2016-11-28 10:22:30 +07:00
parent 3bb6a27988
commit c250e385d3
1 changed files with 27 additions and 37 deletions

View File

@ -6,6 +6,7 @@
# 4. Optional: tweak FTPSERVER and FTPPATH in this script below. # 4. Optional: tweak FTPSERVER and FTPPATH in this script below.
# 5. Run the daca2 script: python daca2.py FOLDER # 5. Run the daca2 script: python daca2.py FOLDER
import argparse
import subprocess import subprocess
import sys import sys
import shutil import shutil
@ -176,63 +177,52 @@ def scanarchive(filepath, jobs, cpulimit):
results.write('\n') results.write('\n')
results.close() results.close()
FOLDER = None parser = argparse.ArgumentParser(description='Checks debian source code')
JOBS = '-j1' parser.add_argument('folder', metavar='FOLDER')
REV = None parser.add_argument('--rev')
SKIP = [] parser.add_argument('--workdir', default='~/daca2')
WORKDIR = os.path.expanduser('~/daca2') parser.add_argument('-j', '--jobs', default='-j1')
CPULIMIT = None parser.add_argument('--skip', default=[], action='append')
for arg in sys.argv[1:]: parser.add_argument('--cpulimit')
if arg[:6] == '--rev=':
REV = arg[6:]
elif arg[:2] == '-j':
JOBS = arg
elif arg.startswith('--skip='):
SKIP.append(arg[7:])
elif arg.startswith('--workdir='):
WORKDIR = arg[10:]
elif arg.startswith('--cpulimit='):
CPULIMIT = arg[11:]
else:
FOLDER = arg
if not FOLDER: args = parser.parse_args()
print('no folder given')
workdir = os.path.expanduser(args.workdir)
if not os.path.isdir(workdir):
print('workdir \'' + workdir + '\' is not a folder')
sys.exit(1) sys.exit(1)
if not os.path.isdir(WORKDIR): workdir = os.path.join(workdir, args.folder)
print('workdir \'' + WORKDIR + '\' is not a folder') if not os.path.isdir(workdir):
sys.exit(1) os.makedirs(workdir)
archives = getpackages(FOLDER) print(workdir)
archives = getpackages(args.folder)
if len(archives) == 0: if len(archives) == 0:
print('failed to load packages') print('failed to load packages')
sys.exit(1) sys.exit(1)
if not WORKDIR.endswith('/'): if not os.path.isdir(workdir):
WORKDIR = WORKDIR + '/' os.makedirs(workdir)
os.chdir(workdir)
print('~/daca2/' + FOLDER)
if not os.path.isdir(WORKDIR + FOLDER):
os.makedirs(WORKDIR + FOLDER)
os.chdir(WORKDIR + FOLDER)
try: try:
results = open('results.txt', 'wt') results = open('results.txt', 'wt')
results.write('STARTDATE ' + str(datetime.date.today()) + '\n') results.write('STARTDATE ' + str(datetime.date.today()) + '\n')
results.write('STARTTIME ' + strfCurrTime('%H:%M:%S') + '\n') results.write('STARTTIME ' + strfCurrTime('%H:%M:%S') + '\n')
if REV: if args.rev:
results.write('GIT-REVISION ' + REV + '\n') results.write('GIT-REVISION ' + args.rev + '\n')
results.write('\n') results.write('\n')
results.close() results.close()
for archive in archives: for archive in archives:
if len(SKIP) > 0: if len(args.skip) > 0:
a = archive[:archive.rfind('/')] a = archive[:archive.rfind('/')]
a = a[a.rfind('/')+1:] a = a[a.rfind('/')+1:]
if a in SKIP: if a in args.skip:
continue continue
scanarchive(archive, JOBS, CPULIMIT) scanarchive(archive, args.jobs, args.cpulimit)
results = open('results.txt', 'at') results = open('results.txt', 'at')
results.write('DATE ' + str(datetime.date.today()) + '\n') results.write('DATE ' + str(datetime.date.today()) + '\n')