#!/usr/bin/env python
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 = '\n\n
\nMicroformat File\n\n\n\n\n\n'
def generate_activity_info(bundle_id, name, version, size, url):
print '\n'
print '%s' % bundle_id
print '%s' % name
print '%s' % version
print '%s' % size
print 'download' % url
print ' | \n
'
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