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 import call_command
 
   6 from modeltranslation.management.commands.translation2po import get_languages
 
  16 ROOT = os.path.dirname(settings.PROJECT_DIR)
 
  20     return mod.__path__[0].startswith(ROOT)
 
  24     def save(self, output_directory, languages):
 
  27     def generate(self, languages):
 
  31     "I can create a necessary dest directiories, yey!"
 
  32     if not os.path.exists(os.path.dirname(to)):
 
  33         os.makedirs(os.path.dirname(to))
 
  34     shutil.copyfile(frm,to)
 
  36 class AppLocale(Locale):
 
  37     def __init__(self, appmod):
 
  39         if not os.path.exists(os.path.join(self.path, 'locale')):
 
  40             raise LookupError('No locale for app %s' % appmod)
 
  44         return self.app.__path__[0]
 
  48         return self.app.__name__
 
  50     def save(self, output_directory, languages):
 
  53             if os.path.exists(os.path.join(self.path, 'locale', lc)):
 
  54                 copy_f(os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'),
 
  55                           os.path.join(output_directory, lc, self.name + '.po'))
 
  58     def load(self, input_directory, languages):
 
  59         for lc in zip(*languages)[0]:
 
  60             if os.path.exists(os.path.join(input_directory, lc, self.name + '.po')):
 
  61                 out = os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po')
 
  62                 if not os.path.exists(os.path.dirname(out)):
 
  63                     os.makedirs(os.path.dirname(out))
 
  64                 copy_f(os.path.join(input_directory, lc, self.name + '.po'),
 
  70             call_command('compilemessages', settings='wolnelektury.settings')
 
  77     def generate(self, languages):
 
  81             call_command('makemessages', all=True)
 
  88 class ModelTranslation(Locale):
 
  89     def __init__(self, appname, poname=None):
 
  90         self.appname = appname
 
  91         self.poname = poname and poname or appname
 
  93     def save(self, output_directory, languages):
 
  94         call_command('translation2po', self.appname, directory=output_directory, poname=self.poname)
 
  96     def load(self, input_directory, languages):
 
  97         call_command('translation2po', self.appname, directory=input_directory,
 
  98                      load=True, lang=','.join(zip(*languages)[0]), poname=self.poname, keep_running=True)
 
 101 class CustomLocale(Locale):
 
 102     def __init__(self, app_dir,
 
 103                  config=os.path.join(ROOT, "babel.cfg"),
 
 104                  out_file=os.path.join(ROOT, 'wolnelektury/locale-contrib/django.pot'),
 
 106         self.app_dir = app_dir
 
 108         self.out_file = out_file
 
 111     def generate(self, languages):
 
 112         os.system('pybabel extract -F "%s" -o "%s" "%s"' % (self.config, self.out_file, self.app_dir))
 
 113         os.system('pybabel update -D django -i %s -d %s' % (self.out_file, os.path.dirname(self.out_file)))
 
 115     def po_file(self, language):
 
 116         d = os.path.dirname(self.out_file)
 
 117         n = os.path.basename(self.out_file).split('.')[0]
 
 118         return os.path.join(d, language, 'LC_MESSAGES', n + '.po')
 
 120     def save(self, output_directory, languages):
 
 121         for lc in zip(*languages)[0]:
 
 122             if os.path.exists(self.po_file(lc)):
 
 123                 copy_f(self.po_file(lc),
 
 124                              os.path.join(output_directory, lc, self.name + '.po'))
 
 126     def load(self, input_directory, languages):
 
 127         for lc in zip(*languages)[0]:
 
 128             copy_f(os.path.join(input_directory, lc, self.name + '.po'),
 
 130         os.system('pybabel compile -D django -d %s' % os.path.dirname(self.out_file))
 
 135 for appn in settings.INSTALLED_APPS:
 
 136     app = __import__(appn)
 
 139             SOURCES.append(AppLocale(app))
 
 140         except LookupError, e:
 
 141             print "no locales in %s" % app.__name__
 
 143 SOURCES.append(ModelTranslation('infopages', 'infopages_db'))
 
 144 SOURCES.append(CustomLocale(os.path.dirname(allauth.__file__), name='contrib'))
 
 147 class Command(BaseCommand):
 
 148     option_list = BaseCommand.option_list + (
 
 149         make_option('-l', '--load', help='load locales back to source', action='store_true', dest='load', default=False),
 
 150         make_option('-L', '--lang', help='load just one language', dest='lang', default=None),
 
 151         make_option('-d', '--directory', help='load from this directory', dest='directory', default=None),
 
 152         make_option('-o', '--outfile', help='Resulting zip file', dest='outfile', default='./wl-locale.zip'),
 
 153         make_option('-m', '--merge', help='Use git to merge. Please use with clean working directory.', action='store_true', dest='merge', default=False),
 
 154         make_option('-M', '--message', help='commit message', dest='message', default='New locale'),
 
 157     help = 'Make a locale pack'
 
 160     def current_rev(self):
 
 161         return os.popen('git rev-parse HEAD').read()
 
 163     def current_branch(self):
 
 164         return os.popen("git branch |grep '^[*]' | cut -c 3-").read()
 
 166     def save(self, options):
 
 167         packname = options.get('outfile')
 
 168         packname_b = os.path.basename(packname).split('.')[0]
 
 169         fmt = '.'.join(os.path.basename(packname).split('.')[1:])
 
 172             raise NotImplementedError('Sorry. Only zip format supported at the moment.')
 
 174         tmp_dir = tempfile.mkdtemp('-wl-locale')
 
 175         out_dir = os.path.join(tmp_dir, packname_b)
 
 179             for lang in settings.LANGUAGES:
 
 180                 os.mkdir(os.path.join(out_dir, lang[0]))
 
 183                 src.generate(settings.LANGUAGES)
 
 184                 src.save(out_dir, settings.LANGUAGES)
 
 185                 #                src.save(settings.LANGUAGES)
 
 188             rev = self.current_rev()
 
 189             rf = open(os.path.join(out_dir, '.revision'), 'w')
 
 196                 os.chdir(os.path.dirname(out_dir))
 
 197                 self.system('zip -r %s %s' % (os.path.join(cwd, packname_b+'.zip'), os.path.basename(out_dir)))
 
 200                 #            shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir), base_dir=os.path.basename(out_dir))
 
 202             shutil.rmtree(tmp_dir, ignore_errors=True)
 
 204     def load(self, options):
 
 205         langs = get_languages(options['lang'])
 
 208             src.load(options['directory'], langs)
 
 210     def handle(self, *a, **options):
 
 212             if not options['directory'] or not os.path.exists(options['directory']):
 
 213                 print "Directory not provided or does not exist, please use -d"
 
 216             if options['merge']: self.merge_setup(options['directory'])
 
 218             if options['merge']: self.merge_finish(options['message'])
 
 222     merge_branch = 'wl-locale-merge'
 
 225     def merge_setup(self, directory):
 
 226         self.last_branch = self.current_branch()
 
 227         rev = open(os.path.join(directory, '.revision')).read()
 
 229         self.system('git checkout -b %s %s' % (self.merge_branch, rev))
 
 231     def merge_finish(self, message):
 
 232         self.system('git commit -a -m "%s"' % message.replace('"', '\\"'))
 
 233         self.system('git checkout %s' % self.last_branch)
 
 234         self.system('git merge -s recursive -X theirs %s' % self.merge_branch)
 
 235         self.system('git branch -d %s' % self.merge_branch)
 
 237     def system(self, fmt, *args):
 
 238         code = os.system(fmt % args)
 
 240             raise OSError('Command %s returned with exit code %d' % (fmt % args, code))