aboutsummaryrefslogtreecommitdiff
path: root/findversion.py
diff options
context:
space:
mode:
authorB Stack <bgstack15@gmail.com>2016-11-01 10:56:40 -0400
committerB Stack <bgstack15@gmail.com>2016-11-01 10:56:40 -0400
commit64b248706d5a53de8a9062f8dd9a6bf4e8e7f6c3 (patch)
tree31848d80ac9388a8b07f87733a9e5f5217e15364 /findversion.py
parentWorking hard-coded prototype (diff)
downloadfindversion-master.tar.gz
findversion-master.tar.bz2
findversion-master.zip
Added argparseHEADmaster
Diffstat (limited to 'findversion.py')
-rwxr-xr-xfindversion.py74
1 files changed, 67 insertions, 7 deletions
diff --git a/findversion.py b/findversion.py
index 8ec02ed..b16b14a 100755
--- a/findversion.py
+++ b/findversion.py
@@ -7,16 +7,19 @@
# History:
# Usage:
# findversion.py scrub.py /home/work
-# findversion.py [--dir] /home/work [--filename] scrub.sh [--newestonly]
+# findversion.py [--dir] /home/work [--filename] scrub.sh
# Reference:
# regex search http://stackoverflow.com/a/10477490
# regex https://docs.python.org/3/howto/regex.html
# manipulating dict https://docs.python.org/2/library/stdtypes.html#dict
+# parser from updateval.py
+# and also https://docs.python.org/3/library/argparse.html#formatter-class
# Improve:
+# Make option for "newestonly" which just does "tail -n1"
-import os, re
+import os, re, argparse, textwrap
-findversionpyversion = "2016-10-31a"
+findversionpyversion = "2016-11-01a"
# DEFINE FUNCTIONS
@@ -35,12 +38,69 @@ searchdir = "/home/work/rpmbuild/SOURCES"
searchfile = "updateval.py"
newestonly = False
-# PREPARE VARIABLES
-#searchstring = re.sub("[^a-zA-Z0-9]", "", searchfile)
-searchstring = "updateval"
-pattern = re.compile(r'.*' + searchstring + r'version ?= ?([\"\'])[0-9]{4}(-[0-9]{2}){2}[a-zA-Z]\1')
+# DEFINE PARAMETERS
+parser = argparse.ArgumentParser(
+ description="Finds and displays a file for each unique version number.",
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ epilog=textwrap.dedent('''\
+ Valid options include:
+ ./findversion.py scrub.py /home/work
+ ./findversion.py [--dir] /home/work [--filename] scrub.sh''') )
+parser.add_argument("dir", nargs='?', default="NONE", help=argparse.SUPPRESS)
+parser.add_argument("filename", nargs='?', default="NONE", help=argparse.SUPPRESS)
+parser.add_argument("-d","--dir", default="NONE", help="the -d is optional")
+parser.add_argument("-f","--filename", default="NONE", help="the -f is optional")
+parser.add_argument("-V","--version", action="version", version="%(prog)s " + findversionpyversion)
+parser.add_argument("-s","--string", default="NONE", help="exact string to put before version= search expression. Default is filename without punctuation.")
+
+# PARSE PARAMETERS
+args = parser.parse_args()
+if args.dir == "NONE" and args.filename == "NONE":
+ # throw an error, because nothing was defined
+ # unless we want to force it to check all versions everywhere
+ parser.parse_args(['-h'])
+elif args.dir == "NONE":
+ # use current directory
+ if os.path.isdir(args.filename):
+ searchdir = args.filename
+ filename = "*"
+ else:
+ searchdir = os.getcwd()
+ filename = args.dir
+elif args.filename == "NONE":
+ if os.path.isdir(args.dir):
+ searchdir = args.dir
+ filename = "*"
+ else:
+ searchdir = os.getcwd()
+ filename = args.dir
+else:
+ if os.path.isdir(args.dir):
+ searchdir = args.dir
+ filename = args.filename
+ else:
+ if os.path.isdir(args.filename):
+ searchdir = args.filename
+ filename = args.dir
+ else:
+ print("Invalid directory: %s" % args.dir)
+ quit()
+
+searchfile = filename
+if args.string == "NONE":
+ searchstring = re.sub("[^a-zA-Z0-9]", "", searchfile)
+else:
+ searchstring = args.string
+
+pattern = re.compile(r'.*' + searchstring + r'(py|sh)?version ?= ?(?P<quot>[\"\'])[0-9]{4}(-[0-9]{2}){2}[a-zA-Z](?P=quot)')
allversions = dict()
+# DEBUG SECTION
+if False:
+ print("searchdir %s" % searchdir)
+ print("searchfile %s" % searchfile)
+ print("searchstring %s" % searchstring)
+
# PERFORM SEARCH HERE
for rootdir, subdirs, files in os.walk(searchdir):
for filename in files:
bgstack15