aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-05-26 18:17:52 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-05-26 18:17:52 +0200
commitf047edbc55a9d8cce7931c0cddc255bb676bd83d (patch)
tree85334a38ab3f405c31655d67bd5103528b71d053
parent51e6fbac98ba1ddd6815bdd84018e1425ef8a70f (diff)
Read release data from /usr/share/distro-info/debian.csv
-rwxr-xr-xdebdate84
1 files changed, 59 insertions, 25 deletions
diff --git a/debdate b/debdate
index bd5cb44..39af2ea 100755
--- a/debdate
+++ b/debdate
@@ -7,7 +7,12 @@
# License, Version 2, as published by Sam Hocevar. See
# http://www.wtfpl.net/ for more details.
+# Debian release dates can be found in the package distro-info-data,
+# but an hardcoded fallback is included to run this script where such
+# package is not available.
+
import argparse
+import csv
import datetime
from dateutil import relativedelta, parser
import unittest
@@ -32,9 +37,31 @@ MSG = "Today is day {dody} of year {yodr} of the {release}"
class DebDate:
+ def __init__(self, distro_info_file='/usr/share/distro-info/debian.csv'):
+ self.releases = []
+ try:
+ with open(distro_info_file) as fp:
+ releases = csv.reader(fp, delimiter=',')
+ for row in releases:
+ if len(row) > 4:
+ try:
+ date = datetime.datetime.strptime(
+ row[4],
+ '%Y-%m-%d'
+ )
+ except ValueError:
+ pass
+ else:
+ self.releases.append((date.date(), row[1]))
+ except FileNotFoundError:
+ releases = []
+ self.releases.sort(key=lambda x: x[0], reverse=True)
+ if not self.releases:
+ self.releases = RELEASES
+
def get_release(self, day):
data = {}
- for r in RELEASES:
+ for r in self.releases:
epoch = r[0]
data['release'] = r[1]
if day > epoch:
@@ -62,38 +89,45 @@ class OutsideTimeError(ValueError):
class TestDebDate(unittest.TestCase):
def setUp(self):
- self.debdate = DebDate()
+ self.debdates = [
+ DebDate(),
+ DebDate(distro_info_file='/no/such/file'),
+ ]
def testFirstYearRelease(self):
- r = RELEASES[-1]
- d = r[0] + relativedelta.relativedelta(days=10)
- data = self.debdate.get_release(d)
- self.assertEqual(data['dody'], 10)
- self.assertEqual(data['yodr'], 1)
- self.assertEqual(data['release'], r[1])
+ for debdate in self.debdates:
+ r = debdate.releases[-1]
+ d = r[0] + relativedelta.relativedelta(days=10)
+ data = debdate.get_release(d)
+ self.assertEqual(data['dody'], 10)
+ self.assertEqual(data['yodr'], 1)
+ self.assertEqual(data['release'], r[1])
def testSecondYearRelease(self):
- r = RELEASES[-6]
- d = r[0] + relativedelta.relativedelta(years=1)
- data = self.debdate.get_release(d)
- self.assertEqual(data['dody'], 227)
- self.assertEqual(data['yodr'], 2)
- self.assertEqual(data['release'], r[1])
+ for debdate in self.debdates:
+ r = debdate.releases[-6]
+ d = r[0] + relativedelta.relativedelta(years=1)
+ data = debdate.get_release(d)
+ self.assertEqual(data['dody'], 227)
+ self.assertEqual(data['yodr'], 2)
+ self.assertEqual(data['release'], r[1])
def testPastLatestRelease(self):
- r = RELEASES[0]
- d = r[0] + relativedelta.relativedelta(years=5)
- data = self.debdate.get_release(d)
- # Day Of The Year will change depending on when is the latest
- # release, so we do not check it
- self.assertEqual(data['yodr'], 6)
- self.assertEqual(data['release'], r[1])
+ for debdate in self.debdates:
+ r = debdate.releases[0]
+ d = r[0] + relativedelta.relativedelta(years=5)
+ data = debdate.get_release(d)
+ # Day Of The Year will change depending on when is the latest
+ # release, so we do not check it
+ self.assertEqual(data['yodr'], 6)
+ self.assertEqual(data['release'], r[1])
def testBeforeBeginningOfTime(self):
- r = RELEASES[-1]
- d = r[0] + relativedelta.relativedelta(days=-1)
- with self.assertRaises(OutsideTimeError):
- self.debdate.get_release(d)
+ for debdate in self.debdates:
+ r = debdate.releases[-1]
+ d = r[0] + relativedelta.relativedelta(days=-1)
+ with self.assertRaises(OutsideTimeError):
+ debdate.get_release(d)
class Command: