Skip to content

Commit

Permalink
Python 3 compatibility fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-blanchard committed Jan 3, 2017
1 parent 4b43132 commit 769a64f
Show file tree
Hide file tree
Showing 47 changed files with 3,315 additions and 3,188 deletions.
8 changes: 5 additions & 3 deletions etc/utils/add_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"""

from __future__ import absolute_import, print_function

import sys
import re

Expand All @@ -25,12 +27,12 @@ def find_version(path):
for line in f:
index = line.find('GMVAULT_VERSION="')
if index > -1:
print(line[index+17:-2])
print((line[index+17:-2]))
return line[index+17:-2]

raise Exception("Cannot find GMVAULT_VERSION in %s\n" % (path))

VERSION_PATTERN = r'###GMVAULTVERSION###'
VERSION_PATTERN = r'###GMVAULTVERSION###'
VERSION_RE = re.compile(VERSION_PATTERN)

def add_version(a_input, a_output, a_version):
Expand All @@ -43,7 +45,7 @@ def add_version(a_input, a_output, a_version):
if __name__ == '__main__':

if len(sys.argv) < 4:
print("Error: need more parameters for %s." % (sys.argv[0]))
print(("Error: need more parameters for %s." % (sys.argv[0])))
print("Usage: add_version.py input_path output_path version.")
exit(-1)

Expand Down
4 changes: 3 additions & 1 deletion etc/utils/find_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"""

from __future__ import absolute_import, print_function

import sys


Expand All @@ -25,7 +27,7 @@ def find_version(path):
for line in f:
index = line.find('GMVAULT_VERSION = "')
if index > -1:
print(line[index+19:-2])
print((line[index+19:-2]))
res = line[index+19:-2]
return res.strip()

Expand Down
1 change: 1 addition & 0 deletions etc/utils/flask_stats.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import
from flask import Flask

import scrapping
Expand Down
97 changes: 50 additions & 47 deletions etc/utils/mem-profiling-tools/dowser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import absolute_import
import cgi
import gc
import os
localDir = os.path.join(os.getcwd(), os.path.dirname(__file__))
from StringIO import StringIO
from io import BytesIO
import sys
import threading
import time
Expand All @@ -13,9 +14,11 @@

import cherrypy

import reftree
from . import reftree


from six import iteritems

def get_repr(obj, limit=250):
return cgi.escape(reftree.get_repr(obj, limit))

Expand Down Expand Up @@ -44,62 +47,62 @@ def template(name, **params):


class Root:

period = 5
maxhistory = 300

def __init__(self):
self.history = {}
self.samples = 0
if cherrypy.__version__ >= '3.1':
cherrypy.engine.subscribe('exit', self.stop)
self.runthread = threading.Thread(target=self.start)
self.runthread.start()

def start(self):
self.running = True
while self.running:
self.tick()
time.sleep(self.period)

def tick(self):
gc.collect()

typecounts = {}
for obj in gc.get_objects():
objtype = type(obj)
if objtype in typecounts:
typecounts[objtype] += 1
else:
typecounts[objtype] = 1
for objtype, count in typecounts.iteritems():

for objtype, count in iteritems(typecounts):
typename = objtype.__module__ + "." + objtype.__name__
if typename not in self.history:
self.history[typename] = [0] * self.samples
self.history[typename].append(count)

samples = self.samples + 1

# Add dummy entries for any types which no longer exist
for typename, hist in self.history.iteritems():
for typename, hist in iteritems(self.history):
diff = samples - len(hist)
if diff > 0:
hist.extend([0] * diff)

# Truncate history to self.maxhistory
if samples > self.maxhistory:
for typename, hist in self.history.iteritems():
for typename, hist in iteritems(self.history):
hist.pop(0)
else:
self.samples = samples

def stop(self):
self.running = False

def index(self, floor=0):
rows = []
typenames = self.history.keys()
typenames = list(self.history.keys())
typenames.sort()
for typename in typenames:
hist = self.history[typename]
Expand All @@ -117,7 +120,7 @@ def index(self, floor=0):
rows.append(row)
return template("graphs.html", output="\n".join(rows))
index.exposed = True

def chart(self, typename):
"""Return a sparkline chart of the given type."""
data = self.history[typename]
Expand All @@ -128,28 +131,28 @@ def chart(self, typename):
draw.line([(i, int(height - (v * scale))) for i, v in enumerate(data)],
fill="#009900")
del draw
f = StringIO()

f = BytesIO()
im.save(f, "PNG")
result = f.getvalue()

cherrypy.response.headers["Content-Type"] = "image/png"
return result
chart.exposed = True

def trace(self, typename, objid=None):
gc.collect()

if objid is None:
rows = self.trace_all(typename)
else:
rows = self.trace_one(typename, objid)

return template("trace.html", output="\n".join(rows),
typename=cgi.escape(typename),
objid=str(objid or ''))
trace.exposed = True

def trace_all(self, typename):
rows = []
for obj in gc.get_objects():
Expand All @@ -160,7 +163,7 @@ def trace_all(self, typename):
if not rows:
rows = ["<h3>The type you requested was not found.</h3>"]
return rows

def trace_one(self, typename, objid):
rows = []
objid = int(objid)
Expand All @@ -181,7 +184,7 @@ def trace_one(self, typename, objid):
(k, get_repr(v)))
del v
rows.append('</div>')

# Referrers
rows.append('<div class="refs"><h3>Referrers (Parents)</h3>')
rows.append('<p class="desc"><a href="%s">Show the '
Expand All @@ -193,7 +196,7 @@ def trace_one(self, typename, objid):
if parentid:
rows.append("<p class='obj'>%s</p>" % parentrepr)
rows.append('</div>')

# Referents
rows.append('<div class="refs"><h3>Referents (Children)</h3>')
for child in gc.get_referents(obj):
Expand All @@ -203,10 +206,10 @@ def trace_one(self, typename, objid):
if not rows:
rows = ["<h3>The object you requested was not found.</h3>"]
return rows

def tree(self, typename, objid):
gc.collect()

rows = []
objid = int(objid)
all_objs = gc.get_objects()
Expand All @@ -218,17 +221,17 @@ def tree(self, typename, objid):
"of the correct type.</h3>"]
else:
rows.append('<div class="obj">')

tree = ReferrerTree(obj)
tree.ignore(all_objs)
for depth, parentid, parentrepr in tree.walk(maxresults=1000):
rows.append(parentrepr)

rows.append('</div>')
break
if not rows:
rows = ["<h3>The object you requested was not found.</h3>"]

params = {'output': "\n".join(rows),
'typename': cgi.escape(typename),
'objid': str(objid),
Expand All @@ -254,17 +257,17 @@ def tree(self, typename, objid):


class ReferrerTree(reftree.Tree):

ignore_modules = True

def _gen(self, obj, depth=0):
if self.maxdepth and depth >= self.maxdepth:
yield depth, 0, "---- Max depth reached ----"
raise StopIteration

if isinstance(obj, ModuleType) and self.ignore_modules:
raise StopIteration

refs = gc.get_referrers(obj)
refiter = iter(refs)
self.ignore(refs, refiter)
Expand All @@ -274,38 +277,38 @@ def _gen(self, obj, depth=0):
if (isinstance(ref, FrameType)
and ref.f_code.co_filename in (thisfile, self.filename)):
continue

# Exclude all functions and classes from this module or reftree.
mod = getattr(ref, "__module__", "")
if "dowser" in mod or "reftree" in mod or mod == '__main__':
continue

# Exclude all parents in our ignore list.
if id(ref) in self._ignore:
continue

# Yield the (depth, id, repr) of our object.
yield depth, 0, '%s<div class="branch">' % (" " * depth)
if id(ref) in self.seen:
yield depth, id(ref), "see %s above" % id(ref)
else:
self.seen[id(ref)] = None
yield depth, id(ref), self.get_repr(ref, obj)

for parent in self._gen(ref, depth + 1):
yield parent
yield depth, 0, '%s</div>' % (" " * depth)

def get_repr(self, obj, referent=None):
"""Return an HTML tree block describing the given object."""
objtype = type(obj)
typename = objtype.__module__ + "." + objtype.__name__
prettytype = typename.replace("__builtin__.", "")

name = getattr(obj, "__name__", "")
if name:
prettytype = "%s %r" % (prettytype, name)

key = ""
if referent:
key = self.get_refkey(obj, referent)
Expand All @@ -315,14 +318,14 @@ def get_repr(self, obj, referent=None):
% (url("/trace/%s/%s" % (typename, id(obj))),
id(obj), prettytype, key, get_repr(obj, 100))
)

def get_refkey(self, obj, referent):
"""Return the dict key or attribute name of obj which refers to referent."""
if isinstance(obj, dict):
for k, v in obj.iteritems():
for k, v in iteritems(obj):
if v is referent:
return " (via its %r key)" % k

for k in dir(obj) + ['__dict__']:
if getattr(obj, k, None) is referent:
return " (via its %r attribute)" % k
Expand Down
Loading

0 comments on commit 769a64f

Please sign in to comment.