Utilities

relengapi.util.synchronized(lock)[source]
Parameters:

lock -- a Lock instance

This decorator will acquire and release the lock before and after the decorated funtion runs. The effect is that, for a given lock, only one function decorated with @synchronized(lock) can execute at a time.

relengapi.util.tz.utcnow()[source]

Returns the datetime.datetime.utcnow() value, with added pytz.UTC tzinfo

relengapi.util.tz.utcfromtimestamp(timestamp)[source]
Parameters:

timestamp -- POSIX timestamp

returns the datetime.datetime.utcfromtimestamp(timestamp) value with added pytz.UTC tzinfo

relengapi.util.tz.dt_as_timezone(obj, dest_tzinfo)[source]
Parameters:
  • obj -- a datetime object, with valid tzinfo

  • dest_tzinfo -- a timezone class as provided by pytz

Converts the passed datetime into a new timezone. (According to world clock)

example:

import pytz
from relengapi.util.tz import dt_as_timezone, utcnow
dt = utcnow()
print repr(dt)
# datetime.datetime(2014, 5, 23, 16, 39, 32, 125099, tzinfo=<UTC>)
print dt
# 2014-05-23 16:39:32.125099+00:00
repr(pytz.timezone("US/Pacific"))
# <DstTzInfo 'US/Pacific' PST-1 day, 16:00:00 STD>
repr(dt_as_timezone(dt, pytz.timezone("US/Pacific")))
# datetime.datetime(2014, 5, 23, 9, 39, 32, 125099, tzinfo=<DstTzInfo 'US/Pacific' PDT-1 day, 17:00:00 DST>)
print dt_as_timezone(dt, pytz.timezone("US/Pacific"))
# 2014-05-23 09:39:32.125099-07:00

Note

Passing in a timezone unaware object will raise ValueError:

import datetime
dt2 = datetime.datetime.utcnow()
dt_as_timezone(dt2, pytz.timezone("US/Pacific"))
# Traceback (most recent call last):
#   File "<stdin>", ....
#   File ".../base/relengapi/util/tz.py", line 17, in dt_as_timezone
#     raise ValueError("Must pass a timezone aware datetime object")
# ValueError: Must pass a timezone aware datetime object

Request ID

Each request is assigned a unique ID, available in g.request_id. This ID is included in structured logging output. It may also be helpful to supply this ID to the user when an error occurs, or anywhere it may be useful for debugging.