summaryrefslogtreecommitdiff
blob: 6ba528970cd0084a087841c8a22f58c21028facd (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2009 Sebastian Pipping <sebastian@pipping.org>
# Licensed under GPL 2 or later

import sys
import os
from optparse import OptionParser


VERSION = '0.9'
USAGE = """
  %prog  [OPTIONS]  [foo/layman-global.txt [bar/repositories.xml]]"""


parser = OptionParser(usage=USAGE, version=VERSION)
parser.add_option('-H', '--human',
    dest = 'human',
    default = False,
    action = 'store_true',
    help = 'more human friendly output instead of XML')
(opts, args) = parser.parse_args()

if len(args) > 1:
    layman_global_txt_location = args[1]
else:
    layman_global_txt_location = '/dev/stdin'

if len(args) > 2:
    repositories_xml_location = args[2]
else:
    repositories_xml_location = '/dev/stdout'


import xml.etree.ElementTree as ET
import codecs

from layman.dbtools.extradata import * # local
from layman.dbtools.feedextractors import * # local
from layman.dbtools.sharedutils import * # local

def to_ascii(o, current_encoding='utf-8'):
    if not isinstance(o, basestring):
        return o

    if isinstance(o, unicode):
        s = o
    else:
        s = unicode(o, current_encoding)
    return codecs.encode(s, 'ascii', 'ignore')

def append_feed(feed_uri, overlay_object):
    feed = ET.Element('feed')
    feed.text = feed_uri
    overlay_object.append(feed)

def strip(node):
    node.text = node.text.strip()

a = ET.parse(open(layman_global_txt_location))
overlays = a.getroot()

for overlay in overlays:
    repo_name = overlay.attrib['name']
    extra_data = TRANSITION_DATA_EXTRA.get(repo_name, {})

    # Move (and strip) 'description' tag
    description = overlay.find('description')
    overlay.remove(description)
    overlay.insert(0, description)
    strip(description)

    # Transform 'overlay' tag
    overlay.tag = 'repo'
    del overlay.attrib['name']
    name = ET.Element('name')
    name.text = repo_name
    overlay.insert(0, name)

    # Transform 'link' tag
    link = overlay.find('link')
    if link != None:
        link.tag = 'homepage'
        strip(link)

    # Transform 'contact' attribute
    owner = ET.Element('owner')
    overlay.append(owner)
    email = ET.Element('email')
    email.text = extra_data.get('owner', {}).\
            get('email', overlay.attrib['contact'])
    del overlay.attrib['contact']
    owner.append(email)

    # Transform 'src' and 'type' attribute
    source = ET.Element('source')
    source.text = overlay.attrib['src']
    del overlay.attrib['src']
    source.attrib['type'] = overlay.attrib['type']
    del overlay.attrib['type']
    overlay.append(source)

    # Extend by quality label
    try:
        overlay.attrib['quality'] = extra_data['quality']
    except KeyError:
        pass

    # Extend by owner type
    if repo_name in TRANSITION_DATA_PROJECTS:
        owner.attrib['type'] = 'project'

    # Extend by owner name
    try:
        maint_name = extra_data['owner']['name']
        name = ET.Element('name')
        name.text = maint_name
        owner.append(name)
    except KeyError:
        pass

    # Extend by feed URIs
    for fe in FEED_EXTRACTORS:
        uri = fe['regex'].sub(fe['format'], source.text)
        if uri != source.text:
            append_feed(uri, overlay)
            break
    try:
        feed_uris = extra_data['feeds']
    except KeyError:
        feed_uris = ()
    for uri in feed_uris:
        append_feed(uri, overlay)

    # Explicify defaults
    if 'status' not in overlay.attrib:
        overlay.attrib['status'] = 'unofficial'
    if 'quality' not in overlay.attrib:
        overlay.attrib['quality'] = 'experimental'

    # Add note on file being a generated one
    overlay.insert(0, ET.Comment('NOTE: This file is generated, do not edit.'))

# Transform 'overlays' tag
overlays.tag = 'repositories'

# Sort overlays
overlays[:] = sorted(overlays[:], key=lambda x: x.find('name').text.lower())


# Extend by format version
overlays.attrib['version'] = '1.0'


if opts.human:
    recurse_print(overlays)
else:
    indent(overlays)
    repositories_xml = open(repositories_xml_location, 'w')
    repositories_xml.write("""\
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Header$ -->
<?xml-stylesheet href="/xsl/repositories.xsl" type="text/xsl"?>
<!DOCTYPE repositories SYSTEM "/dtd/repositories.dtd">
""")
    a.write(repositories_xml, encoding='utf-8')
    repositories_xml.close()