aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/cleanup-workdir
blob: b77e8c664f66beffd6faf4e6626aa590d4af22dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/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
import commands
import shutil

versions = {}
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]

def parse_dir(match, pkgabsdir):
    pkg_name = match.group(1)
    pkg_version = match.group(2)
    if pkg_name in versions:
        if pkg_version != versions[pkg_name]:
            obsolete_dirs.append(pkgabsdir)
        return True
    return False

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)

    builddir = commands.getoutput('echo $BUILDDIR')
    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"
    (ret, output) = commands.getstatusoutput(cmd)
    if ret != 0:
        print "Execute 'bitbake -s' failed. Can't get packages' versions."
        return 1

    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])
        versions[elems[0]] = version

    cmd = "bitbake -e | grep ^TMPDIR"
    (ret, output) = commands.getstatusoutput(cmd)
    if ret != 0:
        print "Execute 'bitbke -e' failed. Can't get TMPDIR."
        return 1

    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

    for archdir in os.listdir(workdir):
        archdir = os.path.join(workdir, archdir)
        if not os.path.isdir(archdir):
            pass

        for pkgdir in sorted(os.listdir(archdir)):
            pkgabsdir = os.path.join(archdir, pkgdir)
            if not os.path.isdir(pkgabsdir):
                pass

            # parse the package directory names
            # parse native/nativesdk packages first
            match = re.match('(.*?-native.*?)-(.*)', pkgdir)
            if match and parse_dir(match, pkgabsdir):
                continue

            # parse package names which ends with numbers such as 'glib-2.0'
            match = re.match('(.*?-[\.\d]+)-(\d.*)', pkgdir)
            if match and parse_dir(match, pkgabsdir):
                continue

            # other packages
            match = re.match('(.*?)-(\d.*)', pkgdir)
            if match and parse_dir(match, pkgabsdir):
                continue

    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)