Created
December 20, 2012 20:31
-
-
Save charettes/4348301 to your computer and use it in GitHub Desktop.
Django ticket #19482 localflavor apps locales
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import unicode_literals | |
import os | |
import re | |
import shutil | |
from subprocess import Popen | |
from django.contrib import localflavor | |
import polib | |
from polib import POEntry | |
LOCALFLAVOR_DIRECTORY = localflavor.__path__[0] | |
FLAVOR_RE = re.compile(r'^(?P<name>[a-z]{2})_?$') | |
flavors = tuple( | |
directory[0:2] for directory in os.listdir(LOCALFLAVOR_DIRECTORY) | |
if FLAVOR_RE.match(directory) and directory != 'uk' # uk was deprecated for gb | |
) | |
print('Cloning localflavor repositories...') | |
if not os.path.isdir('repositories'): | |
os.mkdir('repositories') | |
def clone_or_pull(flavor): | |
localflavor_name = "django-localflavor-%s" % flavor | |
destination = os.path.join('repositories', localflavor_name) | |
if os.path.exists(destination): | |
process = Popen(['git', 'clean', '-f', '-d'], | |
cwd=destination) | |
process.wait() | |
process = Popen(['git', 'checkout', 'master', '-f'], | |
cwd=destination) | |
process.wait() | |
process = Popen(['git', 'pull', 'origin', 'master'], | |
cwd=destination) | |
process.wait() | |
else: | |
repository = "git://github.com/django/%s.git" % localflavor_name | |
process = Popen(['git', 'clone', repository, destination]) | |
process.wait() | |
for flavor in flavors: | |
clone_or_pull(flavor) | |
LOCALFLAVOR_LOCALE_DIRECTORY = os.path.join(LOCALFLAVOR_DIRECTORY, 'locale') | |
locales = tuple(os.listdir(LOCALFLAVOR_LOCALE_DIRECTORY)) | |
print('Extracting messages from deprecated contrib localflavor...') | |
PO_FILE_PATH = os.path.join('LC_MESSAGES', 'django.po') | |
headers = {} | |
last_translators = {} | |
messages = {} | |
for locale in locales: | |
catalog = polib.pofile(os.path.join(LOCALFLAVOR_LOCALE_DIRECTORY, locale, PO_FILE_PATH)) | |
headers[locale] = catalog.header | |
last_translators[locale] = catalog.metadata['Last-Translator'] | |
for entry in catalog: | |
if entry.translated() and entry.msgstr != entry.msgid: | |
for path, line in entry.occurrences: | |
flavor = os.path.split(path)[0][0:2] | |
messages.setdefault(flavor, {}) | |
messages[flavor].setdefault(locale, {}) | |
messages[flavor][locale][entry.msgid] = entry.msgstr | |
print('Making messages for django-localflavor-* apps') | |
DEFAULT_PO_HEADER = """SOME DESCRIPTIVE TITLE. | |
Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER | |
This file is distributed under the same license as the PACKAGE package. | |
FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. | |
""" | |
for flavor in flavors: | |
# No locale for this flavor | |
if flavor not in messages: | |
continue | |
localflavor_name = "django-localflavor-%s" % flavor | |
app_directory = os.path.join('repositories', localflavor_name, | |
localflavor_name.replace('-', '_')) | |
locale_directory = os.path.join(app_directory, 'locale') | |
# Ensure the locale folder exist in order to be able to run makemessages | |
if not os.path.exists(locale_directory): | |
os.mkdir(locale_directory) | |
print("Making message for django-localflavor-%s" % flavor) | |
compile = False | |
for locale in messages[flavor].keys(): | |
catalog_path = os.path.join(locale_directory, locale, PO_FILE_PATH) | |
catalog_exists = os.path.exists(catalog_path) | |
# Create the catalog if it doesn't already exist | |
if not catalog_exists: | |
process = Popen(['django-admin.py', 'makemessages', '-l', locale], | |
cwd=app_directory) | |
process.wait() | |
catalog = polib.pofile(catalog_path) | |
locale_messages = messages[flavor][locale] | |
replaced = False | |
for entry in catalog: | |
msgstr = locale_messages.get(entry.msgid) | |
if msgstr: | |
if entry.flags: | |
raise Exception( | |
"flavor: %s, locale: %s, msgid: %s, flags: %r" % ( | |
flavor, locale, entry.msgid, entry.flags | |
)) | |
if entry.msgstr != msgstr: | |
entry.msgstr = msgstr | |
replaced = True | |
# Save the catalog if we replaced atleast one string | |
if replaced: | |
if catalog.header == DEFAULT_PO_HEADER: | |
catalog.header = headers[locale] | |
if catalog.metadata['Project-Id-Version'] == 'PACKAGE VERSION': | |
catalog.metadata['Project-Id-Version'] = localflavor_name | |
if catalog.metadata['Last-Translator'] == 'FULL NAME <EMAIL@ADDRESS>': | |
catalog.metadata['Last-Translator'] = last_translators[locale] | |
catalog.save() | |
compile = True | |
elif not catalog_exists: | |
# Remove the useless generated catalog file | |
os.remove(catalog_path) | |
if compile: | |
process = Popen(['django-admin.py', 'compilemessages'], | |
cwd=app_directory) | |
process.wait() | |
print('Generating patches') | |
if os.path.exists('patches'): | |
shutil.rmtree('patches') | |
os.mkdir('patches') | |
PATCHES_DIRECTORY = os.path.abspath('patches') | |
for flavor in flavors: | |
localflavor_name = "django-localflavor-%s" % flavor | |
cwd = os.path.join('repositories', localflavor_name) | |
destination = os.path.join(PATCHES_DIRECTORY, "%s.diff" % localflavor_name) | |
process = Popen(['git', 'add', '.'], cwd=cwd) | |
process.wait() | |
with open(destination, 'w+') as stdout: | |
process = Popen(['git', 'diff', '--cached', '--binary'], | |
cwd=cwd, stdout=stdout) | |
process.wait() | |
if not os.path.getsize(destination): | |
os.remove(destination) | |
process = Popen(['git', 'reset', '.'], cwd=cwd) | |
process.wait() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/django/django/archive/stable/1.5.x.tar.gz | |
polib |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment