[tor-commits] [bridgedb/master] Fix several problems with installation of compiled translations files.

isis at torproject.org isis at torproject.org
Sun Jan 12 06:06:30 UTC 2014


commit ca386e9fba287c19227f877425efa173a835c68e
Author: Isis Lovecruft <isis at torproject.org>
Date:   Tue Oct 15 03:09:28 2013 +0000

    Fix several problems with installation of compiled translations files.
    
     * ADD function ``get_supported_langs()`` to setup.py, which determines which
       completed, compiled translations files are present in our source tree, and
       stores three things:
    
         1. The LOCALE code for the translation.
         2. The path of the directory to install the files to, relative to the
            current ``sys.prefix``. (``sys.prefix`` is where Python files are
            going to be installed, and it is set by the OS's interpreter first,
            and then occaisionally changed according to various factors, like
            whether or not we are working in a virtualenv, if the package is being
            installed from a DCVS/egg/tarball/zipfile, if the package is being
            installed via pip or via a package manager, etc.)
         3. A list of files for that LOCALE which should be installed. In our
            case, this should only ever be one <LOCALE>/LC_MESSAGES/bridgedb.mo
            file per LOCALE.
    
       These three things allow us to tell both setuptools (and the underlying
       distutils mechanisms) how/where to install everything.
    
       The next thing that ``get_supported_langs()`` does is take the list of
       collected LOCALEs which are to be installed, and rewrite a line in the
       following new module:
    
     * ADD a new module, lib/bridgedb/_langs.py, which has one variable,
       ``supported``, which is a list of two-letter country codes of languages
       installed. When "python setup.py install" is run (or any setup.py command,
       for that matter) the ``supported`` variable in lib/bridgedb/_langs.py is
       rewritten to be the current list of languages for which we have completed,
       compiled translation files.
    
     * ADD function ``get_langs()`` to lib/bridgedb/_langs.py. This simply fetches
       the current value for ``supported``.
    
     * ADD a package attribute, ``bridgedb.__langs__``, which uses ``get_langs()``
       to store, in the installed version of BridgeDB, the list of supported
       languages which were also installed. :)
    
     * ADD an entry for ``lib/bridgedb/_langs.py`` into .gitignore, so that a
       locally rewritten file is never committed.
---
 .gitignore               |    4 ++++
 lib/bridgedb/__init__.py |    5 +++++
 lib/bridgedb/_langs.py   |   24 +++++++++++++++++++++
 setup.py                 |   53 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 86 insertions(+)

diff --git a/.gitignore b/.gitignore
index 854885e..47ea554 100644
--- a/.gitignore
+++ b/.gitignore
@@ -78,3 +78,7 @@ run/*
 *networkstatus-bridges
 *blocked-bridges
 *extra-infos
+
+# do not recommit lib/bridgedb/_langs.py:
+# (the "supported" variable is supposed to be rewritten at install time)
+lib/bridgedb/_langs.py
diff --git a/lib/bridgedb/__init__.py b/lib/bridgedb/__init__.py
index 3979b43..e3c3764 100644
--- a/lib/bridgedb/__init__.py
+++ b/lib/bridgedb/__init__.py
@@ -2,5 +2,10 @@
 # This file tells Python that this is an honest to goodness package.
 
 from ._version import get_versions
+from ._langs import get_langs
+
 __version__ = get_versions()['version']
+__langs__ = get_langs()
+
 del get_versions
+del get_langs
diff --git a/lib/bridgedb/_langs.py b/lib/bridgedb/_langs.py
new file mode 100644
index 0000000..149265c
--- /dev/null
+++ b/lib/bridgedb/_langs.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+#
+# This file is part of BridgeDB, a Tor bridge distribution system.
+#
+# :authors: Isis Lovecruft 0xA3ADB67A2CDB8B35 <isis at torproject.org>
+#           please also see AUTHORS file
+# :copyright: (c) 2007-2013, The Tor Project, Inc.
+#             (c) 2007-2013, all entities within the AUTHORS file
+# :license: 3-clause BSD, see included LICENSE for information
+
+"""_langs.py - Storage for information on installed language support."""
+
+
+def get_langs():
+    """Return a list of two-letter country codes of translations which were
+    installed (if we've already been installed).
+    """
+    return supported
+
+
+#: This list will be rewritten by :func:`get_supported_langs` in setup.py at
+#: install time, so that the :attr:`bridgedb.__langs__` will hold a list of
+#: two-letter country codes for languages which were installed.
+supported = []
diff --git a/setup.py b/setup.py
index 0290ea2..876baa8 100644
--- a/setup.py
+++ b/setup.py
@@ -106,6 +106,59 @@ def get_requirements():
 
     return requirements
 
+def get_supported_langs():
+    """Get the paths for all compiled translation files.
+
+    The two-letter country code of each language which is going to be
+    installed will be added to a list, and this list will be written to
+    :attr:`repo_langs`, so that lib/bridgedb/__init__.py can store a
+    package-level attribute ``bridgedb.__langs__``, which will be a list of
+    any languages which were installed.
+
+    Then, the paths of the compiled translations files are added to
+    :ivar:`data_files`. These should be included in the ``data_files``
+    parameter in :func:`~setuptools.setup` in order for setuptools to be able
+    to tell the underlying distutils ``install_data`` command to include these
+    files.
+
+    See http://docs.python.org/2/distutils/setupscript.html#installing-additional-files
+    for more information.
+
+    :ivar list supported: A list of two-letter country codes, one for each
+        language we currently provide translations support for.
+    :ivar list lang_dirs: The directories (relative or absolute) to install
+        the compiled translation file to.
+    :ivar list lang_files: The paths to compiled translations files, relative
+        to this setup.py script.
+    :rtype: list
+    :returns: Two lists, ``lang_dirs`` and ``lang_files``.
+    """
+    supported = []
+    lang_dirs = []
+    lang_files = []
+
+    for lang in os.listdir(repo_i18n):
+        if lang.endswith('templates'):
+            continue
+        supported.append(lang)
+        lang_dirs.append(os.path.join(install_i18n, lang))
+        lang_files.append(os.path.join(repo_i18n, lang,
+                                       'LC_MESSAGES', 'bridgedb.mo'))
+
+    # Write our list of supported languages to 'lib/bridgedb/_langs.py':
+    new_langs_lines = []
+    with open(repo_langs, 'r') as langsfile:
+        for line in langsfile.readlines():
+            if line.startswith('supported'):
+                line = "supported = {}\n".format(supported)
+                print("REWROTE supported langs: %s" % line)
+            new_langs_lines.append(line)
+    with open(repo_langs, 'w') as newlangsfile:
+        for line in new_langs_lines:
+            newlangsfile.write(line)
+
+    return lang_dirs, lang_files
+
 def get_data_files():
     """Returns our hard-coded data_files which should be distributed.
 





More information about the tor-commits mailing list