From f62039a8514432d9a6159e3991ad84c45341b376 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Sun, 5 Jul 2015 19:54:56 -0400 Subject: [PATCH] Fix GMR break with new psutil versions psutil 2.x and above has a lot of API changes as described in: https://github.com/giampaolo/psutil/blob/master/HISTORY.rst So we should work correctly with both old and new psutil versions by using a version check and use the correct method/attributes. Note that we cannot remove the g-r cap until all the projects have switched to oslo.reports (OR we fix oslo-incubator GMR code and have sync'ed all the consuming projects). So this review is the first step in a long process of switching all consumers of GMR to use what we have in oslo.reports. Depends-On: I0f07858e96ea3baf46f8a453e253b9ed29c7f7e2 Depends-On: I33bd2d9dff9cb7dc1a50177db7286b7317966784 Closes-Bug: #1430231 Change-Id: I52bc6898d098f599c12204fc6d6eb269e2c884cb --- oslo_reports/models/process.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/oslo_reports/models/process.py b/oslo_reports/models/process.py index 16c57d9..40af0a1 100644 --- a/oslo_reports/models/process.py +++ b/oslo_reports/models/process.py @@ -18,9 +18,13 @@ This module defines a class representing a process, potentially with subprocesses. """ +import psutil + import oslo_reports.models.with_default_views as mwdv import oslo_reports.views.text.process as text_views +PS1 = psutil.version_info[0] == 1 + class ProcessModel(mwdv.ModelWithDefaultViews): """A Process Model @@ -36,27 +40,34 @@ class ProcessModel(mwdv.ModelWithDefaultViews): text_view=text_views.ProcessView()) self['pid'] = process.pid - self['parent_pid'] = process.ppid + self['parent_pid'] = (process.ppid if PS1 else process.ppid()) if hasattr(process, 'uids'): - self['uids'] = {'real': process.uids.real, - 'effective': process.uids.effective, - 'saved': process.uids.saved} + self['uids'] = { + 'real': (process.uids.real if PS1 else process.uids().real), + 'effective': (process.uids.effective if PS1 + else process.uids().effective), + 'saved': (process.uids.saved if PS1 else process.uids().saved) + } else: self['uids'] = {'real': None, 'effective': None, 'saved': None} if hasattr(process, 'gids'): - self['gids'] = {'real': process.gids.real, - 'effective': process.gids.effective, - 'saved': process.gids.saved} + self['gids'] = { + 'real': (process.gids.real if PS1 else process.gids().real), + 'effective': (process.gids.effective if PS1 + else process.gids().effective), + 'saved': (process.gids.saved if PS1 else process.gids().saved) + } else: self['gids'] = {'real': None, 'effective': None, 'saved': None} - self['username'] = process.username - self['command'] = process.cmdline - self['state'] = process.status + self['username'] = process.username if PS1 else process.username() + self['command'] = process.cmdline if PS1 else process.cmdline() + self['state'] = process.status if PS1 else process.status() - self['children'] = [ProcessModel(pr) for pr in process.get_children()] + children = process.get_children() if PS1 else process.children() + self['children'] = [ProcessModel(pr) for pr in children]