Exemple #1
0
def __mergeDirs(srcDirPath, destDirPath, isRecursive=True):
    """ 
    Move the contents of a source directory into a target directory, 
    over writing the target contents where applicable.
    If performed recursively, the destination contents contained 
    within merged sub directory are all preserved. Otherwise,
    the source sub directories replace the target sub directories 
    in their entirety. 
    """
    srcDirPath = absPath(srcDirPath)
    destDirPath = absPath(destDirPath)
    if not isDir(destDirPath): makeDir(destDirPath)
    if not isDir(srcDirPath): return
    if isRecursive:

        def moveItem(root, item, isSubDir=False):
            srcPath = joinPath(root, item)
            destDir = joinPath(destDirPath, relpath(root, srcDirPath))
            if isSubDir and isDir(destDir): return
            __moveToDir(srcPath, destDir)

        for root, dirs, files in walk(srcDirPath, topdown=False):
            for f in files:
                moveItem(root, f)
            for d in dirs:
                moveItem(root, d, True)
    else:
        srcPaths = [joinPath(srcDirPath, item) for item in listdir(srcDirPath)]
        moveToDir(srcPaths, destDirPath)
    removeDir(srcDirPath)
Exemple #2
0
def __removeFromDir(subPath, parentDirPath):
    """ Removes 1 item (recursively) """
    parentDirPath = absPath(parentDirPath)
    remPath = joinPath(parentDirPath, subPath)
    if isFile(remPath):
        removeFile(remPath)
        print('Removed "%s"' % (remPath, ))
    elif isDir(remPath):
        removeDir(remPath)
        print('Removed "%s"' % (remPath, ))
Exemple #3
0
def toZipFile(sourceDir,
              zipDest=None,
              removeScr=True,
              isWrapperDirIncluded=False):
    sourceDir = absPath(sourceDir)
    if zipDest is None:
        zipDest = sourceDir  # make_archive adds extension
    else:
        if isFile(zipDest): removeFile(zipDest)
        zipDest, _ = splitExt(zipDest)
    if isWrapperDirIncluded:
        filePath = make_archive(zipDest, 'zip', dirPath(sourceDir),
                                fileBaseName(sourceDir))
    else:
        filePath = make_archive(zipDest, 'zip', sourceDir)
    print('Created zip file: "%s"' % (filePath, ))
    if removeScr:
        removeDir(sourceDir)
        print('Removed directory: "%s"' % (sourceDir, ))
    return filePath