| 1 |
from __future__ import (absolute_import, division, |
| 2 |
print_function, unicode_literals) |
| 3 |
|
| 4 |
import traceback |
| 5 |
import logging |
| 6 |
import os |
| 7 |
import os.path |
| 8 |
|
| 9 |
|
| 10 |
def update_attribute(attributes, name, value): |
| 11 |
"""Update attribute name to value |
| 12 |
:param attributes: list of tuples to update |
| 13 |
:param name: name to update |
| 14 |
:param value: value to update name to |
| 15 |
:return: attributes with updated tuple |
| 16 |
:rtype: tuple |
| 17 |
""" |
| 18 |
ret = list() |
| 19 |
# find name in attributes |
| 20 |
for n, v in attributes: |
| 21 |
if n == name: |
| 22 |
ret.append((name, value)) |
| 23 |
else: |
| 24 |
ret.append((n, v)) |
| 25 |
return tuple(ret) |
| 26 |
|
| 27 |
|
| 28 |
def split_filename_and_anchor(filename): |
| 29 |
parts = filename.split("#") |
| 30 |
if len(parts) > 2: |
| 31 |
raise Exception("Was passed filename with two (or more) # in it") |
| 32 |
elif len(parts) == 2: |
| 33 |
file = parts[0] |
| 34 |
anchor = parts[1] |
| 35 |
else: |
| 36 |
file = parts[0] |
| 37 |
anchor = None |
| 38 |
return tuple([file, anchor]) |
| 39 |
|
| 40 |
|
| 41 |
def change_filename(filename, newext, orginaldir, destdir, makedirs=True): |
| 42 |
"""Twiddles URL like paths into filesystem paths |
| 43 |
:param filename: path to twiddle |
| 44 |
:param newext: new extention for filename |
| 45 |
:param orginaldir: original directory filename is currently in |
| 46 |
:param destdir: directory to prepare to receive filename |
| 47 |
:param makedirs: create intermediate directories in destdir |
| 48 |
:return: the twiddled filename |
| 49 |
""" |
| 50 |
filename = os.path.normpath(filename) |
| 51 |
filename, anchor = split_filename_and_anchor(filename) |
| 52 |
if len(filename) != 0: |
| 53 |
orginaldir = os.path.normpath(orginaldir) |
| 54 |
destdir = os.path.normpath(destdir) |
| 55 |
logging.debug(" change_filename('%s', '%s', '%s', '%s', %s)", |
| 56 |
filename, newext, orginaldir, destdir, makedirs) |
| 57 |
outfile_name1 = filename.replace(orginaldir, ".") # files relative name |
| 58 |
logging.debug("%s", outfile_name1) |
| 59 |
if newext == None: |
| 60 |
outfile_name3 = outfile_name1 |
| 61 |
else: |
| 62 |
outfile_name2 = os.path.splitext(outfile_name1)[ |
| 63 |
0] # file's name without ext |
| 64 |
logging.debug("%s", outfile_name2) |
| 65 |
outfile_name3 = outfile_name2 + newext |
| 66 |
logging.debug("%s", outfile_name3) |
| 67 |
outfile_name4 = os.path.join(destdir, outfile_name3) |
| 68 |
logging.debug("%s", outfile_name4) |
| 69 |
outfile_name = os.path.normpath(outfile_name4) |
| 70 |
logging.debug("%s", outfile_name) |
| 71 |
|
| 72 |
# make sure that the folder exists to output, if wanted |
| 73 |
if makedirs: |
| 74 |
outfile_path = os.path.dirname(outfile_name) |
| 75 |
if os.path.exists(outfile_path): |
| 76 |
if os.path.isdir(outfile_path): |
| 77 |
pass # do nothing because the folder already exists |
| 78 |
else: |
| 79 |
raise Exception( |
| 80 |
"%s already exists but is not a directory" % ( |
| 81 |
outfile_path)) |
| 82 |
else: |
| 83 |
os.makedirs(outfile_path) |
| 84 |
else: |
| 85 |
outfile_name = filename |
| 86 |
|
| 87 |
if anchor == None: |
| 88 |
return outfile_name |
| 89 |
else: |
| 90 |
return "#".join([outfile_name, anchor]) |
| 91 |
|
| 92 |
|
| 93 |
def make_directory_for_filename(filename): |
| 94 |
"""Returns False if directory for filename already exists, True if |
| 95 |
function was successfully able to create the directory, or raises |
| 96 |
and exception for the error.""" |
| 97 |
path = os.path.dirname(filename) |
| 98 |
if os.path.exists(path): |
| 99 |
if os.path.isdir(path): |
| 100 |
return False |
| 101 |
else: |
| 102 |
raise ExistsButNotDirectoryError(path) |
| 103 |
else: |
| 104 |
# must be a permissions issue if we end up here |
| 105 |
try: |
| 106 |
os.makedirs(path) |
| 107 |
return True |
| 108 |
except: |
| 109 |
raise |
| 110 |
|
| 111 |
|
| 112 |
class ExistsButNotDirectoryError(IOError): |
| 113 |
def __init__(self, path="Not Specified"): |
| 114 |
self.path = path |
| 115 |
|
| 116 |
def __str__(self): |
| 117 |
return "Path (%s) exists but is not a directory!" |
| 118 |
|
| 119 |
|
| 120 |
def rmtree_error_handler(function, path, excinfo): |
| 121 |
if function == os.remove: |
| 122 |
logging.warning(" Unable to remove %s", path) |
| 123 |
elif function == os.rmdir: |
| 124 |
logging.warning(" Unable to remove directory %s", path) |
| 125 |
else: |
| 126 |
(type, value, tb) = excinfo |
| 127 |
logging.error("***EXCEPTION:") |
| 128 |
logging.error(" %s: %s", type.__name__, value) |
| 129 |
for filename, line, function, text in traceback.extract_tb(tb): |
| 130 |
logging.error("%s:%d %s", os.path.basename(filename), line, function) |
| 131 |
logging.error(" %s", text) |