~dpm/+junk/ul10n-sharing-info

« back to all changes in this revision

Viewing changes to ubuntu_pots_list/__init__.py

  • Committer: David Planella
  • Date: 2011-07-12 16:46:45 UTC
  • Revision ID: david.planella@ubuntu.com-20110712164645-4ei83olof7pkkfyn
Use apt to fetch the info corresponding to the given release, use the Launchpad API to return the sharing info. Thanks to Henning Eggers for providing the sharing info code\!

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
from aptsources.sourceslist import SourcesList
9
9
from operator import itemgetter
10
10
import tempfile
 
11
from launchpadlib.launchpad import Launchpad
11
12
 
12
13
import gettext
13
14
from gettext import gettext as _
44
45
    return path
45
46
 
46
47
 
 
48
def init_apt(release):
 
49
    tmpdir = tempfile.mkdtemp()
 
50
    logging.debug('Created a temp directory at {}'.format(tmpdir))
 
51
    rootdir = os.path.join(tmpdir, 'etc', 'apt')
 
52
    os.makedirs(rootdir)
 
53
    logging.debug('Created apt temp directory at {}'.format(rootdir))
 
54
 
 
55
    with open(os.path.join(rootdir, 'sources.list'), 'w+') as fp:
 
56
        fp.write('deb http://archive.ubuntu.com/ubuntu {} main restricted\n'.format(release))
 
57
        fp.write('deb-src http://archive.ubuntu.com/ubuntu {} main restricted\n'.format(release))
 
58
 
 
59
    #sourceslist = SourcesList()
 
60
    #sourceslist.load(fp)
 
61
 
 
62
    logging.info('Initializing cache...')
 
63
    cache = apt.Cache(rootdir = tmpdir)
 
64
    # Now, lets update the package list
 
65
    logging.info('Updating cache...')
 
66
    cache.update()
 
67
    # We need to re-open the cache because it needs to read the package list
 
68
    logging.info('Opening cache...')
 
69
    cache.open(None)
 
70
 
 
71
    apt_pkg.init()
 
72
    #acquire = apt_pkg.Acquire()
 
73
 
47
74
def uses_intltool(template):
48
75
    '''
49
76
    Given a dict containing a translations template with a
51
78
    intltool, and False otherwise.
52
79
    '''
53
80
 
54
 
    rootdir = tempfile.mkdtemp()
55
 
    apt_pkg.init()
56
 
    with open(rootdir + '/etc/apt/sources.list', 'w+') as fp:
57
 
        pass
58
 
 
59
 
    sourceslist = SourcesList()
60
 
    sourceslist.load(fp)
61
 
 
62
 
 
63
 
    #acquire = apt_pkg.Acquire()
64
 
 
65
 
    cache = apt.Cache(rootdir = rootdir)
66
 
    # Now, lets update the package list
67
 
    cache.update()
68
 
    # We need to re-open the cache because it needs to read the package list
69
 
    cache.open(None)
70
 
 
71
 
    # TODO: fetch the source records from another release
72
 
 
 
81
    #logging.info('Processing template {}'.format(template['template_name']))
73
82
    sr = apt_pkg.SourceRecords()
74
83
 
75
84
    src_package = template['sourcepackage']
 
85
    #logging.info('Processing source package {}'.format(src_package))
76
86
    if sr.lookup(src_package):
77
 
        for k, v in sr.build_depends.items():
 
87
        for v in sr.build_depends.values():
78
88
            for dep in v:
79
89
                if dep[0][0] == 'intltool':
80
90
                    return True
81
91
 
82
92
    return False
83
93
 
 
94
 
 
95
def write_templates():
 
96
 
 
97
    with open(ubuntu_pots_listconfig.get_data_file('intltool_templates.json'), 'w') as fp:
 
98
        json.dump(intltool_templates, fp)
 
99
 
 
100
 
 
101
def print_wiki_table(templates, release):
 
102
 
 
103
 
 
104
    SRC_PKG_URL_BASE = 'https://launchpad.net/ubuntu/{0}/+source'.format(release)
 
105
    POT_URL_BASE = 'https://translations.launchpad.net/ubuntu/{0}/+source'.format(release)
 
106
    PROJECT_URL_BASE = 'https://launchpad.net'
 
107
    HEADER = "|| '''Source Package''' || '''Template''' || '''Has upstream project''' || '''Upstream project owner''' || '''Has upstream link''' ||"
 
108
 
 
109
    print HEADER
 
110
    for row in templates:
 
111
        print "|| [[{0}/{1}|{1}]] || [[{2}/{3}|{3}]] || [[{4}/{5}|{5}]] || [[{6}|{7}]] || [[{8}|{9}]]||".format(
 
112
                                                                              SRC_PKG_URL_BASE,
 
113
                                                                              row['sourcepackage'],
 
114
                                                                              POT_URL_BASE,
 
115
                                                                              row['template_name'],
 
116
                                                                              PROJECT_URL_BASE,
 
117
                                                                              row['project_name'],
 
118
                                                                              row['owner_link'],
 
119
                                                                              row['owner_name'],
 
120
                                                                              row['projectseries_link'],
 
121
                                                                              row['projectseries_name'],
 
122
                                                                              )
 
