From 49a1e24fcc6bc0c23181665519fc92ae4fb0de58 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Tue, 27 Oct 2009 22:55:04 +0000 Subject: Add a parsedate() function which emulates rfc822.parsedate(), since python3 doesn't have it. (trunk r14729) svn path=/main/branches/2.1.7/; revision=14737 --- bin/repoman | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'bin/repoman') diff --git a/bin/repoman b/bin/repoman index f6fb55b31..80e7a97df 100755 --- a/bin/repoman +++ b/bin/repoman @@ -32,11 +32,6 @@ try: except ImportError: from urllib import urlopen as urllib_request_urlopen -try: - from rfc822 import parsedate -except ImportError: - parsedate = None - from io import StringIO from itertools import chain from stat import S_ISDIR, ST_CTIME @@ -784,6 +779,33 @@ for x in qacats: xmllint_capable = False metadata_dtd = os.path.join(repoman_settings["DISTDIR"], 'metadata.dtd') +def parsedate(s): + """Parse a RFC 822 date and time string. + This is required for python3 compatibility, since the + rfc822.parsedate() function is not available.""" + + s_split = [] + for x in s.upper().split(): + for y in x.split(','): + if y: + s_split.append(y) + + if len(s_split) != 6: + return None + + # %a, %d %b %Y %H:%M:%S %Z + a, d, b, Y, H_M_S, Z = s_split + + # Convert month to integer, since strptime %w is locale-dependent. + month_map = {'JAN':1, 'FEB':2, 'MAR':3, 'APR':4, 'MAY':5, 'JUN':6, + 'JUL':7, 'AUG':8, 'SEP':9, 'OCT':10, 'NOV':11, 'DEC':12} + m = month_map.get(b) + if m is None: + return None + m = str(m).rjust(2, '0') + + return time.strptime(':'.join((Y, m, d, H_M_S)), '%Y:%m:%d:%H:%M:%S') + def fetch_metadata_dtd(): """ Fetch metadata.dtd if it doesn't exist or the ctime is older than @@ -816,13 +838,7 @@ def fetch_metadata_dtd(): url_f = urllib_request_urlopen(metadata_dtd_uri) msg_info = url_f.info() last_modified = msg_info.get('last-modified') - # Date parsing isn't supported in python3 since it has no - # equivalent of the rfc822.parsedate() function. - # TODO: Convert the last-modified field to locale-independent - # format and then use time.strptime() to parse it. - if parsedate is None: - last_modified = None - elif last_modified is not None: + if last_modified is not None: last_modified = parsedate(last_modified) if last_modified is not None: last_modified = calendar.timegm(last_modified) -- cgit v1.2.3-1-g7c22