util/guid.py merged into ec.py that is the only one using it
[nepi.git] / src / nepi / execution / ec.py
index c138494..f282ac6 100644 (file)
@@ -3,9 +3,8 @@
 #    Copyright (C) 2013 INRIA
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU General Public License version 2 as
+#    published by the Free Software Foundation;
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -17,7 +16,8 @@
 #
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
-from nepi.util import guid
+from six import next
+
 from nepi.util.parallel import ParallelRun
 from nepi.util.timefuncs import tnow, tdiffsec, stabsformat, tsformat 
 from nepi.execution.resource import ResourceFactory, ResourceAction, \
@@ -102,6 +102,23 @@ class ECState(object):
     RELEASED = 3
     TERMINATED = 4
 
+# historical note: this class used to be in util/guid.py but is used only here
+# FIXME: This class is not thread-safe. Should it be made thread-safe?
+class GuidGenerator(object):
+    def __init__(self):
+        self._last_guid = 0
+
+    # historical note: this used to be called `next`
+    # which confused 2to3 - and me - while it has
+    # nothing to do at all with the iteration protocol
+    def generate(self, guid = None):
+        if guid == None:
+            guid = self._last_guid + 1
+
+        self._last_guid = self._last_guid if guid <= self._last_guid else guid
+
+        return guid
+
 class ExperimentController(object):
     """
     .. note::
@@ -224,7 +241,7 @@ class ExperimentController(object):
         self._persist = persist
 
         # generator of globally unique ids
-        self._guid_generator = guid.GuidGenerator()
+        self._guid_generator = GuidGenerator()
         
         # Resource managers
         self._resources = dict()
@@ -241,7 +258,7 @@ class ExperimentController(object):
         self._groups = dict()
 
         # generator of globally unique id for groups
-        self._group_id_generator = guid.GuidGenerator()
+        self._group_id_generator = GuidGenerator()
 
         # Flag to stop processing thread
         self._stop = False
@@ -539,7 +556,7 @@ class ExperimentController(object):
             
         """
         rms = []
-        for guid, rm in self._resources.iteritems():
+        for guid, rm in self._resources.items():
             if rm.get_rtype() == rtype: 
                 rms.append(rm)
         return rms
@@ -555,7 +572,7 @@ class ExperimentController(object):
             :rtype: list
 
         """
-        keys = self._resources.keys()
+        keys = list(self._resources.keys())
 
         return keys
 
@@ -569,7 +586,7 @@ class ExperimentController(object):
             
         """
         rms = []
-        for guid, rm in self._resources.iteritems():
+        for guid, rm in self._resources.items():
             if rm.get_rtype() == rtype: 
                 rms.append(rm.guid)
         return rms
@@ -588,7 +605,8 @@ class ExperimentController(object):
             
         """
         # Get next available guid
-        guid = self._guid_generator.next(guid)
+        # xxx_next_hiccup
+        guid = self._guid_generator.generate(guid)
         
         # Instantiate RM
         rm = ResourceFactory.create(rtype, self, guid)
@@ -974,7 +992,7 @@ class ExperimentController(object):
         if not guids:
             # If no guids list was passed, all 'NEW' RMs will be deployed
             guids = []
-            for guid, rm in self._resources.iteritems():
+            for guid, rm in self._resources.items():
                 if rm.state == ResourceState.NEW:
                     guids.append(guid)
                 
@@ -986,7 +1004,8 @@ class ExperimentController(object):
         new_group = False
         if not group:
             new_group = True
-            group = self._group_id_generator.next()
+            # xxx_next_hiccup
+            group = self._group_id_generator.generate()
 
         if group not in self._groups:
             self._groups[group] = []
@@ -1054,7 +1073,7 @@ class ExperimentController(object):
 
         """
         if self._state == ECState.RELEASED:
-           return 
+            return 
 
         if isinstance(guids, int):
             guids = [guids]
@@ -1187,7 +1206,7 @@ class ExperimentController(object):
             try:
                 self._cond.acquire()
 
-                task = self._scheduler.next()
+                task = next(self._scheduler)
                 
                 if not task:
                     # No task to execute. Wait for a new task to be scheduled.