~knitzsche/+junk/lp-l10n-stats

« back to all changes in this revision

Viewing changes to ubuntu-translations-stats.py

  • Committer: David Planella
  • Date: 2010-09-25 10:43:52 UTC
  • Revision ID: david.planella@ubuntu.com-20100925104352-i0d4qhyybsc07yx4
Refactoring and conversion of the shell script to Python:

* Removed all duplication and enabled specifying the command-line arguments
  from one single point
* Added Narwhal to the list of distros in the settings file
* Renamed the ul10n-* modules so they can be imported by the main script
* Moved blacklisting functions to the module where the working set of domains
  is compiled. Now the working set manipulations and the statistics calculation
  are clearly partitioned in different units
* Removed some obsolete code dealing with specifying different output formats
  (wiki, json). Only json is supported
* Improved the code structure a bit by moving several operations to separate
  functions

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# This script calculates statistics of the percentage of translation per
 
4
# language for all languages in Ubuntu by calling all involved functions in the
 
5
# right order.
 
6
 
 
7
import sys
 
8
import urllib
 
9
import os
 
10
from optparse import OptionParser
 
11
sys.path.append(os.path.join(os.getcwd(), 'lib/python2.6/site-packages'))
 
12
import utils
 
13
import ul10n_iso_pots_get
 
14
import ul10n_stats_calc
 
15
 
 
16
DATA_DIR = 'data'
 
17
CONFIG_DIR = 'config'
 
18
STATS_URL = 'http://people.canonical.com/~danilo/ubuntu'
 
19
 
 
20
def options_parse():
 
21
    """Parses the command line and config file options"""
 
22
 
 
23
    settings = utils.get_settings()
 
24
 
 
25
    USAGE = "%prog [OPTIONS]"
 
26
 
 
27
    DESCRIPTION = """Lists all translation templates and source packages in the LiveCD for the
 
28
    given distro version."""
 
29
 
 
30
    parser = OptionParser(usage = USAGE, description = DESCRIPTION)
 
31
 
 
32
    parser.add_option("-d",
 
33
                    "--domains-file",
 
34
                    dest="domains_file",
 
35
                    default=None,
 
36
                    help="File to read domains from and on which stats are based. Requires -c argument.")
 
37
 
 
38
    parser.add_option("-c",
 
39
                    "--custom",
 
40
                    dest="custom",
 
41
                    action="store_true",
 
42
                    default=False,
 
43
                    help="Turn on custom (non-Ubuntu) mode")
 
44
 
 
45
    parser.add_option("-f",
 
46
                    "--no-file",
 
47
                    dest="no_file",
 
48
                    action="store_true",
 
49
                    default=False,
 
50
                    help="Disables using conf file blacklist.")
 
51
 
 
52
    parser.add_option("-p",
 
53
                    "--no-priorities",
 
54
                    dest="no_priorities",
 
55
                    action="store_true",
 
56
                    default=False,
 
57
                    help="Disables using priorities to generate blacklist.")
 
58
 
 
59
    parser.add_option("-s",
 
60
                    "--source-package-stats",
 
61
                    dest="pkg_stats",
 
62
                    default='{0}/{1}-package-stats.log.gz'.format(DATA_DIR, settings['distro_codename']),
 
63
                    help="specify the file containing the source package statistics")
 
64
 
 
65
    parser.add_option("-b",
 
66
                    "--template-blacklist",
 
67
                    dest="blacklist",
 
68
                    default='config/blacklist.conf',
 
69
                    help="specify the file containing the templates to blacklist during the calculation of statistics")
 
70
 
 
71
    (options, args) = parser.parse_args()
 
72
 
 
73
    return options, settings
 
74
 
 
75
 
 
76
def get_raw_data(distro_codename):
 
77
 
 
78
    print >>sys.stderr, "Fetching data..."
 
79
    for stats_file in ('potemplate', 'package'):
 
80
        url = '{0}/{1}-{2}-stats.log.gz'.format(STATS_URL, distro_codename, stats_file)
 
81
        path = os.path.join(DATA_DIR, os.path.basename(url))
 
82
        urllib.urlretrieve(url, path)
 
83
 
 
84
 
 
85
def get_working_templates(options, settings):
 
86
 
 
87
    distro_codename = settings['distro_codename']
 
88
    distro_id = settings['distro_id']
 
89
    TEMPLATES_FILE = '{0}/{1}-potemplate-stats.log.gz'.format(DATA_DIR, distro_codename)
 
90
 
 
91
    # if normal ubuntu mode, get src pkg list form seeds, else from custom
 
92
    if not options.custom:
 
93
        # Get the source packages from seeds
 
94
        distro_src_pkgs = ul10n_iso_pots_get.get_src_pkgs_from_seeds(distro_id,
 
95
                                                                    distro_codename)
 
96
 
 
97
        # create list ('valid') of lines from lp_stats_pots file that only includes
 
98
        # src pkgs that are: in the seed, enabled, or in extra (for live cd)
 
99
        valid = ul10n_iso_pots_get.apply_valid_pots_filter(TEMPLATES_FILE, distro_src_pkgs)
 
100
 
 
101
        #for line in valid:
 
102
        #    print >> sys.stderr, line.split('|')[0].strip()
 
103
 
 
104
        working_pots = ul10n_iso_pots_get.apply_blacklist_filter(options, settings, valid)
 
105
 
 
106
        return working_pots
 
107
 
 
108
    else:
 
109
        domain_src, domain_linenum, domain_template, lines = ul10n_iso_pots_get.get_dictionaries(TEMPLATES_FILE)
 
110
        if options.domains_file:
 
111
            if not os.path.exists(options.domains_file):
 
112
                print >> sys.stderr, "Error: domains_file not found. Stopping."
 
113
                sys.exit(1)
 
114
            f = open(options.domains_file, 'r')
 
115
            domains = f.read().split('\n')
 
116
            f.close()
 
117
        else:
 
118
            import ul10n_custom
 
119
            domains = ul10n_custom.get_domains()
 
120
        for domain in domains: print >> sys.stderr, domain
 
121
        valid = list()
 
122
        invalid = list()
 
123
        for domain in domains:
 
124
            if domain in domain_src.keys():
 
125
                line = lines[domain_linenum[domain]].rstrip()
 
126
                valid.append(line)
 
127
 
 
128
        return valid
 
129
 
 
130
    # Print lines to stdout for redirection to file and post processing
 
131
    #for item in valid: print item
 
132
 
 
133
 
 
134
def main():
 
135
 
 
136
    # Get the command line options and settings from the config file
 
137
    options, settings = options_parse()
 
138
 
 
139
    # Fetch the raw data export files to be used as the source for the
 
140
    # calculation
 
141
    get_raw_data(settings['distro_codename'])
 
142
 
 
143
    # Compile a list of working templates to be considered for the calculation.
 
144
    # Apply any blacklisting if necessary.
 
145
    working_pots = get_working_templates(options, settings)
 
146
 
 
147
    # Calculate the unified, per language translation statistics from the
 
148
    # working templates set and output them in a report
 
149
    ul10n_stats_calc.calculate_stats(options, settings, working_pots)
 
150
 
 
151
if __name__ == "__main__":
 
152
    main()
 
153
    sys.exit(0)