tar source: use usernames and groupnames from tarfile if available
This commit is contained in:
parent
a0447bc26b
commit
7650aa076d
1 changed files with 17 additions and 20 deletions
37
smem
37
smem
|
|
@ -10,11 +10,10 @@
|
||||||
|
|
||||||
import re, os, sys, pwd, grp, optparse, errno, tarfile
|
import re, os, sys, pwd, grp, optparse, errno, tarfile
|
||||||
|
|
||||||
_ucache = {}
|
|
||||||
_gcache = {}
|
|
||||||
|
|
||||||
class procdata(object):
|
class procdata(object):
|
||||||
def __init__(self, source):
|
def __init__(self, source):
|
||||||
|
self._ucache = {}
|
||||||
|
self._gcache = {}
|
||||||
self.source = source and source or ""
|
self.source = source and source or ""
|
||||||
def _list(self):
|
def _list(self):
|
||||||
return os.listdir(self.source + "/proc")
|
return os.listdir(self.source + "/proc")
|
||||||
|
|
@ -56,7 +55,7 @@ class procdata(object):
|
||||||
|
|
||||||
class tardata(procdata):
|
class tardata(procdata):
|
||||||
def __init__(self, source):
|
def __init__(self, source):
|
||||||
self.source = source
|
procdata.__init__(self, source)
|
||||||
self.tar = tarfile.open(source)
|
self.tar = tarfile.open(source)
|
||||||
def _list(self):
|
def _list(self):
|
||||||
for ti in self.tar:
|
for ti in self.tar:
|
||||||
|
|
@ -68,9 +67,19 @@ class tardata(procdata):
|
||||||
def _readlines(self, f):
|
def _readlines(self, f):
|
||||||
return self.tar.extractfile(f).readlines()
|
return self.tar.extractfile(f).readlines()
|
||||||
def piduser(self, p):
|
def piduser(self, p):
|
||||||
return self.tar.getmember("%s/cmdline" % p).uid
|
t = self.tar.getmember("%s/cmdline" % p)
|
||||||
|
if t.uname:
|
||||||
|
self._ucache[t.uid] = t.uname
|
||||||
|
return t.uid
|
||||||
def pidgroup(self, p):
|
def pidgroup(self, p):
|
||||||
return self.tar.getmember("%s/cmdline" % p).gid
|
t = self.tar.getmember("%s/cmdline" % p)
|
||||||
|
if t.gname:
|
||||||
|
self._gcache[t.gid] = t.gname
|
||||||
|
return t.gid
|
||||||
|
def username(self, u):
|
||||||
|
return self._ucache.get(u, str(u))
|
||||||
|
def groupname(self, g):
|
||||||
|
return self._gcache.get(g, str(g))
|
||||||
|
|
||||||
_totalmem = 0
|
_totalmem = 0
|
||||||
def totalmem():
|
def totalmem():
|
||||||
|
|
@ -155,20 +164,8 @@ def fromunits(x):
|
||||||
if x.endswith(k):
|
if x.endswith(k):
|
||||||
return int(float(x[:len(k)])*v)
|
return int(float(x[:len(k)])*v)
|
||||||
|
|
||||||
_ucache = {}
|
|
||||||
def username(uid):
|
|
||||||
if uid not in _ucache:
|
|
||||||
_ucache[uid] = pwd.getpwuid(uid)[0]
|
|
||||||
return _ucache[uid]
|
|
||||||
|
|
||||||
def pidusername(pid):
|
def pidusername(pid):
|
||||||
return username(src.piduser(pid))
|
return src.username(src.piduser(pid))
|
||||||
|
|
||||||
_gcache = {}
|
|
||||||
def groupname(gid):
|
|
||||||
if gid not in _gcache:
|
|
||||||
_gcache[gid] = grp.getgrgid(gid)[0]
|
|
||||||
return _gcache[gid]
|
|
||||||
|
|
||||||
def showamount(a):
|
def showamount(a):
|
||||||
if options.abbreviate:
|
if options.abbreviate:
|
||||||
|
|
@ -348,7 +345,7 @@ def showusers():
|
||||||
def showuser(u):
|
def showuser(u):
|
||||||
if options.numeric:
|
if options.numeric:
|
||||||
return u
|
return u
|
||||||
return username(u)
|
return src.username(u)
|
||||||
|
|
||||||
fields = dict(
|
fields = dict(
|
||||||
user=('User', showuser, '%-8s', None,
|
user=('User', showuser, '%-8s', None,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue