2
# -*- coding: utf-8 -*-
4
# Copyright (C) 2009 <David Planella> <david.planella@ubuntu.com>
5
#This program is free software: you can redistribute it and/or modify it
6
#under the terms of the GNU General Public License version 3, as published
7
#by the Free Software Foundation.
9
#This program is distributed in the hope that it will be useful, but
10
#WITHOUT ANY WARRANTY; without even the implied warranties of
11
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
#PURPOSE. See the GNU General Public License for more details.
14
#You should have received a copy of the GNU General Public License along
15
#with this program. If not, see <http://www.gnu.org/licenses/>.
26
from LocaleInfo import LocaleInfo
27
import simplejson as json
29
def get_http_timestamp(url):
31
sock = urllib2.urlopen(url)
36
if sock.info()["Last-Modified"]:
37
lm_string = sock.info()["Last-Modified"]
40
timestamp_lm = time.mktime(time.strptime(lm_string, "%a, %d %b %Y %H:%M:%S GMT"))
41
datetime_lm = datetime.datetime.fromtimestamp(timestamp_lm)
45
def get_file_timestamp(filename):
47
mtime = os.path.getmtime(filename)
51
return datetime.datetime.utcfromtimestamp(mtime)
53
def get_langpack_code(langpackname):
54
langpack_bits = langpackname.split('-')
56
if len(langpack_bits) == 5:
57
langpack_code = langpack_bits[2] + '-' + langpack_bits[3]
59
langpack_code = langpack_bits[2]
63
def get_language_name(langcode, li):
65
lang_name = li.translate(langcode, True)
66
lname = lang_name.split(';')[0]
72
DATA_DIR='/home/dpm/langpacks-by-inst/data'
73
PUBLIC_DIR='/home/dpm/public_html'
74
URL = "http://popcon.ubuntu.com/by_inst.gz"
75
REGEXP = re.compile("^language-pack-[a-z]{2,3}(-han[st])?-base")
76
data_file = os.path.join(DATA_DIR, os.path.basename(URL))
77
CSV_FILE = os.path.join(DATA_DIR, 'langpacks_by_inst.csv')
78
JSON_FILE = os.path.join(DATA_DIR, 'langpacks_by_inst.json')
80
http_timestamp = get_http_timestamp(URL)
81
if http_timestamp != get_file_timestamp(data_file):
82
urllib.urlretrieve(URL, data_file)
84
# Extract and put the language pack data in a CSV file
86
data_csv = csv.reader(gzip.open(data_file), delimiter = ' ', skipinitialspace = True)
88
writer = csv.writer(open(CSV_FILE, "wb"))
89
writer.writerow(['# Language', 'Installations', 'Timestamp'])
91
li = LocaleInfo.LocaleInfo()
96
#if not (row[0].startswith('#') or row[0].startswith('-') or (row[1] == 'Total')) :
98
if REGEXP.search(row[1]):
99
lang_code = get_langpack_code(row[1])
101
lang_name = get_language_name(lang_code, li)
102
writer.writerow([lang_code, installs, lang_name, http_timestamp])
103
data_json.append({'installs': installs, 'langpack': lang_name})
108
#print 'file %s, line %d: %s' % (data_file, data_csv.line_num, e)
110
f = open(JSON_FILE, mode='w')
111
json.dump(data_json, f)