view roundup/dist/command/build.py @ 7752:b2dbab2b34bc

fix(refactor): multiple fixups using ruff linter; more testing. Converting to using the ruff linter and its rulesets. Fixed a number of issues. admin.py: sort imports use immutable tuples as default value markers for parameters where a None value is valid. reduced some loops to list comprehensions for performance used ternary to simplify some if statements named some variables to make them less magic (e.g. _default_savepoint_setting = 1000) fixed some tests for argument counts < 2 becomes != 2 so 3 is an error. moved exception handlers outside of loops for performance where exception handler will abort loop anyway. renamed variables called 'id' or 'dir' as they shadow builtin commands. fix translations of form _("string %s" % value) -> _("string %s") % value so translation will be looked up with the key before substitution. end dicts, tuples with a trailing comma to reduce missing comma errors if modified simplified sorted(list(self.setting.keys())) to sorted(self.setting.keys()) as sorted consumes whole list. in if conditions put compared variable on left and threshold condition on right. (no yoda conditions) multiple noqa: suppression removed unneeded noqa as lint rulesets are a bit different do_get - refactor output printing logic: Use fast return if not special formatting is requested; use isinstance with a tuple rather than two isinstance calls; cleaned up flow and removed comments on algorithm as it can be easily read from the code. do_filter, do_find - refactor output printing logic. Reduce duplicate code. do_find - renamed variable 'value' that was set inside a loop. The loop index variable was also named 'value'. do_pragma - added hint to use list subcommand if setting was not found. Replaced condition 'type(x) is bool' with 'isinstance(x, bool)' for various types. test_admin.py added testing for do_list better test coverage for do_get includes: -S and -d for multilinks, error case for -d with non-link. better testing for do_find including all output modes better testing for do_filter including all output modes fixed expected output for do_pragma that now includes hint to use pragma list if setting not found.
author John Rouillard <rouilj@ieee.org>
date Fri, 01 Mar 2024 14:53:18 -0500
parents 1acdc651133b
children fed0f839c260
line wrap: on
line source

#
# Copyright (C) 2009 Stefan Seefeld
# All rights reserved.
# For license terms see the file COPYING.txt.
#
from __future__ import print_function
from roundup import msgfmt
try:
    from setuptool.command.install import install as base
except ImportError:
    from distutils.command.build import build as base
import os
from glob import glob

def list_message_files(suffix=".po"):
    """Return list of all found message files and their intallation paths"""
    _files = glob("locale/*" + suffix)
    _list = []
    for _file in _files:
        # basename (without extension) is a locale name
        _locale = os.path.splitext(os.path.basename(_file))[0]
        _list.append((_file, os.path.join(
            "share", "locale", _locale, "LC_MESSAGES", "roundup.mo")))
    return _list

def check_manifest():
    """Check that the files listed in the MANIFEST are present when the
    source is unpacked.
    """
    manifest_file = 'roundup.egg-info/SOURCES.txt'
    try:
        f=open(manifest_file)
    except:
        print('\n*** SOURCE WARNING: The MANIFEST file "%s" is missing!' % manifest_file)
        return
    try:
        manifest = [l.strip() for l in f.readlines()]
    finally:
        f.close()
    err = set([line for line in manifest if not os.path.exists(line)])
    # ignore auto-generated files
    err = err - set(['roundup-admin', 'roundup-demo', 'roundup-gettext',
        'roundup-mailgw', 'roundup-server', 'roundup-xmlrpc-server'])
    if err:
        n = len(manifest)
        print('\n*** SOURCE WARNING: There are files missing (%d/%d found)!'%(
            n-len(err), n))
        print('Missing:', '\nMissing: '.join(err))

def build_message_files(command):
    """For each locale/*.po, build .mo file in target locale directory"""
    for (_src, _dst) in list_message_files():
        _build_dst = os.path.join("build", _dst)
        command.mkpath(os.path.dirname(_build_dst))
        command.announce("Compiling %s -> %s" % (_src, _build_dst))
        mo = msgfmt.Msgfmt(_src).get()
        open(_build_dst, 'wb').write(mo)


class build(base):

    def run(self):
        check_manifest()
        build_message_files(self)
        base.run(self)


Roundup Issue Tracker: http://roundup-tracker.org/