| 1 |
from os import altsep, chmod, mkdir, remove, sep, stat, walk |
| 2 |
from os.path import dirname, exists, isdir, isfile, islink, join as joinpath |
| 3 |
from shutil import copyfile |
| 4 |
|
| 5 |
try: |
| 6 |
from os import symlink |
| 7 |
except ImportError: |
| 8 |
def symlink(src, dst): |
| 9 |
raise OSError('This platform does not support symbolic links') |
| 10 |
|
| 11 |
def scanTree(baseDir): |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
if altsep is not None: |
| 23 |
|
| 24 |
|
| 25 |
baseDir = baseDir.replace(altsep, sep) |
| 26 |
baseDir = baseDir.rstrip(sep) |
| 27 |
|
| 28 |
if not isdir(baseDir): |
| 29 |
raise OSError('Directory "%s" does not exist' % baseDir) |
| 30 |
|
| 31 |
def escalate(ex): |
| 32 |
raise OSError( |
| 33 |
'Error scanning directory entry "%s": %s' % (ex.filename, ex) |
| 34 |
) |
| 35 |
for dirPath, dirNames, fileNames in walk(baseDir, onerror = escalate): |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
index = 0 |
| 40 |
while index < len(dirNames): |
| 41 |
if dirNames[index].startswith('.'): |
| 42 |
del dirNames[index] |
| 43 |
else: |
| 44 |
index += 1 |
| 45 |
|
| 46 |
|
| 47 |
fileNames = [ name for name in fileNames if not name.startswith('.') ] |
| 48 |
|
| 49 |
if dirPath == baseDir: |
| 50 |
relDir = '' |
| 51 |
else: |
| 52 |
assert dirPath.startswith(baseDir + sep), dirPath |
| 53 |
relDir = dirPath[len(baseDir) + 1 : ] |
| 54 |
yield relDir |
| 55 |
for fileName in fileNames: |
| 56 |
yield joinpath(relDir, fileName) |
| 57 |
|
| 58 |
def installDir(path): |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
if not isdir(path): |
| 65 |
|
| 66 |
|
| 67 |
mkdir(path) |
| 68 |
chmod(path, 0o755) |
| 69 |
|
| 70 |
def _installDirsRec(path): |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
path = path.rstrip(sep) |
| 75 |
if path and not isdir(path): |
| 76 |
index = path.rfind(sep) |
| 77 |
if index != -1: |
| 78 |
_installDirsRec(path[ : index]) |
| 79 |
mkdir(path) |
| 80 |
chmod(path, 0o755) |
| 81 |
|
| 82 |
def installDirs(path): |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if altsep is not None: |
| 90 |
path = path.replace(altsep, sep) |
| 91 |
_installDirsRec(path) |
| 92 |
|
| 93 |
def installFile(srcPath, destPath): |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
copyfile(srcPath, destPath) |
| 101 |
chmod(destPath, 0o755 if (stat(srcPath).st_mode & 0o100) else 0o644) |
| 102 |
|
| 103 |
def installSymlink(target, link): |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if islink(link): |
| 111 |
remove(link) |
| 112 |
symlink(target, link) |
| 113 |
|
| 114 |
def installTree(srcDir, destDir, paths): |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
if not isdir(destDir): |
| 126 |
raise OSError('Destination directory "%s" does not exist' % destDir) |
| 127 |
|
| 128 |
for relPath in paths: |
| 129 |
if altsep is not None: |
| 130 |
relPath = relPath.replace(altsep, sep) |
| 131 |
srcPath = joinpath(srcDir, relPath) |
| 132 |
destPath = joinpath(destDir, relPath) |
| 133 |
if islink(srcPath): |
| 134 |
print('Skipping symbolic link:', srcPath) |
| 135 |
elif isdir(srcPath): |
| 136 |
_installDirsRec(destPath) |
| 137 |
elif isfile(srcPath): |
| 138 |
_installDirsRec(dirname(destPath)) |
| 139 |
installFile(srcPath, destPath) |
| 140 |
elif exists(srcPath): |
| 141 |
print('Skipping unknown kind of file system entry:', srcPath) |
| 142 |
else: |
| 143 |
print('Skipping non-existing path:', srcPath) |