Commit ca3aee4e authored by Luke Macken's avatar Luke Macken

Merge pull request #16 from Qalthos/develop

Allow pyrasite to run on python3
parents 24b39a21 b98a302e
...@@ -100,7 +100,7 @@ class PyrasiteIPC(object): ...@@ -100,7 +100,7 @@ class PyrasiteIPC(object):
(fd, filename) = tempfile.mkstemp() (fd, filename) = tempfile.mkstemp()
tmp = os.fdopen(fd, 'w') tmp = os.fdopen(fd, 'w')
path = dirname(abspath(join(pyrasite.__file__, '..'))) path = dirname(abspath(join(pyrasite.__file__, '..')))
payload = file(join(path, 'pyrasite', 'reverse.py')) payload = open(join(path, 'pyrasite', 'reverse.py'))
tmp.write('import sys; sys.path.insert(0, "%s")\n' % path) tmp.write('import sys; sys.path.insert(0, "%s")\n' % path)
for line in payload.readlines(): for line in payload.readlines():
......
from __future__ import print_function
import sys, traceback import sys, traceback
for thread, frame in sys._current_frames().items(): for thread, frame in sys._current_frames().items():
print('Thread 0x%x' % thread) print('Thread 0x%x' % thread)
traceback.print_stack(frame) traceback.print_stack(frame)
print print()
...@@ -32,7 +32,10 @@ import threading ...@@ -32,7 +32,10 @@ import threading
from os.path import join, abspath, dirname from os.path import join, abspath, dirname
from random import randrange from random import randrange
from meliae import loader try:
from meliae import loader
except:
pass
from gi.repository import GLib, GObject, Pango, Gtk, WebKit from gi.repository import GLib, GObject, Pango, Gtk, WebKit
import pyrasite import pyrasite
...@@ -79,15 +82,16 @@ class Process(pyrasite.PyrasiteIPC, GObject.GObject): ...@@ -79,15 +82,16 @@ class Process(pyrasite.PyrasiteIPC, GObject.GObject):
@property @property
def title(self): def title(self):
if not getattr(self, '_title', None): if not getattr(self, '_title', None):
self._title = run('ps --no-heading -o cmd= -p %d' % self.pid)[1] self._title = run('ps --no-heading -o cmd= -p %d' % self.pid)[1] \
.decode('utf-8')
return self._title return self._title
class ProcessTreeStore(Gtk.TreeStore): class ProcessListStore(Gtk.ListStore):
"""This TreeStore finds all running python processes.""" """This TreeStore finds all running python processes."""
def __init__(self, *args): def __init__(self, *args):
Gtk.TreeStore.__init__(self, str, Process, Pango.Style) Gtk.ListStore.__init__(self, str, Process, Pango.Style)
for pid in os.listdir('/proc'): for pid in os.listdir('/proc'):
try: try:
pid = int(pid) pid = int(pid)
...@@ -95,7 +99,7 @@ class ProcessTreeStore(Gtk.TreeStore): ...@@ -95,7 +99,7 @@ class ProcessTreeStore(Gtk.TreeStore):
try: try:
maps = open('/proc/%d/maps' % pid).read().strip() maps = open('/proc/%d/maps' % pid).read().strip()
if 'python' in maps: if 'python' in maps:
self.append(None, (proc.title.strip(), proc, self.append((proc.title.strip(), proc,
Pango.Style.NORMAL)) Pango.Style.NORMAL))
except IOError: except IOError:
pass pass
...@@ -450,10 +454,10 @@ class PyrasiteWindow(Gtk.Window): ...@@ -450,10 +454,10 @@ class PyrasiteWindow(Gtk.Window):
def inject_js(self): def inject_js(self):
log.debug("Injecting jQuery") log.debug("Injecting jQuery")
js = join(dirname(abspath(__file__)), 'js') js = join(dirname(abspath(__file__)), 'js')
jquery = file(join(js, 'jquery-1.7.1.min.js')) jquery = open(join(js, 'jquery-1.7.1.min.js'))
self.info_view.execute_script(jquery.read()) self.info_view.execute_script(jquery.read())
jquery.close() jquery.close()
sparkline = file(join(js, 'jquery.sparkline.min.js')) sparkline = open(join(js, 'jquery.sparkline.min.js'))
self.info_view.execute_script(sparkline.read()) self.info_view.execute_script(sparkline.read())
sparkline.close() sparkline.close()
...@@ -606,6 +610,9 @@ class PyrasiteWindow(Gtk.Window): ...@@ -606,6 +610,9 @@ class PyrasiteWindow(Gtk.Window):
try: try:
objects = loader.load('/tmp/%d.objects' % proc.pid, objects = loader.load('/tmp/%d.objects' % proc.pid,
show_prog=False) show_prog=False)
except NameError:
log.debug("Meliae not available, continuing...")
return
except: except:
log.debug("Falling back to slower meliae object dump loader") log.debug("Falling back to slower meliae object dump loader")
objects = loader.load('/tmp/%d.objects' % proc.pid, objects = loader.load('/tmp/%d.objects' % proc.pid,
...@@ -638,7 +645,7 @@ class PyrasiteWindow(Gtk.Window): ...@@ -638,7 +645,7 @@ class PyrasiteWindow(Gtk.Window):
payloads = os.path.join(os.path.abspath(os.path.dirname( payloads = os.path.join(os.path.abspath(os.path.dirname(
pyrasite.__file__)), 'payloads') pyrasite.__file__)), 'payloads')
dump_stacks = os.path.join(payloads, 'dump_stacks.py') dump_stacks = os.path.join(payloads, 'dump_stacks.py')
code = proc.cmd(file(dump_stacks).read()) code = proc.cmd(open(dump_stacks).read())
self.update_progress(0.6) self.update_progress(0.6)
self.source_buffer.set_text('') self.source_buffer.set_text('')
...@@ -664,7 +671,7 @@ class PyrasiteWindow(Gtk.Window): ...@@ -664,7 +671,7 @@ class PyrasiteWindow(Gtk.Window):
store.set_value(iter, 2, Pango.Style.NORMAL) store.set_value(iter, 2, Pango.Style.NORMAL)
def create_tree(self): def create_tree(self):
tree_store = ProcessTreeStore() tree_store = ProcessListStore()
tree_view = Gtk.TreeView() tree_view = Gtk.TreeView()
self.tree_view = tree_view self.tree_view = tree_view
tree_view.set_model(tree_store) tree_view.set_model(tree_store)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment