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

login
register
mail settings
Submitter Bryan O'Sullivan
Date Feb. 13, 2017, 6:37 a.m.
Message ID <857b6377296f47a9504e.1486967863@bryano-mbp.local>
Download mbox | patch
Permalink /patch/18426/
State Superseded
Headers show

Comments

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

Patch

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 = []