add utility function to parse durations, for lease-expiration config

This commit is contained in:
Brian Warner 2009-03-17 00:01:17 -07:00
parent 24ab5ec26f
commit 406fdba61f
2 changed files with 40 additions and 1 deletions

View File

@ -8,7 +8,7 @@ from twisted.application import service
from foolscap import eventual from foolscap import eventual
import itertools import itertools
from allmydata import interfaces from allmydata import interfaces
from allmydata.util import fileutil, hashutil, base32, pollmixin from allmydata.util import fileutil, hashutil, base32, pollmixin, time_format
from allmydata.storage.server import StorageServer from allmydata.storage.server import StorageServer
from allmydata.storage.mutable import MutableShareFile from allmydata.storage.mutable import MutableShareFile
from allmydata.storage.immutable import BucketWriter, BucketReader from allmydata.storage.immutable import BucketWriter, BucketReader
@ -2144,6 +2144,21 @@ class LeaseCrawler(unittest.TestCase, pollmixin.PollMixin, WebRenderingMixin):
self.failUnless("garbage-collection mode 'bogus'" self.failUnless("garbage-collection mode 'bogus'"
" must be 'age' or 'date-cutoff'" in str(e), str(e)) " must be 'age' or 'date-cutoff'" in str(e), str(e))
def test_parse_duration(self):
DAY = 24*60*60
MONTH = 31*DAY
YEAR = 365*DAY
p = time_format.parse_duration
self.failUnlessEqual(p("7days"), 7*DAY)
self.failUnlessEqual(p("31day"), 31*DAY)
self.failUnlessEqual(p("60 days"), 60*DAY)
self.failUnlessEqual(p("2mo"), 2*MONTH)
self.failUnlessEqual(p("3 month"), 3*MONTH)
self.failUnlessEqual(p("2years"), 2*YEAR)
e = self.failUnlessRaises(ValueError, p, "2kumquats")
self.failUnless("no unit (like day, month, or year) in '2kumquats'"
in str(e), str(e))
def test_limited_history(self): def test_limited_history(self):
basedir = "storage/LeaseCrawler/limited_history" basedir = "storage/LeaseCrawler/limited_history"
fileutil.make_dirs(basedir) fileutil.make_dirs(basedir)

View File

@ -34,3 +34,27 @@ def iso_utc_time_to_localseconds(isotime, _conversion_re=re.compile(r"(?P<year>\
localseconds += subsecfloat localseconds += subsecfloat
return localseconds return localseconds
def parse_duration(s):
orig = s
unit = None
DAY = 24*60*60
MONTH = 31*DAY
YEAR = 365*DAY
if s.endswith("s"):
s = s[:-1]
if s.endswith("day"):
unit = DAY
s = s[:-len("day")]
elif s.endswith("month"):
unit = MONTH
s = s[:-len("month")]
elif s.endswith("mo"):
unit = MONTH
s = s[:-len("mo")]
elif s.endswith("year"):
unit = YEAR
s = s[:-len("YEAR")]
else:
raise ValueError("no unit (like day, month, or year) in '%s'" % orig)
s = s.strip()
return int(s) * unit