Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/generator.py
blob: a4fb9d9ac7686d338b7435ad1eacf5cb182727d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python

# Copyright (C) 2011, Martin Abente
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

import os
import io
from urlparse import urljoin
from zipfile import ZipFile, is_zipfile
from ConfigParser import ConfigParser, NoOptionError

config = ConfigParser()
script_path = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(script_path, 'config.ini')
config.read(config_path)

ACTIVITIES_BASE_URL = config.get('server', 'base_url')
REL_ACTIVITIES_PATH = 'activities'
ABS_ACTIVITIES_PATH = os.path.join(script_path, REL_ACTIVITIES_PATH)

HEADER = '<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html lang=\"en\">\n<head>\n<title>Microformat File</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n</head>\n<body>\n<table>'
FOOTER = '</table>\n</body>\n</html>'

def generate_activity_info(bundle_id, name, version, size, url):
      print '<tr>\n<td class="olpc-activity-info">'
      print '<span class=\"olpc-activity-id\">%s</span>' % bundle_id
      print '<span class=\"olpc-activity-name\">%s</span>' % name
      print '<span class=\"olpc-activity-version\">%s</span>' % version
      print '<span class=\"olpc-activity-size\">%s</span>' % size
      print '<span class=\"olpc-activity-url\"><a href=\"%s\">download</a></span>' % url
      print '</td>\n</tr>'

def get_value(config, section, option, alt_option=''):
    try:
        if config.has_option(section, option):
            value = config.get(section, option)
        else:
            value = config.get(section, alt_option)
    except NoOptionError:
        value = ''

    return value

def print_info():
    activities_files = os.listdir(ABS_ACTIVITIES_PATH)

    for activity_file in activities_files:

        if not is_zipfile(activity_file):
            continue

        xo_bundle_abs_path = os.path.join(ABS_ACTIVITIES_PATH, activity_file)
        xo_bundle = ZipFile(xo_bundle_abs_path, 'r')

        for file_path in xo_bundle.namelist():
            if file_path.endswith('activity/activity.info'):
                config_data = xo_bundle.read(file_path)
                activity_config = ConfigParser()
                activity_config.readfp(io.BytesIO(config_data))

                bundle_id = get_value(activity_config, 'Activity', 'bundle_id', 'service_name')
                name = get_value(activity_config, 'Activity', 'name')
                version =  get_value(activity_config, 'Activity', 'version')
                size = os.path.getsize(xo_bundle_abs_path)

                xo_bundle_rel_path = os.path.join(REL_ACTIVITIES_PATH, activity_file)
                url = urljoin(ACTIVITIES_BASE_URL, xo_bundle_rel_path)

                generate_activity_info(bundle_id, name, version, size, url)
                break

print HEADER
print_info()
print FOOTER