Skip to content

Commit

Permalink
Migrate all optparse scripts to argparse.
Browse files Browse the repository at this point in the history
 * This will ultimately allow for common argument handling.
 * Scripts that parse sys.argv directly are left alone for now.
  • Loading branch information
jtniehof authored and dnadeau-lanl committed Feb 9, 2021
1 parent 5aad48d commit 80d12b5
Show file tree
Hide file tree
Showing 27 changed files with 328 additions and 384 deletions.
6 changes: 3 additions & 3 deletions docs/scripts.rst
Expand Up @@ -289,16 +289,16 @@ printInfo.py

Prints a table of info about files or products or processes.

.. option:: Database The name of the database to print table of
.. option:: Field Either Product or Mission (more to come)
.. option:: database The name of the database to print table of
.. option:: field Either Product or Mission (more to come)

printProcessQueue.py
--------------------
.. program:: printProcessQueue

Prints the process queue.

.. option:: Database The name of the database to print the queue of
.. option:: database The name of the database to print the queue of
.. option:: -o, --output The name of the file to output to(if blank, print to stdout)
.. option:: --html Output in HTML

Expand Down
17 changes: 8 additions & 9 deletions scripts/CreateDB.py
Expand Up @@ -10,8 +10,8 @@
"""
from __future__ import division # may not be needed but start with it

import argparse
import os
from optparse import OptionParser

from sqlalchemy import schema, types
from sqlalchemy.engine import create_engine
Expand Down Expand Up @@ -66,16 +66,15 @@ def createDB(self):


if __name__ == "__main__":
usage = "usage: %prog [options] filename"
parser = OptionParser(usage=usage)
parser.add_option("-d", "--dialect", dest="dialect", default='sqlite',
help="sqlalchemy dialect (sqlite or postgresql)")
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dialect", dest="dialect", default='sqlite',
help="sqlalchemy dialect (sqlite or postgresql)")
parser.add_argument('dbname', action='store', type=str,
help='Name of database (or sqlite file) to create.')

(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
options = parser.parse_args()

filename = args[0]
filename = options.dbname
if options.dialect == 'sqlite':
filename = os.path.abspath(filename)

Expand Down
1 change: 0 additions & 1 deletion scripts/CreateDBsabrs.py
Expand Up @@ -11,7 +11,6 @@
from __future__ import division # may not be needed but start with it

import os
from optparse import OptionParser

import sqlalchemy
from sqlalchemy import schema, types
Expand Down
71 changes: 32 additions & 39 deletions scripts/ProcessQueue.py
@@ -1,11 +1,10 @@
#!/usr/bin/env python
from __future__ import print_function

import argparse
import datetime
import os
import operator
from optparse import OptionParser
import pdb
import traceback
import subprocess

Expand All @@ -29,43 +28,37 @@
-s -> skip run timebase
-o -> only run listed processes
"""
parser = OptionParser(usage=usage)
parser.add_option("-i", "", dest="i", action="store_true",
help="ingest mode", default=False)
parser.add_option("-p", "", dest="p", action="store_true",
help="process mode", default=False)
parser.add_option("-s", "", dest="s", action="store_true",
help="Skip run timebase processes", default=False)
parser.add_option("-o", "--only", dest="o", type="string",
help='Run only listed processes (either id or name)', default = None)
parser.add_option("-m", "--mission", dest="mission",
help="selected mission database", default=None)
parser.add_option("-d", "--dryrun", dest="dryrun", action="store_true",
help="only do a dryrun processing or ingesting", default=False)
parser.add_option("-r", "--report", dest="report", action="store_true",
help="Make the html report", default=False)
parser.add_option("-l", "--log-level", dest="loglevel",
help="Set the logging level", default="debug")
parser.add_option("-n", "--num-proc", dest="numproc", type='int',
help="Number of processes to run in parallel", default=2)
parser.add_option("", "--echo", dest="echo", action="store_true",
help="Start sqlalchemy with echo in place for debugging", default=False)
parser.add_option("", "--glb", dest="glob", type="string",
help='Glob to use when reading files from incoming: default "*"', default="*")


(options, args) = parser.parse_args()
if len(args) != 0:
parser.error("incorrect number of arguments")

