Files
mongo/buildscripts/cleanbb.py

115 lines
3.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2010-02-04 10:21:36 -05:00
import re
2010-02-04 10:21:36 -05:00
import sys
import os, os.path
import shutil
2010-02-04 10:21:36 -05:00
import utils
import time
from optparse import OptionParser
2010-02-04 10:21:36 -05:00
def shouldKill( c, root=None ):
2010-06-02 13:21:29 -04:00
2012-05-24 17:51:08 -04:00
if "smoke.py" in c:
return False
2012-05-11 13:55:47 -04:00
if "emr.py" in c:
return False
2012-05-24 17:51:08 -04:00
if "java" in c:
return False
# if root directory is provided, see if command line matches mongod process running
# with the same data directory
if root and re.compile("(\W|^)mongod(.exe)?\s+.*--dbpath(\s+|=)%s(\s+|$)" % root).search( c ):
2010-06-02 13:21:29 -04:00
return True
if ( c.find( "buildbot" ) >= 0 or c.find( "slave" ) >= 0 ) and c.find( "/mongo/" ) >= 0:
2010-06-02 13:21:29 -04:00
return True
2013-01-04 18:38:46 -05:00
if c.find( "xml-data/build-dir" ) >= 0: # for bamboo
return True
2010-06-02 13:21:29 -04:00
return False
def killprocs( signal="", root=None ):
2010-02-04 10:21:36 -05:00
killed = 0
2012-04-20 14:10:07 -04:00
if sys.platform == 'win32':
return killed
2010-06-02 15:03:03 -04:00
l = utils.getprocesslist()
print( "num procs:" + str( len( l ) ) )
if len(l) == 0:
print( "no procs" )
try:
print( execsys( "/sbin/ifconfig -a" ) )
except Exception,e:
print( "can't get interfaces" + str( e ) )
for x in l:
2010-02-04 10:21:36 -05:00
x = x.lstrip()
if not shouldKill( x, root=root ):
2010-02-04 10:21:36 -05:00
continue
pid = x.split( " " )[0]
2010-02-04 10:21:36 -05:00
print( "killing: " + x )
utils.execsys( "/bin/kill " + signal + " " + pid )
killed = killed + 1
return killed
def tryToRemove(path):
for _ in range(60):
try:
os.remove(path)
return True
except OSError, e:
errno = getattr(e, 'winerror', None)
# check for the access denied and file in use WindowsErrors
if errno in (5, 32):
print("os.remove(%s) failed, retrying in one second." % path)
time.sleep(1)
else:
raise e
return False
def cleanup( root , nokill ):
if nokill:
print "nokill requested, not killing anybody"
else:
if killprocs( root=root ) > 0:
time.sleep(3)
killprocs( "-9", root=root )
2010-06-02 13:39:01 -04:00
2010-02-04 10:21:36 -05:00
# delete all regular files, directories can stay
# NOTE: if we delete directories later, we can't delete diskfulltest
for ( dirpath , dirnames , filenames ) in os.walk( root , topdown=False ):
for x in filenames:
2010-06-02 15:14:51 -04:00
foo = dirpath + "/" + x
if os.path.exists(foo):
if not tryToRemove(foo):
raise Exception("Couldn't remove file '%s' after 60 seconds" % foo)
2010-02-04 10:21:36 -05:00
# delete all directories under root.
for directoryEntry in os.listdir(root):
if directoryEntry == 'diskfulltest':
continue
path = root + '/' + directoryEntry
if os.path.isdir(path):
shutil.rmtree(path, ignore_errors=True)
2010-02-04 10:21:36 -05:00
if __name__ == "__main__":
parser = OptionParser(usage="read the script")
parser.add_option("--nokill", dest='nokill', default=False, action='store_true')
(options, args) = parser.parse_args()
2010-02-04 10:21:36 -05:00
root = "/data/db/"
if len(args) > 0:
root = args[0]
cleanup( root , options.nokill )