Below is the file 'common.py' from this revision. You can also download the file.

# Copyright (C) 2005 Grahame Bowland <grahame@angrygoats.net>
#
# This program is made available under the GNU GPL version 2.0 or
# greater. See the accompanying file COPYING for details.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.


import datetime
import time
import fcntl
import os
import signal
from web import debug
import traceback

def parse_timecert(value):
    # The datetime may contain microseconds which time.strptime cannot parse.
    # Drop them.  This is easy because Monotone keeps timestamps in ISO 8601
    # format, so microseconds necessarily start with a period.
    index_of_period = value.find ('.')
    if index_of_period > -1:
        value = value[0:index_of_period]
    return apply(datetime.datetime, time.strptime(value, "%Y-%m-%dT%H:%M:%S")[:6])

    return apply(datetime.datetime, time.strptime(value, "%Y-%m-%dT%H:%M:%S")[:6])

def set_nonblocking(fd):
	fl = fcntl.fcntl(fd, fcntl.F_GETFL)
	fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NDELAY)

def terminate_popen3(process):
        debug("[%s] stopping process: %s" % (os.getpid(), process.pid))
        try:
            process.tochild.close()
            process.fromchild.close()
            process.childerr.close()
            if process.poll() == -1:
                # the process is still running, so kill it.
                os.kill(process.pid, signal.SIGKILL)
            process.wait()
        except:
            debug("%s failed_to_stop %s (%s)" % (os.getpid(), process.pid, traceback.format_exc()))

def ago(event):
    def plural(v, singular, plural):
        if v == 1:
            return "%d %s" % (v, singular)
        else:
            return "%d %s" % (v, plural)
    now = datetime.datetime.utcnow()
    ago = now - event
    if ago.days > 0:
        rv = "%s" % (plural(ago.days, "day", "days"))
    elif ago.seconds > 3600:
        hours = ago.seconds / 3600
        minutes = (ago.seconds - (hours * 3600)) / 60
        rv = "%s" % (plural(hours, "hour", "hours"))
    else:
        minutes = ago.seconds / 60
        seconds = (ago.seconds - (minutes * 60))
        rv = "%s" % (plural(minutes, "minute", "minutes"))
    return rv

###
### vi:expandtab:sw=4:ts=4
###