cleanup-workdir 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. cmd = "bitbake -e | grep ^SDK_ARCH="
  46. output = run_command(cmd)
  47. sdk_arch = output.split('"')[1]
  48. # select thest 5 packages to get the dirs of current arch
  49. pkgs = ['hicolor-icon-theme', 'base-files', 'acl-native', 'binutils-crosssdk-' + sdk_arch, 'nativesdk-autoconf']
  50. for pkg in pkgs:
  51. cmd = "bitbake -e " + pkg + " | grep ^IMAGE_ROOTFS="
  52. output = run_command(cmd)
  53. output = output.split('"')[1]
  54. m = re.match(pattern, output)
  55. arch_dirs.append(m.group(1))
  56. def main():
  57. global parser
  58. parser = optparse.OptionParser(
  59. usage = """%prog
  60. %prog removes the obsolete packages' build directories in WORKDIR.
  61. This script must be ran under BUILDDIR after source file \"oe-init-build-env\".
  62. Any file or directory under WORKDIR which is not created by Yocto
  63. will be deleted. Be CAUTIOUS.""")
  64. options, args = parser.parse_args(sys.argv)
  65. builddir = run_command('echo $BUILDDIR').strip()
  66. if len(builddir) == 0:
  67. err_quit("Please source file \"oe-init-build-env\" first.\n")
  68. if os.getcwd() != builddir:
  69. err_quit("Please run %s under: %s\n" % (os.path.basename(args[0]), builddir))
  70. print 'Updating bitbake caches...'
  71. cmd = "bitbake -s"
  72. output = run_command(cmd)
  73. output = output.split('\n')
  74. index = 0
  75. while len(output[index]) > 0:
  76. index += 1
  77. alllines = output[index+1:]
  78. for line in alllines:
  79. # empty again means end of the versions output
  80. if len(line) == 0:
  81. break
  82. line = line.strip()
  83. line = re.sub('\s+', ' ', line)
  84. elems = line.split(' ')
  85. if len(elems) == 2:
  86. version = parse_version(elems[1])
  87. else:
  88. version = parse_version(elems[2])
  89. pkg_cur_dirs[elems[0]] = version
  90. cmd = "bitbake -e"
  91. output = run_command(cmd)
  92. tmpdir = None
  93. image_rootfs = None
  94. output = output.split('\n')
  95. for line in output:
  96. if tmpdir and image_rootfs:
  97. break
  98. if not tmpdir:
  99. m = re.match('TMPDIR="(.*)"', line)
  100. if m:
  101. tmpdir = m.group(1)
  102. if not image_rootfs:
  103. m = re.match('IMAGE_ROOTFS="(.*)"', line)
  104. if m:
  105. image_rootfs = m.group(1)
  106. # won't fail just in case
  107. if not tmpdir or not image_rootfs:
  108. print "Can't get TMPDIR or IMAGE_ROOTFS."
  109. return 1
  110. pattern = tmpdir + '/(.*?)/(.*?)/'
  111. m = re.match(pattern, image_rootfs)
  112. if not m:
  113. print "Can't get WORKDIR."
  114. return 1
  115. workdir = os.path.join(tmpdir, m.group(1))
  116. # we only deal the dirs of current arch, total numbers of dirs are 6
  117. cur_arch_dirs = [m.group(2)]
  118. get_cur_arch_dirs(workdir, cur_arch_dirs)
  119. for workroot, dirs, files in os.walk(workdir):
  120. # For the files, they should NOT exist in WORKDIR. Remove them.
  121. for f in files:
  122. obsolete_dirs.append(os.path.join(workroot, f))
  123. for d in dirs:
  124. if d not in cur_arch_dirs:
  125. continue
  126. for pkgroot, pkgdirs, filenames in os.walk(os.path.join(workroot, d)):
  127. for f in filenames:
  128. obsolete_dirs.append(os.path.join(pkgroot, f))
  129. for pkgdir in sorted(pkgdirs):
  130. if pkgdir not in pkg_cur_dirs:
  131. obsolete_dirs.append(os.path.join(pkgroot, pkgdir))
  132. else:
  133. for verroot, verdirs, verfiles in os.walk(os.path.join(pkgroot, pkgdir)):
  134. for f in verfiles:
  135. obsolete_dirs.append(os.path.join(pkgroot, f))
  136. for v in sorted(verdirs):
  137. if v not in pkg_cur_dirs[pkgdir]:
  138. obsolete_dirs.append(os.path.join(pkgroot, pkgdir, v))
  139. break
  140. # just process the top dir of every package under tmp/work/*/,
  141. # then jump out of the above os.walk()
  142. break
  143. # it is convenient to use os.walk() to get dirs and files at same time
  144. # both of them have been dealed in the loop, so jump out
  145. break
  146. for d in obsolete_dirs:
  147. print "Deleting %s" % d
  148. shutil.rmtree(d, True)
  149. if len(obsolete_dirs):
  150. print '\nTotal %d items.' % len(obsolete_dirs)
  151. else:
  152. print '\nNo obsolete directory found under %s.' % workdir
  153. return 0
  154. if __name__ == '__main__':
  155. try:
  156. ret = main()
  157. except Exception:
  158. ret = 2
  159. import traceback
  160. traceback.print_exc(3)
  161. sys.exit(ret)