Functions | Variables
python.emptydir Namespace Reference

Functions

def main (argv)
 
def help ()
 
def emptydir (dir, verbose)
 
def rmdir (dir, verbose)
 

Variables

 Ifdh = ifdh.ifdh()
 
 rc = main(sys.argv)
 

Function Documentation

def python.emptydir.emptydir (   dir,
  verbose 
)

Definition at line 104 of file emptydir.py.

104 def emptydir(dir, verbose):
105 
106  # Remove trailing '/' character(s), if any (except if or until entire path is '/').
107 
108  while len(dir) > 1 and dir[-1] == '/':
109  dir = dir[:-1]
110 
111  # Get contents of directory.
112 
113  files = []
114  try:
115  files = Ifdh.ls(dir, 1)
116  except:
117  files = []
118  print('Caught exception from Ifdh.ls for directory %s.' % dir)
119 
120  # First pass: delete files.
121 
122  for file in files:
123 
124  # Ifdh signals that a file is a directory by ending the path with '/'.
125 
126  if file[-1] != '/':
127  if verbose:
128  print('Deleting %s' % file)
129  try:
130  Ifdh.rm(file)
131  except:
132  print('Caught exception from Ifdh.rm for file %s.' % file)
133 
134 
135  # Second pass: delete subdirectories.
136 
137  first = True
138  for subdir in files:
139 
140  # Ifdh signals that a file is a directory by ending the path
141  # with '/'. Ifdh seems to include the directory itself in its
142  # listing. Furthermore, it can be hard to recognize that a
143  # listed path is identical with the original directory.
144  # However, ifdh seems to always return the directory itself as
145  # the first element of its listing (not sure if this is
146  # documented behavior. Therefore, we only delete the first
147  # returned element if its basename doesn't match the basename
148  # of the original directory.
149 
150  if subdir[-1] == '/':
151  if not first or os.path.basename(subdir[:-1]) != os.path.basename(dir):
152  rmdir(subdir[:-1], verbose)
153  first = False
154 
155 
156 # Function to recursively delete a directory.
157 
def emptydir(dir, verbose)
Definition: emptydir.py:104
def rmdir(dir, verbose)
Definition: emptydir.py:158
def python.emptydir.help ( )

Definition at line 84 of file emptydir.py.

84 def help():
85  filename = sys.argv[0]
86  file = open(filename, 'r')
87 
88  doprint=0
89 
90  for line in file.readlines():
91  if line[2:13] == 'emptydir.py':
92  doprint = 1
93  elif line[0:6] == '######' and doprint:
94  doprint = 0
95  if doprint:
96  if len(line) > 2:
97  print(line[2:], end=' ')
98  else:
99  print()
100 
101 
102 # This function deletes the contents of a directory, but not the directory itself.
103 
int open(const char *, int)
Opens a file descriptor.
def python.emptydir.main (   argv)

Definition at line 45 of file emptydir.py.

45 def main(argv):
46 
47  deldir = 0
48  verbose = 0
49  dir = ''
50 
51  # Parse arguments.
52 
53  args = argv[1:]
54  if len(args) == 0:
55  help()
56  return 0
57  while len(args) > 0:
58  if args[0] == '-h' or args[0] == '--help' :
59  help()
60  return 0
61  elif args[0] == '-d':
62  deldir = 1
63  del args[0]
64  elif args[0] == '-v':
65  verbose = 1
66  del args[0]
67  elif args[0][0] == '-':
68  print('Unknown option %s' % args[0])
69  return 1
70  else:
71  if dir != '':
72  print('Too many arguments.')
73  return 1
74  dir = args[0]
75  del args[0]
76 
77  if deldir:
78  rmdir(dir, verbose)
79  else:
80  emptydir(dir, verbose)
81 
82 # Help function.
83 
def emptydir(dir, verbose)
Definition: emptydir.py:104
def rmdir(dir, verbose)
Definition: emptydir.py:158
def main(argv)
Definition: emptydir.py:45
def python.emptydir.rmdir (   dir,
  verbose 
)

Definition at line 158 of file emptydir.py.

158 def rmdir(dir, verbose):
159 
160  # Remove trailing '/' character(s), if any (except if or until entire path is '/').
161 
162  while len(dir) > 1 and dir[-1] == '/':
163  dir = dir[:-1]
164 
165  emptydir(dir, verbose)
166  if verbose:
167  print('Deleting directory %s' % dir)
168  try:
169  Ifdh.rmdir(dir)
170  except:
171  print('Caught exception from Ifdh.rmdir for directory %s.' % dir)
172 
173 
174 # Command line.
175 
def emptydir(dir, verbose)
Definition: emptydir.py:104
def rmdir(dir, verbose)
Definition: emptydir.py:158

Variable Documentation

python.emptydir.Ifdh = ifdh.ifdh()

Definition at line 41 of file emptydir.py.

python.emptydir.rc = main(sys.argv)

Definition at line 177 of file emptydir.py.