def _do_deploy(self, dest): target = os.path.normpath(dest) chrome_target = os.path.join(target, "htdocs") script_target = os.path.join(target, "cgi-bin") # Copy static content makedirs(target, overwrite=True) makedirs(chrome_target, overwrite=True) from trac.web.chrome import Chrome printout(_("Copying resources from:")) for provider in Chrome(self.env).template_providers: paths = list(provider.get_htdocs_dirs() or []) if not len(paths): continue printout(" %s.%s" % (provider.__module__, provider.__class__.__name__)) for key, root in paths: if not root: continue source = os.path.normpath(root) printout(" ", source) if os.path.exists(source): dest = os.path.join(chrome_target, key) copytree(source, dest, overwrite=True) # Create and copy scripts makedirs(script_target, overwrite=True) printout(_("Creating scripts.")) data = {"env": self.env, "executable": sys.executable} for script in ("cgi", "fcgi", "wsgi"): dest = os.path.join(script_target, "trac." + script) template = Chrome(self.env).load_template("deploy_trac." + script, "text") stream = template.generate(**data) with open(dest, "w") as out: stream.render("text", out=out, encoding="utf-8")
def _do_hotcopy(self, dest, no_db=None): if no_db not in (None, '--no-database'): raise AdminCommandError(_("Invalid argument '%(arg)s'", arg=no_db), show_usage=True) if os.path.exists(dest): raise TracError( _("hotcopy can't overwrite existing '%(dest)s'", dest=path_to_unicode(dest))) printout( _("Hotcopying %(src)s to %(dst)s ...", src=path_to_unicode(self.env.path), dst=path_to_unicode(dest))) db_str = self.env.config.get('trac', 'database') prefix, db_path = db_str.split(':', 1) skip = [] if prefix == 'sqlite': db_path = os.path.join(self.env.path, os.path.normpath(db_path)) # don't copy the journal (also, this would fail on Windows) skip = [ db_path + '-journal', db_path + '-stmtjrnl', db_path + '-shm', db_path + '-wal' ] if no_db: skip.append(db_path) # Bogus statement to lock the database while copying files with self.env.db_transaction as db: db("UPDATE " + db.quote('system') + " SET name=NULL WHERE name IS NULL") try: copytree(self.env.path, dest, symlinks=1, skip=skip) except shutil.Error as e: retval = 1 printerr( _("The following errors happened while copying " "the environment:")) for src, dst, err in e.args[0]: if src in err: printerr(' %s' % err) else: printerr(" %s: '%s'" % (err, path_to_unicode(src))) else: retval = 0 # db backup for non-sqlite if prefix != 'sqlite' and not no_db: printout(_("Backing up database ...")) sql_backup = os.path.join(dest, 'db', '%s-db-backup.sql' % prefix) self.env.backup(sql_backup) printout(_("Hotcopy done.")) return retval
def _do_deploy(self, dest): target = os.path.normpath(dest) chrome_target = os.path.join(target, 'htdocs') script_target = os.path.join(target, 'cgi-bin') # Check source and destination to avoid recursively copying files for provider in Chrome(self.env).template_providers: paths = list(provider.get_htdocs_dirs() or []) if not paths: continue for key, root in paths: if not root: continue source = os.path.normpath(root) dest = os.path.join(chrome_target, key) if os.path.exists(source) and is_path_below(dest, source): raise AdminCommandError( _( "Resources cannot be deployed to a target " "directory that is equal to or below the source " "directory '%(source)s'.\n\nPlease choose a " "different target directory and try again.", source=source)) # Copy static content makedirs(target, overwrite=True) makedirs(chrome_target, overwrite=True) printout(_("Copying resources from:")) for provider in Chrome(self.env).template_providers: paths = list(provider.get_htdocs_dirs() or []) if not paths: continue printout(' %s.%s' % (provider.__module__, provider.__class__.__name__)) for key, root in paths: if not root: continue source = os.path.normpath(root) printout(' ', source) if os.path.exists(source): dest = os.path.join(chrome_target, key) copytree(source, dest, overwrite=True) # Create and copy scripts makedirs(script_target, overwrite=True) printout(_("Creating scripts.")) data = {'env': self.env, 'executable': sys.executable, 'repr': repr} for script in ('cgi', 'fcgi', 'wsgi'): dest = os.path.join(script_target, 'trac.' + script) chrome = Chrome(self.env) template = chrome.load_template('deploy_trac.' + script, text=True) text = chrome.render_template_string(template, data, text=True) with open(dest, 'w') as out: out.write(text.encode('utf-8'))
def _do_hotcopy(self, dest, no_db=None): if no_db not in (None, '--no-database'): raise AdminCommandError(_("Invalid argument '%(arg)s'", arg=no_db), show_usage=True) if os.path.exists(dest): raise TracError(_("hotcopy can't overwrite existing '%(dest)s'", dest=path_to_unicode(dest))) # Bogus statement to lock the database while copying files with self.env.db_transaction as db: db("UPDATE system SET name=NULL WHERE name IS NULL") printout(_("Hotcopying %(src)s to %(dst)s ...", src=path_to_unicode(self.env.path), dst=path_to_unicode(dest))) db_str = self.env.config.get('trac', 'database') prefix, db_path = db_str.split(':', 1) skip = [] if prefix == 'sqlite': db_path = os.path.join(self.env.path, os.path.normpath(db_path)) # don't copy the journal (also, this would fail on Windows) skip = [db_path + '-journal', db_path + '-stmtjrnl', db_path + '-shm', db_path + '-wal'] if no_db: skip.append(db_path) try: copytree(self.env.path, dest, symlinks=1, skip=skip) retval = 0 except shutil.Error as e: retval = 1 printerr(_("The following errors happened while copying " "the environment:")) for (src, dst, err) in e.args[0]: if src in err: printerr(' %s' % err) else: printerr(" %s: '%s'" % (err, path_to_unicode(src))) # db backup for non-sqlite if prefix != 'sqlite' and not no_db: printout(_("Backing up database ...")) sql_backup = os.path.join(dest, 'db', '%s-db-backup.sql' % prefix) self.env.backup(sql_backup) printout(_("Hotcopy done.")) return retval
def _do_hotcopy(self, dest): if os.path.exists(dest): raise TracError( _("hotcopy can't overwrite existing '%(dest)s'", dest=dest)) import shutil # Bogus statement to lock the database while copying files cnx = self.env.get_db_cnx() cursor = cnx.cursor() cursor.execute("UPDATE system SET name=NULL WHERE name IS NULL") try: printout( _('Hotcopying %(src)s to %(dst)s ...', src=self.env.path, dst=dest)) db_str = self.env.config.get('trac', 'database') prefix, db_path = db_str.split(':', 1) if prefix == 'sqlite': # don't copy the journal (also, this would fail on Windows) db = os.path.join(self.env.path, os.path.normpath(db_path)) skip = [db + '-journal', db + '-stmtjrnl'] else: skip = [] try: copytree(self.env.path, dest, symlinks=1, skip=skip) retval = 0 except shutil.Error, e: retval = 1 printerr( _('The following errors happened while copying ' 'the environment:')) for (src, dst, err) in e.args[0]: if src in err: printerr(' %s' % err) else: printerr(" %s: '%s'" % (err, src)) finally: # Unlock database cnx.rollback() printout(_("Hotcopy done.")) return retval
def _do_hotcopy(self, dest): if os.path.exists(dest): raise TracError(_("hotcopy can't overwrite existing '%(dest)s'", dest=dest)) import shutil # Bogus statement to lock the database while copying files cnx = self.env.get_db_cnx() cursor = cnx.cursor() cursor.execute("UPDATE system SET name=NULL WHERE name IS NULL") try: printout(_('Hotcopying %(src)s to %(dst)s ...', src=self.env.path, dst=dest)) db_str = self.env.config.get('trac', 'database') prefix, db_path = db_str.split(':', 1) if prefix == 'sqlite': # don't copy the journal (also, this would fail on Windows) db = os.path.join(self.env.path, os.path.normpath(db_path)) skip = [db + '-journal', db + '-stmtjrnl'] else: skip = [] try: copytree(self.env.path, dest, symlinks=1, skip=skip) retval = 0 except shutil.Error, e: retval = 1 printerr(_('The following errors happened while copying ' 'the environment:')) for (src, dst, err) in e.args[0]: if src in err: printerr(' %s' % err) else: printerr(" %s: '%s'" % (err, src)) finally: # Unlock database cnx.rollback() printout(_("Hotcopy done.")) return retval
def _do_deploy(self, dest): target = os.path.normpath(dest) chrome_target = os.path.join(target, 'htdocs') script_target = os.path.join(target, 'cgi-bin') # Copy static content makedirs(target, overwrite=True) makedirs(chrome_target, overwrite=True) from trac.web.chrome import Chrome printout(_("Copying resources from:")) for provider in Chrome(self.env).template_providers: paths = list(provider.get_htdocs_dirs() or []) if not len(paths): continue printout(' %s.%s' % (provider.__module__, provider.__class__.__name__)) for key, root in paths: source = os.path.normpath(root) printout(' ', source) if os.path.exists(source): dest = os.path.join(chrome_target, key) copytree(source, dest, overwrite=True) # Create and copy scripts makedirs(script_target, overwrite=True) printout(_("Creating scripts.")) data = {'env': self.env, 'executable': sys.executable} for script in ('cgi', 'fcgi', 'wsgi'): dest = os.path.join(script_target, 'trac.' + script) template = Chrome(self.env).load_template('deploy_trac.' + script, 'text') stream = template.generate(**data) out = file(dest, 'w') try: stream.render('text', out=out) finally: out.close()
def deploy_htdocs(env, dest=None, config_file=None, also_common=False, path=None): """ :param Environment env: Environment instance, may be home or normal project. :param str dest: Destination where to put files. Defaults to [multiproject] static_htdocs_path. """ keys = set() # construct list of enabled pugins in home project and normal project enabled_plugins = [] project_ini = env.config.get('multiproject', 'global_conf_path') home_ini = os.path.join(env.config.get('multiproject', 'sys_projects_root'), env.config.get('multiproject', 'sys_home_project_name'), 'conf', 'trac.ini') enabled_plugins.extend(_get_enabled_components(project_ini)) enabled_plugins.extend(_get_enabled_components(home_ini)) env = MockEnvironment(config_file, enabled_plugins, path) if dest is None: dest = env.config.get('multiproject', 'static_htdocs_path', default=None) if dest is None: raise AdminCommandError('Destination not given and ' '[multiproject] static_htdocs_path configuration is not set') chrome_target = dest # A slightly edited snippet from trac.env.EnvironmentAdmin._do_deploy chrome = Chrome(env) os.path.normpath(dest) for provider in chrome.template_providers: paths = list(provider.get_htdocs_dirs() or []) if not len(paths): continue for key, root in paths: if key == 'site': continue if key == 'common' and not also_common: continue keys.add(key) source = os.path.normpath(root) if os.path.exists(source): dest = os.path.join(chrome_target, key) copytree(source, dest, overwrite=True) printout(' Static htdocs deployed to directory %s ' % chrome_target) dirs = sorted([str(dir) for dir in env.config.getlist('multiproject', 'static_htdocs_plugin_dirs', default=['*'])]) if len(dirs) == 1 and dirs[0] == '*': pass elif dirs and set(dirs) != keys: printout(' Warning: [multiproject] static_htdocs_plugin_dirs is not up-to-date!') extra_dirs = [dir for dir in dirs if dir not in keys] if extra_dirs: printout(' It contains the following extra directories, which should be removed:') printout(' %s' % extra_dirs) extra_keys = [key for key in keys if key not in dirs] if extra_keys: printout(' The urls of the htdocs of the following plugins are not changed ') printout(' to use [multiproject] static_htdocs_location:') printout(' %s' % sorted(extra_keys)) printout(' To fix these errors, change the configuration to be as follows:') printout(' "static_htdocs_plugin_dirs = %s"' % ','.join(sorted(list(keys)))) elif not dirs: printout(' To use the static htdocs of the global plugins as static files, ') printout(' change the configuration to be as follows:') printout(' "static_htdocs_plugin_dirs = %s"' % ','.join(sorted(list(keys))))
def deploy_htdocs(env, dest=None, config_file=None, also_common=False, path=None): """ :param Environment env: Environment instance, may be home or normal project. :param str dest: Destination where to put files. Defaults to [multiproject] static_htdocs_path. """ keys = set() # construct list of enabled pugins in home project and normal project enabled_plugins = [] project_ini = env.config.get('multiproject', 'global_conf_path') home_ini = os.path.join( env.config.get('multiproject', 'sys_projects_root'), env.config.get('multiproject', 'sys_home_project_name'), 'conf', 'trac.ini') enabled_plugins.extend(_get_enabled_components(project_ini)) enabled_plugins.extend(_get_enabled_components(home_ini)) env = MockEnvironment(config_file, enabled_plugins, path) if dest is None: dest = env.config.get('multiproject', 'static_htdocs_path', default=None) if dest is None: raise AdminCommandError( 'Destination not given and ' '[multiproject] static_htdocs_path configuration is not set') chrome_target = dest # A slightly edited snippet from trac.env.EnvironmentAdmin._do_deploy chrome = Chrome(env) os.path.normpath(dest) for provider in chrome.template_providers: paths = list(provider.get_htdocs_dirs() or []) if not len(paths): continue for key, root in paths: if key == 'site': continue if key == 'common' and not also_common: continue keys.add(key) source = os.path.normpath(root) if os.path.exists(source): dest = os.path.join(chrome_target, key) copytree(source, dest, overwrite=True) printout(' Static htdocs deployed to directory %s ' % chrome_target) dirs = sorted([ str(dir) for dir in env.config.getlist( 'multiproject', 'static_htdocs_plugin_dirs', default=['*']) ]) if len(dirs) == 1 and dirs[0] == '*': pass elif dirs and set(dirs) != keys: printout( ' Warning: [multiproject] static_htdocs_plugin_dirs is not up-to-date!' ) extra_dirs = [dir for dir in dirs if dir not in keys] if extra_dirs: printout( ' It contains the following extra directories, which should be removed:' ) printout(' %s' % extra_dirs) extra_keys = [key for key in keys if key not in dirs] if extra_keys: printout( ' The urls of the htdocs of the following plugins are not changed ' ) printout(' to use [multiproject] static_htdocs_location:') printout(' %s' % sorted(extra_keys)) printout( ' To fix these errors, change the configuration to be as follows:' ) printout(' "static_htdocs_plugin_dirs = %s"' % ','.join(sorted(list(keys)))) elif not dirs: printout( ' To use the static htdocs of the global plugins as static files, ' ) printout(' change the configuration to be as follows:') printout(' "static_htdocs_plugin_dirs = %s"' % ','.join(sorted(list(keys))))