Repository navigation
Expand file tree
/
Copy pathclean.py
More file actions
executable file
·194 lines (168 loc) · 5.68 KB
/
Copy pathclean.py
File metadata and controls
executable file
·194 lines (168 loc) · 5.68 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#! /usr/bin/python2 -tt
# ______________________________________________________________________________
#
# CLEAN
# ______________________________________________________________________________
# By Jules Baratoux on Wed Aug 28 2013
# Just a simple project to learn python
#
# NAME
# clean - remove dirt from directories
#
# SYNOPSIS
# clean [options] [directories]
#
# DESCRIPTION
# Remove dirt files from directories. Multiples directories can be
# set. By default clean the current directory.
#
# OPTIONS
# -d, --maxdepth levels
# Descend at most levels (a non-negative integer) levels
# of directories below the command line arguments.
#
# -f, --force
# ignore nonexistent files, never prompt
#
# -i Prompt before every removal
#
# -I prompt once before removing more than three files, or
# when removing recursively. Less intrusive than -i,
# while still giving protection against most mistakes
#
# -L, --follow
# Follow symbolic
#
# -H, --no-follow
# Do not follow symbolic links, except while processing
# the command line arguments. This is the default
# behaviour.
#
# -R, --recursive
# Clean directories and their subdirectories recursively
#
# -h, --help
# Display this help and exit
#
# -v, --verbose
# Explain what is being done
# ______________________________________________________________________________
import getopt, sys, os, re
class Param(object):
def __init__(self, follow=False, recursive=False, verbose=False, maxdepth=3):
self.follow = follow
self.recursive = recursive
self.verbose = verbose
self.maxdepth = maxdepth
self.rm_options = ""
# ______________________________________________________________________________
param = Param()
def main():
try:
options, argv = getopt.gnu_getopt(sys.argv[1:], "d:fiILHRhv", ["maxdepth=", "force", "follow",
"no-follow", "help", "recursive", "verbose"])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
for option, arg in options:
if option in ("-h", "--help"):
usage()
sys.exit()
elif option in ("-d", "--maxdepth"):
param.maxdepth = arg
elif option in ("-f", "--force"):
param.rm_options += "--force "
elif option in ("-I"):
param.rm_options += "-I "
elif option in ("-i"):
param.rm_options += "-i "
elif option in ("-R", "--recursive"):
param.recursive = True
elif option in ("-v", "--verbose"):
param.verbose = True
param.rm_options += "--verbose "
elif option in ("-L", "--follow"):
param.follow = True
elif option in ("-H", "--no-follow"):
param.follow = False
else:
print option
assert False, "unhandled option"
if len(argv) > 0:
for arg in argv:
clean(arg)
else:
clean('.')
# ______________________________________________________________________________
def resolve_link(pathname):
from os import readlink;
pathname = readlink(pathname)
return "." if pathname is None else pathname
def isdir(pathname):
try:
return os.path.isdir(pathname) or (os.path.islink(pathname) and os.path.isdir(resolve_link(pathname)))
except OSError as e:
print str(e)
return False
def isfile(pathname):
try:
return os.path.isfile(pathname) or (os.path.islink(pathname) and os.path.isfile(resolve_link(pathname)))
except OSError as e:
print str(e)
return False
def islink(pathname):
return os.path.islink(pathname)
# ______________________________________________________________________________
def file_handler(path):
if re.match( "([^~]+~$)" + '|' +
"(#[^#]+#$)" + '|' +
r"(\.#[^#]+$)" + '|' +
r"(.+\.stackdump$)", os.path.basename(path)):
remove(path)
def dir_handler(pathname, depth):
depth += 1;
for name in os.listdir(pathname):
name = pathname + "/" + name
if (depth <= param.maxdepth) and (isdir(name) and param.recursive) and (not islink(name) or param.follow):
try:
dir_handler(name, depth)
except OSError as e:
print str(e)
elif isfile(name) or isdir(name):
file_handler(name)
def clean(pathname, depth = 0):
if isdir(pathname):
dir_handler(pathname, depth)
if isfile(pathname):
file_handler(pathname)
# ______________________________________________________________________________
def remove(filename):
os.system("rm " + param.rm_options + filename)
def usage():
synopsis = "usage: "
text = []
f = open(sys.argv[0], 'r')
if f:
flag = False
for line in f:
if flag:
synopsis += line[9:-1]
break
elif line == "# SYNOPSIS\n":
flag = True;
flag = False
for line in f:
if flag:
if line[0:4] == "# ____..."[0:4]:
break
text.append(line[1:-1])
elif line == "# OPTIONS\n":
flag = True
f.close()
print synopsis, "\n"
for line in text:
print line
# ______________________________________________________________________________
if __name__ == "__main__":
main()