if options.i and options.p:
parser.error("options -i and -p are mutually exclusive")
if options.i and options.s:
parser.error("options -i and -s are mutually exclusive")
if options.i and options.o:
parser.error("options -i and -o are mutually exclusive")
if not options.i and not options.p:
parser.error("either -i or -p must be specified")
parser = argparse.ArgumentParser()
action_group = parser.add_mutually_exclusive_group(required=True)
action_group.add_argument("-i", action="store_true",
help="ingest mode", default=False)
action_group.add_argument("-p", action="store_true",
help="process mode", default=False)
parser.add_argument("-s", dest="s", action="store_true",
help="Skip run timebase processes", default=False)
parser.add_argument("-o", "--only", dest="o", type=str,
help='Run only listed processes (either id or name)', default = None)
parser.add_argument("-m", "--mission", required=True,
help="selected mission database", default=None)
parser.add_argument("-d", "--dryrun", action="store_true",
help="only do a dryrun processing or ingesting", default=False)
parser.add_argument("-r", "--report", action="store_true",
help="Make the html report", default=False)
parser.add_argument("-l", "--log-level", dest="loglevel",
help="Set the logging level", default="debug")
parser.add_argument("-n", "--num-proc", dest="numproc", type=int,
help="Number of processes to run in parallel", default=2)
parser.add_argument("--echo", action="store_true",
help="Start sqlalchemy with echo in place for debugging", default=False)
parser.add_argument("--glb", dest="glob", type=str,
help='Glob to use when reading files from incoming: default "*"', default="*")

options = parser.parse_args()

if options.s and not options.p:
parser.error('-s requires -p')
if options.o and not options.p:
parser.error('-o requires -p')

logname = os.path.basename(options.mission).replace('.', '_')
DBlogging.change_logfile(logname)
Expand Down
25 changes: 12 additions & 13 deletions scripts/addFromConfig.py
Expand Up @@ -23,7 +23,7 @@
import shutil
import sys
import tempfile
from optparse import OptionParser
import argparse
from ast import literal_eval as make_tuple

from sqlalchemy.orm.exc import NoResultFound
Expand Down Expand Up @@ -298,18 +298,17 @@ def addStuff(cfg, options):


if __name__ == "__main__":
usage = "usage: %prog [options] -m mission_db filename"
parser = OptionParser(usage=usage)
parser.add_option("-m", "--mission", dest="mission", type="string",
help="mission to connect to", default='')
parser.add_option("-v", "--verify", dest="verify", action='store_true',
help="Don't do anything other than verify the config file", default=False)

(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")

filename = os.path.expanduser(args[0])
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--mission", type=str,
help="mission to connect to", default='')
parser.add_argument("-v", "--verify", action='store_true',
help="Don't do anything other than verify the config file", default=False)
parser.add_argument('config_file', action='store', type=str,
help='Configuration file to read from')

options = parser.parse_args()

filename = os.path.expanduser(options.config_file)

if not os.path.isfile(filename):
parser.error("file: {0} does not exist or is not readable".format(filename))
Expand Down
37 changes: 20 additions & 17 deletions scripts/configFromDB.py
Expand Up @@ -3,7 +3,7 @@

import ConfigParser
import os
from optparse import OptionParser
import argparse

from dbprocessing import DButils
from dbprocessing import Utils
Expand Down Expand Up @@ -102,29 +102,32 @@
"""

if __name__ == "__main__":
usage = "usage: %prog [options] -m mission_db filename"
parser = OptionParser(usage=usage)
parser = argparse.ArgumentParser()
parser.set_defaults(mission=None)
parser.set_defaults(force=False)
parser.set_defaults(satellite=None)
parser.set_defaults(nocomments=False)

parser.add_option("-m", "--mission", dest="mission", type="string",
help="mission to connect to")
parser.add_option("-f", "--force", dest="force", action="store_true",
help="Force the processing, overwrite the outfile")
parser.add_option("-s", "--satellite", dest="satellite", type="string",
help="satellite to write to conf file (one per file)")
parser.add_option("-i", "--instrument", dest="instrument", type="string",
help="instrument to write to conf file (one per file)")
parser.add_option("-c", "--nocomments", dest="nocomments", action='store_true',
help="Do not add comments to the top of the config file")
parser.add_argument("-m", "--mission", dest="mission", type=str,
default=None,
help="mission to connect to")
parser.add_argument("-f", "--force", dest="force", action="store_true",
default=False,
help="Force the processing, overwrite the outfile")
parser.add_argument("-s", "--satellite", dest="satellite", type=str,
default=None,
help="satellite to write to conf file (one per file)")
parser.add_argument("-i", "--instrument", dest="instrument", type=str,
help="instrument to write to conf file (one per file)")
parser.add_argument("-c", "--nocomments", dest="nocomments", action='store_true',
default=False,
help="Do not add comments to the top of the config file")
parser.add_argument('config_file', action='store', type=str,
help='Configuration file to write')

(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
options = parser.parse_args()

filename = os.path.expandvars(os.path.expanduser(args[0]))
filename = os.path.expandvars(os.path.expanduser(options.config_file))

if os.path.isfile(filename) and not options.force:
parser.error("file: {0} exists and will not be overwritten (use --force)".format(filename))
Expand Down
14 changes: 7 additions & 7 deletions scripts/coveragePlot.py
Expand Up @@ -8,12 +8,12 @@
"""
from __future__ import division

