Beispiel #1
0
def normalizeNumericalFilePaths(globRegex):
    """
        This function get a glob path and rename all file1.json file2.json ... file20.json
        to file01.json file02.json ... file20.json to better sort the folder by file names
    """
    # We get all paths:
    allPaths = sortedGlob(globRegex)
    allNumbers = []
    # We get all ints:
    for path in allPaths:
        # Get the filename without extension:
        (dir, filename, ext, filenameExt) = decomposePath(path)
        # Get all numbers:
        currentNumbers = getAllNumbers(filename)
        # Check if we have a int first:
        if currentNumbers is None or len(currentNumbers) == 0:
            print("A filename has no number.")
            return False
        firstNumber = currentNumbers[0]
        if not isinstance(firstNumber, int):
            print("A filename has no float as first number.")
            return False
        # Add it in the list:
        allNumbers.append(firstNumber)
    # Get the max int:
    maxInt = max(allNumbers)
    # Calculate the nmber of digit:
    digitCountHasToBe = len(str(maxInt))
    # Replace all :
    i = 0
    for i in range(len(allNumbers)):
        currentPath = allPaths[i]
        (dir, filename, ext, filenameExt) = decomposePath(currentPath)
        currentInt = allNumbers[i]
        currentRegex = "0*" + str(currentInt)
        zerosCountToAdd = digitCountHasToBe - len(str(currentInt))
        zerosStr = "0" * zerosCountToAdd
        newFilename = re.sub(currentRegex, zerosStr + str(currentInt), filename, count=1)
        newFilename = dir + newFilename + "." + ext
        if currentPath != newFilename:
            os.rename(currentPath, newFilename)
            print(newFilename + " done.")
        i += 1
    return True
Beispiel #2
0
def cleanDir\
(
    path,
    startsWith=None,
    endsWith=None,
    olderHour=4,
    onlyOwner=True,
    verbose=False,
    logger=None,
    dryRun=False,
    removeKwargs={},
    pathContains="/tmp" # For security purpose

):
    me = getpass.getuser()
    elementsToDelete = []
    for element in sortedGlob(path + "/*"):
        if onlyOwner and owner(element) != me:
            continue
        if olderHour is not None and getLastModifiedTimeSpent(element, timeSpentUnit=TIMESPENT_UNIT.HOURS, logger=logger, verbose=False) < olderHour:
            continue
        if startsWith is not None and not decomposePath(element)[3].startswith(startsWith):
            continue
        if endsWith is not None and not decomposePath(element)[3].endswith(endsWith):
            continue
        elementsToDelete.append(element)
    for element in elementsToDelete:
        if pathContains in element:
            try:
                if not dryRun:
                    if "secure" not in removeKwargs:
                        removeKwargs["secure"] = False
                    remove(element, **removeKwargs)
                if verbose:
                    msg = "We removed " + element
                    if logger is not None:
                        try:
                            logger.log(msg)
                        except: pass
                    else:
                        print(msg)
            except Exception as e:
                print(e)
Beispiel #3
0
def extract(filePath, destinationDir=None, upIfUnique=True, doDoubleExtract=True):
    if not isFile(filePath):
        print(filePath + " does not exist")
        return None
    # We get the dir of the file to extract:
    (dirPath, _, _, filenameExt) = decomposePath(filePath)
    # We extract it:
    extractedDirPath = xtract.xtract(filePath)
    # Here we check if the file end with ".tar":
    if doDoubleExtract and extractedDirPath[-4:] == ".tar":
        # So we re-extract it:
        previousPath = extractedDirPath
        extractedDirPath = xtract.xtract(extractedDirPath)
        # We remove the previous element:
        if isDir(previousPath):
            remove(previousPath, minSlashCount=4)
        elif isFile(previousPath):
            remove(previousPath, minSlashCount=4)
    # If there is only one folder or file under extractedDirPath, we up it:
    if upIfUnique and len(sortedGlob(extractedDirPath + "/*")) == 1:
        # We get the element path:
        elementPath = sortedGlob(extractedDirPath + "/*")[0]
        # We make the dst path:
        dst = dirPath + "/" + elementPath.split("/")[-1]
        # First we check if the element exists inthe parent dir:
        if isFile(dst) or isDir(dst):
            dst += time.strftime("-%Y.%m.%d-%H.%M.%S")
        # then we move it:
        shutil.move(elementPath, dst)
        # And finally we remove the dir:
        remove(extractedDirPath, minSlashCount=4)
        # We update extractedDirPath:
        extractedDirPath = dst
    # We move the element:
    if destinationDir is not None:
        # We move it:
        newDestFilePath = destinationDir + "/" + decomposePath(extractedDirPath)[3]
        shutil.move(extractedDirPath, newDestFilePath)
        # We update extractedDirPath:
        extractedDirPath = newDestFilePath
    # Finally we return the new path:
    return extractedDirPath
Beispiel #4
0
def copyDir(src, dst):
    if not (src.startswith("/") and src.startswith("/")):
        raise Exception("Pls give absolute path.")
    if src.count("/") < 2 or dst.count("/") < 2:
        raise Exception("Pls give a deep folder (by security).")
    (dir, filename, ext, filenameExt) = decomposePath(src)
    if dir[-1] != "/":
        dir += "/"
    dir = dir + filenameExt
    if dir != src or not isDir(dir):
        raise Exception("Pls give a right dir path.")
    dirName = dir.split("/")[-1]
    return copy_tree(src, dst + "/" + dirName)
Beispiel #5
0
def decryptFile(path, key, ext=".encrypted.zip", remove=False, logger=None, verbose=True):
    """
        This function decrypt a file and return the text
    """
    try:
        (dir, _, _, _) = decomposePath(path)
        key = str.encode(key)
        if path[-len(ext):] != ext:
            decryptedFilePath = path
            cryptedFilePath = decryptedFilePath + ext
        else:
            cryptedFilePath = path
            decryptedFilePath = path[:-len(ext)]
        zipfile.ZipFile(cryptedFilePath).extractall(dir, None, key)
        if remove:
            removeFile(cryptedFilePath)
        return fileToStr(decryptedFilePath)
    except Exception as e:
        if verbose:
            if logger is None:
                print(str(e))
            else:
                logger.error(str(e))
        return None