def transition(self, project, input): """create the project from TTW input""" # add input (form) variables to the project variables project['vars'].update(input) # don't add the repository directory here so that we can # sync asynchronously # XXX hack repository_dir = project['vars'].get('repository_dir') project['vars']['repository_dir'] = '' # create the project self.view.legos.create_project(project['vars']['project'], self.templates(project), project['vars'], database=project['database'], repository=project['repository']) project_dir = os.path.join(self.view.directory, project['vars']['project']) # write the logo_file to its new location logo_file = project['logo_file'] if logo_file: logo_file_name = os.path.basename(project['vars']['logo']) filename = os.path.join(project_dir, 'htdocs', logo_file_name) logo = file(filename, 'wb') logo.write(logo_file.read()) logo.close() # TODO: favicons from logo or alternate url # TODO: add authenticated user to TRAC_ADMIN of the new site # (and redirect to the admin panel?) # XXX hack for now # for admin in project['admins']: # subprocess.call(['trac-admin', project_dir, 'permission', 'add', admin, 'TRAC_ADMIN']) # XXX hack to sync the repository asynchronously if repository_dir: ini = os.path.join(project_dir, 'conf', 'trac.ini') conf = ConfigMunger(ini, {'trac': { 'repository_dir': repository_dir }}) f = file(ini, 'w') conf.write(f) subprocess.Popen(['trac-admin', project_dir, 'resync'])
def site_configuration(*ini_files): """returns a dictionary of configuration from .ini files""" conf = ConfigMunger(*ini_files).dict() for section in sections: if section not in conf: conf[section] = {} return conf
def transition(self, project, input): """create the project from TTW input""" # add input (form) variables to the project variables project['vars'].update(input) # don't add the repository directory here so that we can # sync asynchronously # XXX hack repository_dir = project['vars'].get('repository_dir') project['vars']['repository_dir'] = '' # create the project self.view.legos.create_project(project['vars']['project'], self.templates(project), project['vars'], database=project['database'], repository=project['repository']) project_dir = os.path.join(self.view.directory, project['vars']['project']) # write the logo_file to its new location logo_file = project['logo_file'] if logo_file: logo_file_name = os.path.basename(project['vars']['logo']) filename = os.path.join(project_dir, 'htdocs', logo_file_name) logo = file(filename, 'wb') logo.write(logo_file.read()) logo.close() # TODO: favicons from logo or alternate url # TODO: add authenticated user to TRAC_ADMIN of the new site # (and redirect to the admin panel?) # XXX hack for now # for admin in project['admins']: # subprocess.call(['trac-admin', project_dir, 'permission', 'add', admin, 'TRAC_ADMIN']) # XXX hack to sync the repository asynchronously if repository_dir: ini = os.path.join(project_dir, 'conf', 'trac.ini') conf = ConfigMunger(ini, { 'trac': {'repository_dir': repository_dir}}) f = file(ini, 'w') conf.write(f) subprocess.Popen(['trac-admin', project_dir, 'resync'])
def apply_permissions(envs, ini): munger = ConfigMunger(*ini) if munger.has_section('permissions'): add_permissions = dict([(i, getlist(j)) for i, j in munger['permissions'].items()]) else: add_permissions = None if munger.has_section('remove-permissions'): remove_permissions = dict([(i, getlist(j)) for i, j in munger['remove-permissions'].items()]) else: remove_permissions = None for env in envs: admin = TracLegosAdmin(env) if add_permissions: admin.add_permissions(add_permissions) if remove_permissions: admin.remove_permissions(remove_permissions)
def apply_permissions(envs, ini): munger = ConfigMunger(*ini) if munger.has_section('permissions'): add_permissions = dict([(i, getlist(j)) for i, j in munger['permissions'].items()]) else: add_permissions = None if munger.has_section('remove-permissions'): remove_permissions = dict([ (i, getlist(j)) for i, j in munger['remove-permissions'].items() ]) else: remove_permissions = None for env in envs: admin = TracLegosAdmin(env) if add_permissions: admin.add_permissions(add_permissions) if remove_permissions: admin.remove_permissions(remove_permissions)
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)
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)