Exemplo n.º 1
0
    def move(src, dst, overwrite, make_safe_path=get_safe_path):

        real_dst = os.path.join(dst, _basename(src))
        if not overwrite:
            real_dst = make_safe_path(real_dst)
        yield from raw_move(src, dst, overwrite, make_safe_path)
        client.move_buf(src, real_dst)
Exemplo n.º 2
0
def move(src,
         dst,
         overwrite=False,
         make_safe_path=get_safe_path,
         name_pairs=[]):
    """Recursively move a file or directory to another location. This is
    similar to the Unix "mv" command.

    If the destination is a directory or a symlink to a directory, the source
    is moved inside the directory. The destination path must not already
    exist.

    If the destination already exists but is not a directory, it may be
    overwritten depending on os.rename() semantics.

    If the destination is on our current filesystem, then rename() is used.
    Otherwise, src is copied to the destination and then removed.
    A lot more could be done here...  A look at a mv.c shows a lot of
    the issues this implementation glosses over.

    """
    real_dst = dst
    if os.path.isdir(dst):
        if _samefile(src, dst):
            # We might be on a case insensitive filesystem,
            # perform the rename anyway.
            os.rename(src, dst)
            name_pairs.append((src, dst))
            return

        real_dst = os.path.join(dst, _basename(src))
    if not overwrite:
        real_dst = make_safe_path(real_dst)
    try:
        os.rename(src, real_dst)
        name_pairs.append((src, real_dst))
    except OSError:
        if os.path.isdir(src):
            if _destinsrc(src, dst):
                raise Error("Cannot move a directory '%s' into itself '%s'." %
                            (src, dst))
            for done in copytree(src,
                                 real_dst,
                                 symlinks=True,
                                 overwrite=overwrite,
                                 make_safe_path=make_safe_path):
                yield done
            rmtree(src)
            name_pairs.append((src, real_dst))
        else:
            for done in copy2(src,
                              real_dst,
                              symlinks=True,
                              overwrite=overwrite,
                              make_safe_path=make_safe_path):
                yield done
            os.unlink(src)
            name_pairs.append((src, real_dst))
Exemplo n.º 3
0
def loadTempLibrary(directory, name):
    #print("Loading", name, "from", directory)
    libPath = find_library(directory, name) or path.join(
        directory, name, "lib" + name + '.so')
    hexHash = hashlib.md5(open(libPath, 'rb').read()).hexdigest()
    #print("Loading", name, "from", directory, ":", libPath)

    # Create a temporary file, based on the MD5 hash, that is a copy of the target library, and load it.
    libTemp = path.join(tempfile.mkdtemp(),
                        hexHash + "-" + shutil._basename(libPath))
    shutil.copyfile(libPath, libTemp)
    #print("Loaded as", libTemp)
    return ctypes.cdll.LoadLibrary(libTemp)
Exemplo n.º 4
0
def shutil_move(src, dst):
    """Recursively move a file or directory to another location. This is
    similar to the Unix "mv" command. Return the file or directory's
    destination.

    If the destination is a directory or a symlink to a directory, the source
    is moved inside the directory. The destination path must not already
    exist.

    If the destination already exists but is not a directory, it may be
    overwritten depending on os.rename() semantics.

    If the destination is on our current filesystem, then rename() is used.
    Otherwise, src is copied to the destination and then removed. Symlinks are
    recreated under the new name if os.rename() fails because of cross
    filesystem renames.

    A lot more could be done here...  A look at a mv.c shows a lot of
    the issues this implementation glosses over.

    """
    real_dst = dst
    if _os.path.isdir(dst):
        if shutil._samefile(src, dst):
            # We might be on a case insensitive filesystem,
            # perform the rename anyway.
            _os.rename(src, dst)
            return

        real_dst = _os.path.join(dst, shutil._basename(src))
        if _os.path.exists(real_dst):
            raise shutil.Error("Destination path '%s' already exists" % real_dst)
    try:
        _os.rename(src, real_dst)
    except OSError:
        if _os.path.islink(src):
            linkto = _os.readlink(src)
            _os.symlink(linkto, real_dst)
            _os.unlink(src)
        elif _os.path.isdir(src):
            if shutil._destinsrc(src, dst):
                raise shutil.Error("Cannot move a directory '%s' into itself '%s'." % (src, dst))
            shutil.copytree(src, real_dst, symlinks=True)
            shutil.rmtree(src)
        else:
            shutil.copy2(src, real_dst)
            _os.unlink(src)
    return real_dst
Exemplo n.º 5
0
Arquivo: utils.py Projeto: tylii/ddap
def check_file_type(file):
    name = shutil._basename(file)
    with open(file) as f:
        first_line = f.readline().rstrip()
        second_line = f.readline().rstrip()

    if re.search('gbk$|gb$', name, re.IGNORECASE):
        return 'antiSMASH'
    if re.search(',', second_line) and re.search('csv$', name, re.IGNORECASE):
        return 'csv'
    if first_line.startswith('>') and re.search('fa$|fasta$', name,
                                                re.IGNORECASE):
        return 'fa'

    print(
        '[ERROR] Invalid file format. Accepted file formats are ".gbk", ".csv" and ".fasta"/".fa".'
    )
    sys.exit()
