1# Copyright 2016 The Meson development team
2
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6
7#     http://www.apache.org/licenses/LICENSE-2.0
8
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import sys
17import shutil
18import pickle
19
20def rmtrees(build_dir, trees):
21    for t in trees:
22        # Never delete trees outside of the builddir
23        if os.path.isabs(t):
24            print('Cannot delete dir with absolute path {!r}'.format(t))
25            continue
26        bt = os.path.join(build_dir, t)
27        # Skip if it doesn't exist, or if it is not a directory
28        if os.path.isdir(bt):
29            shutil.rmtree(bt, ignore_errors=True)
30
31def run(args):
32    if len(args) != 1:
33        print('Cleaner script for Meson. Do not run on your own please.')
34        print('cleantrees.py <data-file>')
35        return 1
36    with open(args[0], 'rb') as f:
37        data = pickle.load(f)
38    rmtrees(data.build_dir, data.trees)
39    # Never fail cleaning
40    return 0
41
42if __name__ == '__main__':
43    run(sys.argv[1:])
44