import argparse
import ConfigParser
import datetime
import fnmatch
import os
import subprocess
from optparse import OptionParser

import matplotlib

Expand Down Expand Up @@ -163,14 +163,14 @@ def _combine_coverage(inval):


if __name__ == "__main__":
usage = "usage: %prog [options] configfile"
parser = OptionParser(usage=usage)
parser = argparse.ArgumentParser()
parser.add_argument('configfile', action='store', type=str,
help='Plot configuration file to read')

(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
options = parser.parse_args()

conffile = os.path.expanduser(os.path.expandvars(os.path.abspath(args[0])))
conffile = os.path.expanduser(os.path.expandvars(os.path.abspath(
options.configfile)))
if not os.path.isfile(conffile):
parser.error("could not read config file: {0}".format(conffile))

Expand Down
45 changes: 21 additions & 24 deletions scripts/dbOnlyFiles.py
Expand Up @@ -6,10 +6,10 @@
"""
from __future__ import division

import argparse
import bisect
import datetime
import os
from optparse import OptionParser

from dateutil import parser as dup
from dateutil.relativedelta import relativedelta
Expand All @@ -19,31 +19,28 @@
from dbprocessing.Utils import progressbar

if __name__ == "__main__":
usage = "%prog -m mission"
parser = OptionParser(usage=usage)
parser.add_option("-s", "--startDate", dest="startDate", type="string",
help="Date to start reprocessing (e.g. 2012-10-02)", default=None)
parser.add_option("-e", "--endDate", dest="endDate", type="string",
help="Date to end reprocessing (e.g. 2012-10-25)", default=None)
parser.add_option("-f", "--fix", dest="fix", action='store_true',
help="Fix the database exists_on_disk field ", default=False)
parser.add_option("-m", "--mission", dest="mission",
help="selected mission database", default=None)
parser.add_option("", "--echo", dest="echo", action='store_true',
help="echo sql queries for debugging", default=False)
parser.add_option("-n", "--newest", dest="newest", action='store_true',
help="Only check the newest files", default=False)
parser.add_option("", "--startID", dest="startID", type="int",
help="The File id to start on", default=1)
parser.add_option("-v", "--verbose", dest="verbose", action='store_true',
help="Print out each file as it is checked", default=False)
parser.add_option("-p", "--path", dest="path", action="store_true",
help="Print full file path of missing files", default=False)
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--startDate", type=str,
help="Date to start reprocessing (e.g. 2012-10-02)", default=None)
parser.add_argument("-e", "--endDate", type=str,
help="Date to end reprocessing (e.g. 2012-10-25)", default=None)
parser.add_argument("-f", "--fix", action='store_true',
help="Fix the database exists_on_disk field ", default=False)
parser.add_argument("-m", "--mission",
help="selected mission database", required=True)
parser.add_argument("--echo", action='store_true',
help="echo sql queries for debugging", default=False)
parser.add_argument("-n", "--newest", action='store_true',
help="Only check the newest files", default=False)
parser.add_argument("--startID", type=int,
help="The File id to start on", default=1)
parser.add_argument("-v", "--verbose", action='store_true',
help="Print out each file as it is checked", default=False)
parser.add_argument("-p", "--path", action="store_true",
help="Print full file path of missing files", default=False)


(options, args) = parser.parse_args()
if len(args) != 0:
parser.error("incorrect number of arguments")
options = parser.parse_args()

if options.startDate is not None:
startDate = dup.parse(options.startDate)
Expand Down
17 changes: 5 additions & 12 deletions scripts/deleteAllDBFiles.py
Expand Up @@ -2,22 +2,15 @@

from __future__ import print_function

from optparse import OptionParser
import argparse

from dbprocessing import DButils

if __name__ == "__main__":
usage = \
"""
Usage: %prog -m
-m -> selects mission
"""
parser = OptionParser(usage=usage)
parser.add_option("-m", "--mission", dest="mission",
help="selected mission database", default=None)
(options, args) = parser.parse_args()
if len(args) != 0:
parser.error("incorrect number of arguments")
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--mission", dest="mission",
help="selected mission database", required=True)
options = parser.parse_args()

a = DButils.DButils(options.mission)
f = a.getAllFilenames()
Expand Down

0 comments on commit 80d12b5

Please sign in to comment.