aboutsummaryrefslogtreecommitdiffstats
path: root/janitor/clobberdir
blob: 16b019ece423af87d0914c0737799a3a7f7e13b7 (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
#!/usr/bin/env python3
#
# Copyright Linux Foundation, Richard Purdie
#
# SPDX-License-Identifer: GPL-2.0-only
#
# Delete a directory using the ionice backgrounded command
#
# Called with $1 - Our config file
#             $2 - The directory to delete
#

import os
import sys
import subprocess
import errno
import time
import random

sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'scripts'))

import utils

ourconfig = utils.loadconfig()


def mkdir(path):
    try:
        os.makedirs(path)
    except OSError as e:
        if e.errno != errno.EEXIST:
            # Do not complain if the directory exists
            raise e

if len(sys.argv) != 2:
    print("Incorrect number of parameters, please call as %s <clobber-dir>" % sys.argv[0])
    sys.exit(1)

clobberdir = sys.argv[1]

if clobberdir == "/" or clobberdir == ".":
    print("Deleting %s is probably not what you want" % clobberdir)
    sys.exit(1)


if "TRASH_DIR" not in ourconfig:
    print("Please set TRASH_DIR in the configuration file")
    sys.exit(1)

trashdir = utils.getconfig("TRASH_DIR", ourconfig)

for x in [clobberdir]:
    if os.path.exists(x) and os.path.exists(trashdir):
        if (os.stat(trashdir).st_dev == os.stat(x).st_dev):
            trashdest = trashdir + "/" + str(int(time.time())) + '-'  + str(random.randrange(100, 100000, 2))
            mkdir(trashdest)
            subprocess.check_call(['mv', x, trashdest])
        else:
            subprocess.check_call(['rm', "-rf", x])