Révision | 40f2cd20458cb6aed9c6c680393637df679f6171 (tree) |
---|---|
l'heure | 2019-07-26 19:13:40 |
Auteur | ksmt4699 <ksmt4699@gmai...> |
Commiter | ksmt4699 |
(一応)オブジェクト化
@@ -1,32 +1,28 @@ | ||
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | import subprocess |
3 | 3 | |
4 | -# Remove APT cache | |
5 | -def cleanaptcache(): | |
6 | - print("APT-cache size is {}MiB.\nAre you clean it?".format(get_aptcachesize())) | |
7 | - anser = yesno(input("Y/n ")) | |
8 | - if anser == True: | |
9 | - cleanit() | |
10 | - else: | |
11 | - return 0 | |
12 | - | |
13 | -def get_aptcachesize(): | |
14 | - output = subprocess.run(["sudo", "du", "-sm", "/var/cache/apt"], stdout=subprocess.PIPE) | |
15 | - rawout = output.stdout.decode("UTF-8") | |
16 | - size = [int(s) for s in rawout.split() if s.isdigit()][0] | |
17 | - return size | |
18 | - | |
19 | -def cleanit(): | |
20 | - return subprocess.check_output(["sudo", "apt-get", "clean"]) | |
21 | - | |
4 | +class clearcache(): | |
5 | + def __init__(self, path): | |
6 | + rpath = path + r"/*" | |
7 | + self.searchpath = path.split() | |
8 | + self.removepath = rpath | |
9 | + def get_cachesize(self): | |
10 | + cmd = ["du", "-sm",] | |
11 | + cmd.extend(self.searchpath) | |
12 | + output = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) | |
13 | + rawout = output.stdout.decode("UTF-8") | |
14 | + size = [int(s) for s in rawout.split() if s.isdigit()][0] | |
15 | + return size | |
16 | + def cleanit(self): | |
17 | + cmd = "sudo rm -r " + self.removepath | |
18 | + subprocess.run(cmd, shell=True) | |
19 | + return "Done" | |
22 | 20 | |
23 | 21 | # Remove old kernels |
24 | -def removekernel(): | |
25 | - olders = [] | |
22 | +class removekernel(): | |
26 | 23 | removed = [] |
27 | - def get_oldkernels(): | |
28 | - global olders | |
29 | - global removed | |
24 | + @classmethod | |
25 | + def get_oldkernels(self): | |
30 | 26 | output = subprocess.run(r'dpkg -l | grep -Eo "linux-image-[0-9]+\.[0-9]+\.[0-9]+-[0-9]+[0-9]" | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+-[0-9]+" | uniq | sort -nr', shell=True, stdout=subprocess.PIPE) |
31 | 27 | rawout = output.stdout.decode("UTF-8") |
32 | 28 | installing = rawout.splitlines() |
@@ -36,24 +32,15 @@ def removekernel(): | ||
36 | 32 | print("Do not have to remove old kernel.") |
37 | 33 | exit(0) |
38 | 34 | terget = ("linux-headers-", "linux-image-") |
39 | - removed = [tgt + version for version in olders for tgt in terget ] | |
40 | - return removed | |
41 | - | |
42 | - def removethem(): | |
43 | - global removed | |
35 | + self.removed = [tgt + version for version in olders for tgt in terget ] | |
36 | + return self.removed | |
37 | + @classmethod | |
38 | + def removethem(self): | |
44 | 39 | cmd = "sudo apt-get --purge -y autoremove" |
45 | 40 | command_l = cmd.split() |
46 | - command_l.extend(removed) | |
41 | + command_l.extend(self.removed) | |
47 | 42 | subprocess.call(command_l) |
48 | 43 | |
49 | - #Main | |
50 | - print("These kernels removed. {}".format(get_oldkernels())) | |
51 | - print("Are you remove them? Y/n") | |
52 | - anser = yesno(input()) | |
53 | - if anser == True: | |
54 | - removethem() | |
55 | - else: | |
56 | - return 0 | |
57 | 44 | |
58 | 45 | |
59 | 46 | #Others |
@@ -81,4 +68,13 @@ def swich(choice): | ||
81 | 68 | task = list[choice - 1] |
82 | 69 | except IndexError: |
83 | 70 | return "The task is not find." |
84 | - return task | |
\ No newline at end of file | ||
71 | + return task | |
72 | + | |
73 | +inst = clearcache(r"/var/cache/apt") | |
74 | +#Main | |
75 | +print("APT-cache size is {}MiB.\nAre you clean it?".format(inst.get_cachesize())) | |
76 | +anser = yesno(input("Y/n ")) | |
77 | +if anser == True: | |
78 | + inst.cleanit() | |
79 | +else: | |
80 | + exit(0) |