Esempio n. 1
0
 def _write(self, parser):
     if not self.filename:
         return
     wait_for_file_mtime_change(self.filename)
     with AtomicFile(self.filename, 'w') as fd:
         fd.writelines(['# -*- coding: utf-8 -*-\n', '\n'])
         parser.write(fd)
Esempio n. 2
0
File: config.py Progetto: zxfly/trac
 def _write(self, parser):
     if not self.filename:
         return
     wait_for_file_mtime_change(self.filename)
     with AtomicFile(self.filename, 'w') as fd:
         fd.writelines(['# -*- coding: utf-8 -*-\n', '\n'])
         parser.write(fd)
Esempio n. 3
0
    def enable_authz_permpolicy(self, authz_content, filename=None):
        """Enables the Authz permissions policy. The `authz_content` will
        be written to `filename`, and may be specified in a triple-quoted
        string.::

           [wiki:WikiStart@*]
           * = WIKI_VIEW
           [wiki:PrivatePage@*]
           john = WIKI_VIEW
           * = !WIKI_VIEW

        `authz_content` may also be a dictionary of dictionaries specifying
        the sections and key/value pairs of each section, however this form
        should only be used when the order of the entries in the file is not
        important, as the order cannot be known.::

           {
            'wiki:WikiStart@*': {'*': 'WIKI_VIEW'},
            'wiki:PrivatePage@*': {'john': 'WIKI_VIEW', '*': '!WIKI_VIEW'},
           }

        The `filename` parameter is optional, and if omitted a filename will
        be generated by computing a hash of `authz_content`, prefixed with
        "authz-".
        """
        if filename is None:
            filename = 'authz-' + \
                       hashlib.md5(str(authz_content)).hexdigest()[:9]
        env = self.get_trac_environment()
        authz_file = os.path.join(env.conf_dir, filename)
        if os.path.exists(authz_file):
            wait_for_file_mtime_change(authz_file)
        if isinstance(authz_content, basestring):
            authz_content = [
                line.strip() + '\n'
                for line in authz_content.strip().splitlines()
            ]
            authz_content = ['# -*- coding: utf-8 -*-\n'] + authz_content
            create_file(authz_file, authz_content)
        else:
            parser = UnicodeConfigParser()
            for section, options in authz_content.items():
                parser.add_section(section)
                for key, value in options.items():
                    parser.set(section, key, value)
            with open(authz_file, 'w') as f:
                parser.write(f)
        permission_policies = env.config.get('trac', 'permission_policies')
        env.config.set('trac', 'permission_policies',
                       'AuthzPolicy, ' + permission_policies)
        env.config.set('authz_policy', 'authz_file', authz_file)
        env.config.set('components', 'tracopt.perm.authz_policy.*', 'enabled')
        env.config.save()
Esempio n. 4
0
    def enable_authz_permpolicy(self, authz_content, filename=None):
        """Enables the Authz permissions policy. The `authz_content` will
        be written to `filename`, and may be specified in a triple-quoted
        string.::

           [wiki:WikiStart@*]
           * = WIKI_VIEW
           [wiki:PrivatePage@*]
           john = WIKI_VIEW
           * = !WIKI_VIEW

        `authz_content` may also be a dictionary of dictionaries specifying
        the sections and key/value pairs of each section, however this form
        should only be used when the order of the entries in the file is not
        important, as the order cannot be known.::

           {
            'wiki:WikiStart@*': {'*': 'WIKI_VIEW'},
            'wiki:PrivatePage@*': {'john': 'WIKI_VIEW', '*': '!WIKI_VIEW'},
           }

        The `filename` parameter is optional, and if omitted a filename will
        be generated by computing a hash of `authz_content`, prefixed with
        "authz-".
        """
        if filename is None:
            filename = 'authz-' + \
                       hashlib.md5(str(authz_content)).hexdigest()[:9]
        authz_file = os.path.join(self.tracdir, 'conf', filename)
        if os.path.exists(authz_file):
            wait_for_file_mtime_change(authz_file)
        if isinstance(authz_content, basestring):
            authz_content = [line.strip() + '\n'
                             for line in authz_content.strip().splitlines()]
            authz_content = ['# -*- coding: utf-8 -*-\n'] + authz_content
            create_file(authz_file, authz_content)
        else:
            parser = UnicodeConfigParser()
            for section, options in authz_content.items():
                parser.add_section(section)
                for key, value in options.items():
                    parser.set(section, key, value)
            with open(authz_file, 'w') as f:
                parser.write(f)
        env = self.get_trac_environment()
        permission_policies = env.config.get('trac', 'permission_policies')
        env.config.set('trac', 'permission_policies',
                       'AuthzPolicy, ' + permission_policies)
        env.config.set('authz_policy', 'authz_file', authz_file)
        env.config.set('components', 'tracopt.perm.authz_policy.*', 'enabled')
        env.config.save()
Esempio n. 5
0
    def enable_authz_permpolicy(self, authz_content, filename=None):
        """Enables the Authz permissions policy. The `authz_content` will
        be written to `filename`, and may be specified in a triple-quoted
        string.::

           [wiki:WikiStart@*]
           * = WIKI_VIEW
           [wiki:PrivatePage@*]
           john = WIKI_VIEW
           * = !WIKI_VIEW

        `authz_content` may also be a dictionary of dictionaries specifying
        the sections and key/value pairs of each section, however this form
        should only be used when the order of the entries in the file is not
        important, as the order cannot be known.::

           {
            'wiki:WikiStart@*': {'*': 'WIKI_VIEW'},
            'wiki:PrivatePage@*': {'john': 'WIKI_VIEW', '*': '!WIKI_VIEW'},
           }

        The `filename` parameter is optional, and if omitted a filename will
        be generated by computing a hash of `authz_content`, prefixed with
        "authz-".
        """
        if not ConfigObj:
            raise ImportError("Can't enable authz permissions policy. " +
                              "ConfigObj not installed.")
        if filename is None:
            from hashlib import md5
            filename = 'authz-' + md5(str(authz_content)).hexdigest()[0:9]
        env = self.get_trac_environment()
        permission_policies = env.config.get('trac', 'permission_policies')
        env.config.set('trac', 'permission_policies',
                       'AuthzPolicy, ' + permission_policies)
        authz_file = os.path.join(env.conf_dir, filename)
        if isinstance(authz_content, basestring):
            authz_content = [
                line.strip() for line in authz_content.strip().splitlines()
            ]
        authz_config = ConfigObj(authz_content,
                                 encoding='utf8',
                                 write_empty_values=True,
                                 indent_type='')
        authz_config.filename = authz_file
        wait_for_file_mtime_change(authz_file)
        authz_config.write()
        env.config.set('authz_policy', 'authz_file', authz_file)
        env.config.set('components', 'tracopt.perm.authz_policy.*', 'enabled')
        env.config.save()
Esempio n. 6
0
    def enable_authz_permpolicy(self, authz_content, filename=None):
        """Enables the Authz permissions policy. The `authz_content` will
        be written to `filename`, and may be specified in a triple-quoted
        string.::

           [wiki:WikiStart@*]
           * = WIKI_VIEW
           [wiki:PrivatePage@*]
           john = WIKI_VIEW
           * = !WIKI_VIEW

        `authz_content` may also be a dictionary of dictionaries specifying
        the sections and key/value pairs of each section, however this form
        should only be used when the order of the entries in the file is not
        important, as the order cannot be known.::

           {
            'wiki:WikiStart@*': {'*': 'WIKI_VIEW'},
            'wiki:PrivatePage@*': {'john': 'WIKI_VIEW', '*': '!WIKI_VIEW'},
           }

        The `filename` parameter is optional, and if omitted a filename will
        be generated by computing a hash of `authz_content`, prefixed with
        "authz-".
        """
        if not ConfigObj:
            raise ImportError("Can't enable authz permissions policy. " +
                              "ConfigObj not installed.")
        if filename is None:
            from hashlib import md5
            filename = 'authz-' + md5(str(authz_content)).hexdigest()[0:9]
        env = self.get_trac_environment()
        permission_policies = env.config.get('trac', 'permission_policies')
        env.config.set('trac', 'permission_policies',
                       'AuthzPolicy, ' + permission_policies)
        authz_file = self.tracdir + '/conf/' + filename
        if isinstance(authz_content, basestring):
            authz_content = [line.strip() for line in
                             authz_content.strip().splitlines()]
        authz_config = ConfigObj(authz_content, encoding='utf8',
                                 write_empty_values=True, indent_type='')
        authz_config.filename = authz_file
        wait_for_file_mtime_change(authz_file)
        authz_config.write()
        env.config.set('authz_policy', 'authz_file', authz_file)
        env.config.set('components', 'tracopt.perm.authz_policy.*', 'enabled')
        env.config.save()
Esempio n. 7
0
    def save(self):
        """Write the configuration options to the primary file."""
        if not self.filename:
            return

        # Only save options that differ from the defaults
        sections = []
        for section in self.sections():
            section_str = _to_utf8(section)
            options = []
            for option in self[section]:
                default_str = None
                for parent in self.parents:
                    if parent.has_option(section, option, defaults=False):
                        default_str = _to_utf8(parent.get(section, option))
                        break
                option_str = _to_utf8(option)
                current_str = False
                if self.parser.has_option(section_str, option_str):
                    current_str = self.parser.get(section_str, option_str)
                if current_str is not False and current_str != default_str:
                    options.append((option_str, current_str))
            if options:
                sections.append((section_str, sorted(options)))

        # At this point, all the strings in `sections` are UTF-8 encoded `str`
        try:
            wait_for_file_mtime_change(self.filename)
            with AtomicFile(self.filename, 'w') as fileobj:
                fileobj.write('# -*- coding: utf-8 -*-\n\n')
                for section_str, options in sections:
                    fileobj.write('[%s]\n' % section_str)
                    section = to_unicode(section_str)
                    for key_str, val_str in options:
                        if to_unicode(key_str) in self[section].overridden:
                            fileobj.write('# %s = <inherited>\n' % key_str)
                        else:
                            val_str = val_str.replace(CRLF, '\n') \
                                             .replace('\n', '\n ')
                            fileobj.write('%s = %s\n' % (key_str, val_str))
                    fileobj.write('\n')
            self._old_sections = deepcopy(self.parser._sections)
        except Exception:
            # Revert all changes to avoid inconsistencies
            self.parser._sections = deepcopy(self._old_sections)
            raise
Esempio n. 8
0
    def save(self):
        """Write the configuration options to the primary file."""
        if not self.filename:
            return

        # Only save options that differ from the defaults
        sections = []
        for section in self.sections():
            section_str = _to_utf8(section)
            options = []
            for option in self[section]:
                default_str = None
                for parent in self.parents:
                    if parent.has_option(section, option, defaults=False):
                        default_str = _to_utf8(parent.get(section, option))
                        break
                option_str = _to_utf8(option)
                current_str = False
                if self.parser.has_option(section_str, option_str):
                    current_str = self.parser.get(section_str, option_str)
                if current_str is not False and current_str != default_str:
                    options.append((option_str, current_str))
            if options:
                sections.append((section_str, sorted(options)))

        # At this point, all the strings in `sections` are UTF-8 encoded `str`
        try:
            wait_for_file_mtime_change(self.filename)
            with AtomicFile(self.filename, 'w') as fileobj:
                fileobj.write('# -*- coding: utf-8 -*-\n\n')
                for section_str, options in sections:
                    fileobj.write('[%s]\n' % section_str)
                    section = to_unicode(section_str)
                    for key_str, val_str in options:
                        if to_unicode(key_str) in self[section].overridden:
                            fileobj.write('# %s = <inherited>\n' % key_str)
                        else:
                            val_str = val_str.replace(CRLF, '\n') \
                                             .replace('\n', '\n ')
                            fileobj.write('%s = %s\n' % (key_str, val_str))
                    fileobj.write('\n')
            self._old_sections = deepcopy(self.parser._sections)
        except Exception:
            # Revert all changes to avoid inconsistencies
            self.parser._sections = deepcopy(self._old_sections)
            raise
Esempio n. 9
0
 def touch(self):
     if self.filename and os.path.isfile(self.filename) \
             and os.access(self.filename, os.W_OK):
         wait_for_file_mtime_change(self.filename)
         os.utime(self.filename, None)
Esempio n. 10
0
 def _write(self, lines, site=False):
     filename = self.sitename if site else self.filename
     wait_for_file_mtime_change(filename)
     with open(filename, 'w') as fileobj:
         fileobj.write(('\n'.join(lines + [''])).encode('utf-8'))
Esempio n. 11
0
def _write(filename, lines):
    wait_for_file_mtime_change(filename)
    create_file(filename, '\n'.join(lines + ['']).encode('utf-8'))
Esempio n. 12
0
 def save(self):
     """Write the htpasswd file to disk"""
     wait_for_file_mtime_change(self.filename)
     open(self.filename, 'w').writelines(["%s:%s\n" % (entry[0], entry[1])
                                          for entry in self.entries])
Esempio n. 13
0
 def touch(self):
     if self.filename and os.path.isfile(self.filename) \
             and os.access(self.filename, os.W_OK):
         wait_for_file_mtime_change(self.filename)
Esempio n. 14
0
def _write(filename, lines):
    wait_for_file_mtime_change(filename)
    create_file(filename, '\n'.join(lines + ['']).encode('utf-8'))
Esempio n. 15
0
 def _write(self, lines, site=False):
     filename = self.sitename if site else self.filename
     wait_for_file_mtime_change(filename)
     with open(filename, 'w') as fileobj:
         fileobj.write(('\n'.join(lines + [''])).encode('utf-8'))
Esempio n. 16
0
 def _write(self, content):
     if not self.filename:
         return
     wait_for_file_mtime_change(self.filename)
     with AtomicFile(self.filename, 'w') as fileobj:
         fileobj.writelines(content)
Esempio n. 17
0
 def save(self):
     """Write the htpasswd file to disk"""
     wait_for_file_mtime_change(self.filename)
     with open(self.filename, 'w') as f:
         f.writelines("%s:%s\n" % (entry[0], entry[1])
                      for entry in self.entries)
Esempio n. 18
0
File: config.py Progetto: zxfly/trac
 def touch(self):
     if self.filename and self.exists \
             and os.access(self.filename, os.W_OK):
         wait_for_file_mtime_change(self.filename)