From: Tony Mack Date: Thu, 22 Dec 2011 21:05:47 +0000 (-0500) Subject: replaced epochparse with datetime_to_epoch() X-Git-Tag: sfa-2.0-7~13^2~4 X-Git-Url: http://git.onelab.eu/?p=sfa.git;a=commitdiff_plain;h=d0df9c791e84a92c5d0dd397b92ebcfd3ea4b6f7 replaced epochparse with datetime_to_epoch() --- diff --git a/sfa/managers/registry_manager.py b/sfa/managers/registry_manager.py index 1c66e532..6c8c3686 100644 --- a/sfa/managers/registry_manager.py +++ b/sfa/managers/registry_manager.py @@ -7,6 +7,7 @@ import commands from sfa.util.faults import RecordNotFound, AccountNotEnabled, PermissionError, MissingAuthority, \ UnknownSfaType, ExistingRecord, NonExistingRecord +from sfa.util.sfatime import utcparse, datetime_to_epoch from sfa.util.prefixTree import prefixTree from sfa.util.xrn import Xrn, get_authority, hrn_to_urn, urn_to_hrn from sfa.util.plxrn import hrn_to_pl_login_base @@ -84,7 +85,9 @@ class RegistryManager: new_cred.set_privileges(rights) new_cred.get_privileges().delegate_all_privileges(True) if 'expires' in record: - new_cred.set_expiration(int(record['expires'])) + date = utcparse(record['expires']) + expires = datetime_to_epoch(date) + new_cred.set_expiration(int(expires)) auth_kind = "authority,ma,sa" # Parent not necessary, verify with certs #new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind)) diff --git a/sfa/plc/plaggregate.py b/sfa/plc/plaggregate.py index 7e51c400..d5812107 100644 --- a/sfa/plc/plaggregate.py +++ b/sfa/plc/plaggregate.py @@ -1,6 +1,6 @@ #!/usr/bin/python from sfa.util.xrn import Xrn, hrn_to_urn, urn_to_hrn, urn_to_sliver_id -from sfa.util.sfatime import epochparse +from sfa.util.sfatime import utcparse, datetime_to_string from sfa.util.sfalogging import logger from sfa.rspecs.rspec import RSpec @@ -240,7 +240,7 @@ class PlAggregate: slice, slivers = self.get_slice_and_slivers(slice_xrn) rspec = RSpec(version=rspec_version, user_options=options) if slice and 'expires' in slice: - rspec.xml.set('expires', epochparse(slice['expires'])) + rspec.xml.set('expires', datetime_to_string(utcparse(slice['expires']))) nodes, links = self.get_nodes_and_links(slice, slivers) rspec.version.add_nodes(nodes) diff --git a/sfa/plc/pldriver.py b/sfa/plc/pldriver.py index e9f818c8..62eca377 100644 --- a/sfa/plc/pldriver.py +++ b/sfa/plc/pldriver.py @@ -6,7 +6,7 @@ from sfa.util.faults import MissingSfaInfo, UnknownSfaType, \ from sfa.util.sfalogging import logger from sfa.util.defaultdict import defaultdict -from sfa.util.sfatime import utcparse, epochparse +from sfa.util.sfatime import utcparse, datetime_to_string, datetime_to_epoch from sfa.util.xrn import hrn_to_urn, get_leaf, urn_to_sliver_id from sfa.util.cache import Cache @@ -224,8 +224,10 @@ class PlDriver (Driver): pl_record["url"] = sfa_record["url"] if "description" in sfa_record: pl_record["description"] = sfa_record["description"] - if "expires" in sfa_record: - pl_record["expires"] = int(sfa_record["expires"]) + if "expires" in sfa_record: + date = utcparse(sfa_record['expires']) + expires = datetime_to_epoch(date) + pl_record["expires"] = expires elif type == "node": if not "hostname" in pl_record: @@ -402,7 +404,9 @@ class PlDriver (Driver): record['sites'] = site_hrns if 'expires' in record: - record['expires'] = utcparse(record['expires']) + date = utcparse(record['expires']) + datestring = datetime_to_string(date) + record['expires'] = datestring return records @@ -629,7 +633,7 @@ class PlDriver (Driver): top_level_status = 'ready' result['geni_urn'] = slice_urn result['pl_login'] = slice['name'] - result['pl_expires'] = epochparse(slice['expires']) + result['pl_expires'] = datetime_to_string(utcparse(slice['expires'])) resources = [] for node in nodes: @@ -638,7 +642,8 @@ class PlDriver (Driver): res['pl_boot_state'] = node['boot_state'] res['pl_last_contact'] = node['last_contact'] if node['last_contact'] is not None: - res['pl_last_contact'] = epochparse(node['last_contact']) + + res['pl_last_contact'] = datetime_to_string(utcparse(node['last_contact'])) sliver_id = urn_to_sliver_id(slice_urn, slice['slice_id'], node['node_id']) res['geni_urn'] = sliver_id if node['boot_state'] == 'boot': @@ -719,7 +724,7 @@ class PlDriver (Driver): raise RecordNotFound(slice_hrn) slice = slices[0] requested_time = utcparse(expiration_time) - record = {'expires': int(time.mktime(requested_time.timetuple()))} + record = {'expires': int(datetime_to_epoch(requested_time.timetuple()))} try: self.shell.UpdateSlice(slice['slice_id'], record) return True diff --git a/sfa/util/sfatime.py b/sfa/util/sfatime.py index a1a4bf06..473056e2 100644 --- a/sfa/util/sfatime.py +++ b/sfa/util/sfatime.py @@ -5,6 +5,8 @@ import time from sfa.util.sfalogging import logger +DATEFORMAT = "%Y-%d-%mT%H:%M:%SZ" + def utcparse(input): """ Translate a string into a time using dateutil.parser.parse but make sure it's in UTC time and strip the timezone, so that it's compatible with normal datetime.datetime objects. @@ -25,5 +27,14 @@ For safety this can also handle inputs that are either timestamps, or datetimes else: logger.error("Unexpected type in utcparse [%s]"%type(input)) -def epochparse(input): - return time.strftime("%Y-%d-%mT%H:%M:%SZ", time.localtime(input)) +def datetime_to_string(input): + return datetime.datetime.strftime(input, DATEFORMAT) + +def datetime_to_utc(input): + return time.gmtime(datetime_to_epoch(input)) + +def datetime_to_epoch(input): + return time.mktime(input.timetuple()) + + +