3
# get-lp-templates - Given a source package, lists all its translation
6
# Author: David Planella <david.planella@ubuntu.com>
8
# Copyright 2009 David Planella <david.planella@ubuntu.com>.
10
# This program is free software: you can redistribute it and/or modify it
11
# under the terms of the GNU General Public License version 3, as published
12
# by the Free Software Foundation.
14
# This program is distributed in the hope that it will be useful, but
15
# WITHOUT ANY WARRANTY; without even the implied warranties of
16
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
17
# PURPOSE. See the GNU General Public License for more details.
19
# You should have received a copy of the GNU General Public License along
20
# with this program. If not, see <http://www.gnu.org/licenses/>.
27
LEVELS = (logging.ERROR,
33
LP_TRANSLATIONS_PAGE = "https://translations.launchpad.net/%s/%s/+source/%s"
34
RE_TEMPLATE = '.*<a href="/%s/%s/\+source/%s/\+pots/[a-zA-Z0-9\-\.]*"'
38
class Ignore404ErrorHandler(urllib2.HTTPDefaultErrorHandler):
39
""" Error handler which does not raise a HTTPError if a page is not
40
found, but returns the Error page instead.
43
def http_error_404(self, req, fp, code, msg, hdrs):
46
#def templates_list_page_get(distro, distro_codename, src_pkg):
47
# templates_list_page = []
49
# sock = urllib2.urlopen(LP_TRANSLATIONS_PAGE % (distro,
50
# distro_codename, src_pkg))
51
# templates_list_page = sock.read()
56
# return templates_list_page
58
def templates_get(distro_id, distro_codename, source_package):
60
opener = urllib2.build_opener(Ignore404ErrorHandler)
61
URL_TEMPLATE = LP_TRANSLATIONS_PAGE % (distro_id, distro_codename,
63
logging.debug("Open URL: {0}".format(URL_TEMPLATE))
65
page = opener.open(URL_TEMPLATE)
68
#templates = RE_TEMPLATE.findall(data)
69
templates_page_urls_regex = \
70
r'.*<a href="/%s/%s/\+source/%s/\+pots/[a-zA-Z0-9\-\.]*"' \
71
% (distro_id, distro_codename, source_package)
72
templates_page_urls = re.findall(templates_page_urls_regex, data)
74
return templates_page_urls
78
# Support for command line options.
79
USAGE = """%prog [OPTIONS]
81
Given a source package and a distribution codename, lists all its
82
translation templates available in Launchpad"""
83
parser = optparse.OptionParser(usage = USAGE)
84
parser.add_option('-d', '--debug', dest='debug_mode',
86
help='Print the maximum debugging info (implies -vvv)')
87
parser.add_option('-v', '--verbose', dest='logging_level',
89
help='Set error_level output to warning, info, and then debug')
90
parser.add_option("-c", "--distro-codename", dest="distro_codename",
91
help="Specify the distro's codename. E.g. 'oneiric'")
92
parser.add_option("-s", "--source-package", dest="source_package",
93
help="Specify the source package.")
95
parser.set_defaults(logging_level = 0)
96
(options, args) = parser.parse_args()
99
if options.debug_mode:
100
options.logging_level = 3
101
logging.basicConfig(level=LEVELS[options.logging_level],
102
format='%(asctime)s %(levelname)s %(message)s')
104
# TODO: change to args[0] instead of -s
105
if not options.source_package:
106
print "Must specify a source package"
108
logging.debug("Source package: {0}".format(options.source_package))
109
templates = templates_get(DISTRO_ID, options.distro_codename,
110
options.source_package)
112
for template in templates:
113
print str(template.split("/")[-1:])
115
if __name__ == "__main__":