#!/usr/bin/python -O
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: clean_locks 4884 2006-10-31 04:23:06Z zmedico $

import os,sys,errno
try:
	import portage_locks
except ImportError:
	from os import path as osp
	sys.path.insert(0, osp.join(osp.dirname(osp.dirname(osp.realpath(__file__))), "pym"))
	import portage_locks

if not sys.argv[1:] or "--help" in sys.argv or "-h" in sys.argv:
	import portage
	print
	print "You must specify directories with hardlink-locks to clean."
	print "You may optionally specify --force, which will remove all"
	print "of the locks, even if we can't establish if they are in use."
	print "Please attempt cleaning without force first."
	print
	print "%s %s/.locks" % (sys.argv[0], portage.settings["DISTDIR"])
	print "%s --force %s/.locks" % (sys.argv[0], portage.settings["DISTDIR"])
	print
	sys.exit(1)
	
force = False
if "--force" in sys.argv[1:]:
	force=True
	
for x in sys.argv[1:]:
	if x == "--force":
		continue
	try:
		for y in portage_locks.hardlock_cleanup(x, remove_all_locks=force):
			print y
		print
		
	except OSError, e:
		if e.errno in (errno.ENOENT, errno.ENOTDIR):
			print "!!! %s is not a directory or does not exist" % x
		else:
			raise
		sys.exit(e.errno)
