summaryrefslogtreecommitdiff
path: root/summainlib.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-02-18 22:57:46 +0000
committerLars Wirzenius <liw@liw.fi>2011-02-18 22:57:46 +0000
commitfa78ecc93fe9f4d1638c38e7e9023dc0ca5fd35f (patch)
tree6fe65537335c875a906df05f73457bbb7b5dc9fd /summainlib.py
parenteee02bc86f8867f903e113ca456c498508c7a496 (diff)
downloadsummain-fa78ecc93fe9f4d1638c38e7e9023dc0ca5fd35f.tar.gz
Add relative_path method, with tests.
Diffstat (limited to 'summainlib.py')
-rw-r--r--summainlib.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/summainlib.py b/summainlib.py
index 7f67908..6628ce0 100644
--- a/summainlib.py
+++ b/summainlib.py
@@ -141,7 +141,32 @@ class FilesystemObject(object):
def __getitem__(self, key):
return self.values.get(key, '')
+
+ def _isdir(self):
+ '''Is this a directory?'''
+
+ return stat.S_ISDIR(int(self['Mode'], 8))
+
+ 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
+
def format(self): # pragma: no cover
keys = ['Name'] + [x
for x in sorted(self.values.keys())