7

How can I make sure setup.py compiles projects PO files and include them whenever an sdist is created. This is a Django app and manual process to generate MO files is to run following command in the app's root directory:

django-admin compilemessages

(This means going one level deeper than setup.py)

I would like to avoid manually compiling MO files everytime. And I don't want to store them in the repository at all.

muhuk
  • 15,777
  • 9
  • 59
  • 98

2 Answers2

6

My simple solution (got some idea from Trac):

#!/usr/bin/env python
from setuptools import setup, find_packages
from setuptools.command.install_lib import install_lib as _install_lib
from distutils.command.build import build as _build
from distutils.cmd import Command


class compile_translations(Command):
    description = 'compile message catalogs to MO files via django compilemessages'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        import os
        import sys
        from django.core.management.commands.compilemessages import \
            compile_messages
        curdir = os.getcwd()
        os.chdir(os.path.realpath('app_name'))
        compile_messages(stderr=sys.stderr)
        os.chdir(curdir)


class build(_build):
    sub_commands = [('compile_translations', None)] + _build.sub_commands


class install_lib(_install_lib):
    def run(self):
        self.run_command('compile_translations')
        _install_lib.run(self)

setup(name='app',
    packages=find_packages(),
    include_package_data=True,
    setup_requires=['django'],
    ...
    cmdclass={'build': build, 'install_lib': install_lib,
        'compile_translations': compile_translations}
)

This will help you compile po files when you building egg or install package.

slav0nic
  • 3,646
  • 1
  • 21
  • 15
  • This is nice, but fails when calling `python setup.py install` while django is not installed (The installer tries to run install command before installing django) – Taha Jahangir Apr 06 '14 at 13:28
  • 1
    i added setup_requires=['django'] for fix this, uses this trick in https://bitbucket.org/slav0nic/djangobb/src/de8e836662e8c5e3a294717c24238b20f3cced47/setup.py?at=default – slav0nic Apr 07 '14 at 08:56
3
from django.core.management.commands.compilemessages import compile_messages

and use it in your setup.py script before you run setup and then include created files in setup method.

gruszczy
  • 40,948
  • 31
  • 128
  • 181
  • 1
    The question is about automating this task – Taha Jahangir Apr 06 '14 at 13:09
  • 1
    Setup script is a normal `python` script, so you can use code from `Django` inside it. Consider `setup.py` a normal program, whose goal is to install your package and must follow various steps. One of them is invoking `compile_messages` from `Django`. This will make the task of installing the package automatized - you don't need to call `django-admin compilemessages` before running `setup.py`. – gruszczy Apr 06 '14 at 15:38