emptydir.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 ######################################################################
3 #
4 # Name: emptydir.py
5 #
6 # Purpose: This script uses ifdh to remove contents from a specified
7 # ifdh-accessible directory. The directory itself may or may
8 # not be deleted depending on the presence or absence of option
9 # "-d".
10 #
11 # Environment variable $EXPERIMENT must be defined to properly
12 # initialize ifdh.
13 #
14 # Created: 15-May-2015 H. Greenlee
15 #
16 # Command line usage:
17 #
18 # emptydir.py [-h|--help] [-d] <dir>
19 #
20 # Options:
21 #
22 # -h, --help - Print help.
23 # -d - Delete directory (in addition to contents).
24 # -v - Verbose.
25 #
26 # Arguments:
27 #
28 # <dir> - Directory to empty or delete.
29 #
30 ######################################################################
31 
32 from __future__ import absolute_import
33 from __future__ import print_function
34 
35 # Imports
36 
37 import sys, os, ifdh
38 
39 # Initialize ifdh.
40 
41 Ifdh = ifdh.ifdh()
42 
43 # Main procedure.
44 
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 
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 
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 
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 
176 if __name__ == "__main__":
177  rc = main(sys.argv)
178  sys.exit(rc)
def emptydir(dir, verbose)
Definition: emptydir.py:104
int open(const char *, int)
Opens a file descriptor.
def rmdir(dir, verbose)
Definition: emptydir.py:158
def main(argv)
Definition: emptydir.py:45