def execute(self, options_dict, non_option_args): no_abbrev = options_dict.has_key("no-abbrev") or \ options_dict.has_key("terse") pkcon = self.pkcon() table_keys = ["repo", "name", "version"] table_rows = [] updates = pkcon.get_updates() for new_pkg in updates: row = ruckformat.package_to_row(new_pkg, no_abbrev, table_keys) table_rows.append(row) if len(table_rows): if options_dict.has_key("sort-by-repo"): table_rows.sort(lambda x,y:cmp(string.lower(x[0]), string.lower(y[0])) or\ cmp(string.lower(x[1]), string.lower(y[1]))) else: table_rows.sort(lambda x,y:cmp(string.lower(x[1]), string.lower(y[1]))) ruckformat.tabular(["Repository", "Name", "Version"], table_rows) else: rucktalk.message("--- No updates found ---")
def execute(self, options_dict, non_option_args): if len(non_option_args) < 1: self.usage() return False plist = [] yum = self.yum() for dep in non_option_args: if yum.returnInstalledPackagesByDep (dep): continue try: pkg = yum.returnPackageByDep(dep) plist.append(pkg.name) except: rucktalk.error("Unable to satisfy requirement '%s'" % dep) return False installs, updates = self.find_available_packages(plist) if not installs and not updates: rucktalk.message("Requirements are already met on the system."); return True for i in installs: yum.install(i) for u in updates: exactmatches, matches, unmatched = self.find_packages([u.name], installed=True) yum.tsInfo.addUpdate(u, exactmatches[0]) self.start_transaction(dryrun=options_dict.has_key('dry-run'))
def execute(self, options_dict, non_option_args): size = len(non_option_args) if size < 1: self.usage() return False table_rows = [] yum = self.yum() for file in non_option_args: if not os.access (file, os.F_OK): rucktalk.error("File %s does not exist" % file) continue matches = yum.rpmdb.searchFiles (file); if not matches: rucktalk.message("No package owns file %s" % file) continue for pkg in matches: row = ruckformat.package_to_row(yum, pkg, False, ["name", "version"]) if size > 1: row.insert (0, file) table_rows.append (row) if len(table_rows): if size == 1: ruckformat.tabular(["Name", "Version"], table_rows) else: ruckformat.tabular(["File", "Name", "Version"], table_rows)
def execute(self, options_dict, non_option_args): # FIXME: does not know about status, not sure all is right default pkcon = self.pkcon() table_rows = [] no_abbrev = options_dict.has_key("no-abbrev") sort_idx = 2 table_headers = ["S", "Repository", "Name", "Version"] table_keys = ["installed", "repo", "name", "version"] filter = pkenums.FILTER_NEWEST if options_dict.has_key("uninstalled-only"): filter = filter + pkenums.FILTER_NOT_INSTALLED elif options_dict.has_key("installed-only"): filter = pkenums.FILTER_INSTALLED filter = pkenums.FILTER_INSTALLED pkgs = pkcon.get_packages(filter) remote_tuples = {} for p in pkgs: row = ruckformat.package_to_row(p, no_abbrev, table_keys) table_rows.append(row) if table_rows: if options_dict.has_key("sort-by-repo"): table_rows.sort(lambda x,y:cmp(string.lower(x[1]), string.lower(y[1])) or\ cmp(string.lower(x[2]), string.lower(y[2]))) else: table_rows.sort(lambda x,y:cmp(string.lower(x[sort_idx]), string.lower(y[sort_idx]))) ruckformat.tabular(table_headers, table_rows) else: rucktalk.message("--- No packages found ---")
def execute(self, options_dict, non_option_args): pkcon = self.pkcon() searchlist = ['name'] if options_dict.has_key('search-descriptions'): method = pkcon.search_details else: method = pkcon.search_name filter = pkenums.FILTER_NONE if options_dict.has_key('installed-only'): filter = pkenums.FILTER_INSTALLED elif options_dict.has_key('uninstalled-only'): filter = pkenums.FILTER_NOT_INSTALLED result = {} matches = method(non_option_args[0], filter) if len(matches) > 0: table_keys = ["installed", "repo", "name", "version"] table_rows = [] no_abbrev = options_dict.has_key("no-abbrev") for pkg in matches: row = ruckformat.package_to_row(pkg, no_abbrev, table_keys) table_rows.append(row) if options_dict.has_key("sort-by-repo"): table_rows.sort(lambda x,y:cmp(string.lower(x[1]), string.lower(y[1])) or\ cmp(string.lower(x[2]), string.lower(y[2]))) else: table_rows.sort(lambda x,y:cmp(string.lower(x[2]), string.lower(y[2]))) ruckformat.tabular(["S", "Repository", "Name", "Version"], table_rows) else: rucktalk.message("--- No packages found ---")
def _do_start(self, now=None): if self.text is not None: text = self.text else: text = self.basename rucktalk.message(text)
def opt_table(table): opt_list = [] for r in table: opt = "--" + r[1] if r[0]: opt = "-" + r[0] + ", " + opt if r[2]: opt = opt + "=<" + r[2] + ">" opt_list.append([opt + " ", r[3]]) # By appending [0,0], we insure that this will work even if # opt_list is empty (which it never should be) max_len = apply(max, map(lambda x:len(x[0]), opt_list) + [0,0]) for opt, desc_str in opt_list: if 79 - max_len > 10: desc = linebreak(desc_str, 79 - max_len) else: desc = [desc_str] desc_first = desc.pop(0) rucktalk.message(string.ljust(opt, max_len) + desc_first) for d in desc: rucktalk.message(" " * max_len + d)
def setenabled(self, repos, val): pkcon = self.pkcon() for repo in repos: pkcon.repo_enable(repo, val) if val: rucktalk.message("--- Enabled repository '%s' ---" % repo) else: rucktalk.message("--- Disabled repository '%s' ---" % repo)
def execute(self, options_dict, non_option_args): if len(non_option_args) != 1: self.usage() return False repo = None if options_dict.has_key('repo'): repo = options_dict['repo'] rucklocks.add_lock(non_option_args[0], repo=repo) rucktalk.message("--- Lock successfully added ---")
def execute(self, options_dict, non_option_args): if len(non_option_args) != 1: self.usage() return False locks = rucklocks.get_locks() i = int(non_option_args[0]) - 1 if i >= len(locks): rucktalk.error("Invalid lock %s" % str(i + 1)) return False rucklocks.remove_lock(i) rucktalk.message("--- Lock successfully removed ---")
def execute(self, options_dict, non_option_args): if len(non_option_args) != 2: self.usage() return False repoid = non_option_args[0] url = non_option_args[1] if not self.check_url(url): rucktalk.error("Invalid url '%s'" % url) return False if self.find_repo_file(repoid) != None: rucktalk.error("Repository '%s' already exists" % repoid) return False yum = self.yum(repos=False) repopath = os.path.join(yum.conf.reposdir[0], repoid + ".repo") if not os.path.exists(os.path.dirname(repopath)): os.makedirs(os.path.dirname(repopath)) parser = ConfigParser() parser.add_section(repoid) name = repoid if options_dict.has_key('name'): name = options_dict['name'] parser.set(repoid, "name", name) if options_dict.has_key('mirrorlist'): parser.set(repoid, "mirrorlist", url) else: parser.set(repoid, "baseurl", url) parser.set(repoid, "enabled", "1") gpgval = "0" if options_dict.has_key('check-signatures'): gpgval = "1" parser.set(repoid, "gpgcheck", gpgval) parser.write(file(repopath, "w+")) rucktalk.message("--- Successfully added '%s' ---" % repoid)
def progressbar(self, current, total, name=None): if current > total: return msg = 'Updating metadata' if name != None: msg = "Updating repository: %s" % name if msg != self.last_text: rucktalk.message_finished(msg) self.last_text = msg rucktalk.message_status(ruckformat.progress_to_str(float(current) / float(total) * 100, -1, -1, -1, -1)) if current == total: rucktalk.message('\n') self.last_text = None
def execute(self, options_dict, non_option_args): yum = self.yum() table_rows = [] locks = rucklocks.get_locks() i = 1 for (repo, lock) in locks: if repo is None: repo = '' table_rows.append((str(i), repo, lock)) i += 1 if len(table_rows): ruckformat.tabular(["#", "Repository", "Lock"], table_rows) else: rucktalk.message("--- No locks found ---")
def execute(self, options_dict, non_option_args): if len(non_option_args) != 1: self.usage() return False repoid = non_option_args[0] repopath = self.find_repo_file(repoid) if repopath == None: rucktalk.error("Repository '%s' does not exist" % repoid) return False parser = ConfigParser() parser.read(repopath) parser.remove_section(repoid) if len(parser.sections()) == 0: os.unlink(repopath) else: parser.write(file(repopath, 'w+')) rucktalk.message("--- Successfully removed '%s' ---" % repoid)
def execute(self, options_dict, non_option_args): pkcon = self.pkcon() updates = pkcon.get_updates() repos = {} for pkg in updates: bits = ruckformat.package_to_row(pkg, False, ['repo']) if not repos.has_key(bits[0]): repos[bits[0]] = [bits[0], 0] repos[bits[0]][1] = str(int(repos[bits[0]][1])+1) repolist = [] for repo in repos.keys(): repolist.append(repos[repo]) if len(repolist) > 0: repolist.sort(lambda x,y:cmp(y[1], x[1])) headers = ["Repository", "Total"] ruckformat.tabular(headers, repolist) else: rucktalk.message("--- No updates found ---")
def execute(self, options_dict, non_option_args): pkcon = self.pkcon() updates = pkcon.get_updates() if (len(updates) == 0): rucktalk.message("--- No updates found ---") exit() rucktalk.message("The following packages will be updated:") table_keys = ["repo", "name", "version"] table_rows = [] for new_pkg in updates: row = ruckformat.package_to_row(new_pkg, False, table_keys) table_rows.append(row) table_rows.sort(lambda x,y:cmp(string.lower(x[1]), string.lower(y[1]))) ruckformat.tabular(["Repository", "Name","Version"], table_rows) # FIXME: this prompt is horrid resp = rucktalk.prompt("Continue? Y/[N]") if (resp == 'y'): # FIXME: needs to deal with progress better pkcon.update_packages(updates) else: rucktalk.message("Update aborted")
def show_ts_packages(self): yum = self.yum() yum.tsInfo.makelists() list = yum.tsInfo.installed dep_list = yum.tsInfo.depinstalled if len(list) > 0 or len(dep_list) > 0: rucktalk.message('The following packages will be installed:') self.show_ts_list(list) self.show_ts_list(dep_list, True) list = yum.tsInfo.updated dep_list = yum.tsInfo.depupdated if len(list) > 0 or len(dep_list) > 0: rucktalk.message('The following packages will be upgraded:') self.show_ts_list(list) self.show_ts_list(dep_list, True) list = yum.tsInfo.removed dep_list = yum.tsInfo.depremoved if len(list) > 0 or len(dep_list) > 0: rucktalk.message('The following packages will be removed:') self.show_ts_list(list) self.show_ts_list(dep_list, True)
def execute(self, options_dict, non_option_args): if len(non_option_args) != 1: self.usage() return False yum = self.yum() pkg = non_option_args[0] matches = yum.rpmdb.searchNevra (name=pkg); if not matches: yum.doSackFilelistPopulate() matches = yum.pkgSack.searchNevra (name=pkg); if not matches: rucktalk.message("--- No package found ---") for p in matches: files = None # FIXME: returnFileEntries() is always empty for installed # packages if p.repoid == 'installed': files = p.returnSimple('filenames'); else: files = p.returnFileEntries(); if not files: rucktalk.message("--- No files available ---") files.sort(lambda x,y:cmp(x,y)) for file in files: rucktalk.message(file)
def execute(self, options_dict, non_option_args): if len(non_option_args) != 0: self.usage() return False table_headers = ["Name", "Version"] table_keys = ["name", "version"] table_rows = [] no_abbrev = options_dict.has_key("no-abbrev") yum = self.yum() for installed in yum.rpmdb.returnPackages(): matches = yum.pkgSack.searchNevra(name=installed.name) if len(matches) == 0: table_rows.append(ruckformat.package_to_row(yum, installed, no_abbrev, table_keys)) if len(table_rows) > 0: ruckformat.tabular(table_headers, table_rows) else: rucktalk.message('--- No orphans found ---')
def execute(self, options_dict, non_option_args): if len(non_option_args) < 1: self.usage() return False table_rows = [] yum = self.yum() for dep in non_option_args: dep_info = self.parse_dep_str(dep) matches = [] if not options_dict.has_key('installed-only'): matches = self.dep_search_uninstalled (dep_info["dep"]); if not options_dict.has_key('uninstalled-only'): matches += self.dep_search_installed (dep_info["dep"]); if dep_info.has_key("relation"): matches = [p for p in matches if self.check_relation (dep_info, p)] if not matches: rucktalk.message("--- No matches for %s ---" % dep) continue for pkg in matches: row = ruckformat.package_to_row(yum, pkg, False, ["name", "version"]) repo = pkg.repoid if yum.rpmdb.installed(name=pkg.name, arch=pkg.arch): repo = "installed" row.insert(0, repo) table_rows.append (row) if len(table_rows): ruckformat.tabular(["Repository", "Name", "Version"], table_rows)
def execute(self, options_dict, non_option_args): pkcon = self.pkcon() enabled_only = not options_dict.has_key("disabled") repos = [] for repo in pkcon.get_repo_list(): if enabled_only: if repo['enabled']: line = [repo['id'], repo['desc']] repos.append(line) else: line = [repo['id'], ruckformat.bool_to_short_str(repo['enabled']), repo['desc']] repos.append(line) repos.sort(lambda x,y:cmp(x[0], y[0])) if len(repos): headers = ['Name', 'Description'] if not enabled_only: headers.insert(1, 'Enabled') ruckformat.tabular(headers, repos) else: rucktalk.message("--- No repositories found ---")
def execute(self, options_dict, non_option_args): if len(non_option_args) < 1 or (options_dict.has_key('uninstalled-only') and options_dict.has_key('installed-only')): self.usage() return False table_rows = [] yum = self.yum() dtype = self.dep_type(); plist = [] unmatched1 = None unmatched2 = None if not options_dict.has_key('uninstalled-only'): exactmatches, matches, unmatched1 = self.find_packages (non_option_args, installed=True) plist = exactmatches + matches if not options_dict.has_key('installed-only'): exactmatches, matches, unmatched2 = self.find_packages (non_option_args, installed=False) plist += exactmatches plist += matches if (unmatched1 is None or len(unmatched1) > 0) and (unmatched2 is None or len(unmatched2)) > 0: if unmatched1 != None: arg = unmatched1[0] else: arg = unmatched2[0] rucktalk.error("Could not find package '%s'" % arg) return False for p in plist: rucktalk.message("--- %s ---" % ruckformat.package_to_str(p)) deps = p.returnPrco(dtype) if len(deps) == 0: rucktalk.message("\nNo %s found\n" % dtype) else: for dep in deps: #FIXME: piece of crap prcoPrintable sometimes chokes try: rucktalk.message(p.prcoPrintable(dep)) except: pass rucktalk.message('')
def print_command_list(commands, with_categories=0): max_len = 0 cmd_list = [] for c in commands: name, aliases, description, category = c if aliases: name = name + " (" + string.join(aliases, ", ") + ")" cmd_list.append([name, description, category]) max_len = max(max_len, len(name)) desc_len = max_len + 4 cmd_list.sort(command_sort) previous_category = "we will banish all dwarves from the love kingdom" for c in cmd_list: name, description, category = c if with_categories and category != previous_category: if category_data.has_key(category): category_name = category_data[category][0] else: category_name = string.upper(category[0]) + category[1:] rucktalk.message("\n" + category_name + " commands:") previous_category = category # If, for some reason, the command list is *really* wide (which it never should # be), don't do something stupid. if 79 - desc_len > 10: desc = ruckformat.linebreak(description, 79-desc_len) else: desc = [description] desc_first = desc.pop(0) rucktalk.message(" " + string.ljust(name, max_len) + " " + desc_first) for d in desc: rucktalk.message(" " * desc_len + d)
def tabular(headers, table): def row_to_string(row, col_sizes): if rucktalk.be_terse: return string.join(row, "|") else: return string.join(pad_row(row, col_sizes), " | ") col_sizes = max_col_widths(table) if headers and not rucktalk.be_terse: col_sizes = map(max, map(len,headers), col_sizes) # print headers rucktalk.message(string.join(pad_row(headers, col_sizes), " | ")) # print head/body separator rucktalk.message(string.join (map(lambda x:stutter("-",x), col_sizes), "-+-")) # print table body for r in table: rucktalk.message(row_to_string(r, col_sizes))
def main(ver, ruck_dir): global local global ruck_version ruck_version = ver if os.environ.has_key("RUCK_DEBUG"): rucktalk.show_debug = 1 import rucklocks rucklocks.init() import_commands(ruck_dir) ### ### Grab the option list and extract the first non-option argument that ### looks like a command. This could get weird if someone passes the name ### of a command as the argument to an option. ### argv = sys.argv[1:] argv = ruckcommand.expand_synthetic_args(argv) if "--version" in argv: print print ruck_name + " " + ruck_version print ruck_copyright print sys.exit(0) command = ruckcommand.extract_command_from_argv(argv) if "-?" in argv or "--help" in argv: command.usage() sys.exit(0) # A hack to suppress extra whitespace when dumping. if command.name() == "dump": rucktalk.be_terse = 1 argv = ruckcommand.get_user_default_args(argv, command) opt_dict, args = command.process_argv(argv) ### ### Control verbosity ### if opt_dict.has_key("terse"): rucktalk.be_terse = 1 if opt_dict.has_key("quiet"): rucktalk.show_messages = 0 rucktalk.show_warnings = 0 if opt_dict.has_key("verbose"): rucktalk.show_verbose = 1 ### Whitespace is nice, so we always print a blank line before ### executing the command if not rucktalk.be_terse: rucktalk.message("") if opt_dict.has_key("cache-only") or os.getuid() != 0: command.cache_only = True elif opt_dict.has_key("no-plugins"): command.no_plugins = True try: command.execute(opt_dict, args) except IOError, e: if e.errno == 13: rucktalk.error("You must be root to execute this command") else: show_exception(e) sys.exit(1)
### Whitespace is nice, so we always print a blank line before ### executing the command if not rucktalk.be_terse: rucktalk.message("") if opt_dict.has_key("cache-only") or os.getuid() != 0: command.cache_only = True elif opt_dict.has_key("no-plugins"): command.no_plugins = True try: command.execute(opt_dict, args) except IOError, e: if e.errno == 13: rucktalk.error("You must be root to execute this command") else: show_exception(e) sys.exit(1) except Exception, e: show_exception(e) sys.exit(1) ### Whitespace is nice, so we always print a blank line after ### executing the command if not rucktalk.be_terse: rucktalk.message("")
def aligned(table): col_sizes = max_col_widths(table) for r in table: rucktalk.message(string.join(pad_row(r, col_sizes), " "))
def separated(table, separator): for r in table: rucktalk.message(string.join(clean_row(r, separator), separator + " "))
def callback(self, what, bytes, total, h, user): if what == rpm.RPMCALLBACK_TRANS_START: if bytes == 6: self.total_actions = total elif what == rpm.RPMCALLBACK_TRANS_PROGRESS: pass elif what == rpm.RPMCALLBACK_TRANS_STOP: pass elif what == rpm.RPMCALLBACK_INST_OPEN_FILE: hdr = None if h is not None: hdr, rpmloc = h handle = self._makeHandle(hdr) fd = os.open(rpmloc, os.O_RDONLY) self.callbackfilehandles[handle]=fd self.total_installed += 1 self.installed_pkg_names.append(hdr['name']) return fd else: self._localprint("No header - huh?") elif what == rpm.RPMCALLBACK_INST_CLOSE_FILE: hdr = None if h is not None: hdr, rpmloc = h handle = self._makeHandle(hdr) os.close(self.callbackfilehandles[handle]) fd = 0 if self.output: rucktalk.message('') elif what == rpm.RPMCALLBACK_INST_PROGRESS: if h is not None: # If h is a string, we're repackaging. # Why the RPMCALLBACK_REPACKAGE_PROGRESS flag isn't set, I have no idea if type(h) == type(""): if total == 0: percent = 0 else: percent = (bytes*100L)/total if self.output and sys.stdout.isatty(): self.show_progress(percent, 'Repackage', h) if bytes == total: sys.stdout.write('\n') sys.stdout.flush() else: hdr, rpmloc = h if total == 0: percent = 0 else: percent = (bytes*100L)/total pkgtup = self._dopkgtup(hdr) txmbrs = self.tsInfo.getMembers(pkgtup=pkgtup) for txmbr in txmbrs: try: process = self.myprocess[txmbr.output_state] except KeyError, e: rucktalk.message("Error: invalid output state: %s for %s" % \ (txmbr.output_state, hdr['name'])) else: if self.output and (sys.stdout.isatty() or bytes == total): self.show_progress(percent, process, hdr['name'])
def downloadHeader(self, name): rucktalk.message("Downloading header for '%s'" % name)