コード例 #1
0
ファイル: utils.py プロジェクト: LlianeFR/craft
def copyFile(src,
             dest,
             linkOnly=CraftCore.settings.getboolean("General", "UseHardlinks",
                                                    False)):
    """ copy file from src to dest"""
    CraftCore.log.debug("copy file from %s to %s" % (src, dest))
    destDir = os.path.dirname(dest)
    if not os.path.exists(destDir):
        os.makedirs(destDir)
    if os.path.lexists(dest):
        CraftCore.log.warning(f"Overriding {dest}")
        if src == dest:
            CraftCore.log.error(f"Can't copy a file into itself {src}=={dest}")
            return False
        OsUtils.rm(dest, True)
    # don't link to links
    if linkOnly and not os.path.islink(src):
        try:
            os.link(src, dest)
            return True
        except:
            CraftCore.log.warning("Failed to create hardlink %s for %s" %
                                  (dest, src))
    shutil.copy2(src, dest, follow_symlinks=False)
    return True
コード例 #2
0
def copyFile(src: Path,
             dest: Path,
             linkOnly=CraftCore.settings.getboolean("General", "UseHardlinks",
                                                    False)):
    """ copy file from src to dest"""
    CraftCore.log.debug("copy file from %s to %s" % (src, dest))
    src = Path(src)
    dest = Path(dest)
    if dest.is_dir():
        dest = dest / src.name
    else:
        createDir(dest.parent)
    if os.path.lexists(dest):
        CraftCore.log.warning(f"Overriding:\t{dest} with\n\t\t{src}")
        if src == dest:
            CraftCore.log.error(f"Can't copy a file into itself {src}=={dest}")
            return False
        OsUtils.rm(dest, True)
    # don't link to links
    if linkOnly and not os.path.islink(src):
        try:
            os.link(src, dest)
            return True
        except:
            CraftCore.log.warning("Failed to create hardlink %s for %s" %
                                  (dest, src))
    try:
        shutil.copy2(src, dest, follow_symlinks=False)
    except Exception as e:
        CraftCore.log.error(f"Failed to copy file:\n{src} to\n{dest}",
                            exc_info=e)
        return False
    return True
コード例 #3
0
def copyFile(src,
             dest,
             linkOnly=craftSettings.getboolean("General", "UseHardlinks",
                                               False)):
    """ copy file from src to dest"""
    craftDebug.log.debug("copy file from %s to %s" % (src, dest))
    destDir = os.path.dirname(dest)
    if not os.path.exists(destDir):
        os.makedirs(destDir)
    if os.path.exists(dest):
        craftDebug.log.warning("Overriding %s" % dest)
        OsUtils.rm(dest, True)
    if linkOnly:
        try:
            os.link(src, dest)
            return True
        except:
            craftDebug.log.warning("Failed to create hardlink %s for %s" %
                                   (dest, src))
    shutil.copy(src, dest)
    return True
コード例 #4
0
ファイル: utils.py プロジェクト: juergenhoetzel/craft
def cleanDirectory(directory):
    CraftCore.log.debug("clean directory %s" % directory)
    if (os.path.exists(directory)):
        for root, dirs, files in os.walk(directory, topdown=False):
            for name in files:
                if not OsUtils.rm(os.path.join(root, name), True):
                    CraftCore.log.critical("couldn't delete file %s\n ( %s )" % (name, os.path.join(root, name)))
            for name in dirs:
                if not OsUtils.rmDir(os.path.join(root, name), True):
                    CraftCore.log.critical("couldn't delete directory %s\n( %s )" % (name, os.path.join(root, name)))
    else:
        os.makedirs(directory)
コード例 #5
0
def cleanDirectory(directory):
    CraftCore.log.debug("clean directory %s" % directory)
    if os.path.exists(directory):
        # don't delete containg directrory as it might be a symlink and replacing it with a folder
        # breaks the behaviour
        with os.scandir(directory) as scan:
            for f in scan:
                if f.is_dir():
                    if not OsUtils.rmDir(f.path, force=True):
                        return False
                else:
                    if not OsUtils.rm(f.path, force=True):
                        return False
        return True
    else:
        return createDir(directory)
コード例 #6
0
ファイル: test_OsUtils.py プロジェクト: juergenhoetzel/craft
 def test_rm(self):
     _, fileName = tempfile.mkstemp()
     OsUtils.rm(fileName)
コード例 #7
0
 def test_rm(self):
     fd, fileName = tempfile.mkstemp()
     os.close(fd)
     OsUtils.rm(fileName)