File: oz-cleanup-cache

package info (click to toggle)
oz 0.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,620 kB
  • sloc: python: 6,993; xml: 654; perl: 614; makefile: 45; sh: 21
file content (119 lines) | stat: -rwxr-xr-x 3,866 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

# Copyright (C) 2011       Chris Lalancette <clalance@redhat.com>
# Copyright (C) 2012-2016  Chris Lalancette <clalancette@gmail.com>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.

# This library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

import sys
import getopt
import os
import shutil
import logging

import oz.ozutil

def usage():
    print("Usage: oz-cleanup-cache [OPTIONS]")
    print("  -c <config>\tGet config from <config> (default is /etc/oz/oz.cfg)")
    print("  -d <level>\tTurn up logging level.  The levels are:")
    print("\t\t\t0 - errors only (this is the default)")
    print("\t\t\t1 - errors and warnings")
    print("\t\t\t2 - errors, warnings, and information")
    print("\t\t\t3 - all messages")
    print("\t\t\t4 - all messages, prepended with the level and classname")
    print("  -f\t\tDon't ask any questions and just blindly remove all oz data")
    print("  -h\t\tPrint this help message")
    sys.exit(1)

try:
    opts, args = getopt.gnu_getopt(sys.argv[1:], 'c:d:fh',
                                   ['config', 'debug', 'force', 'help'])
except getopt.GetoptError as err:
    print(str(err))
    usage()

force = False
config_file = None
loglevel = logging.ERROR
logformat = "%(message)s"
for o, a in opts:
    if o in ("-c", "--config"):
        config_file = a
    elif o in ("-d", "--debug"):
        try:
            d_int = int(a)
        except ValueError:
            usage()
        if d_int == 0:
            loglevel = logging.ERROR
        elif d_int == 1:
            loglevel = logging.WARNING
        elif d_int == 2:
            loglevel = logging.INFO
        elif d_int == 3:
            loglevel = logging.DEBUG
        elif d_int >= 4:
            loglevel = logging.DEBUG
            logformat = logging.BASIC_FORMAT
    elif o in ("-f", "--force"):
        force = True
    elif o in ("-h", "--help"):
        usage()
    else:
        assert False, "unhandled option"

if len(args) != 0:
    usage()

try:
    config = oz.ozutil.parse_config(config_file)

    data_dir = oz.ozutil.config_get_path(config, 'paths', 'data_dir',
                                         oz.ozutil.default_data_dir())

    dirs = ["floppies", "floppycontent", "icicletmp", "isocontent", "isos",
            "jeos", "kernels", "screenshots"]
    caches = []
    for path in dirs:
        caches.append(os.path.join(data_dir, path))

    if not force:
        while True:
            response = raw_input("Going to remove all content from %s; continue? (y/N) " % (', '.join(caches)))
            if len(response) == 0 or response.lower() == 'n':
                sys.exit(0)
            elif response.lower() == 'y':
                break
            else:
                print("Please enter 'y' or 'n'")

    for cache in caches:
        print("Removing cached content from %s" % (cache))
        for root, dirs, files in os.walk(cache):
            for f in files:
                os.unlink(os.path.join(root, f))
            for d in dirs:
                shutil.rmtree(os.path.join(root, d))

except Exception as exc:
    if loglevel > logging.DEBUG:
        print("")
        print("ERROR: %s" % (str(exc)))
        print("")
        print("(use -d3 to get the full backtrace)")
        print("")
    else:
        raise