def globally_get_command_help(self, *args): sys_home_project_name = self.config.get('multiproject', 'sys_home_project_name') for env_name, in self.projects_iterator(['env_name'], batch_size=1): if env_name == sys_home_project_name: continue env = None try: env_path = safe_path(self.config.get('multiproject', 'sys_projects_root'), env_name) env = open_environment(env_path, True) except TracError as e: printout(_('ERROR: Opening environment %(env_name)s failed', env_name=env_name)) continue try: command_manager = AdminCommandManager(env) helps = command_manager.get_command_help(list(args)) if not args: TracAdmin.print_doc(helps, short=True) elif len(helps) == 1: TracAdmin.print_doc(helps, long=True) else: TracAdmin.print_doc(helps) except AdminCommandError as e: printout(_('ERROR: Getting command help in environment %(env_name)s failed: ', env_name=env_name) + e) break
def globally_execute_command(self, *args): offset = 0 max_index = -1 if args and args[0].isdigit(): offset = int(args[0]) if len(args) > 1 and args[1].isdigit(): limit = int(args[1]) max_index = limit + offset args = args[2:] else: args = args[1:] upgrade_check = False env_list = False if args and args[0] == 'upgrade-check': upgrade_check = True elif args and args[0] == 'list-env': env_list = True sys_home_project_name = self.config.get('multiproject', 'sys_home_project_name') for index, row in enumerate(self.projects_iterator(['env_name'], batch_size=10)): env_name, = row if index < offset: continue if max_index != -1 and index >= max_index: break if env_name == sys_home_project_name: continue if env_list: printout("{0:4} env:'{1}'".format(index, env_name)) continue env = None try: env_path = safe_path(self.config.get('multiproject', 'sys_projects_root'), env_name) env = Environment(env_path) except TracError as e: printout(_('ERROR: Opening environment %(env_name)s failed', env_name=env_name)) continue if upgrade_check: if env.needs_upgrade(): printout("[+] {0:4} env:'{1}'".format(index, env_name)) else: printout("[ ] {0:4} env:'{1}'".format(index, env_name)) continue # To setup MultiProject specific things like 'project_identifier' MultiProjectEnvironmentInit(env).environment_needs_upgrade(None) try: command_manager = AdminCommandManager(env) printout(_("{0:4} Run in env:'{1}'".format(index, env_name))) command_manager.execute_command(*args) except AdminCommandError as e: printout(_('ERROR: Executing command in environment %(env_name)s failed: ', env_name=env_name) + str(e))
def __init__(self, project): # Import this here to avoid circular references Command.__init__(self) self.name = "CreateFilesDownloads" self.project = project self.default_directory = FilesConfiguration().default_downloads_directory self.download_config = FilesDownloadConfig(self.project.env_name) self.downloads_dir = filesystem.safe_path(self.download_config.base_path.encode('utf-8'), self.default_directory)
def __init__(self, env_name, base_url=None): """ :param env_name: valid environment name aka identifier (not validated) :param base_url: the base url, like '/files' """ self._downloads_dir = None self.env_name = env_name files_conf = FilesConfiguration() self.base_path = filesystem.get_normalized_base_path( filesystem.safe_path(files_conf.sys_dav_root, self.env_name)) if base_url is not None: self.base_url = normalized_base_url(base_url) else: self.base_url = None if not files_conf.downloads_dir_customizable: self._downloads_dir_fetched = True self._default = files_conf.default_downloads_directory self._downloads_dir = self._default else: self._downloads_dir = None self._downloads_dir_fetched = False self._default = None self.mc = conf.getMemcachedInstance()
def get_base_path(self, env_name): return safe_path(self.sys_dav_root, env_name)
def import_files(self, dry_run=False): dry_run = True if dry_run in ['-n', '--dry-run'] else False try: env_name = self.env.project_identifier except AttributeError: # Since open_environment is not used in trac-admin commands # we need to manually set the project_identifier env_name = self.env.path.split('/')[-1] self.env.project_identifier = env_name download_data_list = self.get_download_data() path = conf.getEnvironmentDownloadsPath(self.env) if download_data_list is None: printout("env:%(env_name)s, download table was not found" % {'env_name': self.env.project_identifier}) return files_core = FilesCoreComponent(self.env) node_factory, download_config = files_core.files_node_factory_and_config( ) env_name = download_config.env_name project_files = {} first_file = {} for download_data in download_data_list: filename = download_data['file'] id_ = download_data['id'] if filename not in project_files: project_files[filename] = [] first_file[filename] = id_ project_files[filename].append(id_) for download_data in download_data_list: filename = download_data['file'] id_ = download_data['id'] if not download_data['author_id']: printout( "env:%(env_name)s file:%(download)s id:%(id_)s: " "The author %(author)s of download %(download)s was not found." % { 'env_name': env_name, 'download': filename, 'id_': id_, 'author': download_data['author'] }) continue base_downloads_path = filesystem.safe_path(path, to_unicode(id_)) original_node = FileSystemNode(base_downloads_path) original_node.populate_file_data(filename) from_path = original_node._abs_path_encoded existing_node = MappedFileNode.from_download_path( filename, node_factory, True) download_path = filename if len(project_files[filename]) > 1: download_path = get_download_path(id_, filename) to_node = MappedFileNode.from_download_path( download_path, node_factory, True) else: # No duplicate downloads, put it into root to_node = existing_node if not to_node.is_download(): printout( "env:%(env_name)s file:%(download)s id:%(id_)s: " "With %(rel_path)s: Download information is incorrect" % { 'env_name': env_name, 'download': filename, 'id_': id_, 'rel_path': to_node.relative_path }) continue if to_node.download().is_available(): printout( "env:%(env_name)s file:%(download)s id:%(id_)s: " "With %(rel_path)s: The download information is already available" % { 'env_name': env_name, 'download': filename, 'id_': id_, 'rel_path': to_node.relative_path }) continue elif to_node.exists(): printout( "env:%(env_name)s file:%(download)s id:%(id_)s: " "With %(rel_path)s: The download already exists" % { 'env_name': env_name, 'download': filename, 'id_': id_, 'rel_path': to_node.relative_path }) continue can_be_removed = False download = self.populate_new_download(to_node.download(), original_node, download_data) if len(project_files[filename]) > 1: # If there were duplicate filenames, special handling for them is needed if (existing_node.exists() and existing_node.is_file() and existing_node.is_download()): old_download = existing_node.download() if (old_download.is_available() and old_download.hash == download.hash and old_download.version == 1 and download.uploader_id == old_download.uploader_id and download.created == old_download.created): # Copy all information, which might be changed download.clone_user_values(old_download) download.count = old_download.count can_be_removed = True else: # Else, we just accept that there has been changes # Download count might be duplicated. In that case, manual work # could be done. printout( "env:%(env_name)s file:%(download)s id:%(id_)s: " "Cannot remove download because it is not original or has changed, " "download count was %(count)s" % { 'env_name': env_name, 'id_': id_, 'download': filename, 'count': download.count }) if not dry_run: if os.path.sep in download_path: parent_dir = to_node.get_parent_dir() if not parent_dir.exists(): data = {'type': 'dir'} FileSystemNode.create_check(parent_dir, data) FileSystemNode.create_do(parent_dir, data) FileSystemNode.create_post_process(parent_dir, data) shutil.copy2(from_path, to_node._abs_path_encoded) to_node.chmod() self.save_new_download(download) if can_be_removed: existing_node.download().delete_completely() existing_node.remove_do({}) else: printout( "env:%(env_name)s file:%(download)s id:%(id_)s: " "Would copy file to %(download_path)s%(other)s" % { 'env_name': env_name, 'id_': id_, 'download': filename, 'download_path': to_node.download().download_path, 'other': can_be_removed and ', and would also remove original' or '' }) was_enabled = False if not self.env.is_component_enabled(DOWNLOADS_GLUE_COMPONENT): if not dry_run: self.env.config.set('components', DOWNLOADS_GLUE_COMPONENT, 'enabled') self.env.config.save() was_enabled = True if download_data_list: if was_enabled: printout( "env:%(env_name)s: downloads handled, component %(component)s enabled." % { 'env_name': env_name, 'component': DOWNLOADS_GLUE_COMPONENT }) else: printout("env:%(env_name)s: downloads handled." % {'env_name': env_name}) else: printout( "env:%(env_name)s: no downloads found, component %(component)s enabled." % { 'env_name': env_name, 'component': DOWNLOADS_GLUE_COMPONENT })
def import_files(self, dry_run=False): dry_run = True if dry_run in ['-n', '--dry-run'] else False try: env_name = self.env.project_identifier except AttributeError: # Since open_environment is not used in trac-admin commands # we need to manually set the project_identifier env_name = self.env.path.split('/')[-1] self.env.project_identifier = env_name download_data_list = self.get_download_data() path = conf.getEnvironmentDownloadsPath(self.env) if download_data_list is None: printout("env:%(env_name)s, download table was not found" % {'env_name': self.env.project_identifier}) return files_core = FilesCoreComponent(self.env) node_factory, download_config = files_core.files_node_factory_and_config() env_name = download_config.env_name project_files = {} first_file = {} for download_data in download_data_list: filename = download_data['file'] id_ = download_data['id'] if filename not in project_files: project_files[filename] = [] first_file[filename] = id_ project_files[filename].append(id_) for download_data in download_data_list: filename = download_data['file'] id_ = download_data['id'] if not download_data['author_id']: printout("env:%(env_name)s file:%(download)s id:%(id_)s: " "The author %(author)s of download %(download)s was not found." % {'env_name':env_name, 'download': filename, 'id_': id_, 'author':download_data['author']}) continue base_downloads_path = filesystem.safe_path(path, to_unicode(id_)) original_node = FileSystemNode(base_downloads_path) original_node.populate_file_data(filename) from_path = original_node._abs_path_encoded existing_node = MappedFileNode.from_download_path(filename, node_factory, True) download_path = filename if len(project_files[filename]) > 1: download_path = get_download_path(id_, filename) to_node = MappedFileNode.from_download_path(download_path, node_factory, True) else: # No duplicate downloads, put it into root to_node = existing_node if not to_node.is_download(): printout("env:%(env_name)s file:%(download)s id:%(id_)s: " "With %(rel_path)s: Download information is incorrect" % {'env_name':env_name, 'download': filename, 'id_': id_, 'rel_path':to_node.relative_path}) continue if to_node.download().is_available(): printout("env:%(env_name)s file:%(download)s id:%(id_)s: " "With %(rel_path)s: The download information is already available" % {'env_name':env_name, 'download': filename, 'id_': id_, 'rel_path':to_node.relative_path}) continue elif to_node.exists(): printout("env:%(env_name)s file:%(download)s id:%(id_)s: " "With %(rel_path)s: The download already exists" % {'env_name':env_name, 'download': filename, 'id_': id_, 'rel_path':to_node.relative_path}) continue can_be_removed = False download = self.populate_new_download(to_node.download(), original_node, download_data) if len(project_files[filename]) > 1: # If there were duplicate filenames, special handling for them is needed if (existing_node.exists() and existing_node.is_file() and existing_node.is_download()): old_download = existing_node.download() if (old_download.is_available() and old_download.hash == download.hash and old_download.version == 1 and download.uploader_id == old_download.uploader_id and download.created == old_download.created): # Copy all information, which might be changed download.clone_user_values(old_download) download.count = old_download.count can_be_removed = True else: # Else, we just accept that there has been changes # Download count might be duplicated. In that case, manual work # could be done. printout("env:%(env_name)s file:%(download)s id:%(id_)s: " "Cannot remove download because it is not original or has changed, " "download count was %(count)s" % {'env_name':env_name, 'id_': id_, 'download': filename, 'count': download.count}) if not dry_run: if os.path.sep in download_path: parent_dir = to_node.get_parent_dir() if not parent_dir.exists(): data = {'type': 'dir'} FileSystemNode.create_check(parent_dir, data) FileSystemNode.create_do(parent_dir, data) FileSystemNode.create_post_process(parent_dir, data) shutil.copy2(from_path, to_node._abs_path_encoded) to_node.chmod() self.save_new_download(download) if can_be_removed: existing_node.download().delete_completely() existing_node.remove_do({}) else: printout("env:%(env_name)s file:%(download)s id:%(id_)s: " "Would copy file to %(download_path)s%(other)s" % {'env_name':env_name, 'id_': id_, 'download': filename, 'download_path': to_node.download().download_path, 'other': can_be_removed and ', and would also remove original' or ''}) was_enabled = False if not self.env.is_component_enabled(DOWNLOADS_GLUE_COMPONENT): if not dry_run: self.env.config.set('components', DOWNLOADS_GLUE_COMPONENT, 'enabled') self.env.config.save() was_enabled = True if download_data_list: if was_enabled: printout("env:%(env_name)s: downloads handled, component %(component)s enabled." %{'env_name': env_name, 'component': DOWNLOADS_GLUE_COMPONENT}) else: printout("env:%(env_name)s: downloads handled." % {'env_name': env_name}) else: printout("env:%(env_name)s: no downloads found, component %(component)s enabled." %{'env_name': env_name, 'component': DOWNLOADS_GLUE_COMPONENT})