[Git][NTPsec/ntpsec][master] 2 commits: gps-log: add CLI options, -h, -l, -o, -v, -V

Gary E. Miller gitlab at mg.gitlab.com
Fri Jan 6 01:43:52 UTC 2017


Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
4268f433 by Gary E. Miller at 2017-01-05T17:43:31-08:00
gps-log: add CLI options, -h, -l, -o, -v, -V

-h help
-l LOGFILE log to file, not stdout
-o log one item and exit
-v verbose
-V print Version and exit

- - - - -
cd6d8b81 by Gary E. Miller at 2017-01-05T17:43:31-08:00
ntpviz: clean up CLI arguments.

- - - - -


2 changed files:

- contrib/gps-log
- ntpclients/ntpviz


Changes:

=====================================
contrib/gps-log
=====================================
--- a/contrib/gps-log
+++ b/contrib/gps-log
@@ -3,10 +3,73 @@
 
 from __future__ import print_function
 
-import time
+import io
 import sys
 import threading
-import gps
+import time
+
+try:
+    import gps
+    import ntp.util
+except ImportError as e:
+    sys.stderr.write("gps-log: can't find Python NTP modules "
+                     "-- check PYTHONPATH.\n")
+    sys.stderr.write("%s\n" % e)
+    sys.exit(1)
+
+try:
+    import argparse
+except ImportError as e:
+    sys.stderr.write("""
+gps-log: can't find the Python argparse module
+         If your Python version is < 2.7, then manual installation is needed:
+         # pip install argparse
+%s
+""")
+    sys.exit(1)
+
+
+parser = argparse.ArgumentParser(description="gpsd log file generator",
+                                 epilog="""
+See the manual page for details.
+""")
+
+parser.add_argument('-o', '--once',
+                    action="store_true",
+                    dest='once',
+                    help="log one line, then exit")
+
+parser.add_argument('-l', '--logfile',
+                    dest='logfile',
+                    help="append log data to FILE instead of stdout",
+                    nargs=1)
+
+parser.add_argument('-v', '--verbose',
+                    action="store_true",
+                    dest='verbose',
+                    help="be verbose")
+
+parser.add_argument('-V', '--version',
+                    action="version",
+                    version="gps-out %s" % ntp.util.stdversion())
+
+args = parser.parse_args()
+
+if args.logfile:
+    # log to logfile
+    try:
+        out = open(args.logfile[0], mode='a')
+    except io.UnsupportedOperation as e:
+        sys.stderr.write("gps-log: can't open logfile %s\n" % args.logfile)
+        sys.stderr.write("%s\n" % e)
+        sys.exit(1)
+
+    if args.verbose:
+        print("gps-log: opened log file %s" % args.logfile[0])
+
+else:
+    # log to stdout
+    out = sys.stdout
 
 
 class GpsPoller(threading.Thread):
@@ -26,20 +89,18 @@ class GpsPoller(threading.Thread):
 
     def display(self):
         "Displays the time, device, TDOP, and nSat data collected"
-        # print(gpsd.fix.latitude,', ',gpsd.fix.longitude,'  Time: ',gpsd.utc)
-        print('%s %s %f %d'
-              % (gps.isotime(self.get_time()),
-                 self.gpsd.device,
-                 self.gpsd.tdop,
-                 self.gpsd.satellites_used))
+        out.write('%s %s %f %d\n' % (gps.isotime(self.get_time()),
+                                     self.gpsd.device,
+                                     self.gpsd.tdop,
+                                     self.gpsd.satellites_used))
 
 if __name__ == '__main__':
     gpsp = GpsPoller()    # create the thread
     try:
         gpsp.start()      # start it up
         last_time = 0
-        print("")         # print blank line to prevent log corruption
-        print("# Time       Device     TDOP     nSat")
+        out.write("\n")         # print blank line to prevent log corruption
+        out.write("# Time       Device     TDOP     nSat\n")
         while True:
             # It may take a second or two to get good data
 
@@ -48,17 +109,31 @@ if __name__ == '__main__':
                     if last_time != gpsp.get_time():
                         gpsp.display()
                     last_time = gpsp.get_time()
+                    if args.once:
+                        # just once
+                        break
+
             except AttributeError as e:
                 print('parse error\n')
 
-            sys.stdout.flush()
+            out.flush()
             time.sleep(5)        # set to whatever
 
     except (KeyboardInterrupt, SystemExit):    # when you press ctrl+c
-        print("\nKilling Thread...")
+        if args.verbose:
+            print("\nKilling Thread...")
+        else:
+            print("")
     except Exception as e:       # any error, signal
         print(e)
 
+    # tell the thread to die
     gpsp.running = False
-    gpsp.join()      # wait for the thread to finish what it's doing
-    print("Done.\nExiting.")
+    # mom says:  be nice and flush
+    out.flush()
+    out.close()
+    # wait for the thread to finish what it's doing
+    gpsp.join()
+
+    if args.verbose:
+        print("gps-log Done -- Exiting.")


