from foolscap import eventual
import itertools
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.mutable import MutableShareFile
from allmydata.storage.immutable import BucketWriter, BucketReader
self.failUnless("garbage-collection mode 'bogus'"
" 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):
basedir = "storage/LeaseCrawler/limited_history"
fileutil.make_dirs(basedir)
localseconds += subsecfloat
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