reviewboard: allow fake ids file path to be passed in; r=dminor draft
authorGregory Szorc <gps@mozilla.com>
Thu, 28 Jan 2016 11:57:26 -0800
changeset 7032 88d7765ded31ee03578548e2c554f5d1a9f0d604
parent 7031 b7d67d015789389ae530619de60acc450009e474
child 7033 f57e76a45c9939f86b98a6f11015f37a3b43bfd7
push id583
push usergszorc@mozilla.com
push dateTue, 02 Feb 2016 00:32:12 +0000
reviewersdminor
reviewboard: allow fake ids file path to be passed in; r=dminor So we can eventually re-use this function from Git. MozReview-Commit-ID: Ljspt79gOA8
hgext/reviewboard/hgrb/util.py
--- a/hgext/reviewboard/hgrb/util.py
+++ b/hgext/reviewboard/hgrb/util.py
@@ -1,11 +1,12 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
+import errno
 import random
 import time
 
 from mercurial import util
 from mercurial.i18n import _
 
 class ReviewID(object):
     """Represents a parsed review identifier."""
@@ -58,17 +59,17 @@ class ReviewID(object):
         return s
 
 
 BASE62_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
 
 # (datetime.datetime(2000, 1, 1, 0, 0, 0, 0) - datetime.datetime.utcfromtimestamp(0)).total_seconds()
 EPOCH = 946684800
 
-def genid(repo):
+def genid(repo=None, fakeidpath=None):
     """Generate a unique identifier.
 
     Unique identifiers are treated as a black box. But under the hood, they
     consist of a time component and a random component.
 
     Each identifier is up to 64 bits. The first 32 bits are random. The
     final 32 bits are integer seconds since midnight UTC on January 1, 2000. We
     don't use UNIX epoch because dates from the 70's aren't interesting to us.
@@ -81,26 +82,38 @@ def genid(repo):
     Base62 is used as the encoding mechanism because it is safe for both
     URLs and revsets. We could get base66 for URLs, but the characters
     -~. could conflict with revsets.
     """
     # Provide a backdoor to generate deterministic IDs. This is used for
     # testing purposes because tests want constant output. And since
     # commit IDs go into the commit and are part of the SHA-1, they need
     # to be deterministic.
-    if repo.ui.configbool('reviewboard', 'fakeids'):
-        data = repo.vfs.tryread('genid')
+    if repo and repo.ui.configbool('reviewboard', 'fakeids'):
+        fakeidpath = repo.vfs.join('genid')
+
+    if fakeidpath:
+        try:
+            with open(fakeidpath, 'rb') as fh:
+                data = fh.read()
+        except IOError as e:
+            if e.errno != errno.ENOENT:
+                raise
+
+            data = None
+
         if data:
             n = int(data)
         else:
             n = 0
 
         seconds = EPOCH
         rnd = n
-        repo.vfs.write('genid', str(n + 1))
+        with open(fakeidpath, 'wb') as fh:
+            fh.write(str(n + 1))
     else:
         now = int(time.time())
         # May 5, 2015 sometime.
         if now < 1430860700:
             raise util.Abort('your system clock is wrong; fix your system '
                              'clock')
         seconds = now - EPOCH
         rnd = random.SystemRandom().getrandbits(32)