EEXIST is in errno
[wolnelektury.git] / apps / wolnelektury_core / management / commands / localepack.py
1
2 from optparse import make_option
3 from django.conf import settings
4 from django.core.management.base import BaseCommand
5 from django.core.management.color import color_style
6 from django.core.management import call_command
7
8 import os
9 import shutil
10 import tempfile
11
12 import allauth
13
14 ROOT = os.path.dirname(settings.PROJECT_DIR)
15
16
17 def is_our_app(mod):
18     return mod.__path__[0].startswith(ROOT)
19
20
21 class Locale(object):
22     def save(self, output_directory, languages):
23         pass
24
25     def generate(self, languages):
26         pass
27
28
29 class AppLocale(Locale):
30     def __init__(self, appmod):
31         self.app = appmod
32         if not os.path.exists(os.path.join(self.path, 'locale')):
33             raise LookupError('No locale for app %s' % appmod)
34
35     @property
36     def path(self):
37         return self.app.__path__[0]
38
39     @property
40     def name(self):
41         return self.app.__name__
42
43     def save(self, output_directory, languages):
44         for lc in languages:
45             lc = lc[0]
46             if os.path.exists(os.path.join(self.path, 'locale', lc)):
47                 shutil.copy2(os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'),
48                          os.path.join(output_directory, lc, self.name + '.po'))
49
50     def load(self, input_directory, languages):
51         for lc in zip(*languages)[0]:
52             shutil.copy2(os.path.join(input_directory, lc, self.name + '.po'),
53                          os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'))
54
55     def generate(self, languages):
56         os.chdir(self.path)
57         print "in %s" % os.getcwd()
58         try:
59             call_command('makemessages', all=True)
60         except:
61             pass
62
63
64 class ModelTranslation(Locale):
65     def __init__(self, appname):
66         self.appname = appname
67
68     def save(self, output_directory, languages):
69         call_command('translation2po', self.appname, directory=output_directory)
70
71     def load(self, input_directory, languages):
72         call_command('translation2po', self.appname, directory=input_directory, load=True)
73
74
75 class CustomLocale(Locale):
76     def __init__(self, app_dir,
77                  config=os.path.join(ROOT, "babel.cfg"),
78                  out_file=os.path.join(ROOT, 'wolnelektury/locale-contrib/django.pot'),
79                  name=None):
80         self.app_dir = app_dir
81         self.config = config
82         self.out_file = out_file
83         self.name = name
84
85     def generate(self, languages):
86         os.system('pybabel extract -F "%s" -o "%s" "%s"' % (self.config, self.out_file, self.app_dir))
87         os.system('pybabel update -D django -i %s -d %s' % (self.out_file, os.path.dirname(self.out_file)))
88
89     def po_file(self, language):
90         d = os.path.dirname(self.out_file)
91         n = os.path.basename(self.out_file).split('.')[0]
92         return os.path.join(d, language, 'LC_MESSAGES', n + '.po')
93
94     def save(self, output_directory, languages):
95         for lc in zip(*languages)[0]:
96             if os.path.exists(self.po_file(lc)):
97                 shutil.copy2(self.po_file(lc),
98                              os.path.join(output_directory, lc, self.name + '.po'))
99
100     def load(self, input_directory, languages):
101         for lc in zip(*languages)[0]:
102             shutil.copy2(os.path.join(input_directory, lc, self.name + '.po'),
103                          self.po_file(lc))
104         os.system('pybabel compile -D django -d %s' % os.dirname(self.out_file))
105
106
107 SOURCES = []
108
109 for appn in settings.INSTALLED_APPS:
110     app = __import__(appn)
111     if is_our_app(app):
112         try:
113             SOURCES.append(AppLocale(app))
114         except LookupError, e:
115             print "no locales in %s" % app
116
117 SOURCES.append(ModelTranslation('infopages'))
118 SOURCES.append(CustomLocale(os.path.dirname(allauth.__file__), name='contrib'))
119
120
121 class Command(BaseCommand):
122     option_list = BaseCommand.option_list + (
123         make_option('-l', '--load', help='load locales back to source', action='store_true', dest='load', default=False),
124         make_option('-o', '--outfile', help='Resulting zip file', dest='outfile', default='./wl-locale.zip'),
125         )
126     help = 'Make a locale pack'
127     args = ''
128
129     def handle(self, *a, **options):
130         tmp_dir = tempfile.mkdtemp('-wl-locale')
131         out_dir = os.path.join(tmp_dir, 'wl-locale')
132         os.mkdir(out_dir)
133
134         try:
135             for lang in settings.LANGUAGES:
136                 os.mkdir(os.path.join(out_dir, lang[0]))
137
138             for src in SOURCES:
139                 src.generate(settings.LANGUAGES)
140                 src.save(out_dir, settings.LANGUAGES)
141                 #                src.save(settings.LANGUAGES)
142
143             packname = options.get('outfile')
144             packname_b = os.path.basename(packname).split('.')[0]
145             fmt = '.'.join(os.path.basename(packname).split('.')[1:])
146             shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir), base_dir=os.path.basename(out_dir))
147         finally:
148             shutil.rmtree(tmp_dir, ignore_errors=True)