Ejemplo n.º 1
0
    def __init__(self, string, name=None, summary='', interactive=False, 
                 vars=None):

        self.string = string
        self.cmd = create_distro_command(interactive)

        # make a temporary directory + file for the string
        directory = tempfile.mkdtemp()
        if not name:
            name = tempfile.mktemp(dir=directory)
        fd = file(os.path.join(directory, name), 'w')
        print >> fd, string
        fd.close()
        self.name = name

        # variables for PasteScript's template
        Template.__init__(self, name)
        self._template_dir = directory
        self.summary = summary
        self.vars = vars or []
Ejemplo n.º 2
0
    def __init__(self,
                 string,
                 name=None,
                 summary='',
                 interactive=False,
                 vars=None):

        self.string = string
        self.cmd = create_distro_command(interactive)

        # make a temporary directory + file for the string
        directory = tempfile.mkdtemp()
        if not name:
            name = tempfile.mktemp(dir=directory)
        fd = file(os.path.join(directory, name), 'w')
        print >> fd, string
        fd.close()
        self.name = name

        # variables for PasteScript's template
        Template.__init__(self, name)
        self._template_dir = directory
        self.summary = summary
        self.vars = vars or []
Ejemplo n.º 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)
Ejemplo n.º 4
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)