123
 
 
124
 
 
125
def iter_upstream_info(distroseries, templates):
 
126
    for template in templates:
 
127
        package = distroseries.getSourcePackage(name = template['sourcepackage'])
 
128
        project_series = package.productseries
 
129
        if project_series is None:
 
130
            project_name = None
 
131
            projectseries_name = None
 
132
            projectseries_link = None
 
133
            owner_name = None
 
134
            owner_link = None
 
135
        else:
 
136
            project = project_series.project
 
137
            owner = project.owner
 
138
 
 
139
            project_name = project.name
 
140
            projectseries_name = project_series.name
 
141
            projectseries_link = project_series.web_link
 
142
            owner_name = owner.name
 
143
            owner_link = owner.web_link
 
144
 
 
145
        template['project_name'] = project_name
 
146
        template['projectseries_name'] = projectseries_name
 
147
        template['projectseries_link'] = projectseries_link
 
148
        template['owner_name'] = owner_name
 
149
        template['owner_link'] = owner_link
 
150
        yield template
 
151
 
 
152
 
 
153
def get_upstream_info(series_name, templates):
 
154
    logging.info('Logging into Launchpad...')
 
155
    launchpad = Launchpad.login_anonymously(
 
156
        'get_packaging', 'production', version='devel')
 
157
    logging.info('Getting Ubuntu distro series...')
 
158
    distroseries = launchpad.distributions['ubuntu'].getSeries(
 
159
        name_or_version = series_name)
 
160
 
 
161
    logging.info('Getting upstream info...')
 
162
    return list(iter_upstream_info(distroseries, templates))
 
163
 
 
164
 
84
165
def main():
85
166
    version = ubuntu_pots_listconfig.__version__
86
167
    # Support for command line options.
90
171
        help=_('Print the maximum debugging info (implies -vv)'))
91
172
    parser.add_option('-v', '--verbose', dest='logging_level', action='count',
92
173
        help=_('set error_level output to warning, info, and then debug'))
93
 
    # exemple of silly CLI option
94
174
    parser.add_option("-r", "--release", action="store", dest="release",
95
175
                      help=_("Release to get the list of templates from"))
96
176
    parser.set_defaults(logging_level = 0,
102
182
        options.logging_level = 3
103
183
    logging.basicConfig(level=LEVELS[options.logging_level], format='%(asctime)s %(levelname)s %(message)s')
104
184
 
105
 
 
 
185
    release = options.release
106
186
    # Fetch the translations data
107
 
    raw_data = get_raw_data(options.release)
 
187
    raw_data = get_raw_data(release)
108
188
 
109
189
    # Read data
110
190
    with open(raw_data, 'r') as fp:
114
194
    active_templates = [pot for pot in templates if pot['enabled']]
115
195
 
116
196
    # Find out which templates depend on intltool and return a sorted list
 
197
    init_apt(release)
 
198
    logging.info('Finding out intltool templates...')
117
199
    intltool_templates = filter(uses_intltool, active_templates)
118
200
    intltool_templates = sorted(intltool_templates, key = itemgetter('priority'), reverse = True)
119
 
    logging.debug(len(intltool_templates))
120
 
 
121
 
    with open(ubuntu_pots_listconfig.get_data_file('intltool_templates.json'), 'w') as fp:
122
 
        json.dump(intltool_templates, fp)
123
 
 
124
 
    SRC_PKG_URL_BASE = 'https://launchpad.net/ubuntu/{0}/+source'.format(options.release)
125
 
    POT_URL_BASE = 'https://translations.launchpad.net/ubuntu/{0}/+source'.format(options.release)
126
 
    print "|| '''Source Package''' || '''Template''' || '''Has upstream project''' || '''Upstream project owner''' || '''Has upstream link''' ||"
127
 
    for row in intltool_templates:
128
 
        print "|| [[{1}/{0}|{0}]] || [[{3}/{2}|{2}]] || {4} || {5} ||".format(row['sourcepackage'],
129
 
                                                                              SRC_PKG_URL_BASE,
130
 
                                                                              row['template_name'],
131
 
                                                                              POT_URL_BASE,
132
 
                                                                              '-',
133
 
                                                                              '-',
134
 
                                                                              '-',
135
 
                                                                              )
 
201
 
 
202
    # Find out upstream info
 
203
    intltool_templates = get_upstream_info(release, intltool_templates)
 
204
    #print intltool_templates
 
205
 
 
206
    # Print the data
 
207
    print_wiki_table(intltool_templates, release)
 
208
 
136
209
 
137
210
if __name__ == "__main__":
138
211
    main()