=====================================
ntpclients/ntpviz
=====================================
--- a/ntpclients/ntpviz
+++ b/ntpclients/ntpviz
@@ -48,8 +48,8 @@ try:
     import argparse
 except ImportError as e:
     sys.stderr.write("""
-ntpviz: can't find Python argparse module
-        If your Python < 2.7, then manual installation is needed:
+ntpviz: can't find the Python argparse module
+        If your Python version is < 2.7, then manual installation is needed:
         # pip install argparse
 %s
 """)
@@ -1205,7 +1205,6 @@ Python by ESR, concept and gnuplot code by Dan Drown.
 
     parser.add_argument('-c', '--clip',
                         action="store_true",
-                        default=None,
                         dest='clip',
                         help="Clip plots at 1%% and 99%%")
     parser.add_argument('-d', '--datadir',
@@ -1214,13 +1213,11 @@ Python by ESR, concept and gnuplot code by Dan Drown.
                         help="one or more logfile directories to read",
                         type=str)
     parser.add_argument('-e', '--endtime',
-                        default=None,
                         dest='endtime',
                         help="End time in POSIX (seconds) or ISO 8601",
                         type=str)
     parser.add_argument('-g', '--generate',
                         action="store_true",
-                        default=False,
                         dest='generate',
                         help="Run plot through gnuplot to make png")
     parser.add_argument('-n', '--name',
@@ -1239,7 +1236,6 @@ Python by ESR, concept and gnuplot code by Dan Drown.
                         help="period in days to graph (float)",
                         type=float)
     parser.add_argument('-s', '--starttime',
-                        default=None,
                         dest='starttime',
                         help="Start time in POSIX (seconds) or ISO 8601",
                         type=str)
@@ -1250,7 +1246,6 @@ Python by ESR, concept and gnuplot code by Dan Drown.
                         help="PNG width: s, m, or l",
                         type=str)
     group.add_argument('--all-peer-jitters',
-                       default=False,
                        action="store_true",
                        dest='show_peer_jitters',
                        help="Plot all peer jitters")
@@ -1260,7 +1255,6 @@ Python by ESR, concept and gnuplot code by Dan Drown.
                        help="Plot peer jitters.  Comma separated host list.",
                        type=str)
     group.add_argument('--all-peer-offsets',
-                       default=False,
                        action="store_true",
                        dest='show_peer_offsets',
                        help="Plot all peer offsets")
@@ -1270,48 +1264,39 @@ Python by ESR, concept and gnuplot code by Dan Drown.
                        help="Plot peer offsets.  Comma separated host list.",
                        type=str)
     group.add_argument('--local-error',
-                       default=False,
                        action="store_true",
                        dest='show_local_error',
                        help="Plot local clock frequency offsets")
     group.add_argument('--local-freq-temps',
-                       default=False,
                        action="store_true",
                        dest='show_freq_temps',
                        help="Plot local frequency vs temperature data")
     group.add_argument('--local-gps',
-                       default=False,
                        action="store_true",
                        dest='show_gps',
                        help="Plot gpsd TDOP and nSats")
     group.add_argument('--local-jitter',
-                       default=False,
                        action="store_true",
                        dest='show_local_jitter',
                        help="Plot clock time jitter")
     group.add_argument('--local-offset',
-                       default=False,
                        action="store_true",
                        dest='show_local_offset',
                        help="Plot Clock frequency offset")
     group.add_argument('--local-offset-histogram',
-                       default=False,
                        action="store_true",
                        dest='show_local_offset_histogram',
                        help="Plot histogram of loopstats time offsets")
     group.add_argument('--local-offset-multiplot',
-                       default=False,
                        action="store_true",
                        dest='show_local_offset_multiplot',
                        help="Plot comparative local offsets for "
                             "multiple directories")
     group.add_argument('--local-stability',
-                       default=False,
                        action="store_true",
                        dest='show_local_stability',
                        help="Plot RMS frequency-jitter")
     group.add_argument('--local-temps',
-                       default=False,
                        action="store_true",
                        dest='show_temps',
                        help="Plot local temperature data")
@@ -1321,19 +1306,11 @@ Python by ESR, concept and gnuplot code by Dan Drown.
                         help="debug level, 0 (none) to 9 (most)",
                         type=int)
     parser.add_argument('-V', '--version',
-                        action="store_true",
-                        default=False,
-                        dest='version',
-                        help="Print version and exit")
+                        action="version",
+                        version= "ntpviz %s" % ntp.util.stdversion())
 
     args = parser.parse_args()
 
-    version = ntp.util.stdversion()
-
-    if args.version:
-        print(version)
-        raise SystemExit(1)
-
     if 's' == args.width:
         # fit in 1024x768 browser
         # in 2016 this is 22% of all browsers



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/cacf62fcfc4e0c7d8733e1e16404665c74ce345b...cd6d8b8129d6a88a19b059523498934bd7d860c9
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170106/08213a12/attachment.html>


More information about the vc mailing list