Functions | Variables
python.mkdir Namespace Reference

Functions

def main (argv)
 
def help ()
 
def mkdir (dir, verbose)
 
def existdir (dir, verbose)
 

Variables

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

Function Documentation

def python.mkdir.existdir (   dir,
  verbose 
)

Definition at line 161 of file mkdir.py.

161 def existdir(dir, verbose):
162 
163  # Remove trailing '/' character(s), if any (except if or until entire path is '/').
164 
165  while len(dir) > 1 and dir[-1] == '/':
166  dir = dir[:-1]
167 
168  # If at this point, the directory path has been reduced to just '/', return True.
169 
170  if dir == '/':
171  return True
172 
173  # Check contents of parent directory (if any).
174  # If ifdh ls for the parent directory fails, return false.
175  # Note that ifdh may print out various alarming but harmless
176  # message at this point.
177 
178  parent = os.path.dirname(dir)
179  base = os.path.basename(dir)
180  contents = []
181  try:
182  contents = Ifdh.ls(parent, 1)
183  except:
184  contents = []
185 
186  # Loop over parent directory contents.
187  # Only compare the base part of the path, since the mountpoint may differ.
188 
189  for content in contents:
190 
191  # Is this a directory (ifdh signals by adding '/' at end)?
192 
193  if len(content) > 1 and content[-1] == '/':
194 
195  # Does base match?
196 
197  if os.path.basename(content[:-1]) == base:
198  return True
199 
200  # If we fall out of the loopp, that means we didn't find this directory
201  # in the parent directory.
202 
203  return False
204 
205 # Command line.
206 
def existdir(dir, verbose)
Definition: mkdir.py:161
def python.mkdir.help ( )

Definition at line 79 of file mkdir.py.

79 def help():
80  filename = sys.argv[0]
81  file = open(filename, 'r')
82 
83  doprint=0
84 
85  for line in file.readlines():
86  if line[2:10] == 'mkdir.py':
87  doprint = 1
88  elif line[0:6] == '######' and doprint:
89  doprint = 0
90  if doprint:
91  if len(line) > 2:
92  print(line[2:], end=' ')
93  else:
94  print()
95  return
96 
97 
98 # This function safely creates the specified directory and parent directories
99 # if they don't exist. It is not an error if the specified directory already
100 # exists.
101 
int open(const char *, int)
Opens a file descriptor.
def help()
Definition: mkdir.py:79
def python.mkdir.main (   argv)

Definition at line 46 of file mkdir.py.

46 def main(argv):
47 
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] == '-v':
62  verbose = 1
63  del args[0]
64  elif args[0][0] == '-':
65  print('Unknown option %s' % args[0])
66  return 1
67  else:
68  if dir != '':
69  print('Too many arguments.')
70  return 1
71  dir = args[0]
72  del args[0]
73 
74  mkdir(dir, verbose)
75  return
76 
77 # Help function.
78 
def mkdir(dir, verbose)
Definition: mkdir.py:102
def help()
Definition: mkdir.py:79
def main(argv)
Definition: mkdir.py:46
def python.mkdir.mkdir (   dir,
  verbose 
)

Definition at line 102 of file mkdir.py.

102 def mkdir(dir, verbose):
103 
104  # Remove trailing '/' character(s), if any (except if or until entire path is '/').
105 
106  while len(dir) > 1 and dir[-1] == '/':
107  dir = dir[:-1]
108 
109  # If at this point, the directory path has been reduced to just '/', quit.
110 
111  if dir == '/':
112  if verbose:
113  print('mkdir asked to make directory \'/\'.')
114  return
115 
116  # Test whether target directory already exists.
117 
118  exists = existdir(dir, verbose)
119  if exists:
120 
121  # If target directory already exists, just return.
122 
123  if verbose:
124  print('Directory %s already exists.' % dir)
125  return
126 
127  else:
128 
129  # Target directoroy doesn't exist.
130 
131  if verbose:
132  print('Directory %s doesn\'t exist.' % dir)
133 
134  # Make sure that the parent directory exists by calling this function recursively.
135 
136  parent = os.path.dirname(dir)
137  mkdir(parent, verbose)
138 
139  # Make the directory.
140  # Catch errors and exit with error status in that case.
141 
142  ok = False
143  try:
144  if verbose:
145  print('Making directory %s' % dir)
146  Ifdh.mkdir(dir)
147  ok = True
148  except:
149  print('Caught exception from Ifdh.mkdir for directory %s.' % dir)
150  ok = False
151  if not ok:
152  sys.exit(1)
153 
154  # Done (success).
155 
156  return
157 
158 
159 # This function tests whether the specified directory exists.
160 
def mkdir(dir, verbose)
Definition: mkdir.py:102
def existdir(dir, verbose)
Definition: mkdir.py:161

Variable Documentation

python.mkdir.Ifdh = ifdh.ifdh()

Definition at line 42 of file mkdir.py.

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

Definition at line 208 of file mkdir.py.