summaryrefslogtreecommitdiff
path: root/summain
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-08-08 14:50:00 +0100
committerLars Wirzenius <liw@liw.fi>2011-08-08 14:50:00 +0100
commit68f67f9b71e1bbc9647d350cb6b62d72ebfbeab9 (patch)
treedb154257120b54ab0b83c46fc2cb627dbed8743f /summain
parent74d9866867cc38dd0857b071c0ed09d4fb7512ef (diff)
downloadsummain-68f67f9b71e1bbc9647d350cb6b62d72ebfbeab9.tar.gz
Move output formatting out from FilesystemObject class.
Diffstat (limited to 'summain')
-rwxr-xr-xsummain59
1 files changed, 54 insertions, 5 deletions
diff --git a/summain b/summain
index 1525570..717d398 100755
--- a/summain
+++ b/summain
@@ -22,6 +22,30 @@ import sys
import summainlib
+class OutputFormat(object):
+
+ def __init__(self, output):
+ self.output = output
+
+ def close(self):
+ pass
+
+ def write_object(self, name, o, checksums):
+ raise NotImplemented()
+
+
+class Rfc822(OutputFormat):
+
+ def write_object(self, name, o, checksums):
+ keys = (['Mtime', 'Mode', 'Ino', 'Dev', 'Nlink', 'Size',
+ 'Uid', 'Username', 'Gid', 'Group', 'Target'] +
+ checksums)
+ values = [('Name', name)]
+ values += [(k, o[k]) for k in keys if o[k] != '']
+ record = ''.join('%s: %s\n' % (k, v) for k, v in values if v != '')
+ self.output.write('%s\n' % record)
+
+
class Summain(cliapp.Application):
def add_settings(self):
@@ -63,18 +87,43 @@ class Summain(cliapp.Application):
pn = summainlib.SamePath()
checksums = [x.upper()
for x in self.settings['checksum'] or ['SHA1']]
- o = summainlib.FilesystemObject('.', nn, pn, exclude, checksums)
+ o = summainlib.FilesystemObject('.', nn, pn, exclude)
for checksum in checksums:
try:
o[checksum]
except KeyError:
raise cliapp.AppException('Unknown checksum %s' % checksum)
+
+ fmt = Rfc822(self.output)
for root in args:
for filename in self.files(root):
- o = summainlib.FilesystemObject(filename, nn, pn, exclude,
- checksums)
- self.output.write(o.format(root if relative else None))
- self.output.write('\n')
+ o = summainlib.FilesystemObject(filename, nn, pn, exclude)
+ if relative:
+ name = self.relative_path(root)
+ else:
+ name = o['Name']
+ fmt.write_object(name, o, checksums)
+ fmt.close()
+
+ def relative_path(self, root):
+ '''Return a path that is relative to root, if possible.
+
+ If pathname does not start with root, then return it
+ unmodified.
+
+ '''
+
+ if root.endswith(os.sep):
+ root2 = root
+ else:
+ root2 = root + os.sep
+ pathname = self['Name']
+ if pathname.startswith(root2):
+ return pathname[len(root2):]
+ elif pathname == root and self._isdir():
+ return '.'
+ else:
+ return pathname
Summain(version=summainlib.__version__).run()