Skip to content
This repository has been archived by the owner on Sep 15, 2021. It is now read-only.

Commit

Permalink
Bug 475276 - Move dirs aside before deleting them - r=catlee
Browse files Browse the repository at this point in the history
  • Loading branch information
ccooper committed Jun 13, 2009
1 parent 488e7f7 commit cbebf9e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
52 changes: 51 additions & 1 deletion buildfarm/maintenance/purge_builds.py
Expand Up @@ -16,6 +16,8 @@

import os, shutil, re, sys

clobber_suffix='.deleteme'

if sys.platform == 'win32':
# os.statvfs doesn't work on Windows
import win32file
Expand All @@ -32,6 +34,43 @@ def mtime_sort(p1, p2):
"sorting function for sorting a list of paths by mtime"
return cmp(os.path.getmtime(p1), os.path.getmtime(p2))

def rmdirRecursive(dir):
"""This is a replacement for shutil.rmtree that works better under
windows. Thanks to Bear at the OSAF for the code.
(Borrowed from buildbot.slave.commands)"""
if not os.path.exists(dir):
# This handles broken links
if os.path.islink(dir):
os.remove(dir)
return

if os.path.islink(dir):
os.remove(dir)
return

# Verify the directory is read/write/execute for the current user
os.chmod(dir, 0700)

for name in os.listdir(dir):
full_name = os.path.join(dir, name)
# on Windows, if we don't have write permission we can't remove
# the file/directory either, so turn that on
if os.name == 'nt':
if not os.access(full_name, os.W_OK):
# I think this is now redundant, but I don't have an NT
# machine to test on, so I'm going to leave it in place
# -warner
os.chmod(full_name, 0600)

if os.path.isdir(full_name):
rmdirRecursive(full_name)
else:
# Don't try to chmod links
if not os.path.islink(full_name):
os.chmod(full_name, 0700)
os.remove(full_name)
os.rmdir(dir)

def purge(base_dir, gigs, ignore, dry_run=False):
"""Delete directories under `base_dir` until `gigs` GB are free
Expand All @@ -47,7 +86,18 @@ def purge(base_dir, gigs, ignore, dry_run=False):
d = dirs.pop(0)
print "Deleting", d
if not dry_run:
shutil.rmtree(d, ignore_errors=True)
try:
clobber_path=d+clobber_suffix
if os.path.exists(clobber_path):
rmdirRecursive(clobber_path)
# Prevent repeated moving.
if d.endswith(clobber_suffix):
rmdirRecursive(d)
else:
shutil.move(d, clobber_path)
rmdirRecursive(clobber_path)
except:
print >>sys.stderr, "Couldn't purge %s properly. Skipping." % d

if __name__ == '__main__':
import sys
Expand Down
21 changes: 19 additions & 2 deletions clobberer/clobberer.py
@@ -1,6 +1,8 @@
import sys, shutil, urllib2, urllib, os
from datetime import datetime, timedelta

clobber_suffix='.deleteme'

def str_to_datetime(s):
return datetime.strptime(s, "%Y-%m-%d %H:%M:%S")

Expand Down Expand Up @@ -67,14 +69,29 @@ def do_clobber(dryrun=False, skip=None):
if skip is not None and f in skip:
print "Skipping", f
continue
clobber_path=f+clobber_suffix
if os.path.isfile(f):
print "Removing", f
if not dryrun:
os.unlink(f)
if os.path.exists(clobber_path):
os.unlink(clobber_path)
# Prevent repeated moving.
if f.endswith(clobber_suffix):
os.unlink(f)
else:
shutil.move(f, clobber_path)
os.unlink(clobber_path)
elif os.path.isdir(f):
print "Removing %s/" % f
if not dryrun:
rmdirRecursive(f)
if os.path.exists(clobber_path):
rmdirRecursive(clobber_path)
# Prevent repeated moving.
if d.endswith(clobber_suffix):
rmdirRecursive(d)
else:
shutil.move(f, clobber_path)
rmdirRecursive(clobber_path)
except:
print "Couldn't clobber properly, bailing out."
sys.exit(1)
Expand Down

0 comments on commit cbebf9e

Please sign in to comment.