[PATCH 3 of 4] statprof: add a path simplification function

Bryan O'Sullivan bos at serpentine.com
Mon Feb 13 01:37:43 EST 2017


# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1486966618 28800
#      Sun Feb 12 22:16:58 2017 -0800
# Node ID 857b6377296f47a9504e482981c90303986108b6
# Parent  270342e743efcba76028840c357bb930cd1166f4
statprof: add a path simplification function

diff --git a/mercurial/statprof.py b/mercurial/statprof.py
--- a/mercurial/statprof.py
+++ b/mercurial/statprof.py
@@ -713,6 +713,23 @@ def write_to_flame(data, fp, scriptpath=
     os.system("perl ~/flamegraph.pl %s > %s" % (path, outputfile))
     print("Written to %s" % outputfile, file=fp)
 
+_pathcache = {}
+def simplifypath(path):
+    '''Attempt to make the path to a Python module easier to read by
+    removing whatever part of the Python search path it was found
+    on.'''
+
+    if path in _pathcache:
+        return _pathcache[path]
+    hgpath = encoding.__file__.rsplit(os.sep, 2)[0]
+    for p in [hgpath] + sys.path:
+        prefix = p + os.sep
+        if path.startswith(prefix):
+            path = path[len(prefix):]
+            break
+    _pathcache[path] = path
+    return path
+
 def write_to_json(data, fp):
     samples = []
 


More information about the Mercurial-devel mailing list