def test_is_path_below(self): self.assert_below("/svn/project1", "/svn/project1") self.assert_below("/svn/project1/repos", "/svn/project1") self.assert_below("/svn/project1/sub/repos", "/svn/project1") self.assert_below("/svn/project1/sub/../repos", "/svn/project1") self.assert_not_below("/svn/project2/repos", "/svn/project1") self.assert_not_below("/svn/project2/sub/repos", "/svn/project1") self.assert_not_below("/svn/project1/../project2/repos", "/svn/project1") self.assert_(util.is_path_below("repos", os.path.join(os.getcwd()))) self.assert_(not util.is_path_below("../sub/repos", os.path.join(os.getcwd())))
def test_is_path_below(self): self.assert_below('/svn/project1', '/svn/project1') self.assert_below('/svn/project1/repos', '/svn/project1') self.assert_below('/svn/project1/sub/repos', '/svn/project1') self.assert_below('/svn/project1/sub/../repos', '/svn/project1') self.assert_not_below('/svn/project2/repos', '/svn/project1') self.assert_not_below('/svn/project2/sub/repos', '/svn/project1') self.assert_not_below('/svn/project1/../project2/repos', '/svn/project1') self.assert_(util.is_path_below('repos', os.path.join(os.getcwd()))) self.assert_(not util.is_path_below('../sub/repos', os.path.join(os.getcwd())))
def test_is_path_below(self): self.assert_below('/svn/project1', '/svn/project1') self.assert_below('/svn/project1/repos', '/svn/project1') self.assert_below('/svn/project1/sub/repos', '/svn/project1') self.assert_below('/svn/project1/sub/../repos', '/svn/project1') self.assert_not_below('/svn/project2/repos', '/svn/project1') self.assert_not_below('/svn/project2/sub/repos', '/svn/project1') self.assert_not_below('/svn/project1/../project2/repos', '/svn/project1') self.assert_(util.is_path_below('repos', os.path.join(os.getcwd()))) self.assert_( not util.is_path_below('../sub/repos', os.path.join(os.getcwd())))
def _do_deploy(self, dest): from trac.web.chrome import Chrome 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} 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 _check_dir(self, req, dir): """Check that a repository directory is valid, and add a warning message if not. """ if not os.path.isabs(dir): add_warning(req, _('The repository directory must be an absolute ' 'path.')) return False prefixes = [os.path.join(self.env.path, prefix) for prefix in self.allowed_repository_dir_prefixes] if prefixes and not any(is_path_below(dir, prefix) for prefix in prefixes): add_warning(req, _('The repository directory must be located ' 'below one of the following directories: ' '%(dirs)s', dirs=', '.join(prefixes))) return False return True
def _check_and_update_repository(self, req, repo, old_repo=None): """Check if a repository is valid, does not already exist, update the dict and add a warning message otherwise. """ if not repo['dir']: add_warning(req, _("The directory is missing.")) return False rm = RepositoryManager(self.env) base_directory = rm.get_base_directory(repo['type']) directory = os.path.join(base_directory, repo['dir']) if not old_repo or old_repo.directory != directory: if os.path.lexists(directory): add_warning(req, _('Directory "%(name)s" already exists', name=directory)) return False rap = RepositoryAdminPanel(self.env) prefixes = [os.path.join(self.env.path, prefix) for prefix in rap.allowed_repository_dir_prefixes] if prefixes and not any(is_path_below(directory, prefix) for prefix in prefixes): add_warning(req, _("The repository directory must be located " "below one of the following directories: " "%(dirs)s", dirs=', '.join(prefixes))) return False if not old_repo or old_repo.reponame != repo['name']: if rm.get_repository(repo['name']): add_warning(req, _('Repository "%(name)s" already exists', name=repo['name'])) return False repo.update({'dir': directory}) return True
def assert_not_below(self, path, parent): self.assert_(not util.is_path_below(path.replace('/', os.sep), parent.replace('/', os.sep)))
def assert_below(self, path, parent): self.assert_(util.is_path_below(path.replace("/", os.sep), parent.replace("/", os.sep)))