From 4707f32a416dfb8e6a6956bb1e2fe60997cc9f4b Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 18 Feb 2011 23:08:35 +0000 Subject: Implement relative path output. --- summain | 7 ++++++- summain.1 | 23 +++++++++++++++++++++++ summainlib.py | 15 ++++++++++----- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/summain b/summain index 58ac582..c37e57c 100755 --- a/summain +++ b/summain @@ -24,6 +24,10 @@ import summainlib class Summain(cliapp.Application): + def add_settings(self): + self.add_boolean_setting(['relative-paths', 'r'], + 'print paths relative to arguments') + def files(self, root): if os.path.isdir(root): for dirname, dirnames, filenames in os.walk(root): @@ -34,11 +38,12 @@ class Summain(cliapp.Application): yield root def process_args(self, args): + relative = self['relative-paths'] normalizer = summainlib.NumberNormalizer() for root in args: for filename in self.files(root): o = summainlib.FilesystemObject(filename, normalizer) - sys.stdout.write(o.format()) + sys.stdout.write(o.format(root if relative else None)) sys.stdout.write('\n') diff --git a/summain.1 b/summain.1 index ebecb01..d163210 100644 --- a/summain.1 +++ b/summain.1 @@ -3,6 +3,8 @@ summain \- gather file checksums and metadata .SH SYNOPSIS .B summain +.RB [ \-r ] +.RB [ \-\-relative\-paths ] .RI [ file ...] .SH DESCRIPTION .B summain @@ -39,3 +41,24 @@ Normalized means that there will be no differences. The numbers are reported so that hard links can be checked. .PP Directories named on the command line will be recursed automatically. +.SH OPTIONS +.TP +.BR \-r ", " \-\-relative\-paths +Print pathnames relative to the command line argument they derive from. +If the command line argument is the directory called +.I foo +and there is a file called +.IR bar , +normally +.I foo/bar +is printed as the name. +With this option, +.I bar +is printed instead. +This can be handy for normalizing the paths for comparing two copies of the +same directory tree in two different locations on disk: +run +.B summain +on both, +and compare the output with +.BR diff (1). diff --git a/summainlib.py b/summainlib.py index 6628ce0..e20b447 100644 --- a/summainlib.py +++ b/summainlib.py @@ -167,9 +167,14 @@ class FilesystemObject(object): else: return pathname - def format(self): # pragma: no cover - keys = ['Name'] + [x - for x in sorted(self.values.keys()) - if x != 'Name'] - return ''.join('%s: %s\n' % (key, self[key]) for key in keys) + def format(self, root=None): # pragma: no cover + if root is None: + name = self['Name'] + else: + name = self.relative_path(root) + + values = ([('Name', name)] + + [(x, self[x]) + for x in sorted(self.values.keys()) if x != 'Name']) + return ''.join('%s: %s\n' % (k, v) for k, v in values) -- cgit v1.2.1