예제 #1
0
def get_console_locale(env=None,
                       lang=None,
                       categories=('LANGUAGE', 'LC_ALL', 'LC_MESSAGES',
                                   'LANG')):
    """Return negotiated locale for console by locale environments and
    [trac] default_language."""
    if has_babel:
        from babel.core import UnknownLocaleError, parse_locale

        def normalize(value):
            if not value:
                return None
            try:
                return '_'.join(filter(None, parse_locale(value)))
            except:
                return None

        locales = [lang]
        for category in categories:
            value = os.environ.get(category)
            if not value:
                continue
            if category == 'LANGUAGE' and ':' in value:
                value = value.split(':')[0]
            locales.append(value)
        if env:
            locales.append(env.config.get('trac', 'default_language'))
        locales = filter(None, map(normalize, locales))
        try:
            return get_negotiated_locale(locales)
        except UnknownLocaleError:
            pass
    return None
예제 #2
0
파일: main.py 프로젝트: moreati/trac-gitsvn
 def _get_locale(self, req):
     if has_babel:
         preferred = req.session.get('language')
         default = self.env.config.get('trac', 'default_language', '')
         negotiated = get_negotiated_locale([preferred, default] +
                                            req.languages)
         self.log.debug("Negotiated locale: %s -> %s", preferred, negotiated)
         return negotiated
예제 #3
0
 def _get_locale(self, req):
     if has_babel:
         preferred = req.session.get('language')
         default = self.env.config.get('trac', 'default_language', '')
         negotiated = get_negotiated_locale([preferred, default] +
                                            req.languages)
         self.log.debug("Negotiated locale: %s -> %s", preferred, negotiated)
         return negotiated
예제 #4
0
파일: api.py 프로젝트: timgraham/trac
def get_console_locale(env=None, lang=LANG):
    """Return negotiated locale for console by LANG environment and
    [trac] default_language."""
    if has_babel:
        from babel.core import Locale, UnknownLocaleError, parse_locale
        try:
            lang = '_'.join(filter(None, parse_locale(lang)))
        except:
            lang = None
        default = env.config.get('trac', 'default_language', '') \
                  if env else None
        try:
            return get_negotiated_locale([lang, default]) or Locale.default()
        except UnknownLocaleError:
            pass
    return None
예제 #5
0
파일: api.py 프로젝트: exocad/exotrac
def get_console_locale(env=None, lang=LANG):
    """Return negotiated locale for console by LANG environment and
    [trac] default_language."""
    if has_babel:
        from babel.core import Locale, UnknownLocaleError, parse_locale
        try:
            lang = '_'.join(filter(None, parse_locale(lang)))
        except:
            lang = None
        default = env.config.get('trac', 'default_language', '') \
                  if env else None
        try:
            return get_negotiated_locale([lang, default]) or Locale.default()
        except UnknownLocaleError:
            pass
    return None
