Ticket #6073: compile-messages.py.diff

File compile-messages.py.diff, 2.6 KB (added by Wonlay, 16 years ago)
  • django/bin/compile-messages.py

     
    3434            if f.endswith('.po'):
    3535                sys.stderr.write('processing file %s in %s\n' % (f, dirpath))
    3636                pf = os.path.splitext(os.path.join(dirpath, f))[0]
     37                # msgfmt does not support files with byte order marks, so
     38                # we have to make a tmp file without marks for compiling.
     39                pofile = pf + '.po'
     40                tmpfile = None
     41                po = file(pofile)
     42                s = po.read(4)
     43                if s[:3] == '\xef\xbb\xbf':    # UTF-8
     44                    remove = 3
     45                elif s[:2] == '\xfe\xff':    # UTF-16 Big Endian
     46                    remove = 2
     47                elif s[:2] == '\xff\xfe':    # UTF-16 Little Endian
     48                    remove = 2
     49                elif s[:4] == '\x00\x00\xfe\xff':    # UTF-32 Big Endian
     50                    remove = 4
     51                elif s[:4] == '\xff\xfe\x00\x00':    # UTF-32 Little Endian
     52                    remove = 4
     53                else:
     54                    remove = 0
     55                if remove > 0:
     56                    po.seek(remove)
     57                    tmpfile = pofile + '.tmp'
     58                    tmp = file(tmpfile, 'w+b')
     59                    tmp.write(po.read())
     60                    tmp.close()
     61                    pofile = tmpfile
     62                po.close()
    3763                # Store the names of the .mo and .po files in an environment
    3864                # variable, rather than doing a string replacement into the
    3965                # command, so that we can take advantage of shell quoting, to
    4066                # quote any malicious characters/escaping.
    4167                # See http://cyberelk.net/tim/articles/cmdline/ar01s02.html
    4268                os.environ['djangocompilemo'] = pf + '.mo'
    43                 os.environ['djangocompilepo'] = pf + '.po'
     69                os.environ['djangocompilepo'] = pofile
    4470                if sys.platform == 'win32': # Different shell-variable syntax
    4571                    cmd = 'msgfmt --check-format -o "%djangocompilemo%" "%djangocompilepo%"'
    4672                else:
    4773                    cmd = 'msgfmt --check-format -o "$djangocompilemo" "$djangocompilepo"'
    4874                os.system(cmd)
    4975
     76                if tmpfile:
     77                    # Remove the tmp file we created.
     78                    os.remove(tmpfile)
     79
    5080def main():
    5181    parser = optparse.OptionParser()
    5282    parser.add_option('-l', '--locale', dest='locale',
Back to Top