2012-03-06 03:19:15 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright (c) 2012 Wind River Systems, Inc.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
# See the GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import optparse
|
|
|
|
import re
|
2012-06-15 02:56:11 +00:00
|
|
|
import subprocess
|
2012-03-06 03:19:15 +00:00
|
|
|
import shutil
|
|
|
|
|
2012-06-15 02:20:18 +00:00
|
|
|
pkg_cur_dirs = []
|
2012-03-06 03:19:15 +00:00
|
|
|
obsolete_dirs = []
|
|
|
|
parser = None
|
|
|
|
|
|
|
|
def err_quit(msg):
|
|
|
|
print msg
|
|
|
|
parser.print_usage()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def parse_version(verstr):
|
|
|
|
elems = verstr.split(':')
|
|
|
|
epoch = elems[0]
|
|
|
|
if len(epoch) == 0:
|
|
|
|
return elems[1]
|
|
|
|
else:
|
|
|
|
return epoch + '_' + elems[1]
|
|
|
|
|
2012-06-15 02:56:11 +00:00
|
|
|
def run_command(cmd):
|
|
|
|
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
|
|
|
|
output = pipe.communicate()[0]
|
|
|
|
if pipe.returncode != 0:
|
|
|
|
print "Execute command '%s' failed." % cmd
|
|
|
|
sys.exit(1)
|
|
|
|
return output
|
|
|
|
|
2012-03-06 03:19:15 +00:00
|
|
|
def main():
|
|
|
|
global parser
|
|
|
|
parser = optparse.OptionParser(
|
|
|
|
usage = """%prog
|
|
|
|
|
|
|
|
Remove the obsolete packages' build directories in WORKDIR.
|
|
|
|
This script must be ran under BUILDDIR after source file \"oe-init-build-env\".""")
|
|
|
|
|
|
|
|
options, args = parser.parse_args(sys.argv)
|
|
|
|
|
2012-06-15 02:56:11 +00:00
|
|
|
builddir = run_command('echo $BUILDDIR').strip()
|
2012-03-06 03:19:15 +00:00
|
|
|
if len(builddir) == 0:
|
|
|
|
err_quit("Please source file \"oe-init-build-env\" first.\n")
|
|
|
|
|
|
|
|
if os.getcwd() != builddir:
|
|
|
|
err_quit("Please run %s under: %s\n" % (os.path.basename(args[0]), builddir))
|
|
|
|
|
|
|
|
print 'Updating bitbake caches...'
|
|
|
|
cmd = "bitbake -s"
|
2012-06-15 02:56:11 +00:00
|
|
|
output = run_command(cmd)
|
2012-03-06 03:19:15 +00:00
|
|
|
|
|
|
|
output = output.split('\n')
|
|
|
|
index = 0
|
|
|
|
while len(output[index]) > 0:
|
|
|
|
index += 1
|
|
|
|
alllines = output[index+1:]
|
|
|
|
|
|
|
|
for line in alllines:
|
|
|
|
# empty again means end of the versions output
|
|
|
|
if len(line) == 0:
|
|
|
|
break
|
|
|
|
line = line.strip()
|
|
|
|
line = re.sub('\s+', ' ', line)
|
|
|
|
elems = line.split(' ')
|
|
|
|
if len(elems) == 2:
|
|
|
|
version = parse_version(elems[1])
|
|
|
|
else:
|
|
|
|
version = parse_version(elems[2])
|
2012-06-15 02:20:18 +00:00
|
|
|
pkg_cur_dirs.append(elems[0] + '-' + version)
|
2012-03-06 03:19:15 +00:00
|
|
|
|
|
|
|
cmd = "bitbake -e | grep ^TMPDIR"
|
2012-06-15 02:56:11 +00:00
|
|
|
output = run_command(cmd)
|
2012-03-06 03:19:15 +00:00
|
|
|
|
|
|
|
tmpdir = output.split('"')[1]
|
|
|
|
workdir = os.path.join(tmpdir, 'work')
|
|
|
|
if not os.path.exists(workdir):
|
|
|
|
print "WORKDIR %s does NOT exist. Quit." % workdir
|
|
|
|
return 1
|
|
|
|
|
2012-06-15 02:20:18 +00:00
|
|
|
for workroot, dirs, files in os.walk(workdir):
|
|
|
|
# For the files, they should NOT exist in WORKDIR. Romve them.
|
|
|
|
for f in files:
|
|
|
|
obsolete_dirs.append(os.path.join(workroot, f))
|
|
|
|
|
|
|
|
for d in dirs:
|
|
|
|
for pkgroot, pkgdirs, filenames in os.walk(os.path.join(workroot, d)):
|
|
|
|
for f in filenames:
|
|
|
|
obsolete_dirs.append(os.path.join(pkgroot, f))
|
|
|
|
|
|
|
|
for pkgdir in sorted(pkgdirs):
|
|
|
|
if pkgdir not in pkg_cur_dirs:
|
|
|
|
obsolete_dirs.append(os.path.join(pkgroot, pkgdir))
|
|
|
|
|
|
|
|
# just process the top dir of every package under tmp/work/*/,
|
|
|
|
# then jump out of the above os.walk()
|
|
|
|
break
|
|
|
|
|
|
|
|
# it is convenient to use os.walk() to get dirs and files at same time
|
|
|
|
# both of them have been dealed in the loop, so jump out
|
|
|
|
break
|
2012-03-06 03:19:15 +00:00
|
|
|
|
|
|
|
for d in obsolete_dirs:
|
|
|
|
print "Deleleting %s" % d
|
|
|
|
shutil.rmtree(d, True)
|
|
|
|
|
|
|
|
if len(obsolete_dirs):
|
|
|
|
print '\nTotal %d items.' % len(obsolete_dirs)
|
|
|
|
else:
|
|
|
|
print '\nNo obsolete directory found under %s.' % workdir
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
ret = main()
|
|
|
|
except Exception:
|
|
|
|
ret = 2
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc(3)
|
|
|
|
sys.exit(ret)
|