+def format_bytes(bytes, si = True):
+ """
+ Formats bytes into a string
+ """
+ if si:
+ kilo = 1000.
+ else:
+ # Officially, a kibibyte
+ kilo = 1024.
+
+ if bytes >= (kilo * kilo * kilo):
+ return "%.1f GB" % (bytes / (kilo * kilo * kilo))
+ elif bytes >= 1000000:
+ return "%.1f MB" % (bytes / (kilo * kilo))
+ elif bytes >= 1000:
+ return "%.1f KB" % (bytes / kilo)
+ else:
+ return "%.0f bytes" % bytes
+
+def format_period(seconds):
+ """
+ Formats a period in seconds into a string
+ """
+
+ if seconds == (24 * 60 * 60):
+ return "day"
+ elif seconds == (60 * 60):
+ return "hour"
+ elif seconds > (24 * 60 * 60):
+ return "%.1f days" % (seconds / 24. / 60. / 60.)
+ elif seconds > (60 * 60):
+ return "%.1f hours" % (seconds / 60. / 60.)
+ elif seconds > (60):
+ return "%.1f minutes" % (seconds / 60.)
+ else:
+ return "%.0f seconds" % seconds
+
+def slicemail(slice, subject, body):
+ sendmail = os.popen("/usr/sbin/sendmail -N never -t -f%s" % PLC_MAIL_SUPPORT_ADDRESS, "w")
+
+ # PLC has a separate list for pl_mom messages
+ if PLC_MAIL_SUPPORT_ADDRESS == "support@planet-lab.org":
+ to = ["pl-mom@planet-lab.org"]
+ else:
+ to = [PLC_MAIL_SUPPORT_ADDRESS]
+
+ if slice is not None and slice != "root":
+ to.append(PLC_MAIL_SLICE_ADDRESS.replace("SLICE", slice))
+
+ header = {'from': "%s Support <%s>" % (PLC_NAME, PLC_MAIL_SUPPORT_ADDRESS),
+ 'to': ", ".join(to),
+ 'version': sys.version.split(" ")[0],
+ 'subject': subject}
+
+ # Write headers
+ sendmail.write(
+"""
+Content-type: text/plain
+From: %(from)s
+Reply-To: %(from)s
+To: %(to)s
+X-Mailer: Python/%(version)s
+Subject: %(subject)s
+
+""".lstrip() % header)
+
+ # Write body
+ sendmail.write(body)
+ # Done
+ sendmail.close()
+
+