예제 #6
0
    def setup(self, **kwargs):
        """Do the setup. A kwargs dictionary may be passed to override base
        options, potentially allowing for multiple environment creation."""
        
        if has_babel:
            import babel
            try:
                locale = get_negotiated_locale([LANG]) 
                locale = locale or babel.Locale.default()
            except babel.UnknownLocaleError:
                pass
            translation.activate(locale)
        
        options = dict(self.options)
        options.update(kwargs)
        if psycopg2 is None and options.get('dbtype') == 'postgres':
            print "psycopg2 needs to be installed to initialise a postgresql db"
            return False

        environments_path = options['envsdir']
        if not os.path.exists(environments_path):
            os.makedirs(environments_path)

        new_env =  os.path.join(environments_path, options['project'])
        tracini = os.path.abspath(os.path.join(new_env, 'conf', 'trac.ini'))
        baseini = os.path.abspath(os.path.join(new_env, 'conf', 'base.ini'))
        options['inherit'] = baseini

        options['db'] = self._generate_db_str(options)
        if 'repo_type' not in options or options['repo_type'] is None:
            options['repo_type'] = ''
        if 'repo_path' not in options or options['repo_path'] is None:
            options['repo_path'] = ''
        if (len(options['repo_type']) > 0) ^ (len(options['repo_path']) > 0):
            print "Error: Specifying a repository requires both the "\
                  "repository-type and the repository-path options."
            return False

        digestfile = os.path.abspath(os.path.join(new_env,
                                                  options['digestfile']))
        realm =  options['realm']
        adminuser = options['adminuser']
        adminpass = options['adminpass']

        # create base options:
        accounts_config = dict(ACCOUNTS_CONFIG)
        accounts_config['account-manager']['htdigest_file'] = digestfile
        accounts_config['account-manager']['htdigest_realm'] = realm

        trac = TracAdmin(os.path.abspath(new_env))
        if not trac.env_check():
            try:
                trac.do_initenv('%(project)s %(db)s '
                                '%(repo_type)s %(repo_path)s '
                                '--inherit=%(inherit)s '
                                '--nowiki'
                                % options)
            except SystemExit:
                print ("Error: Unable to initialise the database"
                       "Traceback for error is above")
                return False
        else:
            print ("Warning: Environment already exists at %s." % new_env)
            self.writeconfig(tracini, [{'inherit': {'file': baseini},},])

        self.writeconfig(baseini, [BASE_CONFIG, accounts_config])

        if os.path.exists(digestfile):
            backupfile(digestfile)
        htdigest_create(digestfile, adminuser, realm, adminpass)

        print "Adding TRAC_ADMIN permissions to the admin user %s" % adminuser
        trac.onecmd('permission add %s TRAC_ADMIN' % adminuser)

        # get fresh TracAdmin instance (original does not know about base.ini)
        bloodhound = TracAdmin(os.path.abspath(new_env))

        # final upgrade
        print "Running upgrades"
        bloodhound.onecmd('upgrade')
        pages = []
        pages.append(pkg_resources.resource_filename('bhdashboard',
                                                     'default-pages'))
        bloodhound.onecmd('wiki load %s' % " ".join(pages))

        print "Running wiki upgrades"
        bloodhound.onecmd('wiki upgrade')
        
        print "Running wiki bh upgrades"
        bloodhound.onecmd('wiki bh-upgrade')

        print """
You can now start Bloodhound by running:

  tracd --port=8000 %s

And point your browser at http://localhost:8000/%s
""" % (os.path.abspath(new_env), options['project'])
        return True
예제 #7
0
 def _get_locale(self, req):
     if trac_translation.has_babel:
         # TODO: Use django-cms function (get_language_from_request) instead? Why req is not used?
         return trac_translation.get_negotiated_locale([django_translation.get_language()])
