Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Included Tony's uptime enhancement.

  • Loading branch information...
commit c3d28b3653c59b63e05bf398d7dd5a9c361fadba 1 parent f88b1da
@commandline authored
Showing with 49 additions and 2 deletions.
  1. +1 −0  CREDITS.txt
  2. +48 −2 flashbake/plugins/uptime.py
View
1  CREDITS.txt
@@ -11,6 +11,7 @@ and working directly on the project:
* Ben Snider, bensnider.com - the original code for microblogs.py, docs
* garthrk, github.org/garthrk - random fixes and tests, docs
* Jason Penney, jasonpenney.net - brain storming, random fixes and enhancements
+* Tony Giunta - alternate implementation of uptime that uses uptime command
I want to give a special thanks to Jonathan Coulton, Brad Turcotte and Beatnik
Turtle for freely offering their songs for download. Being able to grab a
View
50 flashbake/plugins/uptime.py
@@ -20,6 +20,7 @@
import string
import os.path
import logging
+from subprocess import Popen, PIPE
from flashbake.plugins import AbstractMessagePlugin
class UpTime(AbstractMessagePlugin):
@@ -43,8 +44,7 @@ def __calcuptime(self):
http://thesmithfam.org/blog/2005/11/19/python-uptime-script/ """
if not os.path.exists('/proc/uptime'):
- logging.warn('/proc/uptime doesn\'t exist.')
- return None
+ return self.__run_uptime()
f = open( "/proc/uptime" )
try:
@@ -78,3 +78,49 @@ def __calcuptime(self):
string += str(seconds) + " " + (seconds == 1 and "second" or "seconds" )
return string
+
+
+ def __run_uptime(self):
+ """ For OSes that don't provide procfs, then try to use the updtime command.
+
+ Thanks to Tony Giunta for this contribution. """
+ # Try to capture output of 'uptime' command,
+ # if not found, catch OSError, log and return None
+ try:
+ output = Popen("uptime", stdout=PIPE).communicate()[0].split()
+ except OSError:
+ logging.warn("Can't find 'uptime' command in $PATH")
+ return None
+
+ # Parse uptime output string
+ # if len == 10 or 11, uptime is less than a day
+ if len(output) in [10,11]:
+ days = "00"
+ hours_and_minutes = output[2].strip(",")
+ elif len(output) == 12:
+ days = output[2]
+ hours_and_minutes = output[4].strip(",")
+ else:
+ return None
+
+ # If time is exactly x hours/mins, no ":" in "hours_and_minutes"
+ # and the interpreter will throw a ValueError
+ try:
+ hours, minutes = hours_and_minutes.split(":")
+ except ValueError:
+ if output[3].startswith("hr"):
+ hours = hours_and_minutes
+ minutes = "00"
+ elif output[3].startwwith("min"):
+ hours = "00"
+ minutes = hours_and_minutes
+ else:
+ return None
+
+ # Build up output string, might require Python 2.5+
+ uptime = (days + (" day, " if days == "1" else " days, ") +
+ hours + (" hour, " if hours == "1" else " hours, ") +
+ minutes + (" minute" if minutes == "1" else " minutes"))
+
+ return uptime
+
Please sign in to comment.
Something went wrong with that request. Please try again.