All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions | Variables
python.emptydir Namespace Reference

Functions

def main
 
def help
 
def emptydir
 
def rmdir
 

Variables

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

Function Documentation

def python.emptydir.emptydir (   dir,
  verbose 
)

Definition at line 104 of file emptydir.py.

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

Definition at line 84 of file emptydir.py.

84 
85 def help():
86  filename = sys.argv[0]
87  file = open(filename, 'r')
88 
89  doprint=0
90 
91  for line in file.readlines():
92  if line[2:13] == 'emptydir.py':
93  doprint = 1
94  elif line[0:6] == '######' and doprint:
95  doprint = 0
96  if doprint:
97  if len(line) > 2:
98  print(line[2:], end=' ')
99  else:
100  print()
101 
102 
103 # This function deletes the contents of a directory, but not the directory itself.
do one_file $F done echo for F in find $TOP name CMakeLists txt print
open(RACETRACK) or die("Could not open file $RACETRACK for writing")
def python.emptydir.main (   argv)

Definition at line 45 of file emptydir.py.

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

Definition at line 158 of file emptydir.py.

159 def rmdir(dir, verbose):
160 
161  # Remove trailing '/' character(s), if any (except if or until entire path is '/').
162 
163  while len(dir) > 1 and dir[-1] == '/':
164  dir = dir[:-1]
165 
166  emptydir(dir, verbose)
167  if verbose:
168  print('Deleting directory %s' % dir)
169  try:
170  Ifdh.rmdir(dir)
171  except:
172  print('Caught exception from Ifdh.rmdir for directory %s.' % dir)
173 
174 
175 # Command line.
do one_file $F done echo for F in find $TOP name CMakeLists txt print

Variable Documentation

tuple python.emptydir.Ifdh = ifdh.ifdh()

Definition at line 41 of file emptydir.py.

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

Definition at line 177 of file emptydir.py.