#!/usr/bin/env python # AugmentCommandNotFoundDatabase - augments the existing database with data from this system # Copyright (C) 2008 Michael Homer <=mwh> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. import sys import os import os.path from PythonUtils import getGoboVariable goboPrograms = getGoboVariable('goboPrograms') path = goboPrograms+'/Scripts/Current/Data/CommandNotFound.data' commands = dict() if os.path.exists(path) and os.path.getsize(path) > 0: for entry in open(path).readlines(): entrydata = entry.split() commands[entrydata[0]] = entrydata[1:] def out(s): sys.stdout.write(s) def eout(s): sys.stderr.write(s) changes = 0 programs = os.listdir(goboPrograms) for prog in programs: # Skip various problematic entries: local or non-installable # program entries, renames and case changes if ("LocalScripts" == prog or 'LiveCD' == prog or 'Installer' == prog or 'LibUngif' == prog or 'Koffice' == prog or 'ESP-GhostScript' == prog): continue # Cover both executable directories for dtype in ('bin', 'sbin'): # Not all programs have executables try: executables = os.listdir('%s/%s/Current/%s' % (goboPrograms, prog, dtype)) for ex in executables: if os.path.isdir('%s/%s/Current/%s/%s' % (goboPrograms, prog, dtype, ex)): continue if ("." == ex[0] or "CVS" == ex or ex.endswith("~") or ex.endswith('.bin') or ex.endswith(".bak")): continue if not ex in commands: (base, sep, vers) = ex.rpartition('-') # Short version: skip entries like zsh-4.3.5 if vers and vers.replace('.', '').isdigit(): continue commands[ex] = [] eout("Adding executable %s\n" % (ex)) if not prog in commands[ex]: commands[ex].append(prog) changes += 1 eout("Adding program %s for executable %s\n" % (prog, ex)) except OSError: pass eout("Completed scan with %i change%s.\n" % (changes, changes != 1 and 's' or '')) if changes == 0: eout("Nothing to do. No data saved.\n") exit() target = path if len(sys.argv) > 1: target = sys.argv[1] try: if '-' != target: sys.stdout = open(target, 'w') eout("Will save data to %s\n"%(target)) except IOError: eout("No target supplied or target is not writable. " "Will dump to stdout.\n") eout("Use AugmentCommandNotFoundDatabase " "to save to a file.\n") eout(" defaults to %s\n" % (path)) if sys.stdout.isatty(): eout("Press enter to continue...\n") sys.stdin.readline() # Output data in sorted order so it's deterministic and amenable to # short diffs when maintained in SVN. executables = sorted(commands.keys()) for ex in executables: out(ex + ' ') out(' '.join(sorted(commands[ex]))) out('\n')