diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/cleanup-workdir | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/scripts/cleanup-workdir b/scripts/cleanup-workdir new file mode 100755 index 0000000000..b77e8c664f --- /dev/null +++ b/scripts/cleanup-workdir | |||
@@ -0,0 +1,150 @@ | |||
1 | #!/usr/bin/env python | ||
2 | |||
3 | # Copyright (c) 2012 Wind River Systems, Inc. | ||
4 | # | ||
5 | # This program is free software; you can redistribute it and/or modify | ||
6 | # it under the terms of the GNU General Public License version 2 as | ||
7 | # published by the Free Software Foundation. | ||
8 | # | ||
9 | # This program is distributed in the hope that it will be useful, | ||
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
12 | # See the GNU General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU General Public License | ||
15 | # along with this program; if not, write to the Free Software | ||
16 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
17 | |||
18 | import os | ||
19 | import sys | ||
20 | import optparse | ||
21 | import re | ||
22 | import commands | ||
23 | import shutil | ||
24 | |||
25 | versions = {} | ||
26 | obsolete_dirs = [] | ||
27 | parser = None | ||
28 | |||
29 | def err_quit(msg): | ||
30 | print msg | ||
31 | parser.print_usage() | ||
32 | sys.exit(1) | ||
33 | |||
34 | def parse_version(verstr): | ||
35 | elems = verstr.split(':') | ||
36 | epoch = elems[0] | ||
37 | if len(epoch) == 0: | ||
38 | return elems[1] | ||
39 | else: | ||
40 | return epoch + '_' + elems[1] | ||
41 | |||
42 | def parse_dir(match, pkgabsdir): | ||
43 | pkg_name = match.group(1) | ||
44 | pkg_version = match.group(2) | ||
45 | if pkg_name in versions: | ||
46 | if pkg_version != versions[pkg_name]: | ||
47 | obsolete_dirs.append(pkgabsdir) | ||
48 | return True | ||
49 | return False | ||
50 | |||
51 | def main(): | ||
52 | global parser | ||
53 | parser = optparse.OptionParser( | ||
54 | usage = """%prog | ||
55 | |||
56 | Remove the obsolete packages' build directories in WORKDIR. | ||
57 | This script must be ran under BUILDDIR after source file \"oe-init-build-env\".""") | ||
58 | |||
59 | options, args = parser.parse_args(sys.argv) | ||
60 | |||
61 | builddir = commands.getoutput('echo $BUILDDIR') | ||
62 | if len(builddir) == 0: | ||
63 | err_quit("Please source file \"oe-init-build-env\" first.\n") | ||
64 | |||
65 | if os.getcwd() != builddir: | ||
66 | err_quit("Please run %s under: %s\n" % (os.path.basename(args[0]), builddir)) | ||
67 | |||
68 | print 'Updating bitbake caches...' | ||
69 | cmd = "bitbake -s" | ||
70 | (ret, output) = commands.getstatusoutput(cmd) | ||
71 | if ret != 0: | ||
72 | print "Execute 'bitbake -s' failed. Can't get packages' versions." | ||
73 | return 1 | ||
74 | |||
75 | output = output.split('\n') | ||
76 | index = 0 | ||
77 | while len(output[index]) > 0: | ||
78 | index += 1 | ||
79 | alllines = output[index+1:] | ||
80 | |||
81 | for line in alllines: | ||
82 | # empty again means end of the versions output | ||
83 | if len(line) == 0: | ||
84 | break | ||
85 | line = line.strip() | ||
86 | line = re.sub('\s+', ' ', line) | ||
87 | elems = line.split(' ') | ||
88 | if len(elems) == 2: | ||
89 | version = parse_version(elems[1]) | ||
90 | else: | ||
91 | version = parse_version(elems[2]) | ||
92 | versions[elems[0]] = version | ||
93 | |||
94 | cmd = "bitbake -e | grep ^TMPDIR" | ||
95 | (ret, output) = commands.getstatusoutput(cmd) | ||
96 | if ret != 0: | ||
97 | print "Execute 'bitbke -e' failed. Can't get TMPDIR." | ||
98 | return 1 | ||
99 | |||
100 | tmpdir = output.split('"')[1] | ||
101 | workdir = os.path.join(tmpdir, 'work') | ||
102 | if not os.path.exists(workdir): | ||
103 | print "WORKDIR %s does NOT exist. Quit." % workdir | ||
104 | return 1 | ||
105 | |||
106 | for archdir in os.listdir(workdir): | ||
107 | archdir = os.path.join(workdir, archdir) | ||
108 | if not os.path.isdir(archdir): | ||
109 | pass | ||
110 | |||
111 | for pkgdir in sorted(os.listdir(archdir)): | ||
112 | pkgabsdir = os.path.join(archdir, pkgdir) | ||
113 | if not os.path.isdir(pkgabsdir): | ||
114 | pass | ||
115 | |||
116 | # parse the package directory names | ||
117 | # parse native/nativesdk packages first | ||
118 | match = re.match('(.*?-native.*?)-(.*)', pkgdir) | ||
119 | if match and parse_dir(match, pkgabsdir): | ||
120 | continue | ||
121 | |||
122 | # parse package names which ends with numbers such as 'glib-2.0' | ||
123 | match = re.match('(.*?-[\.\d]+)-(\d.*)', pkgdir) | ||
124 | if match and parse_dir(match, pkgabsdir): | ||
125 | continue | ||
126 | |||
127 | # other packages | ||
128 | match = re.match('(.*?)-(\d.*)', pkgdir) | ||
129 | if match and parse_dir(match, pkgabsdir): | ||
130 | continue | ||
131 | |||
132 | for d in obsolete_dirs: | ||
133 | print "Deleleting %s" % d | ||
134 | shutil.rmtree(d, True) | ||
135 | |||
136 | if len(obsolete_dirs): | ||
137 | print '\nTotal %d items.' % len(obsolete_dirs) | ||
138 | else: | ||
139 | print '\nNo obsolete directory found under %s.' % workdir | ||
140 | |||
141 | return 0 | ||
142 | |||
143 | if __name__ == '__main__': | ||
144 | try: | ||
145 | ret = main() | ||
146 | except Exception: | ||
147 | ret = 2 | ||
148 | import traceback | ||
149 | traceback.print_exc(3) | ||
150 | sys.exit(ret) | ||