예제 #8
0
    def setup(self, **kwargs):
        """Do the setup. A kwargs dictionary may be passed to override base
        options, potentially allowing for multiple environment creation."""

        if has_babel:
            import babel
            try:
                locale = get_negotiated_locale([LANG])
                locale = locale or babel.Locale.default()
            except babel.UnknownLocaleError:
                pass
            translation.activate(locale)

        options = dict(self.options)
        options.update(kwargs)
        if psycopg2 is None and options.get('dbtype') == 'postgres':
            print "psycopg2 needs to be installed to initialise a postgresql db"
            return False
        elif mysqldb is None and options.get('dbtype') == 'mysql':
            print "MySQLdb needs to be installed to initialise a mysql db"
            return False

        environments_path = options['envsdir']
        if not os.path.exists(environments_path):
            os.makedirs(environments_path)

        new_env =  os.path.join(environments_path, options['project'])
        tracini = os.path.abspath(os.path.join(new_env, 'conf', 'trac.ini'))
        baseini = os.path.abspath(os.path.join(new_env, 'conf', 'base.ini'))
        options['inherit'] = '"' + baseini + '"'

        options['db'] = self._generate_db_str(options)
        if 'repo_type' not in options or options['repo_type'] is None:
            options['repo_type'] = ''
        if 'repo_path' not in options or options['repo_path'] is None:
            options['repo_path'] = ''
        if (len(options['repo_type']) > 0) ^ (len(options['repo_path']) > 0):
            print "Error: Specifying a repository requires both the "\
                  "repository-type and the repository-path options."
            return False

        custom_prefix = 'default_product_prefix'
        if custom_prefix in options and options[custom_prefix]:
            default_product_prefix = options[custom_prefix]
        else:
            default_product_prefix = '@'

        digestfile = os.path.abspath(os.path.join(new_env,
                                                  options['digestfile']))
        realm =  options['realm']
        adminuser = options['adminuser']
        adminpass = options['adminpass']

        # create base options:
        accounts_config = dict(ACCOUNTS_CONFIG)
        accounts_config['account-manager']['htdigest_file'] = digestfile
        accounts_config['account-manager']['htdigest_realm'] = realm

        trac = TracAdmin(os.path.abspath(new_env))
        if not trac.env_check():
            try:
                rv = trac.do_initenv('%(project)s %(db)s '
                                     '%(repo_type)s %(repo_path)s '
                                     '--inherit=%(inherit)s '
                                     '--nowiki'
                                     % options)
                if rv == 2:
                    raise SystemExit
            except SystemExit:
                print ("Error: Unable to initialise the environment.")
                return False
        else:
            print ("Warning: Environment already exists at %s." % new_env)
            self.writeconfig(tracini, [{'inherit': {'file': baseini},},])

        base_config = dict(BASE_CONFIG)
        base_config['trac']['environment_factory'] = \
            'multiproduct.hooks.MultiProductEnvironmentFactory'
        base_config['trac']['request_factory'] = \
            'multiproduct.hooks.ProductRequestFactory'
        if default_product_prefix != '@':
            base_config['multiproduct'] = dict(
                default_product_prefix=default_product_prefix
            )

        self.writeconfig(baseini, [base_config, accounts_config])

        if os.path.exists(digestfile):
            backupfile(digestfile)
        htdigest_create(digestfile, adminuser, realm, adminpass)

        print "Adding TRAC_ADMIN permissions to the admin user %s" % adminuser
        trac.onecmd('permission add %s TRAC_ADMIN' % adminuser)

        # get fresh TracAdmin instance (original does not know about base.ini)
        bloodhound = TracAdmin(os.path.abspath(new_env))

        # final upgrade
        print "Running upgrades"
        bloodhound.onecmd('upgrade')
        pages = []
        pages.append(pkg_resources.resource_filename('bhdashboard',
                                                 'default-pages'))
        pages.append(pkg_resources.resource_filename('bhsearch',
                                                 'default-pages'))
        bloodhound.onecmd('wiki load %s' % " ".join(pages))

        print "Running wiki upgrades"
        bloodhound.onecmd('wiki upgrade')

        if self.apply_bhwiki_upgrades:
            print "Running wiki Bloodhound upgrades"
            bloodhound.onecmd('wiki bh-upgrade')
        else:
            print "Skipping Bloodhound wiki upgrades"

        print "Loading default product wiki"
        bloodhound.onecmd('product admin %s wiki load %s' %
                          (default_product_prefix,
                           " ".join(pages)))

        print "Running default product wiki upgrades"
        bloodhound.onecmd('product admin %s wiki upgrade' %
                          default_product_prefix)

        if self.apply_bhwiki_upgrades:
            print "Running default product Bloodhound wiki upgrades"
            bloodhound.onecmd('product admin %s wiki bh-upgrade' %
                              default_product_prefix)
        else:
            print "Skipping default product Bloodhound wiki upgrades"

        print """
You can now start Bloodhound by running:

  tracd --port=8000 %s

And point your browser at http://localhost:8000/%s
""" % (os.path.abspath(new_env), options['project'])
        return True