-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclean_from_old_mac.py
More file actions
executable file
·105 lines (91 loc) · 2.89 KB
/
clean_from_old_mac.py
File metadata and controls
executable file
·105 lines (91 loc) · 2.89 KB
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of my scripts project
#
# Copyright (c) 2011 Marco Antonio Islas Cruz
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# @author Marco Antonio Islas Cruz <markuz@islascruz.org>
# @copyright 2011 Marco Antonio Islas Cruz
# @license http://www.gnu.org/licenses/gpl.txt
#TODO: print something about sockets
import os
import sys
import shutil
import subprocess
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-v','--verbose',dest = 'verbose', action='store_true',
help='Print extra information')
options, args = parser.parse_args()
entries = []
process_info = os.popen("locate 'from old Mac'")
tmpentries = process_info.read().split("\n")
print "Calculating size..."
totsize = 0
totfiles = 0
totdirs = 0
totlinks = 0
totunknown = 0
for item in tmpentries:
if not os.path.exists(item):
continue
entries.append(item)
if os.path.isfile(item):
stat = os.stat(item)
size = stat.st_size
totsize += size
totfiles += 1
elif os.path.isdir(item):
totdirs += 1
elif os.path.islink(item):
totlinks += 1
else:
print item
totunknown += 1
#Resume
print "Number of files: ", totfiles
print "Number of directories: ", totdirs
print "Number of links: ", totlinks
print "Number of unknown files: ", totunknown
print "Total space saved: ", (totsize/1024)/1024, "MB"
cont = raw_input("Continue [y/N]")
if cont.lower() != "y":
sys.exit()
while entries:
entry = entries.pop()
if not "(from old Mac)" in entry:
continue
if os.path.isfile( entry ) or os.path.islink( entry ):
if options.debug:
print "delete file", entry
func = os.remove
elif os.path.isdir( entry ):
if options.debug:
print "rm dir", entry
func = shutil.rmtree
#As rmtree will delete everything from this directory, we will
#skip the files that contains this directory on its name.
entries = [k for k in entries if not k.startwith(entry)]
else:
print "Unknown type for '%s' didn't touch it."%entry
continue
try:
func(entry)
except Exception, e:
sys.stderr.write(e)
sys.stderr.flush()