334 if errorMsg: |
334 if errorMsg: |
335 print "\n ERROR: %s" % errorMsg |
335 print "\n ERROR: %s" % errorMsg |
336 |
336 |
337 |
337 |
338 def fileBaseOf(filename, withPath=0): |
338 def fileBaseOf(filename, withPath=0): |
339 """fileBaseOf(filename,withPath) ---> string |
339 """fileBaseOf(filename,withPath) ---> string |
340 |
340 |
341 Return base name of filename. The returned string never includes the extension. |
341 Return base name of filename. The returned string never includes the extension. |
342 Use os.path.basename() to return the basename with the extension. The |
342 Use os.path.basename() to return the basename with the extension. The |
343 second argument is optional. If specified and if set to 'true' (non zero) |
343 second argument is optional. If specified and if set to 'true' (non zero) |
344 the string returned contains the full path of the file name. Otherwise the |
344 the string returned contains the full path of the file name. Otherwise the |
345 path is excluded. |
345 path is excluded. |
346 |
346 |
347 [Example] |
347 [Example] |
348 >>> fn = 'd:/dev/telepath/tvapp/code/test.html' |
348 >>> fn = 'd:/dev/telepath/tvapp/code/test.html' |
349 >>> fileBaseOf(fn) |
349 >>> fileBaseOf(fn) |
350 'test' |
350 'test' |
351 >>> fileBaseOf(fn) |
351 >>> fileBaseOf(fn) |
352 'test' |
352 'test' |
353 >>> fileBaseOf(fn,1) |
353 >>> fileBaseOf(fn,1) |
354 'd:/dev/telepath/tvapp/code/test' |
354 'd:/dev/telepath/tvapp/code/test' |
355 >>> fileBaseOf(fn,0) |
355 >>> fileBaseOf(fn,0) |
356 'test' |
356 'test' |
357 >>> fn = 'abcdef' |
357 >>> fn = 'abcdef' |
358 >>> fileBaseOf(fn) |
358 >>> fileBaseOf(fn) |
359 'abcdef' |
359 'abcdef' |
360 >>> fileBaseOf(fn,1) |
360 >>> fileBaseOf(fn,1) |
361 'abcdef' |
361 'abcdef' |
362 >>> fn = "abcdef." |
362 >>> fn = "abcdef." |
363 >>> fileBaseOf(fn) |
363 >>> fileBaseOf(fn) |
364 'abcdef' |
364 'abcdef' |
365 >>> fileBaseOf(fn,1) |
365 >>> fileBaseOf(fn,1) |
366 'abcdef' |
366 'abcdef' |
367 """ |
367 """ |
368 pos = filename.rfind('.') |
368 pos = filename.rfind('.') |
369 if pos > 0: |
369 if pos > 0: |
370 filename = filename[:pos] |
370 filename = filename[:pos] |
371 if withPath: |
371 if withPath: |
372 return filename |
372 return filename |
373 else: |
373 else: |
374 return os.path.basename(filename) |
374 return os.path.basename(filename) |
375 |
375 |
376 |
376 |
377 def mkdir(directory): |
377 def mkdir(directory): |
378 """Create a directory (and possibly the entire tree). |
378 """Create a directory (and possibly the entire tree). |
379 |
379 |
380 The os.mkdir() will fail to create a directory if one of the |
380 The os.mkdir() will fail to create a directory if one of the |
381 directory in the specified path does not exist. mkdir() |
381 directory in the specified path does not exist. mkdir() |
382 solves this problem. It creates every intermediate directory |
382 solves this problem. It creates every intermediate directory |
383 required to create the final path. Under Unix, the function |
383 required to create the final path. Under Unix, the function |
384 only supports forward slash separator, but under Windows and MacOS |
384 only supports forward slash separator, but under Windows and MacOS |
385 the function supports the forward slash and the OS separator (backslash |
385 the function supports the forward slash and the OS separator (backslash |
386 under windows). |
386 under windows). |
387 """ |
387 """ |
388 |
388 |
389 # translate the path separators |
389 # translate the path separators |
390 directory = unixpath(directory) |
390 directory = unixpath(directory) |
391 # build a list of all directory elements |
391 # build a list of all directory elements |
392 aList = filter(lambda x: len(x) > 0, directory.split('/')) |
392 aList = filter(lambda x: len(x) > 0, directory.split('/')) |
393 theLen = len(aList) |
393 theLen = len(aList) |
394 # if the first element is a Windows-style disk drive |
394 # if the first element is a Windows-style disk drive |
395 # concatenate it with the first directory |
395 # concatenate it with the first directory |
396 if aList[0].endswith(':'): |
396 if aList[0].endswith(':'): |
397 if theLen > 1: |
397 if theLen > 1: |
398 aList[1] = aList[0] + '/' + aList[1] |
398 aList[1] = aList[0] + '/' + aList[1] |
399 del aList[0] |
399 del aList[0] |
400 theLen -= 1 |
400 theLen -= 1 |
401 # if the original directory starts at root, |
401 # if the original directory starts at root, |
402 # make sure the first element of the list |
402 # make sure the first element of the list |
403 # starts at root too |
403 # starts at root too |
404 if directory[0] == '/': |
404 if directory[0] == '/': |
405 aList[0] = '/' + aList[0] |
405 aList[0] = '/' + aList[0] |
406 # Now iterate through the list, check if the |
406 # Now iterate through the list, check if the |
407 # directory exists and if not create it |
407 # directory exists and if not create it |
408 theDir = '' |
408 theDir = '' |
409 for i in range(theLen): |
409 for i in range(theLen): |
410 theDir += aList[i] |
410 theDir += aList[i] |
411 if not os.path.exists(theDir): |
411 if not os.path.exists(theDir): |
412 os.mkdir(theDir) |
412 os.mkdir(theDir) |
413 theDir += '/' |
413 theDir += '/' |
414 |
414 |
415 |
415 |
416 def unixpath(thePath): |
416 def unixpath(thePath): |
417 r"""Return a path name that contains Unix separator. |
417 r"""Return a path name that contains Unix separator. |
418 |
418 |
419 [Example] |
419 [Example] |
420 >>> unixpath(r"d:\test") |
420 >>> unixpath(r"d:\test") |
421 'd:/test' |
421 'd:/test' |
422 >>> unixpath("d:/test/file.txt") |
422 >>> unixpath("d:/test/file.txt") |
423 'd:/test/file.txt' |
423 'd:/test/file.txt' |
424 >>> |
424 >>> |
425 """ |
425 """ |
426 thePath = os.path.normpath(thePath) |
426 thePath = os.path.normpath(thePath) |
427 if os.sep == '/': |
427 if os.sep == '/': |
428 return thePath |
428 return thePath |
429 else: |
429 else: |
430 return thePath.replace(os.sep, '/') |
430 return thePath.replace(os.sep, '/') |
431 |
431 |
432 |
432 |
433 # ----------------------------------------------------------------------------- |
433 # ----------------------------------------------------------------------------- |
434 |
434 |
435 # S c r i p t e x e c u t i o n -- Runs when invoked from the command line -- |
435 # S c r i p t e x e c u t i o n -- Runs when invoked from the command line -- |