summaryrefslogtreecommitdiff
path: root/buildstream/utils.py
diff options
context:
space:
mode:
authorAngelos Evripiotis <jevripiotis@bloomberg.net>2017-10-27 16:00:36 +0100
committerTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2017-10-30 20:15:43 +0900
commit4bc656c598d5eaa47bfe52c4956311a0ef64aacc (patch)
treedbf3725b2d0adfa6204e6b56a50960fa9bb2a3e2 /buildstream/utils.py
parentb87b91614476c379aa991a3d1baaf3223c5090ed (diff)
downloadbuildstream-4bc656c598d5eaa47bfe52c4956311a0ef64aacc.tar.gz
utils.list_relative_paths: avoid os.path.relpath
Profiling suggested that the cumulative time spent in os.path.relpath() was the dominant cost of utils.list_relative_paths(). Try to call this only once per directory walked. In Python 3.5, we can optimise further by using os.scandir() and maintaining relative paths as we go.
Diffstat (limited to 'buildstream/utils.py')
-rw-r--r--buildstream/utils.py15
1 files changed, 9 insertions, 6 deletions
diff --git a/buildstream/utils.py b/buildstream/utils.py
index f48a7e4b5..1e77741fa 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -79,6 +79,13 @@ def list_relative_paths(directory):
Relative filenames in `directory`
"""
for (dirpath, dirnames, filenames) in os.walk(directory):
+
+ relpath = os.path.relpath(dirpath, directory)
+
+ # We don't want "./" pre-pended to all the entries in the root of
+ # `directory`, prefer to have no prefix in that case.
+ basepath = relpath if relpath != '.' and dirpath != directory else ''
+
# os.walk does not decend into symlink directories, which
# makes sense because otherwise we might have redundant
# directories, or end up descending into directories outside
@@ -91,21 +98,17 @@ def list_relative_paths(directory):
for d in dirnames:
fullpath = os.path.join(dirpath, d)
if os.path.islink(fullpath):
- relpath = os.path.relpath(fullpath, directory)
- yield relpath
+ yield os.path.join(basepath, d)
# We've decended into an empty directory, in this case we
# want to include the directory itself, but not in any other
# case.
if not filenames:
- relpath = os.path.relpath(dirpath, directory)
yield relpath
# List the filenames in the walked directory
for f in filenames:
- fullpath = os.path.join(dirpath, f)
- relpath = os.path.relpath(fullpath, directory)
- yield relpath
+ yield os.path.join(basepath, f)
def glob(paths, pattern):