X-Git-Url: https://git.camperquake.de/gitweb.cgi?p=time-slider.git;a=blobdiff_plain;f=usr%2Fshare%2Ftime-slider%2Flib%2Ftime_slider%2Futil.py;h=e2d7cf7eaff35866fef2d0b671c43186f668fda4;hp=600240e0f8af82da1ec0371633aebe4fd98e98ce;hb=f5fc3bcdc5e88e15cdb81d2644188a7eb5cebf3b;hpb=d1252e4e09fea3b47e1e6eb76c6506ea83dda572 diff --git a/usr/share/time-slider/lib/time_slider/util.py b/usr/share/time-slider/lib/time_slider/util.py index 600240e..e2d7cf7 100644 --- a/usr/share/time-slider/lib/time_slider/util.py +++ b/usr/share/time-slider/lib/time_slider/util.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2 +#!/usr/bin/python3 # # CDDL HEADER START # @@ -24,9 +24,9 @@ import os import subprocess import sys import syslog -import statvfs import math import gio +import logging def run_command(command, raise_on_try=True): """ @@ -34,6 +34,8 @@ def run_command(command, raise_on_try=True): Returns a tuple of standard out and stander error. Throws a RunTimeError if the command failed to execute or if the command returns a non-zero exit status. + + Assume the output is UTF-8 encoded """ debug("Trying to run command %s" % (command), True) @@ -42,14 +44,14 @@ def run_command(command, raise_on_try=True): stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) - outdata,errdata = p.communicate() + outdata,errdata = (x.decode('utf-8') for x in p.communicate()) err = p.wait() - except OSError, message: - raise RuntimeError, "%s subprocess error:\n %s" % \ - (command, str(message)) + except OSError as message: + raise RuntimeError("%s subprocess error:\n %s" % \ + (command, str(message))) if err != 0 and raise_on_try: - raise RuntimeError, '%s failed with exit code %d\n%s' % \ - (str(command), err, errdata) + raise RuntimeError('%s failed with exit code %d\n%s' % \ + (str(command), err, errdata)) return outdata,errdata def debug(message, verbose): @@ -60,8 +62,7 @@ def debug(message, verbose): context using syslog.openlog() """ if verbose: - syslog.syslog(syslog.LOG_NOTICE, message + '\n') - sys.stderr.write(message + '\n') + logging.getLogger('time-slider').debug(message) def log_error(loglevel, message): """ @@ -69,8 +70,7 @@ def log_error(loglevel, message): Requires caller to have first opened a syslog session using syslog.openlog() """ - syslog.syslog(loglevel, message + '\n') - sys.stderr.write(message + '\n') + logging.getLogger('time-slider').error(message) def get_filesystem_capacity(path): """Returns filesystem space usage of path as an integer percentage of @@ -80,8 +80,8 @@ def get_filesystem_capacity(path): raise ValueError("%s is a non-existent path" % path) f = os.statvfs(path) - unavailBlocks = f[statvfs.F_BLOCKS] - f[statvfs.F_BAVAIL] - capacity = int(math.ceil(100 * (unavailBlocks / float(f[statvfs.F_BLOCKS])))) + unavailBlocks = f.f_blocks - f.f_bavail + capacity = int(math.ceil(100 * (unavailBlocks / float(f.f_blocks)))) return capacity @@ -90,8 +90,8 @@ def get_available_size(path): if not os.path.exists(path): raise ValueError("%s is a non-existent path" % path) f = os.statvfs(path) - free = long(f[statvfs.F_BAVAIL] * f[statvfs.F_FRSIZE]) - + free = int(f.f_bavail * f.f_frsize) + return free def get_used_size(path): @@ -101,8 +101,8 @@ def get_used_size(path): raise ValueError("%s is a non-existent path" % path) f = os.statvfs(path) - unavailBlocks = f[statvfs.F_BLOCKS] - f[statvfs.F_BAVAIL] - used = long(unavailBlocks * f[statvfs.F_FRSIZE]) + unavailBlocks = f.f_blocks - f.f_bavail + used = int(unavailBlocks * f.f_frsize) return used @@ -112,7 +112,7 @@ def get_total_size(path): if not os.path.exists(path): raise ValueError("%s is a non-existent path" % path) f = os.statvfs(path) - total = long(f[statvfs.F_BLOCKS] * f[statvfs.F_FRSIZE]) + total = int(f.f_blocks * f.f_frsize) return total