| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Copyright © 2009 Germán Póo-Caamaño <gpoo@gnome.org> |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU Library General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
| 19 |
"""Handle the names of repositories, releases and tags |
| 20 |
used for each released version. |
| 21 |
""" |
| 22 |
|
| 23 |
import csv |
| 24 |
from settings import settings |
| 25 |
|
| 26 |
def load_modules_from_csv(filename): |
| 27 |
"""Load a dictionary with packages, version and tags from |
| 28 |
a CSV file. The format is the following: |
| 29 |
1st row: Release dates |
| 30 |
2nd row: Headers |
| 31 |
repo_name, tarball_name, 2.0, 2.2, 2.4, ... |
| 32 |
3rd-...: Data |
| 33 |
""" |
| 34 |
|
| 35 |
csv_file = open(filename) |
| 36 |
reader = csv.reader(csv_file) |
| 37 |
release_dates = reader.next() |
| 38 |
header = reader.next() |
| 39 |
|
| 40 |
releases = {} |
| 41 |
for i in range(2, len(header)): |
| 42 |
release_date = release_dates[i] |
| 43 |
version = header[i] |
| 44 |
releases[version] = release_date |
| 45 |
|
| 46 |
modules = {} |
| 47 |
for row in reader: |
| 48 |
repo = row[0] |
| 49 |
tarball = row[1] |
| 50 |
tags = {} |
| 51 |
|
| 52 |
for i in range(2, len(row)): |
| 53 |
if len(row[i]) == 0: |
| 54 |
continue |
| 55 |
version = header[i] |
| 56 |
tags[version] = row[i] |
| 57 |
|
| 58 |
pkg = {} |
| 59 |
pkg['tarball'] = tarball |
| 60 |
pkg['tags'] = tags |
| 61 |
modules[repo] = pkg |
| 62 |
|
| 63 |
csv_file.close() |
| 64 |
|
| 65 |
return releases, modules |
| 66 |
|
| 67 |
def cmp_version(x, y): |
| 68 |
"""Compare numeric versions (2.28 is greater than 2.6)""" |
| 69 |
(x_major, x_minor) = x.split('.') |
| 70 |
(y_major, y_minor) = y.split('.') |
| 71 |
|
| 72 |
if int(y_major) != int(x_major): |
| 73 |
return int(x_major) - int(y_major) |
| 74 |
|
| 75 |
return int(x_minor) - int(y_minor) |
| 76 |
|
| 77 |
def _print_modules(modules): |
| 78 |
"""Pretty Print for a dictionary, sorting by version number |
| 79 |
when it is possible |
| 80 |
""" |
| 81 |
|
| 82 |
for (repo, pkg) in modules.iteritems(): |
| 83 |
print ("'%s': {\n" |
| 84 |
" 'tarball': '%s',\n" |
| 85 |
" 'tags': {") % (repo, pkg['tarball']) |
| 86 |
for version in sorted(pkg['tags'].keys(), cmp_version): |
| 87 |
print " '%s': '%s'," % (version, pkg['tags'][version]) |
| 88 |
print " }," |
| 89 |
print "}" |
| 90 |
|
| 91 |
def _print_releases(releases): |
| 92 |
"""Pretty Print for a dictionary, sorting by version number |
| 93 |
when it is possible |
| 94 |
""" |
| 95 |
|
| 96 |
print "{" |
| 97 |
for version in sorted(releases.keys(), cmp_version): |
| 98 |
print " '%s': '%s'," % (version, releases[version]) |
| 99 |
print "}" |
| 100 |
|
| 101 |
if __name__ == '__main__': |
| 102 |
releases, platform = load_modules_from_csv(settings['csvfile']) |
| 103 |
|
| 104 |
# We use our own pprint (pretty print) |
| 105 |
_print_modules(platform) |
| 106 |
_print_releases(releases) |