Skip to content
Snippets Groups Projects
Commit f5c8c3f4 authored by Maeva Pourpoint's avatar Maeva Pourpoint
Browse files

Refactoring of refscrub main module - Use argparse.ArgumentParser instead of optparse.OptionParser

parent b6b8b781
No related branches found
No related tags found
1 merge request!4Porting Python 2 code to Python 3
...@@ -14,11 +14,11 @@ Updates to work on both Python 2 & 3. ...@@ -14,11 +14,11 @@ Updates to work on both Python 2 & 3.
Code cleanup to match PEP 8. Code cleanup to match PEP 8.
Cleanup global vars. Cleanup global vars.
""" """
from os.path import join, basename, getsize
import sys import sys
import argparse
import struct import struct
from os.path import basename, getsize, isfile
USAGE = 'usage: %prog [options] infile1 [ infile2 ... infileN]'
PROG_VERSION = '2018.228' PROG_VERSION = '2018.228'
VERBOSE = False VERBOSE = False
EXTRACT = False EXTRACT = False
...@@ -220,26 +220,40 @@ def main(): ...@@ -220,26 +220,40 @@ def main():
global PREFIX global PREFIX
global FILESIZE global FILESIZE
summaryfh = None summaryfh = None
from optparse import OptionParser parser = argparse.ArgumentParser(prog="refscrub",
parser = OptionParser(USAGE, version="%prog " + PROG_VERSION) usage="%(prog)s [options] infile1 "
parser.description = "infile can be a REFTEK file or a raw disk (/dev/disk1) of a CF card." "[ infile2 ... infileN]")
parser.add_option('-v', '--verbose', dest="VERBOSE", action='store_true', default=False, parser.add_argument('infile', nargs='*', metavar='infile',
help="Prints info about each packet, good or bad. This will increase runtime.") help="infile can be a REFTEK file or a raw disk "
parser.add_option('-e', '--extract', dest='EXTRACT', action='store_true', default=False, "(/dev/disk1) of a CF card.")
help='Writes good packets to files named infile.SNXX.scrub.ref OR PREFIX.SNXX.scrub.ref, ' parser.add_argument('--version', action='version',
' if given, for each Serial Number found. If output file exists it will append. Be careful ' version="%(prog)s " + PROG_VERSION)
'not to duplicate data by running more than once on the same file in the same dir.') parser.add_argument('-v', '--verbose', dest="VERBOSE", action='store_true',
parser.add_option('-p', '--prefix', dest='PREFIX', default=False, help="Prints info about each packet, "
help='Prefix of output filename. Defaults to inputfilename') "good or bad. This will increase runtime.")
parser.add_option('-s', '--savesum', dest='SUMMARY', action='store_true', default=False, parser.add_argument('-e', '--extract', dest='EXTRACT', action='store_true',
help='Appends summary to %s' % SUMMARY_FILE) default=False, help="Writes good packets to files "
options, args = parser.parse_args() "named infile.SNXX.scrub.ref OR "
VERBOSE = options.VERBOSE "PREFIX.SNXX.scrub.ref, if given, for each Serial "
EXTRACT = options.EXTRACT "Number found. If output file exists it will append. "
PREFIX = options.PREFIX "Be careful not to duplicate data by running more "
if options.SUMMARY: "than once on the same file in the same dir.")
parser.add_argument('-p', '--prefix', dest='PREFIX',
help="Prefix of output filename. Defaults to input"
"filename")
parser.add_argument('-s', '--savesum', dest='SUMMARY', action='store_true',
default=False, help='Appends summary to %s'
% SUMMARY_FILE)
args = parser.parse_args()
if not args.infile:
parser.print_help()
sys.exit(1)
VERBOSE = args.VERBOSE
EXTRACT = args.EXTRACT
PREFIX = args.PREFIX
if args.SUMMARY:
summaryfh = open(SUMMARY_FILE, 'a') summaryfh = open(SUMMARY_FILE, 'a')
for infilename in args: for infilename in args.infile:
print("Processing: %s" % infilename) print("Processing: %s" % infilename)
if not PREFIX: if not PREFIX:
PREFIX = basename(infilename) PREFIX = basename(infilename)
...@@ -248,11 +262,13 @@ def main(): ...@@ -248,11 +262,13 @@ def main():
# Must open the file in binary mode or else we will have unicode issues when reading. # Must open the file in binary mode or else we will have unicode issues when reading.
with open(infilename, "rb") as infile: with open(infilename, "rb") as infile:
readfile(infile) readfile(infile)
if options.SUMMARY: if args.SUMMARY:
summaryfh.write(infilename + ', ' + PREFIX + '\n') summaryfh.write(infilename + ', ' + PREFIX + '\n')
summaryfh.write(summary(infile)) summaryfh.write(summary(infile))
print("----------------------------------------") print("----------------------------------------")
if isfile(SUMMARY_FILE):
summaryfh.close()
if __name__ == '__main__': if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment