コード例 #1
0
ファイル: web.py プロジェクト: nyuhuhuu/trachacks
    def data(self, project):
        """data needed for template rendering"""
        project_name = project['vars']['project']
        repositories = [ self.view.repositories[name] for name in self.view.available_repositories ]
        excluded_fields = dict((key, value.keys()) for key, value in self.view.legos.repository_fields(project_name).items())
        for name in self.view.available_repositories:
            excluded_fields.setdefault(name, []).extend(project['vars'].keys())
        data = {'project': project_name,
                'repositories': repositories,
                'excluded_fields': excluded_fields,
                'databases': [ self.view.databases[name] for name in self.view.available_databases ] } 

        # get the database strings
        data['db_string'] = {}
        for database in data['databases']:
            dbstring = database.db_string()
            dbstring = string.Template(dbstring).safe_substitute(**project['vars'])
            template = PasteScriptStringTemplate(dbstring)
            missing = template.missing()
            if missing:
                vars = vars2dict(None, *database.options)
                missing = dict([(i, 
                                 '<input type="text" name="%s-%s" value="%s"/>' % (database.name, i, getattr(vars.get(i), 'default', '')))
                                for i in missing])
                dbstring = string.Template(dbstring).substitute(**missing)
                dbstring = Markup(dbstring)
            data['db_string'][database.name] = dbstring
        return data
コード例 #2
0
ファイル: web.py プロジェクト: pombredanne/trachacks
    def data(self, project):
        """data needed for template rendering"""
        project_name = project['vars']['project']
        repositories = [
            self.view.repositories[name]
            for name in self.view.available_repositories
        ]
        excluded_fields = dict(
            (key, value.keys())
            for key, value in self.view.legos.repository_fields(
                project_name).items())
        for name in self.view.available_repositories:
            excluded_fields.setdefault(name, []).extend(project['vars'].keys())
        data = {
            'project':
            project_name,
            'repositories':
            repositories,
            'excluded_fields':
            excluded_fields,
            'databases': [
                self.view.databases[name]
                for name in self.view.available_databases
            ]
        }

        # get the database strings
        data['db_string'] = {}
        for database in data['databases']:
            dbstring = database.db_string()
            dbstring = string.Template(dbstring).safe_substitute(
                **project['vars'])
            template = PasteScriptStringTemplate(dbstring)
            missing = template.missing()
            if missing:
                vars = vars2dict(None, *database.options)
                missing = dict([
                    (i, '<input type="text" name="%s-%s" value="%s"/>' %
                     (database.name, i, getattr(vars.get(i), 'default', '')))
                    for i in missing
                ])
                dbstring = string.Template(dbstring).substitute(**missing)
                dbstring = Markup(dbstring)
            data['db_string'][database.name] = dbstring
        return data
コード例 #3
0
    def create_project(self,
                       project,
                       templates,
                       vars=None,
                       database=None,
                       repository=None,
                       return_missing=False):
        """
        * project: path name of project
        * templates: templates to be applied on project creation
        * vars: variables for interpolation
        * database:  type of database to use
        * repository: type of repository to use
        """

        ### set variables

        dirname = os.path.join(self.directory, project)

        if os.path.isdir(dirname) and os.listdir(dirname):
            raise ValueError("Project directory %r already exists, "
                             "cowardly refusing to create anything" % dirname)

        _vars = vars or {}
        vars = self.vars.copy()
        vars.update(_vars)
        vars['project'] = project
        permissions = dict([(key, value[:])
                            for key, value in self.permissions.items()])
        wiki = self.wiki[:]

        ### munge configuration

        # get templates

        # XXX hack to get the specified DB out of pastescript templates
        if not database:
            if isinstance(templates, ProjectTemplates):
                pastescript_templates = templates.pastescript_templates
            else:
                pastescript_templates = ProjectTemplates(
                    *(templates + self.site_templates)).pastescript_templates
            databases = set([
                template.database() for template in pastescript_templates
                if template.db is not None
            ])
            if databases:
                assert len(databases) == 1
                database = databases.pop()
        if not database:
            database = SQLite()

        _templates = []
        _templates.append(database.config())
        if repository:
            _templates.append(repository.config())

        if isinstance(templates, ProjectTemplates):
            if _templates:
                templates.append(*_templates)
        else:
            templates += _templates
            templates = self.project_templates(templates)

        # determine the vars/options
        optdict = templates.vars(self.options)
        repo_fields = {}
        if database:
            vars2dict(optdict, *database.options)
        if repository:
            vars2dict(optdict, *repository.options)
            repo_fields = self.repository_fields(project).get(
                repository.name, {})

        ### interpolate configuration

        command = create_distro_command(interactive=self.interactive)

        # check for missing variables
        missing = templates.missing(vars)
        missing.update(set(optdict.keys()).difference(vars.keys()))
        if return_missing:
            return missing
        if missing:

            # use default repo fields if they are missing
            for field in repo_fields:
                if field in missing:
                    vars[field] = repo_fields[field]
                    missing.remove(field)

            # add missing variables to the optdict
            for missed in missing:
                if missed not in optdict:
                    optdict[missed] = var(missed, '')

            if missing:
                paste_template = Template(project)
                paste_template._read_vars = dict2vars(optdict)  # XXX bad touch
                paste_template.check_vars(vars, command)

        # run the pre method of the pastescript templates
        # XXX should this be done here?
        command.interactive = False
        for paste_template in templates.pastescript_templates:
            paste_template.pre(command, dirname, vars)

        ### create the database
        if database:
            database.setup(**vars)

        ### create the trac environment
        options = templates.options_tuples(vars)
        options.append(('project', 'name', project))  # XXX needed?
        if self.inherit:
            options.append(('inherit', 'file', self.inherit))
        env = Environment(dirname, create=True, options=options)

        ### repository setup
        if repository:
            repository.setup(**vars)
            try:
                repos = env.get_repository()
                repos.sync()
            except TracError:
                pass

        ### read the generated configuration
        _conf_file = os.path.join(dirname, 'conf', 'trac.ini')
        fp = file(_conf_file)
        _conf = fp.read()
        fp.close()

        ### run pastescript templates
        for paste_template in templates.pastescript_templates:
            paste_template.write_files(command, dirname, vars)
            paste_template.post(command, dirname, vars)

            # read permissions
            for agent, perm in paste_template.permissions.items():
                permissions.setdefault(agent, []).extend(perm)

            # read wiki directories
            wiki_dir = paste_template.wiki_dir()
            if wiki_dir is not None:
                wiki.append(wiki_dir)

        # write back munged configuration
        munger = ConfigMunger(_conf, options)
        fp = file(_conf_file, 'w')
        munger.write(fp)
        fp.close()

        # TODO: update the inherited file:
        # * intertrac

        # trac-admin upgrade the project
        env = Environment(dirname)
        if env.needs_upgrade():
            env.upgrade()

        ### trac-admin operations
        admin = TracLegosAdmin(dirname)

        # remove the default items
        admin.delete_all()

        # load wiki pages
        admin.load_pages()  # default wiki pages
        for page_dir in wiki:
            admin.load_pages(page_dir)

        # add permissions
        if permissions:
            admin.add_permissions(permissions)
コード例 #4
0
ファイル: legos.py プロジェクト: nyuhuhuu/trachacks
    def create_project(self, project, templates, vars=None,
                       database=None, repository=None,
                       return_missing=False):
        """
        * project: path name of project
        * templates: templates to be applied on project creation
        * vars: variables for interpolation
        * database:  type of database to use
        * repository: type of repository to use
        """
        
        ### set variables

        dirname = os.path.join(self.directory, project)
        
        if os.path.isdir(dirname) and os.listdir(dirname):
            raise ValueError("Project directory %r already exists, "
                             "cowardly refusing to create anything" % dirname)

        _vars = vars or {}
        vars = self.vars.copy()
        vars.update(_vars)
        vars['project'] = project        
        permissions = dict([(key, value[:]) 
                            for key, value in self.permissions.items()])
        wiki = self.wiki[:]

        ### munge configuration

        # get templates

        # XXX hack to get the specified DB out of pastescript templates
        if not database:
            if isinstance(templates, ProjectTemplates):
                pastescript_templates = templates.pastescript_templates
            else:
                pastescript_templates = ProjectTemplates(*(templates + self.site_templates)).pastescript_templates
            databases = set([ template.database() for template in pastescript_templates
                              if template.db is not None])
            if databases:
                assert len(databases) == 1
                database = databases.pop()
        if not database:
            database = SQLite()

        _templates = []
        _templates.append(database.config())
        if repository:
            _templates.append(repository.config())

        if isinstance(templates, ProjectTemplates):
            if _templates:
                templates.append(*_templates)
        else:
            templates += _templates
            templates = self.project_templates(templates)

        # determine the vars/options
        optdict = templates.vars(self.options)
        repo_fields = {}
        if database:
            vars2dict(optdict, *database.options)
        if repository:
            vars2dict(optdict, *repository.options)
            repo_fields = self.repository_fields(project).get(repository.name, {})

        ### interpolate configuration

        command = create_distro_command(interactive=self.interactive)
        
        # check for missing variables
        missing = templates.missing(vars)
        missing.update(set(optdict.keys()).difference(vars.keys()))
        if return_missing:
            return missing
        if missing:

            # use default repo fields if they are missing
            for field in repo_fields:
                if field in missing:
                    vars[field] = repo_fields[field]
                    missing.remove(field)

            # add missing variables to the optdict
            for missed in missing:
                if missed not in optdict:
                    optdict[missed] = var(missed, '')

            if missing:
                paste_template = Template(project)
                paste_template._read_vars = dict2vars(optdict) # XXX bad touch
                paste_template.check_vars(vars, command)

        # run the pre method of the pastescript templates
        # XXX should this be done here?
        command.interactive = False
        for paste_template in templates.pastescript_templates:
            paste_template.pre(command, dirname, vars)

        ### create the database
        if database:
            database.setup(**vars)
        
        ### create the trac environment
        options = templates.options_tuples(vars)
        options.append(('project', 'name', project)) # XXX needed?
        if self.inherit:
            options.append(('inherit', 'file', self.inherit))
        env = Environment(dirname, create=True, options=options)

        ### repository setup
        if repository:
            repository.setup(**vars)
            try:
                repos = env.get_repository()
                repos.sync()
            except TracError:
                pass

        ### read the generated configuration 
        _conf_file = os.path.join(dirname, 'conf', 'trac.ini')
        fp = file(_conf_file)
        _conf = fp.read()
        fp.close()

        ### run pastescript templates
        for paste_template in templates.pastescript_templates:
            paste_template.write_files(command, dirname, vars)
            paste_template.post(command, dirname, vars)

            # read permissions
            for agent, perm in paste_template.permissions.items():
                permissions.setdefault(agent, []).extend(perm)

            # read wiki directories
            wiki_dir = paste_template.wiki_dir()
            if wiki_dir is not None:
                wiki.append(wiki_dir)

        # write back munged configuration 
        munger = ConfigMunger(_conf, options)
        fp = file(_conf_file, 'w')
        munger.write(fp)
        fp.close()

        # TODO: update the inherited file:
        # * intertrac

        # trac-admin upgrade the project
        env = Environment(dirname)
        if env.needs_upgrade():
            env.upgrade()

        ### trac-admin operations
        admin = TracLegosAdmin(dirname)

        # remove the default items
        admin.delete_all()

        # load wiki pages
        admin.load_pages() # default wiki pages
        for page_dir in wiki:
            admin.load_pages(page_dir)

        # add permissions
        if permissions:
            admin.add_permissions(permissions)