spacewalk/certs-tools/sslToolLib.py | 17 --
spacewalk/certs-tools/timeLib.py | 263 ------------------------------------
2 files changed, 5 insertions(+), 275 deletions(-)
New commits:
commit a9efe2116fe3dcf6022e00f6cbfab811f876888a
Author: Miroslav Suchý <msuchy(a)redhat.com>
Date: Fri Aug 19 10:44:38 2011 +0200
code cleanup - remove unused code from timeLib.py
diff --git a/spacewalk/certs-tools/timeLib.py b/spacewalk/certs-tools/timeLib.py
index 3e0339e..23c64f3 100644
--- a/spacewalk/certs-tools/timeLib.py
+++ b/spacewalk/certs-tools/timeLib.py
@@ -42,168 +42,17 @@ DAY = 24 * HOUR
WEEK = 7 * DAY
YEAR = 365 * DAY
-MAXINT, MININT = int(2L**31 - 1), int(-2L**31)
-
-class LeapYearException(Exception):
- "exception for leap year related things"
-
-def isLeapYearYN(year):
- yn4 = year/4.0
- if round(yn4) == yn4:
- # so far, yes
- yn100 = year/100.0
- if round(yn100) == yn100:
- yn400 = year/400.0
- return round(yn400) == yn400
- return 1
- return 0
-
-def checkLeapDay(tup):
- "raises an exception if a leap day but not valid"
- if not isLeapYearYN(tup[0]) and tup[1] == 2 and tup[2] == 29:
- raise LeapYearException(repr(tup))
-
-
def now():
return round(time())
def secs2str(format, secs):
assert type(secs) in (type(1), type(1.0))
return strftime(format, gmtime(round(secs)))
-str2tup = strptime
- # str2tup(s, format)
-def tup2secs(tup):
- assert isinstance(tup, struct_time)
- return mktime(tup) - timezone
-def secs2tup(secs):
- assert type(secs) in (type(1), type(1L), type(1.0))
- return gmtime(round(secs))
-def tup2str(format, tup):
- return secs2str(format, tup2secs(tup))
def str2secs(s, format):
- return tup2secs(str2tup(s, format))
-
-def str2str(s, formatIn, formatOut):
- return tup2str(formatOut, str2tup(s, formatIn))
-
-
-def checkTuple(tup, ignoreLeapYear=0):
- """ checks validity of a time tuple. Raises ValueError or
LeapYearException
- Time tuple is: (YYYY, MM, DD, hh, mm, ss, wkday, day, dst)
- ( 0, 1, 2, 3, 4, 5, 6, 7 8)
- YYYY = 1901 - 1938 (for 32-bit boxes)
- MM = 01 - 12
- DD = 01 - 31
- hh, mm, ss = the obvious
- wkday = 0 - 6
- day = 1 - 366
- dst = -1, 0, 1 (-1=no-info, 0=not-dst, 1=dst)
- """
-
- if 0 in tup[0:3]:
- raise ValueError("Year, month, and day must be a non-negative, positive
number: %s" % tup)
-
- # month
- if tup[1] not in range(1, 13):
- raise ValueError("Month value is calculated to within a range of 1-12:
%s" % tup)
-
- # day
- if tup[2] not in range(1, 32):
- raise ValueError("Day value is calculated to within a range of 1-31:
%s" % tup)
-
- # days
- if tup[7] not in range(1, 367):
- raise ValueError("Days value is calculated to within a range of 1-367:
%s" % tup)
-
- # leapyear check
- if not ignoreLeapYear:
- # can raise LeapYearException
- checkLeapDay(tup)
-
- # 31-day month check
- if tup[2] == 31:
- if tup[1] not in (1, 3, 5, 7, 8, 10, 12):
- raise ValueError("Day value can't exceed 31 for months (2, 4, 6, 9,
11): %s" % tup)
-
- # epoch rollunder check
- t = secs2tup(MININT)
- underYN = 0
- if tup[0] < t[0]:
- underYN = 1
- elif tup[0] == t[0]:
- if tup[1] < t[1]:
- underYN = 1
- elif tup[1] == t[1]:
- if tup[2] < t[2]:
- underYN = 1
- elif tup[2] == t[2]:
- if tup[3] < t[3]:
- underYN = 1
- elif tup[3] == t[3]:
- if tup[4] < t[4]:
- underYN = 1
- elif tup[4] == t[4]:
- if tup[5] < t[5]:
- underYN = 1
- if underYN:
- raise ValueError('Tuple "under"flows the POSIX Epoch: %s' %
tup)
-
- # epoch rollover check
- t = secs2tup(MAXINT)
- overYN = 0
- if tup[0] > t[0]:
- overYN = 1
- elif tup[0] == t[0]:
- if tup[1] > t[1]:
- overYN = 1
- elif tup[1] == t[1]:
- if tup[2] > t[2]:
- overYN = 1
- elif tup[2] == t[2]:
- if tup[3] > t[3]:
- overYN = 1
- elif tup[3] == t[3]:
- if tup[4] > t[4]:
- overYN = 1
- elif tup[4] == t[4]:
- if tup[5] > t[5]:
- overYN = 1
- if overYN:
- raise ValueError("Tuple overlows the POSIX Epoch: %s" % tup)
-
+ return mktime(strptime(s, format)) - timezone
-def tup_addYMD(tup, years=0, months=0, days=0, ignoreLeapYear=0):
- """ add years, months, days to a time tuple, checking for leap year
- sensibilities. Can throw a LeapYearException or ValueError.
- """
- tup = list(tup)
- tup[0], tup[1], tup[2], tup[7] = tup[0]+years, tup[1]+months, tup[2]+days,
tup[7]+days
- tup = struct_time(tup)
- checkTuple(tup)
- return tup
-
-
-def years2secs(years):
- "an approximation"
- return years*YEAR
-def weeks2secs(weeks):
- return weeks*WEEK
-def days2secs(days):
- return days*DAY
-def hours2secs(hours):
- return hours*HOUR
-def mins2secs(mins):
- return mins*MIN
-
-
-def secs2mins(secs):
- return round(secs/MIN)
-def secs2hours(secs):
- return round(secs/HOUR)
def secs2days(secs):
return round(secs/DAY)
-def secs2weeks(secs):
- return round(secs/WEEK)
def secs2years(secs):
"an approximation"
return round(secs/YEAR)
@@ -216,111 +65,10 @@ def _test():
print 'Right now, in seconds (epoch): ', nowS
print 'Right now, stringified: ', secs2str(F, nowS)
- print 'One year from now, in seconds:
', nowS+YEAR
- print 'One year from now, secs2tup:
', secs2tup(nowS+YEAR)
- print '(1)...secs2str(F, ...):
',\
- secs2str(F, nowS+YEAR)
- print '(2)...str2tup(secs2str(F, ...), F):
',\
- str2tup(secs2str(F, nowS+YEAR), F)
- print '(3)...tup2secs(str2tup(secs2str(F, ...), F)):
',\
- tup2secs(str2tup(secs2str(F, nowS+YEAR), F))
- print '(4)...secs2tup(tup2secs(str2tup(secs2str(F, ...), F))):
',\
- secs2tup(tup2secs(str2tup(secs2str(F, nowS+YEAR), F)))
- print '(5)...tup2str(F, secs2tup(tup2secs(str2tup(secs2str(F, ...), F)))):
',\
- tup2str(F, secs2tup(tup2secs(str2tup(secs2str(F, nowS+YEAR), F))))
- print '(6)...str2secs(tup2str(F, secs2tup(tup2secs(str2tup(secs2str(F, ...),
F)))), F):',\
- str2secs(tup2str(F, secs2tup(tup2secs(str2tup(secs2str(F, nowS+YEAR), F)))), F)
- print "%s --> %s" % (secs2str(F, nowS+YEAR),
- str2str(secs2str(F, nowS+YEAR), F,
- "%Y-%m-%d %H:%M:%S"))
-
-
print 'YEAR, WEEK, DAY, HOUR, MIN: ', YEAR, WEEK, DAY, HOUR, MIN
- print 'years2secs(1): ', years2secs(1)
- print 'weeks2secs(1): ', weeks2secs(1)
- print 'days2secs(1): ', days2secs(1)
- print 'hours2secs(1): ', hours2secs(1)
- print 'mins2secs(1): ', mins2secs(1)
- print 'secs2mins(MIN): ', secs2mins(MIN)
- print 'secs2hours(HOUR):', secs2hours(HOUR)
print 'secs2days(DAY): ', secs2days(DAY)
- print 'secs2weeks(WEEK):', secs2weeks(WEEK)
print 'secs2years(YEAR):', secs2years(YEAR)
- print
- print 'Now (reminder) now: %s, %s' % (nowS, secs2tup(nowS))
- print ('TRULY one year, one month, one day from now:\n'
- '- in seconds: %s'
- % str(tup2secs(tup_addYMD(secs2tup(nowS),
- 1,1,1, ignoreLeapYear=1))))
- print ('- as a tuple: %s'
- % str(tup_addYMD(secs2tup(nowS), 1,1,1, ignoreLeapYear=1)))
-
- try:
- tup = struct_time((2004, 02, 28, 6, 35, 14, 0, 285, 1))
- print 'This is a good leap day: %s' % str(tup_addYMD(tup, 0,0,1))
- except LeapYearException, e:
- print 'Verified correctly bad! - %s' % e
- else:
- print 'Verified good!'
-
- try:
- tup = struct_time((2005, 02, 28, 6, 35, 14, 0, 285, 1))
- # for printing
- print 'This is a bad leap day: %s' % str(tup_addYMD(tup, 0,0,1,
ignoreLeapYear=1))
- # for breaking
- tup_addYMD(tup, 0,0,1)
- except LeapYearException, e:
- print 'Verified correctly bad! - %s' % e
- else:
- print 'Verified good!'
-
- print "Tuple sanity checks"
- t = secs2tup(MAXINT)
- t = list(t)
- t[5] = t[5] + 1
- t = struct_time(t)
- print "One second over the POSIX Epoch max: %s" % t
- try:
- print "checkTuple(t)..."; checkTuple(t)
- except Exception, e:
- print e
- t = secs2tup(MININT)
- t = list(t)
- t[5] = t[5] - 1
- t = struct_time(t)
- print "One second under the POSIX Epoch min: %s" % t
- try:
- print "checkTuple(t)..."; checkTuple(t)
- except Exception, e:
- print e
-
- print "Not testing all checkTuple checks."
-
- print "April does not have 31 days..."
- t = secs2tup(0)
- t = list(t)
- t[1] = 4 # april
- t[2] = 31
- t = struct_time(t)
- try:
- print "checkTuple(t)..."; checkTuple(t)
- except Exception, e:
- print e
-
- print "March does have 31 days..."
- t = secs2tup(0)
- t = list(t)
- t[1] = 3 # march
- t[2] = 31
- t = struct_time(t)
- try:
- print "checkTuple(t)..."; checkTuple(t)
- except Exception, e:
- print e
-
-
-
if __name__ == '__main__':
_test()
commit 0beb9ccdb1482f7d14eb92f04b8289556e95ad86
Author: Miroslav Suchý <msuchy(a)redhat.com>
Date: Fri Aug 19 09:56:29 2011 +0200
struct_time is in python since 2.2
diff --git a/spacewalk/certs-tools/timeLib.py b/spacewalk/certs-tools/timeLib.py
index 2454f4d..3e0339e 100644
--- a/spacewalk/certs-tools/timeLib.py
+++ b/spacewalk/certs-tools/timeLib.py
@@ -33,14 +33,7 @@
#
-from time import strftime, strptime, mktime, gmtime, timezone
-_struct_time = type(())
-try:
- # struct_time not in python 1.5.2
- from time import struct_time
- _struct_time = struct_time
-except ImportError:
- pass
+from time import strftime, strptime, mktime, gmtime, timezone, struct_time
from time import time
MIN = 60.0
@@ -80,7 +73,7 @@ def secs2str(format, secs):
str2tup = strptime
# str2tup(s, format)
def tup2secs(tup):
- assert isinstance(tup, _struct_time)
+ assert isinstance(tup, struct_time)
return mktime(tup) - timezone
def secs2tup(secs):
assert type(secs) in (type(1), type(1L), type(1.0))
@@ -185,7 +178,7 @@ def tup_addYMD(tup, years=0, months=0, days=0, ignoreLeapYear=0):
"""
tup = list(tup)
tup[0], tup[1], tup[2], tup[7] = tup[0]+years, tup[1]+months, tup[2]+days,
tup[7]+days
- tup = _struct_time(tup)
+ tup = struct_time(tup)
checkTuple(tup)
return tup
@@ -264,7 +257,7 @@ def _test():
% str(tup_addYMD(secs2tup(nowS), 1,1,1, ignoreLeapYear=1)))
try:
- tup = _struct_time((2004, 02, 28, 6, 35, 14, 0, 285, 1))
+ tup = struct_time((2004, 02, 28, 6, 35, 14, 0, 285, 1))
print 'This is a good leap day: %s' % str(tup_addYMD(tup, 0,0,1))
except LeapYearException, e:
print 'Verified correctly bad! - %s' % e
@@ -272,7 +265,7 @@ def _test():
print 'Verified good!'
try:
- tup = _struct_time((2005, 02, 28, 6, 35, 14, 0, 285, 1))
+ tup = struct_time((2005, 02, 28, 6, 35, 14, 0, 285, 1))
# for printing
print 'This is a bad leap day: %s' % str(tup_addYMD(tup, 0,0,1,
ignoreLeapYear=1))
# for breaking
@@ -286,7 +279,7 @@ def _test():
t = secs2tup(MAXINT)
t = list(t)
t[5] = t[5] + 1
- t = _struct_time(t)
+ t = struct_time(t)
print "One second over the POSIX Epoch max: %s" % t
try:
print "checkTuple(t)..."; checkTuple(t)
@@ -295,7 +288,7 @@ def _test():
t = secs2tup(MININT)
t = list(t)
t[5] = t[5] - 1
- t = _struct_time(t)
+ t = struct_time(t)
print "One second under the POSIX Epoch min: %s" % t
try:
print "checkTuple(t)..."; checkTuple(t)
@@ -309,7 +302,7 @@ def _test():
t = list(t)
t[1] = 4 # april
t[2] = 31
- t = _struct_time(t)
+ t = struct_time(t)
try:
print "checkTuple(t)..."; checkTuple(t)
except Exception, e:
@@ -320,7 +313,7 @@ def _test():
t = list(t)
t[1] = 3 # march
t[2] = 31
- t = _struct_time(t)
+ t = struct_time(t)
try:
print "checkTuple(t)..."; checkTuple(t)
except Exception, e:
commit ab414bd4d4ba52d47831463e9550bad1b5f73a1f
Author: Miroslav Suchý <msuchy(a)redhat.com>
Date: Fri Aug 19 09:50:39 2011 +0200
simplify code - utilize os.urandom()
new in python 2.4
diff --git a/spacewalk/certs-tools/sslToolLib.py b/spacewalk/certs-tools/sslToolLib.py
index 93634c5..9cd4fb0 100644
--- a/spacewalk/certs-tools/sslToolLib.py
+++ b/spacewalk/certs-tools/sslToolLib.py
@@ -195,10 +195,8 @@ class TempDir:
# add some quick and dirty randomness to the tempfilename
s = ''
- x = open('/dev/urandom', 'rb')
while len(s) < 10:
- s = s + str(ord(x.read(1)))
- x.close()
+ s = s + str(ord(os.urandom(1)))
self.path = tempfile.mkdtemp(suffix='-'+s+suffix)
def getdir(self):
commit a7e27aab08ca9cace6a9dc016b2291c90e4b8650
Author: Miroslav Suchý <msuchy(a)redhat.com>
Date: Fri Aug 19 09:45:44 2011 +0200
remove rant about python 1.5
diff --git a/spacewalk/certs-tools/sslToolLib.py b/spacewalk/certs-tools/sslToolLib.py
index 04abd54..93634c5 100644
--- a/spacewalk/certs-tools/sslToolLib.py
+++ b/spacewalk/certs-tools/sslToolLib.py
@@ -206,7 +206,7 @@ class TempDir:
getpath = getdir
def __del__(self):
- "a destructor that may never be called because python 1.5.2 sucks"
+ """ delete temporary directory when done with it
"""
self._shutil.rmtree(self.path)
close = __del__
commit 9c43c82126599eb0c6553f5bc48d9a34696eb490
Author: Miroslav Suchý <msuchy(a)redhat.com>
Date: Fri Aug 19 09:43:58 2011 +0200
simplify code
this was due tempfile confusion prior python 2.3
diff --git a/spacewalk/certs-tools/sslToolLib.py b/spacewalk/certs-tools/sslToolLib.py
index d972d35..04abd54 100644
--- a/spacewalk/certs-tools/sslToolLib.py
+++ b/spacewalk/certs-tools/sslToolLib.py
@@ -199,16 +199,7 @@ class TempDir:
while len(s) < 10:
s = s + str(ord(x.read(1)))
x.close()
- self.path = self._getTempPath(suffix='-'+s+suffix)
- # tempfile.mkdtemp actaully *creates* the directory
- if not os.path.exists(self.path):
- os.makedirs(self.path, 0700)
-
- def _getTempPath(self, suffix):
- """ fetch the temporary directory path using the most
"correct"
- mk*temp function for this python version
- """
- return tempfile.mkdtemp(suffix=suffix)
+ self.path = tempfile.mkdtemp(suffix='-'+s+suffix)
def getdir(self):
return self.path