mkdir.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 ######################################################################
3 #
4 # Name: mkdir.py
5 #
6 # Purpose: This script uses ifdh to safely create a directory in an
7 # ifdh-accessible location. This script will create parent
8 # directories, if necessary. It is not an error if the
9 # specified directory alrady exists. This script functions
10 # similarly to the shell command "mkdir -p", except everything
11 # is done using ifdh commands.
12 #
13 # Environment variable $EXPERIMENT must be defined to properly
14 # initialize ifdh.
15 #
16 # Created: 15-May-2015 H. Greenlee
17 #
18 # Command line usage:
19 #
20 # mkdir.py [-h|--help] <dir>
21 #
22 # Options:
23 #
24 # -h, --help - Print help.
25 # -v - Verbose.
26 #
27 # Arguments:
28 #
29 # <dir> - Directory to create.
30 #
31 ######################################################################
32 
33 from __future__ import absolute_import
34 from __future__ import print_function
35 
36 # Imports
37 
38 import sys, os, ifdh
39 
40 # Initialize ifdh.
41 
42 Ifdh = ifdh.ifdh()
43 
44 # Main procedure.
45 
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 
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 
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 
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 
207 if __name__ == "__main__":
208  rc = main(sys.argv)
209  sys.exit(rc)
int open(const char *, int)
Opens a file descriptor.
def mkdir(dir, verbose)
Definition: mkdir.py:102
def help()
Definition: mkdir.py:79
def main(argv)
Definition: mkdir.py:46
def existdir(dir, verbose)
Definition: mkdir.py:161