Last active
December 17, 2015 16:59
-
-
Save jezdez/5642551 to your computer and use it in GitHub Desktop.
a script to re-combine all django-localflavor-* apps
This file contains 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 | |
"""Port localflavors | |
Usage: | |
port.py clone [--dry-run] | |
port.py copy [--dry-run] | |
port.py merge_po [--dry-run] | |
port.py (-h | --help) | |
port.py --version | |
Options: | |
-h --help Show this screen. | |
--version Show version. | |
--dry-run Don't actually do something. | |
""" | |
import errno | |
import glob | |
import os | |
import envoy | |
import polib | |
import shutil | |
from docopt import docopt | |
from os.path import join, abspath, exists, dirname, split | |
github_url = 'https://github.com/django/django-localflavor-%(country)s' | |
here = abspath(dirname(__file__)) | |
repos_path = join(here, 'repos') | |
if not exists(repos_path): | |
os.makedirs(repos_path) | |
countries = [ | |
'us', 'ar', 'au', 'at', 'be', 'br', 'ca', 'cl', 'cn', 'co', 'hr', | |
'cz', 'ec', 'fi', 'fr', 'de', 'hk', 'nl', 'is', 'in', 'ie', 'id', | |
'il', 'it', 'jp', 'kw', 'mk', 'mx', 'no', 'py', 'pe', 'pl', 'pt', | |
'ro', 'ru', 'sk', 'si', 'za', 'es', 'se', 'ch', 'tr', 'gb', 'uy', | |
] | |
def clone(dry_run=False): | |
for country in countries: | |
country_url = github_url % dict(country=country) | |
command = 'git clone %s' % country_url | |
print command | |
if not dry_run: | |
response = envoy.run(command, cwd=repos_path) | |
print response.std_out | |
def copy(dry_run=False): | |
for country in countries: | |
root_name = 'django-localflavor-%s' % country | |
package_name = 'django_localflavor_%s' % country | |
src = join(here, 'repos', root_name, package_name) | |
dst = join(here, 'localflavor', country) | |
print 'copying', src, '-->', dst | |
if not dry_run: | |
shutil.copytree(src, dst) | |
test_src = join(here, 'repos', root_name, 'tests') | |
if exists(test_src): | |
test_dst = join(dst, 'tests') | |
print 'found test dir', test_src, '| copying to', test_dst | |
if not dry_run: | |
shutil.copytree(test_src, test_dst) | |
def mkdir_p(path): | |
try: | |
os.makedirs(path) | |
except OSError as exc: # Python >2.5 | |
if exc.errno == errno.EEXIST and os.path.isdir(path): | |
pass | |
else: | |
raise | |
def merge(src, dst): | |
# Store entries in dict/set for faster access | |
src_entries = dict((entry.msgid, entry) for entry in src) | |
# Merge entries that are in the dst | |
for entry in dst: | |
e = src_entries.get(entry.msgid) | |
if e is None: | |
e = polib.POEntry() | |
src.append(e) | |
e.merge(entry) | |
def merge_po(dry_run=False): | |
locale_dir = join(here, 'localflavor', 'locale') | |
# create new locale dir | |
if not exists(locale_dir): | |
os.makedirs(locale_dir) | |
for country in countries: | |
# source directory | |
country_locale_dir = join(here, 'localflavor', country, 'locale') | |
# iterate over all languages in directory | |
for country_locale_subdir in glob.glob('%s/*' % country_locale_dir): | |
print 'merging', country_locale_subdir | |
locale_locale_po_file = join(country_locale_subdir, | |
'LC_MESSAGES', 'django.po') | |
# get locale to update from dir name | |
_, subdir_locale = split(country_locale_subdir) | |
# build path to new destination locale po file, create dirs | |
locale_subdir = join(locale_dir, subdir_locale, 'LC_MESSAGES') | |
locale_po_file = join(locale_subdir, 'django.po') | |
mkdir_p(locale_subdir) | |
# either load existing file and add the entries of the current source file | |
if exists(locale_po_file): | |
po = polib.pofile(locale_po_file) | |
po2 = polib.pofile(locale_locale_po_file) | |
merge(po, po2) | |
# or just load the source file directly | |
else: | |
po = polib.pofile(locale_locale_po_file) | |
# update the project-id-version to be consistent | |
po.metadata['Project-Id-Version'] = 'django-localflavor' | |
# save new po file | |
po.save(locale_po_file) | |
# and compile it | |
po.save_as_mofile(join(locale_subdir, 'django.mo')) | |
# throw away the old locale subdir | |
if exists(country_locale_dir): | |
shutil.rmtree(country_locale_dir) | |
# run makemessages with locale to see if it errors out | |
# write custom fixes per language | |
if __name__ == '__main__': | |
args = docopt(__doc__, version='port.py 1.0') | |
if args.get('clone'): | |
clone(args['--dry-run']) | |
elif args.get('copy'): | |
copy(args['--dry-run']) | |
elif args.get('merge_po'): | |
merge_po(args['--dry-run']) |
This file contains 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
Django | |
requests | |
git+https://github.com/kennethreitz/envoy | |
github3.py | |
docopt | |
polib |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment