cleanup-workdir 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 Wind River Systems, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License version 2 as
  6. # published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. # See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. import os
  17. import sys
  18. import optparse
  19. import re
  20. import subprocess
  21. import shutil
  22. pkg_cur_dirs = {}
  23. obsolete_dirs = []
  24. parser = None
  25. def err_quit(msg):
  26. print msg
  27. parser.print_usage()
  28. sys.exit(1)
  29. def parse_version(verstr):
  30. elems = verstr.split(':')
  31. epoch = elems[0]
  32. if len(epoch) == 0:
  33. return elems[1]
  34. else:
  35. return epoch + '_' + elems[1]
  36. def run_command(cmd):
  37. pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
  38. output = pipe.communicate()[0]
  39. if pipe.returncode != 0:
  40. print "Execute command '%s' failed." % cmd
  41. sys.exit(1)
  42. return output
  43. def get_cur_arch_dirs(workdir, arch_dirs):
  44. pattern = workdir + '/(.*?)/'
  45. # select thest 5 packages to get the dirs of current arch
  46. pkgs = ['hicolor-icon-theme', 'base-files', 'acl-native', 'binutils-crosssdk', 'nativesdk-autoconf']
  47. for pkg in pkgs:
  48. cmd = "bitbake -e " + pkg + " | grep ^IMAGE_ROOTFS="
  49. output = run_command(cmd)
  50. output = output.split('"')[1]
  51. m = re.match(pattern, output)
  52. arch_dirs.append(m.group(1))
  53. def main():
  54. global parser
  55. parser = optparse.OptionParser(
  56. usage = """%prog
  57. %prog removes the obsolete packages' build directories in WORKDIR.
  58. This script must be ran under BUILDDIR after source file \"oe-init-build-env\".
  59. Any file or directory under WORKDIR which is not created by Yocto
  60. will be deleted. Be CAUTIOUS.""")
  61. options, args = parser.parse_args(sys.argv)
  62. builddir = run_command('echo $BUILDDIR').strip()
  63. if len(builddir) == 0:
  64. err_quit("Please source file \"oe-init-build-env\" first.\n")
  65. if os.getcwd() != builddir:
  66. err_quit("Please run %s under: %s\n" % (os.path.basename(args[0]), builddir))
  67. print 'Updating bitbake caches...'
  68. cmd = "bitbake -s"
  69. output = run_command(cmd)
  70. output = output.split('\n')
  71. index = 0
  72. while len(output[index]) > 0:
  73. index += 1
  74. alllines = output[index+1:]
  75. for line in alllines:
  76. # empty again means end of the versions output
  77. if len(line) == 0:
  78. break
  79. line = line.strip()
  80. line = re.sub('\s+', ' ', line)
  81. elems = line.split(' ')
  82. if len(elems) == 2:
  83. version = parse_version(elems[1])
  84. else:
  85. version = parse_version(elems[2])
  86. pkg_cur_dirs[elems[0]] = version
  87. cmd = "bitbake -e"
  88. output = run_command(cmd)
  89. tmpdir = None
  90. image_rootfs = None
  91. output = output.split('\n')
  92. for line in output:
  93. if tmpdir and image_rootfs:
  94. break
  95. if not tmpdir:
  96. m = re.match('TMPDIR="(.*)"', line)
  97. if m:
  98. tmpdir = m.group(1)
  99. if not image_rootfs:
  100. m = re.match('IMAGE_ROOTFS="(.*)"', line)
  101. if m:
  102. image_rootfs = m.group(1)
  103. # won't fail just in case
  104. if not tmpdir or not image_rootfs:
  105. print "Can't get TMPDIR or IMAGE_ROOTFS."
  106. return 1
  107. pattern = tmpdir + '/(.*?)/(.*?)/'
  108. m = re.match(pattern, image_rootfs)
  109. if not m:
  110. print "Can't get WORKDIR."
  111. return 1
  112. workdir = os.path.join(tmpdir, m.group(1))
  113. # we only deal the dirs of current arch, total numbers of dirs are 6
  114. cur_arch_dirs = [m.group(2)]
  115. get_cur_arch_dirs(workdir, cur_arch_dirs)
  116. for workroot, dirs, files in os.walk(workdir):
  117. # For the files, they should NOT exist in WORKDIR. Remove them.
  118. for f in files:
  119. obsolete_dirs.append(os.path.join(workroot, f))
  120. for d in dirs:
  121. if d not in cur_arch_dirs:
  122. continue
  123. for pkgroot, pkgdirs, filenames in os.walk(os.path.join(workroot, d)):
  124. for f in filenames:
  125. obsolete_dirs.append(os.path.join(pkgroot, f))
  126. for pkgdir in sorted(pkgdirs):
  127. if pkgdir not in pkg_cur_dirs:
  128. obsolete_dirs.append(os.path.join(pkgroot, pkgdir))
  129. else:
  130. for verroot, verdirs, verfiles in os.walk(os.path.join(pkgroot, pkgdir)):
  131. for f in verfiles:
  132. obsolete_dirs.append(os.path.join(pkgroot, f))
  133. for v in sorted(verdirs):
  134. if v not in pkg_cur_dirs[pkgdir]:
  135. obsolete_dirs.append(os.path.join(pkgroot, pkgdir, v))
  136. break
  137. # just process the top dir of every package under tmp/work/*/,
  138. # then jump out of the above os.walk()
  139. break
  140. # it is convenient to use os.walk() to get dirs and files at same time
  141. # both of them have been dealed in the loop, so jump out
  142. break
  143. for d in obsolete_dirs:
  144. print "Deleting %s" % d
  145. shutil.rmtree(d, True)
  146. if len(obsolete_dirs):
  147. print '\nTotal %d items.' % len(obsolete_dirs)
  148. else:
  149. print '\nNo obsolete directory found under %s.' % workdir
  150. return 0
  151. if __name__ == '__main__':
  152. try:
  153. ret = main()
  154. except Exception:
  155. ret = 2
  156. import traceback
  157. traceback.print_exc(3)
  158. sys.exit(ret)