From 7e48c863c46af6574baa31737f6a141eec982903 Mon Sep 17 00:00:00 2001 From: Martinez Date: Tue, 27 Sep 2016 22:23:07 +0200 Subject: [PATCH] - port to over-1.99 - remove --output - remove unused filters --- find-orphans.py | 73 +++++++++++++++++++++++-------------------------- version.py | 8 ++++++ 2 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 version.py diff --git a/find-orphans.py b/find-orphans.py index 596b76e..c84ad3b 100755 --- a/find-orphans.py +++ b/find-orphans.py @@ -10,31 +10,26 @@ import re import sys import time -# -------------------------------------------------- - -prefix = over.core.textui.prefix -_print = over.core.textui.Output('Find Orphans') - -# -------------------------------------------------- -# Functions - -# -------------------------------------------------- -# Classes +# local imports +import version # -------------------------------------------------- -if __name__ == '__main__': - main = over.core.app.Main('Find Orphans', None, 'AWARE-Overwatch Joint Software License') - main.add_option('filter', 'str', ['/home/*', '/var/*', '*.pyc', '/lib*/modules/*', '*.pyo', '/usr/share/mime/*', '/usr/local/*'], 'Shell globs that should be filtered out.', plural=True) - main.add_option('null-separated', 'bool', False, 'Output each filename separated with a null character. Otherwise use a newline.', short_name='0') - main.add_option('output', 'bool', True, 'Output filenames to stdout. Otherwise just counts them.') - main.add_help('Description', ['Finds files on Gentoo systems that aren\'t owned by any installed package and lists them.']) - main.enable_help('h') - main.parse() +if __name__ == "__main__": + main = over.app.Main("Find Orhpans", version.str, "AO-JSL", features={"config_file": True}) + #main.add_option("filter", "Shell globs that should be filtered out.", over.callback.strings, abbr="f", count=1) + main.add_option("filter", "Shell globs that should be filtered out.", over.callback.strings, abbr="f", count=1, overwrite=False) + main.add_option("null-separated", "Output each filename separated with a null character. Otherwise use a newline.", bool, [False], abbr="0", count=1) + main.add_doc("Description", ["Finds files on Gentoo systems that aren\"t owned by any installed package and lists them."]) + main.setup() + + # works around bug over:#15 (https://git.covalent.cz/overwatch/over/issues/15) + if not main.cfg.filter: + main.cfg.filter = ["*.pyc", "/lib*/modules/*", "*.pyo", "/usr/share/mime/*", "/usr/local/*", "/usr/src/*"] if not main.targets: - _print('specify at least one directory to work on, e.g. \'/\'', prefix.fail) - main.exit(silent=True) + main.print("specify at least one directory to work on, e.g. /usr<.> or /lib64<.>", main.print.tl.fail) + main.exit(1) # -------------------------------------------------- # Create a list of all files @@ -43,20 +38,20 @@ if __name__ == '__main__': threads = [] # {proc, output_buffer} for root in main.targets: - proc = over.core.cmd.Command('find', root, '-type', 'f', '-print0') + proc = over.cmd.Command("find", root, "-type", "f", "-print0") proc.run() - threads.append({'proc': proc, 'output_buffer': [], 'root': root}) + threads.append({"proc": proc, "output_buffer": [], "root": root}) - _print('gathering a list of all files using %d processes' %(len(threads)), prefix.start) + main.print("gathering a list of all files using %d processes" %(len(threads)), main.print.tl.start) while True: at_least_one_lives = False for thread in threads: - tmp = thread['proc'].get_output() + tmp = thread["proc"].get_output() if not tmp is None: - thread['output_buffer'].append(tmp) + thread["output_buffer"].append(tmp) at_least_one_lives = True if at_least_one_lives: @@ -64,33 +59,33 @@ if __name__ == '__main__': else: break - _print('post-processing file list') + main.print("post-processing file list") for thread in threads: - output = b''.join(thread['output_buffer']).decode('utf-8') - all_files.update(os.path.normpath(os.path.join(os.path.abspath(thread['root']), f)) for f in output.split('\x00') if f) + output = b"".join(thread["output_buffer"]).decode("utf-8") + all_files.update(os.path.normpath(os.path.join(os.path.abspath(thread["root"]), f)) for f in output.split("\x00") if f) - _print('found %d files' %(len(all_files)), prefix.done) + main.print("found %d files" %(len(all_files)), main.print.tl.done) # -------------------------------------------------- # create a list of owned files - _print('gathering a list of files owned by installed packages', prefix.start) + main.print("gathering a list of files owned by installed packages", main.print.tl.start) owned_files = set() - for contents_filename in glob.glob('/var/db/pkg/*/*/CONTENTS'): - owned_files.update(re.findall('obj (.+) [0-9a-f]+ \d+', open(contents_filename).read())) + for contents_filename in glob.glob("/var/db/pkg/*/*/CONTENTS"): + owned_files.update(re.findall("obj (.+) [0-9a-f]+ \d+", open(contents_filename).read())) unowned_files = all_files - owned_files - _print('found %d owned files' %(len(owned_files)), prefix.done) - _print('there are %d unowned files' %(len(unowned_files))) + main.print("found %d owned files" %(len(owned_files)), main.print.tl.done) + main.print("there are %d unowned files" %(len(unowned_files))) # -------------------------------------------------- # apply filters - _print('applying filters', prefix.start) + main.print("applying filters", main.print.tl.start) unowned_files_ignored = set() @@ -98,12 +93,12 @@ if __name__ == '__main__': unowned_files_ignored.update(fnmatch.filter(unowned_files, filter)) files_to_output = unowned_files - unowned_files_ignored - - _print('found §y%d unowned files§/' %(len(files_to_output)), prefix.done) + color = "y" if files_to_output else "g" + main.print("found <%s>%d unowned files<.>" %(color, len(files_to_output)), main.print.tl.done) # -------------------------------------------------- # output - if main.cfg.output: - joiner = '\x00' if main.cfg.null_separated else '\n' + if files_to_output: + joiner = "\x00" if main.cfg.null_separated else "\n" print(joiner.join(sorted(files_to_output))) diff --git a/version.py b/version.py new file mode 100644 index 0000000..65cede7 --- /dev/null +++ b/version.py @@ -0,0 +1,8 @@ +#! /usr/bin/env python3 +# encoding: utf-8 + +major = 0 # VERSION_MAJOR_IDENTIFIER +minor = 2 # VERSION_MINOR_IDENTIFIER +# VERSION_LAST_MM 0.2 +patch = 2 # VERSION_PATCH_IDENTIFIER +str = ".".join(str(v) for v in (major, minor, patch))