summaryrefslogtreecommitdiff
path: root/src/mailman/utilities/uid.py
diff options
context:
space:
mode:
authorBarry Warsaw2011-04-22 19:26:43 -0400
committerBarry Warsaw2011-04-22 19:26:43 -0400
commitb5b015ce524157cfef4b795e4b0c7ff17b4d0fe2 (patch)
tree260169450ab382cf59143f0fe8bae0fc0507452f /src/mailman/utilities/uid.py
parent6959e6adcb582172aeec01ef6b6a75b9ba85017b (diff)
downloadmailman-b5b015ce524157cfef4b795e4b0c7ff17b4d0fe2.tar.gz
mailman-b5b015ce524157cfef4b795e4b0c7ff17b4d0fe2.tar.zst
mailman-b5b015ce524157cfef4b795e4b0c7ff17b4d0fe2.zip
Diffstat (limited to 'src/mailman/utilities/uid.py')
-rw-r--r--src/mailman/utilities/uid.py25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/mailman/utilities/uid.py b/src/mailman/utilities/uid.py
index 3d58cace5..e44ed2983 100644
--- a/src/mailman/utilities/uid.py
+++ b/src/mailman/utilities/uid.py
@@ -38,6 +38,7 @@ import hashlib
from flufl.lock import Lock
from mailman.config import config
+from mailman.model.uid import UID
from mailman.testing import layers
from mailman.utilities.passwords import SALT_LENGTH
@@ -46,13 +47,15 @@ from mailman.utilities.passwords import SALT_LENGTH
class UniqueIDFactory:
"""A factory for unique ids."""
- def __init__(self):
+ def __init__(self, context=None):
# We can't call reset() when the factory is created below, because
# config.VAR_DIR will not be set at that time. So initialize it at
# the first use.
self._uid_file = None
self._lock_file = None
self._lockobj = None
+ self._context = context
+ layers.MockAndMonkeyLayer.register_reset(self.reset)
@property
def _lock(self):
@@ -60,6 +63,8 @@ class UniqueIDFactory:
# These will get automatically cleaned up by the test
# infrastructure.
self._uid_file = os.path.join(config.VAR_DIR, '.uid')
+ if self._context:
+ self._uid_file += '.' + self._context
self._lock_file = self._uid_file + '.lock'
self._lockobj = Lock(self._lock_file)
return self._lockobj
@@ -76,11 +81,18 @@ class UniqueIDFactory:
# tests) that it will not be a problem. Maybe.
return self._next_uid()
salt = os.urandom(SALT_LENGTH)
- h = hashlib.sha1(repr(time.time()))
- h.update(salt)
- if bytes is not None:
- h.update(bytes)
- return unicode(h.hexdigest(), 'us-ascii')
+ while True:
+ h = hashlib.sha1(repr(time.time()))
+ h.update(salt)
+ if bytes is not None:
+ h.update(bytes)
+ uid = unicode(h.hexdigest(), 'us-ascii')
+ try:
+ UID.record(uid)
+ except ValueError:
+ pass
+ else:
+ return uid
def _next_uid(self):
with self._lock:
@@ -106,4 +118,3 @@ class UniqueIDFactory:
factory = UniqueIDFactory()
-layers.MockAndMonkeyLayer.register_reset(factory.reset)