Exemplo n.º 6
0
 def _copyFile(self, oldPath=None, newPath=None):
     if not oldPath: return False
     try:
         oldPath = ProfileFunc.slashFormat(oldPath)
         newPath = ProfileFunc.slashFormat(newPath)
         if not os.path.exists(oldPath):
             return False
         if not os.path.exists(newPath):
             UtilFunc.makeDirs(newPath)
         real_dst = os.path.join(newPath, shutil._basename(oldPath))
         real_dst = self._isExist(real_dst)
         if not real_dst: return False
         if os.path.isdir(oldPath):
             self.copytree(oldPath, real_dst)
         else:
             shutil.copyfile(oldPath, real_dst)
         return True
     except Exception, e:
         Log.error('Copy file failed!!! Reason[%s]' % e)
         return False
Exemplo n.º 7
0
def move(src, dst):
    """Recursively move a file or directory to another location. This is
    similar to the Unix "mv" command.

    If the destination is a directory or a symlink to a directory, the source
    is moved inside the directory. The destination path must not already
    exist.

    If the destination already exists but is not a directory, it may be
    overwritten depending on os.rename() semantics.

    If the destination is on our current filesystem, then rename() is used.
    Otherwise, src is copied to the destination and then removed.
    A lot more could be done here...  A look at a mv.c shows a lot of
    the issues this implementation glosses over.

    """
    real_dst = dst
    if os.path.isdir(dst):
        if _samefile(src, dst):
            # We might be on a case insensitive filesystem,
            # perform the rename anyway.
            os.rename(src, dst)
            return

        real_dst = os.path.join(dst, _basename(src))
        if os.path.exists(real_dst):
            raise Error("Destination path '%s' already exists" % real_dst)
    try:
        os.rename(src, real_dst)
    except OSError:
        if os.path.isdir(src):
            if _destinsrc(src, dst):
                msg = "Cannot move a directory '%s' into itself '%s'." % (src,
                                                                          dst)
                raise Error(msg)
            copytree(src, real_dst, symlinks=True)
            rmtree(src)
        else:
            copy(src, real_dst)
            os.unlink(src)
Exemplo n.º 8
0
def usb_autorun_basicvirus_remover(path, virus_not_removed_list):
    '''remove auto run virus for drives'''
    autorun_viruses = [
        "ravmon.exe", "ntdelect.com", "new folder.exe", "kavo.exe",
        "autorun.inf", "newfolder.exe", "scvvhsot.exe", "scvhsot.exe",
        "hinhem.scr", "scvhosts.exe", "new_folder.exe", "regsvr.exe",
        "svichossst.exe", "autorun.ini", "blastclnnn.exe", "csrss.exe"
    ]  #"arona.exe", "logon.bat"

    ppath = os.path.normpath(path)

    if os.path.isfile(ppath):
        if re.match(r'^C:\\Windows\\System(32|64)\\Csrss.exe', ppath,
                    re.IGNORECASE):
            return  #if is csrss is in system path return

        if ppath[0:2].lower() == "c:" and \
         not re.match(r'^C:\\Windows(\\System(32|64))*\\Ravmon.exe', ppath, re.IGNORECASE) is None:  #if revmon is found in c:windows folder then is 90% malware
            return

        basename = shutil._basename(ppath)
        try:
            autorun_viruses.index(basename.lower())

            if os_type == "Windows":
                windows_cmd['KILL'].append(basename)
                subprocess.check_call(
                    windows_cmd['KILL'],
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL)  # Kill process if running
                windows_cmd['KILL'].pop()

        except ValueError:
            return
        except subprocess.CalledProcessError:  #can't block deleting the file
            pass

        try:
            os.unlink(ppath)
        except:
            virus_not_removed_list.append(ppath)
Exemplo n.º 9
0
    def _move(self, oldPath=None, newPath=None):
        if not oldPath: return False
        try:
            oldPath = ProfileFunc.slashFormat(oldPath)
            newPath = ProfileFunc.slashFormat(newPath)
            if not os.path.exists(oldPath):
                return False
            if not os.path.exists(newPath):
                UtilFunc.makeDirs(newPath)

            newPath = os.path.join(newPath, shutil._basename(oldPath))
            newPath = self._isExist(newPath)
            if not newPath: return False
            if os.path.isdir(oldPath):
                if not os.path.exists(newPath):
                    UtilFunc.makeDirs(newPath)
            self._moveFolder(oldPath, newPath)
            return True
        except Exception, e:
            Log.error('Move file failed!!! Reason[%s]' % e)
            return False
Exemplo n.º 10
0
 def test_rstrip(self):
     "Rstrip should be available. (shutil.move)"
     p = Path('/tmp/foo/bar/')
     bn = shutil._basename(p)
     self.assertEqual('bar', bn)
def copy_deps(templates_path, build_path):
    deps = map(lambda x: "{}/{}".format(templates_path, x), ('jquery.min.js',))
    map(lambda x: shutil.copyfile(x, build_path + '/' + shutil._basename(x)), deps)
Exemplo n.º 12
0
def checkmedia():
    try:
        media_dir_idx = os.listdir(shutil._basename('d:\\') + '/').index('media')
        return True, media_dir_idx
    except ValueError as e:
        return False, e
Exemplo n.º 13
0
 def update_event(self, inp=-1):
     self.set_output_val(0, shutil._basename(self.input(0)))