[Git][NTPsec/ntpsec][master] 5 commits: ntpviz: Open icon files in binary mode

Gary E. Miller gitlab at mg.gitlab.com
Tue Dec 13 01:20:27 UTC 2016


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


Commits:
db1e9ae1 by Richard Laager at 2016-12-12T17:18:40-08:00
ntpviz: Open icon files in binary mode

They are binary data.  This is necessary on Python 3.

- - - - -
390a1ac8 by Richard Laager at 2016-12-12T17:18:40-08:00
ntpviz: Fix temp file binary vs. text

This fixes the following error from os.write() on Python 3:
TypeError: a bytes-like object is required, not 'str'

- - - - -
ef79a388 by Richard Laager at 2016-12-12T17:18:40-08:00
ntpviz: Fix dict_keys sorting

On Python 3, {}.keys() returns a dict_keys object, which has no sort()
method.  We have to turn it to a list first.

- - - - -
67febc75 by Richard Laager at 2016-12-12T17:18:40-08:00
ntpviz: Open HTML and CSV files as text

This is necessary for Python 3.

- - - - -
bf084ff6 by Richard Laager at 2016-12-12T17:18:40-08:00
ntpviz: Open the log files as text

This is necessary for Python 3.

- - - - -


2 changed files:

- ntpclients/ntpviz
- pylib/statfiles.py


Changes:

=====================================
ntpclients/ntpviz
=====================================
--- a/ntpclients/ntpviz
+++ b/ntpclients/ntpviz
@@ -290,14 +290,14 @@ def gnuplot(template, outfile=None):
 
     # can be 30% faster to write to a tmp file than to pipe to gnuplot
     # bonus, we can keep the plot file for debug.
-    tmp_file, tmp_filename = tempfile.mkstemp( suffix='.plt')
+    tmp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.plt', delete=False)
     # note that tmp_file is a file handle, it is not a file object
-    os.write( tmp_file, template)
-    os.close(tmp_file)
+    tmp_file.write(template)
+    tmp_file.close()
 
     # shell=True is a security hazard, do not use
     try:
-        rcode = subprocess.call( ['gnuplot', tmp_filename], stdout=out)
+        rcode = subprocess.call(['gnuplot', tmp_file.name], stdout=out)
     except OSError as e:
         if e.errno == os.errno.ENOENT:
             # gnuplot not found
@@ -309,12 +309,12 @@ def gnuplot(template, outfile=None):
 
     if 0 != rcode:
         sys.stderr.write("ntpviz: WARNING: plot returned %s\n" % rcode)
-        sys.stderr.write("ntpviz: WARNING: plot file %s\n" % tmp_filename)
+        sys.stderr.write("ntpviz: WARNING: plot file %s\n" % tmp_file.name)
     elif 2 <= args.debug_level:
-        sys.stderr.write("ntpviz: INFO: plot file %s\n" % tmp_filename)
+        sys.stderr.write("ntpviz: INFO: plot file %s\n" % tmp_file.name)
     else:
         # remove tmp file
-        os.remove(tmp_filename)
+        os.remove(tmp_file.name)
 
     return rcode
 
@@ -444,7 +444,7 @@ file.</p>
             return ''
 
         tempsmap = self.tempssplit()
-        tempslist = tempsmap.keys()
+        tempslist = list(tempsmap.keys())
         tempslist.sort()
         if not len( tempsmap) or not len( tempslist):
             sys.stderr.write("ntpviz: WARNING: no temps to graph\n")
@@ -525,7 +525,7 @@ file, and field 3 from the temp log .</p>
         "Generate gnuplot code graphing local temperature statistics"
         sitename = self.sitename
         tempsmap = self.tempssplit()
-        tempslist = tempsmap.keys()
+        tempslist = list(tempsmap.keys())
         tempslist.sort()
 
         if not len( tempsmap) or not len( tempslist):
@@ -576,7 +576,7 @@ component of frequency drift.</p>
         "Generate gnuplot code graphing local GPS statistics"
         sitename = self.sitename
         gpsmap = self.gpssplit()
-        gpslist = gpsmap.keys()
+        gpslist = list(gpsmap.keys())
         gpslist.sort()
 
         if not len( gpsmap) or not len( gpslist):
@@ -758,7 +758,7 @@ plot \
 
         peerdict = self.peersplit()
         if not peerlist:
-            peerlist = peerdict.keys()
+            peerlist = list(peerdict.keys())
         if not len( peerlist):
             sys.stderr.write("ntpviz: WARNING: no peer data to graph\n")
             return ''
@@ -1461,13 +1461,13 @@ Python by ESR, concept and gnuplot code by Dan Drown.
     # if no ntpsec favicon.ico, write one.
     ico_filename = os.path.join(args.outdir, "favicon.ico")
     if not os.path.lexists( ico_filename ):
-        with open( ico_filename, "w" ) as wp:
+        with open( ico_filename, "wb" ) as wp:
             wp.write(binascii.a2b_base64(ntpsec_ico))
 
     # if no ntpsec logo, write one.
     logo_filename = os.path.join(args.outdir, "ntpsec-logo.png")
     if not os.path.lexists( logo_filename ):
-        with open( logo_filename, "w" ) as wp:
+        with open( logo_filename, "wb" ) as wp:
             wp.write(binascii.a2b_base64(ntpsec_logo))
 
     report_time = datetime.datetime.utcnow() # the time now is...
@@ -1645,7 +1645,7 @@ ntpviz</a>, part of the <a href="https://www.ntpsec.org/">NTPsec project</a>
             ("peer-offsets", stats.peer_offsets_gnuplot()),
         ]
 
-        peerlist = stats.peersplit().keys()
+        peerlist = list(stats.peersplit().keys())
         # sort for output order stability
         peerlist.sort()
         for key in peerlist:
@@ -1714,12 +1714,12 @@ ntpviz</a>, part of the <a href="https://www.ntpsec.org/">NTPsec project</a>
 
     # and send the file buffer
     index_filename = os.path.join(args.outdir, "index.html")
-    with open(index_filename + ".tmp", "wb") as ifile:
+    with open(index_filename + ".tmp", "w") as ifile:
         ifile.write(index_buffer)
 
     # create csv file, as a tmp file
     csv_filename = os.path.join(args.outdir, "summary.csv")
-    with open( csv_filename + ".tmp", "wb" ) as csv_file:
+    with open( csv_filename + ".tmp", "w" ) as csv_file:
         csv_ob = csv.writer(csv_file)
         csv_ob.writerow(VizStats.csv_head)
         for row in csvs:


=====================================
pylib/statfiles.py
=====================================
--- a/pylib/statfiles.py
+++ b/pylib/statfiles.py
@@ -88,9 +88,9 @@ class NTPStats:
                     if starttime > os.path.getmtime(logpart):
                         continue
                     if logpart.endswith("gz"):
-                        lines += gzip.open(logpart, 'rb').readlines()
+                        lines += gzip.open(logpart, 'rt').readlines()
                     else:
-                        lines += open(logpart, 'rb').readlines()
+                        lines += open(logpart, 'r').readlines()
             except IOError:
                 sys.stderr.write("ntpviz: WARNING: could not read %s\n" \
                      % logpart)



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/26fe5ea0908baab12f8a6456d7cb843665056097...bf084ff6ad88822f50255f262994e5b226e88d8a
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161213/58b2d567/attachment.html>


More information about the vc mailing list