Skip to content

Commit 4e7a895

Browse files
author
Jens Timmerman
authored
Merge pull request #222 from stdweird/rin_killtasks_graceful
vsc.utils.run: improve handling tasks and pgids (HPC-5756)
2 parents 2dc7c04 + 07975d3 commit 4e7a895

17 files changed

+234
-130
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
setup.cfg
77

88
# Packages
9+
.eggs
910
*.egg
1011
*.egg-info
1112
dist

bin/logdaemon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def run(self):
114114
self.logger.handle(logrecord)
115115
except (KeyboardInterrupt, SystemExit):
116116
raise
117-
except:
117+
except Exception:
118118
traceback.print_exc()
119119

120120

lib/vsc/utils/affinity.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def convert_hr_bits(self, txt):
120120
for rng in txt.split(','):
121121
indices = [int(x) for x in rng.split('-')] * 2 # always at least 2 elements: twice the same or start,end,start,end
122122

123-
## sanity check
123+
# sanity check
124124
if indices[1] < indices[0]:
125125
self.log.raiseException("convert_hr_bits: end is lower then start in '%s'" % rng)
126126
elif indices[0] < 0:
@@ -153,7 +153,7 @@ def get_cpus(self):
153153
"""
154154
self.cpus = []
155155
for bitmask in getattr(self, '__bits'):
156-
for idx in xrange(NCPUBITS):
156+
for _ in range(NCPUBITS):
157157
self.cpus.append(bitmask & 1)
158158
bitmask >>= 1
159159
return self.cpus
@@ -179,12 +179,12 @@ def set_bits(self, cpus=None):
179179
for idx in xrange(NMASKBITS):
180180
cpus = [2 ** cpuidx for cpuidx, val in enumerate(self.cpus[idx * NCPUBITS:(idx + 1) * NCPUBITS]) if val == 1]
181181
__bits[idx] = cpu_mask_t(sum(cpus))
182-
## sanity check
183-
if not prev_cpus == self.get_cpus():
184-
## get_cpus() rescans
185-
self.log.raiseException("set_bits: something went wrong: previous cpus %s; current ones %s" % (prev_cpus[:20], self.cpus[:20]))
186-
else:
182+
# sanity check
183+
if prev_cpus == self.get_cpus():
187184
self.log.debug("set_bits: new set to %s" % self.convert_bits_hr())
185+
else:
186+
# get_cpus() rescans
187+
self.log.raiseException("set_bits: something went wrong: previous cpus %s; current ones %s" % (prev_cpus[:20], self.cpus[:20]))
188188

189189
def str_cpus(self):
190190
"""Return a string representation of the cpus"""
@@ -234,18 +234,6 @@ def sched_getcpu():
234234
"""Get currently used cpu"""
235235
return _libc.sched_getcpu()
236236

237-
#Utility function
238-
# tobin not used anymore
239-
def tobin(s):
240-
"""Convert integer to binary format"""
241-
## bin() missing in 2.4
242-
# eg: self.cpus.extend([int(x) for x in tobin(bitmask).zfill(NCPUBITS)[::-1]])
243-
if s <= 1:
244-
return str(s)
245-
else:
246-
return tobin(s >> 1) + str(s & 1)
247-
248-
249237
#/* Return the highest priority of any process specified by WHICH and WHO
250238
# (see above); if WHO is zero, the current process, process group, or user
251239
# (as specified by WHO) is used. A lower priority number means higher
@@ -260,7 +248,7 @@ def getpriority(which=None, who=None):
260248
"""Get the priority"""
261249
if which is None:
262250
which = PRIO_PROCESS
263-
elif not which in (PRIO_PROCESS, PRIO_PGRP, PRIO_USER,):
251+
elif which not in (PRIO_PROCESS, PRIO_PGRP, PRIO_USER,):
264252
_logger.raiseException("getpriority: which %s not in correct range" % which)
265253
if who is None:
266254
who = 0 # current which-ever
@@ -275,13 +263,14 @@ def setpriority(prio, which=None, who=None):
275263
"""Set the priority (aka nice)"""
276264
if which is None:
277265
which = PRIO_PROCESS
278-
elif not which in (PRIO_PROCESS, PRIO_PGRP, PRIO_USER,):
266+
elif which not in (PRIO_PROCESS, PRIO_PGRP, PRIO_USER,):
279267
_logger.raiseException("setpriority: which %s not in correct range" % which)
280268
if who is None:
281269
who = 0 # current which-ever
270+
282271
try:
283272
prio = int(prio)
284-
except:
273+
except ValueError:
285274
_logger.raiseException("setpriority: failed to convert priority %s into int" % prio)
286275

287276
if prio < PRIO_MIN or prio > PRIO_MAX:
@@ -298,7 +287,7 @@ def setpriority(prio, which=None, who=None):
298287

299288

300289
if __name__ == '__main__':
301-
## some examples of usage
290+
# some examples of usage
302291
setLogLevelDebug()
303292

304293
cs = cpu_set_t()
@@ -323,8 +312,8 @@ def setpriority(prio, which=None, who=None):
323312

324313
print sched_getcpu()
325314

326-
## resources
327-
## nice -n 5 python affinity.py prints 5 here
315+
# resources
316+
# nice -n 5 python affinity.py prints 5 here
328317
currentprio = getpriority()
329318
print "getpriority", currentprio
330319
newprio = 10

lib/vsc/utils/dateandtime.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@
3434
import time as _time
3535
from datetime import tzinfo, timedelta, datetime, date
3636

37-
try:
38-
any([0, 1])
39-
except:
40-
from vsc.utils.missing import any
41-
4237

4338
class FancyMonth:
4439
"""Convenience class for month math"""
@@ -152,7 +147,7 @@ def interval(self, otherdate):
152147
raise(Exception(msg))
153148
else:
154149
nr = self.number(otherdate)
155-
startdate, enddate = self.get_start_end(otherdate)
150+
startdate, _ = self.get_start_end(otherdate)
156151

157152
start = self.__class__(startdate)
158153
all_dates = [start.get_other(m) for m in range(nr)]
@@ -250,7 +245,7 @@ def datetime_parser(txt):
250245

251246
try:
252247
sects = tmpts[1].split(':')[2].split('.')
253-
except:
248+
except (AttributeError, IndexError):
254249
sects = [0]
255250
# add seconds
256251
datetuple.append(int(sects[0]))
@@ -280,13 +275,13 @@ def timestamp_parser(timestamp):
280275
class UTC(tzinfo):
281276
"""UTC"""
282277

283-
def utcoffset(self, dt):
278+
def utcoffset(self, dt): # pylint:disable=unused-argument
284279
return ZERO
285280

286-
def tzname(self, dt):
281+
def tzname(self, dt): # pylint:disable=unused-argument
287282
return "UTC"
288283

289-
def dst(self, dt):
284+
def dst(self, dt): # pylint:disable=unused-argument
290285
return ZERO
291286

292287
utc = UTC()
@@ -303,13 +298,13 @@ def __init__(self, offset, name):
303298
self.__offset = timedelta(minutes=offset)
304299
self.__name = name
305300

306-
def utcoffset(self, dt):
301+
def utcoffset(self, dt): # pylint:disable=unused-argument
307302
return self.__offset
308303

309-
def tzname(self, dt):
304+
def tzname(self, dt): # pylint:disable=unused-argument
310305
return self.__name
311306

312-
def dst(self, dt):
307+
def dst(self, dt): # pylint:disable=unused-argument
313308
return ZERO
314309

315310

lib/vsc/utils/docs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ def mk_rst_table(titles, columns):
4545
msg = "Number of titles/columns should be equal, found %d titles and %d columns" % (title_cnt, col_cnt)
4646
raise LengthNotEqualException, msg
4747
table = []
48-
col_widths = []
4948
tmpl = []
5049
line= []
5150

lib/vsc/utils/fancylogger.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def log_method(self, msg):
194194
RAISE_EXCEPTION_LOG_METHOD = log_method
195195

196196
# method definition as it is in logging, can't change this
197-
def makeRecord(self, name, level, pathname, lineno, msg, args, excinfo, func=None, extra=None):
197+
def makeRecord(self, name, level, pathname, lineno, msg, args, excinfo, func=None, extra=None): # pylint: disable=unused-argument
198198
"""
199199
overwrite make record to use a fancy record (with more options)
200200
"""
@@ -243,7 +243,7 @@ def raiseException(self, message, exception=None, catch=False):
243243
self.RAISE_EXCEPTION_LOG_METHOD(fullmessage)
244244
raise exception, message, tb
245245

246-
def deprecated(self, msg, cur_ver, max_ver, depth=2, exception=None, *args, **kwargs):
246+
def deprecated(self, msg, cur_ver, max_ver, depth=2, exception=None, *args, **kwargs): # pylint: disable=unused-argument
247247
"""
248248
Log deprecation message, throw error if current version is passed given threshold.
249249
@@ -718,7 +718,7 @@ def _enable_disable_default_handlers(enable):
718718

719719
try:
720720
_default_logTo(enable=enable, handler=handler)
721-
except:
721+
except Exception:
722722
pass
723723

724724

0 commit comments

Comments
 (0)