@@ -47,10 +47,14 @@ def find_ancestor(pid=None, username=None):
47
47
username = get_username ()
48
48
process = psutil .Process (pid )
49
49
parents = process .parents ()
50
- for parent in reversed (parents ):
51
- if parent .username () == username :
52
- return parent
53
- return process
50
+ return next (
51
+ (
52
+ parent
53
+ for parent in reversed (parents )
54
+ if parent .username () == username
55
+ ),
56
+ process ,
57
+ )
54
58
55
59
56
60
def get_cmdline (process ):
@@ -63,11 +67,11 @@ def get_affinity(process):
63
67
64
68
65
69
def get_read_open_files (process ):
66
- open_files = list ()
70
+ open_files = []
67
71
try :
68
72
for file in process .open_files ():
69
73
try :
70
- if 'r' == file . mode :
74
+ if file . mode == 'r' :
71
75
open_files .append (file .path )
72
76
except :
73
77
pass
@@ -77,11 +81,11 @@ def get_read_open_files(process):
77
81
78
82
79
83
def get_write_open_files (process ):
80
- open_files = list ()
84
+ open_files = []
81
85
try :
82
86
for file in process .open_files ():
83
87
try :
84
- if 'r' != file . mode :
88
+ if file . mode != 'r' :
85
89
open_files .append (f'{ file .path } :{ Path (file .path ).stat ().st_size } ' )
86
90
except :
87
91
pass
@@ -91,8 +95,7 @@ def get_write_open_files(process):
91
95
92
96
93
97
def define_actions (inactive = None ):
94
- metrics = dict ()
95
- metrics ['time' ] = Metric ('time' , lambda x : time .time ())
98
+ metrics = {'time' : Metric ('time' , lambda x : time .time ())}
96
99
metrics ['node' ] = Metric ('node' , lambda x : platform .node ())
97
100
metrics ['pid' ] = Metric ('pid' , lambda x : x .pid )
98
101
metrics ['ppid' ] = Metric ('ppid' , lambda x : x .ppid ())
@@ -121,11 +124,13 @@ def status_header(metrics):
121
124
122
125
def process_status (process , metrics ):
123
126
'''Show properties of the specified process'''
124
- status = list ()
127
+ status = []
125
128
with process .oneshot ():
126
- for metric in metrics .values ():
127
- if metric .is_active :
128
- status .append (metric .measure (process ))
129
+ status .extend (
130
+ metric .measure (process )
131
+ for metric in metrics .values ()
132
+ if metric .is_active
133
+ )
129
134
return ',' .join (status )
130
135
131
136
@@ -150,20 +155,18 @@ def main():
150
155
if not options .affinity :
151
156
inactive .append ('affinity' )
152
157
if not options .files :
153
- inactive .append ('read_files' )
154
- inactive .append ('write_files' )
158
+ inactive .extend (('read_files' , 'write_files' ))
155
159
metrics = define_actions (inactive )
156
- if options .output_file :
157
- file = open (options .output_file , 'w' )
158
- else :
159
- file = sys .stdout
160
+ file = open (options .output_file , 'w' ) if options .output_file else sys .stdout
160
161
try :
161
162
with file :
162
163
print (status_header (metrics ), file = file )
163
164
while True :
164
165
process_info = [process_status (process , metrics )]
165
- for child_process in process .children (recursive = True ):
166
- process_info .append (process_status (child_process , metrics ))
166
+ process_info .extend (
167
+ process_status (child_process , metrics )
168
+ for child_process in process .children (recursive = True )
169
+ )
167
170
print ('\n ' .join (process_info ), file = file )
168
171
time .sleep (options .delta )
169
172
except KeyboardInterrupt :
0 commit comments