예제 #1
0
def copydescriptions(src, dst):
    """
    Just copy the descriptions portion of an environment
    directory from src to dst.
    """

    if not dst.endswith('.d'):
        raise RuntimeError(f"[-] destination '{dst}' is not a descriptions "
                           "('.d') directory")
    # Ensure destination directory exists.
    os.makedirs(dst, exist_ok=True)
    errors = []
    try:
        if src.endswith('.d') and os.path.isdir(src):
            copytree(src, dst)
        else:
            raise RuntimeError(f"[-] source '{src}' is not a descriptions "
                               "('.d') directory")
    except OSError as err:
        errors.append((src, dst, str(err)))
    # catch the Error from the recursive copytree so that we can
    # continue with other files
    except Error as err:
        errors.extend(err.args[0])
    if errors:
        raise Error(errors)
    psec.utils.remove_other_perms(dst)
예제 #2
0
def copy_file(log_filename_list, dest_path):  
    """ copy file """
    for src_file in log_filename_list:
        if os.path.isfile(src_file): 
            if os.path.isdir(dest_path):      
                pass
            else:
                os.makedirs(dest_path)
            errors = []                             
            srcname = src_file    
            filename = os.path.basename(src_file)
            dstname = os.path.join(dest_path, filename)   
            try:                                
                if os.path.isfile(srcname):        
                    copy2(srcname, dstname) 
                elif os.path.isdir(dstname):          
                    os.remove(dstname)
                    copy2(srcname, dstname)    
            except Exception, e:
                initlog(str(e))
                      
            try:                               
                copystat(srcname, dstname)              
            except Exception, e:
                initlog(str(e))
            if errors:                          
                raise Error(errors)             
예제 #3
0
def _preferences_copytree(entries, src, dst):
    import os
    import shutil
    from shutil import Error

    os.makedirs(dst, exist_ok=True)
    errors = []

    for srcentry in entries:
        srcname = os.path.join(src, srcentry.name)
        dstname = os.path.join(dst, srcentry.name)
        srcobj = srcentry
        try:
            if srcentry.is_symlink():
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
                shutil.copystat(srcobj, dstname, follow_symlinks=False)
            elif srcentry.is_dir():
                preferences_copytree(srcobj, dstname)
            else:
                shutil.copy2(srcentry, dstname)
        except Error as err:
            errors.extend(err.args[0])
        except OSError as why:
            errors.append((srcname, dstname, str(why)))
    try:
        shutil.copystat(src, dst)
    except OSError as why:
        if getattr(why, 'winerror', None) is None:
            errors.append((src, dst, str(why)))
    if errors:
        raise Error(errors)
    return dst
예제 #4
0
파일: init.py 프로젝트: tzidore/TuttleOFX
def movetree(src, dst, symlinks=False):
	names = os.listdir(src)
	if not os.path.exists(dst) :
		os.makedirs(dst)
	errors = []
	for name in names:
		srcname = os.path.join(src, name)
		dstname = os.path.join(dst, name)
		try:
			if symlinks and os.path.islink(srcname):
				linkto = os.readlink(srcname)
				os.symlink(linkto, dstname)
			elif os.path.isdir(srcname):
				movetree(srcname, dstname, symlinks)
				#copytree(srcname, dstname, symlinks)
			else:
				move(srcname, dstname)
				#copy2(srcname, dstname)
			# XXX What about devices, sockets etc.?
		except (IOError, os.error) as why:
			errors.append((srcname, dstname, str(why)))
		# catch the Error from the recursive copytree so that we can
		# continue with other files
		except Error as err:
			errors.extend(err.args[0])
	try:
		copystat(src, dst)
	except WindowsError:
		# can't copy file access times on Windows
		pass
	except OSError as why:
		errors.extend((src, dst, str(why)))
	if errors:
		raise Error(errors)
예제 #5
0
def copytree(src, dst, symlinks=False):  # Obtained from shutil
    names = os.listdir(src)
    os.makedirs(dst, exist_ok=True)  # Added exist_ok=True
    errors = []
    for name in names:
        srcName = os.path.join(src, name)
        dstName = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcName):
                linkTo = os.readlink(srcName)
                os.symlink(linkTo, dstName)
            elif os.path.isdir(srcName):
                copytree(srcName, dstName, symlinks)
            else:
                shutil.copy2(srcName, dstName)
        except OSError as why:
            errors.append((srcName, dstName, str(why)))
        # catch the Error from the recursive copytree so that we can
        # continue with other files
        except Error as err:
            errors.extend(err.args[0])

    try:
        shutil.copystat(src, dst)
    except OSError as why:
        # can't copy file access times on Windows
        if why.winerror is None:
            errors.extend((src, dst, str(why)))
    if errors:
        raise Error(errors)
예제 #6
0
def get_random_seconds_divergent_seeds(restset: PreparedDataList,
                                       symbols: SymbolIdDict, seed: int,
                                       seconds: float, samples: int,
                                       n: int) -> OrderedSet[int]:
    available_speaker_data = get_speaker_wise(restset)

    if len(available_speaker_data) > 1:
        raise Error("This method is not supported for multiple speakers.")

    speaker_available = list(available_speaker_data.values())[0]
    speaker_available_dict = prep_data_list_to_dict_with_symbols(
        speaker_available, symbols)
    speaker_avail_durations_s = prep_data_list_to_dict_with_durations_s(
        speaker_available)

    selected_seeds = random_seconds_divergence_seeds(
        data=speaker_available_dict,
        seed=seed,
        durations_s=speaker_avail_durations_s,
        seconds=seconds,
        samples=samples,
        n=n,
    )

    return selected_seeds
def copytree(src, names, type, dst, symlinks=False):
    os.makedirs(dst)
    errors = []
    for name in names:
        name += type
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcname):
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
            elif os.path.isdir(srcname):
                copytree(srcname, dstname, symlinks)
            else:
                copy2(srcname, dstname)
            # XXX What about devices, sockets etc.?
        except OSError as why:
            errors.append((srcname, dstname, str(why)))
        # catch the Error from the recursive copytree so that we can
        # continue with other files
        except Error as err:
            errors.extend(err.args[0])
    try:
        copystat(src, dst)
    except OSError as why:
        # can't copy file access times on Windows
        if why.winerror is None:
            errors.extend((src, dst, str(why)))
    if errors:
        raise Error(errors)
예제 #8
0
def copytree(src, dst, symlinks=False, ignore=None):
    """Recursively copy a directory tree using copy2().

    The destination directory must not already exist.
    If exception(s) occur, an Error is raised with a list of reasons.

    If the optional symlinks flag is true, symbolic links in the
    source tree result in symbolic links in the destination tree; if
    it is false, the contents of the files pointed to by symbolic
    links are copied.

    The optional ignore argument is a callable. If given, it
    is called with the `src` parameter, which is the directory
    being visited by copytree(), and `names` which is the list of
    `src` contents, as returned by os.listdir():

        callable(src, names) -> ignored_names

    Since copytree() is called recursively, the callable will be
    called once for each directory that is copied. It returns a
    list of names relative to the `src` directory that should
    not be copied.

    XXX Consider this example code rather than the ultimate tool.

    """
    names = os.listdir(src)
    if ignore is not None:
        ignored_names = ignore(src, names)
    else:
        ignored_names = set()

    os.makedirs(dst)
    errors = []
    for name in names:
        if name in ignored_names:
            continue
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcname):
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
            elif os.path.isdir(srcname):
                copytree(srcname, dstname, symlinks, ignore)
            else:
                # Will raise a SpecialFileError for unsupported file types
                copy2(srcname, dstname)
        # catch the Error from the recursive copytree so that we can
        # continue with other files
        except Error as err:
            errors.extend(err.args[0])
        except EnvironmentError as why:
            errors.append((srcname, dstname, "%s" % why))
    try:
        copystat(src, dst)
    except OSError as why:
        errors.append((src, dst, "%s" % why))
    if errors:
        raise Error(errors)
예제 #9
0
def get_model(model_name):
    model = None
    if model_name == 'vgg16':
        from models.vgg16 import Vgg16GAP
        model = Vgg16GAP(name="vgg16")
        return model

    if model_name == 'unet':
        from models.unet import UNet
        model = UNet()
        return model

    if model_name == 'deeplab':
        from models.deeplab import DeepLab
        model = DeepLab(name="deeplab")
        return model

    if model_name == 'affinitynet':
        from models.aff_net import AffNet
        model = AffNet(name="affinitynet")
        return model

    if model_name == 'wasscam':
        from models.wass import WASS
        model = WASS()
        return model

    raise Error('Model name has no implementation')
예제 #10
0
def copy_tree(src, dst, symlinks=False, ignore=None):
    """The ``copy_tree`` function is an exact copy of the built-in
    :func:`shutil.copytree`, with one key difference: it will not
    raise an exception if part of the tree already exists. It achieves
    this by using :func:`mkdir_p`.

    As of Python 3.8, you may pass :func:`shutil.copytree` the
    `dirs_exist_ok=True` flag to achieve the same effect.

    Args:
        src (str): Path of the source directory to copy.
        dst (str): Destination path. Existing directories accepted.
        symlinks (bool): If ``True``, copy symlinks rather than their
            contents.
        ignore (callable): A callable that takes a path and directory
            listing, returning the files within the listing to be ignored.

    For more details, check out :func:`shutil.copytree` and
    :func:`shutil.copy2`.

    """
    names = os.listdir(src)
    if ignore is not None:
        ignored_names = ignore(src, names)
    else:
        ignored_names = set()

    mkdir_p(dst)
    errors = []
    for name in names:
        if name in ignored_names:
            continue
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcname):
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
            elif os.path.isdir(srcname):
                copytree(srcname, dstname, symlinks, ignore)
            else:
                # Will raise a SpecialFileError for unsupported file types
                copy2(srcname, dstname)
        # catch the Error from the recursive copytree so that we can
        # continue with other files
        except Error as e:
            errors.extend(e.args[0])
        except EnvironmentError as why:
            errors.append((srcname, dstname, str(why)))
    try:
        copystat(src, dst)
    except OSError as why:
        if WindowsError is not None and isinstance(why, WindowsError):
            # Copying file access times may fail on Windows
            pass
        else:
            errors.append((src, dst, str(why)))
    if errors:
        raise Error(errors)
예제 #11
0
 def fetch_from_db_Deck(self):
     try:
         self.cursor.execute("SELECT * FROM DECK")
         result = self.cursor.fetchall()
         return result
     except sql.OperationalError:
         os.remove(f"{self.file_name}")
         raise Error("Table DECK not found")
예제 #12
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))
예제 #13
0
def copytree(src, dst, symlinks=False, ignore=None):
    """
    Copies src tree to dst

    shutil.copytree method uses copy2() and copystat() to perform the recursive copy of a
    directory. Both of these methods copy the attributes associated with the files. When copying
    files from /var/cache/pulp to /var/lib/pulp, we don't want to copy the SELinux security context
    labels.

    :param src: Source directory rooted at src
    :type  src: basestring
    :param dst: Destination directory, a new directory and any parent directories are created if
                any are missing
    :type  dst: basestring
    :param symlinks: If true, symlinks are copied as symlinks and metadata of original links is not
                     copied. If false, the contents and metadata of symlinks is copied to the new
                     tree.
    :type  symlinks: boolean
    :param ignore: If provided, it receives as its arguments the directory being visited by
                   copytree and the content of directory as returned by os.listdir(). The callable
                   must return a sequence of directory and file names relative to the current
                   directory (i.e. a subset of the items in its second argument); these names will
                   then be ignored in the copy process.
    :type  ignore: Callable
    """

    names = os.listdir(src)
    if ignore is not None:
        ignored_names = ignore(src, names)
    else:
        ignored_names = set()

    os.makedirs(dst)
    errors = []
    for name in names:
        if name in ignored_names:
            continue
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcname):
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
            elif os.path.isdir(srcname):
                copytree(srcname, dstname, symlinks, ignore)
            else:
                # Don't need to copy attributes
                copy(srcname, dstname)
            # XXX What about devices, sockets etc.?
        except (IOError, os.error) as why:
            errors.append((srcname, dstname, str(why)))
        # catch the Error from the recursive copytree so that we can
        # continue with other files
        except Error as err:
            errors.extend(err.args[0])
    if errors:
        raise Error(errors)
예제 #14
0
def copyfile_custom(src, dst):
    """Copy data from src to dst"""
    if _samefile(src, dst):
        raise Error("`%s` and `%s` are the same file" % (src, dst))

    for fn in [src, dst]:
        try:
            st = os.stat(fn)
        except OSError:
            # File most likely does not exist
            pass
        else:
            # XXX What about other special files? (sockets, devices...)
            if stat.S_ISFIFO(st.st_mode):
                try:
                    raise SpecialFileError("`%s` is a named pipe" % fn)
                except NameError:
                    raise Error("`%s` is a named pipe" % fn)

    try:
        # Windows
        O_BINARY = os.O_BINARY
    except:
        O_BINARY = 0

    READ_FLAGS = os.O_RDONLY | O_BINARY
    WRITE_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | O_BINARY
    BUFFER_SIZE = 128 * 1024

    try:
        fin = os.open(src, READ_FLAGS)
        fout = os.open(dst, WRITE_FLAGS)
        for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""):
            os.write(fout, x)
    except Exception as e:
        raise
    finally:
        try:
            os.close(fin)
            os.close(fout)
        except:
            pass
def mergetree(src, dst, symlinks=False, ignore=None):
    """
    Like shutil.copytree except it overwrites the destination

    If dst does not exist it is created.

    If dst exists but is not a directory it is repaced.

    If dst exists and is a directory the files in src are copied to dst. If a file already exists in dst it is overwritten. The child directories of src are copied to dst by recursive calls to mergetree.

    The implementation is adapted from the code for shutil.copytree at https://docs.python.org/2/library/shutil.html#copytree-example
    """
    names = os.listdir(src)
    if ignore is not None:
        ignored_names = ignore(src, names)
    else:
        ignored_names = set()

    if os.path.exists(dst) and not os.path.isdir(dst):
        os.remove(dst)
    if not os.path.isdir(dst):
        os.makedirs(dst)

    errors = []
    for name in names:
        if name in ignored_names:
            continue
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcname):
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
            elif os.path.isdir(srcname):
                mergetree(srcname, dstname, symlinks, ignore)
            else:
                copy2(srcname, dstname)
            # XXX What about devices, sockets etc.?
        except (IOError, os.error) as why:
            errors.append((srcname, dstname, str(why)))
        # catch the Error from the recursive copytree so that we can
        # continue with other files
        except Error as err:
            errors.extend(err.args[0])
    try:
        copystat(src, dst)
    except WindowsError:
        # can't copy file access times on Windows
        pass
    except OSError as why:
        errors.extend((src, dst, str(why)))
    if errors:
        raise Error(errors)
예제 #16
0
def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
             ignore_dangling_symlinks=False, exist_ok=False):
    
    names = os.listdir(src)
    if ignore is not None:
        ignored_names = ignore(src, names)
    else:
        ignored_names = set()

    os.makedirs(dst, exist_ok = exist_ok)
    errors = []
    for name in names:
        if name in ignored_names:
            continue
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if os.path.islink(srcname):
                linkto = os.readlink(srcname)
                if symlinks:
                    # We can't just leave it to `copy_function` because legacy
                    # code with a custom `copy_function` may rely on copytree
                    # doing the right thing.
                    os.symlink(linkto, dstname)
                    copystat(srcname, dstname, follow_symlinks=not symlinks)
                else:
                    # ignore dangling symlink if the flag is on
                    if not os.path.exists(linkto) and ignore_dangling_symlinks:
                        continue
                    # otherwise let the copy occurs. copy2 will raise an error
                    if os.path.isdir(srcname):
                        copytree(srcname, dstname, symlinks, ignore,
                                 copy_function, exist_ok=exist_ok)
                    else:
                        copy_function(srcname, dstname)
            elif os.path.isdir(srcname):
                copytree(srcname, dstname, symlinks, ignore,
                    copy_function, exist_ok = exist_ok
                )
            else:
                # Will raise a SpecialFileError for unsupported file types
                copy_function(srcname, dstname)
        # catch the Error from the recursive copytree so that we can
        # continue with other files
        except Error as err:
            errors.extend(err.args[0])
        except OSError as why:
            errors.append((srcname, dstname, str(why)))
    
    if errors:
        raise Error(errors)
    return dst
예제 #17
0
 def CopyWorker(self):
     """Thread worker for file copying."""
     while True:
         fileName = fileQueue.get()
         try:
             isdir = fileName.is_dir()  # scandir object
             issym = fileName.is_symlink()
             size = fileName.stat().st_size
             fname = fileName.name
         except AttributeError:  # file from txt
             fname, fpath, size = fileName
             isdir = True if os.path.isdir(fpath) else False
             issym = True if os.path.islink(fpath) else False
         if not self.is_package_task:
             srcname = os.path.join(self.src, fname)
         else:
             srcname = fname
         dstname = os.path.join(self.dst, fname)
         try:
             if isdir:
                 #copyf = shutil.copy2 if self.copymeta else shutil.copyfile
                 #shutil.copytree(srcname, dstname, symlinks=self.symlinks,
                 #                ignore=self.ignore, copy_function=copyf)
                 pass
             else:
                 if issym is self.symlinks:
                     if self.copymeta:
                         try:
                             shutil.copy2(srcname, dstname)
                         except (OSError, IOError) as e:
                             self.errors.extend(
                                 (self.src, self.dst, str(e)))
                     else:
                         shutil.copyfile(srcname, dstname)
         except (OSError, IOError) as e:
             self.errors.append((srcname, dstname, str(e)))
         # catch the Error from the recursive copytree so that we can
         # continue with other files
         except Error as e:
             self.errors.extend(e.args[0])
         if self.errors:
             raise Error(self.errors)
         with self.lock:
             self.copyCount += 1
             #percent = (self.copyCount * 100) / self.totalFiles
             #print(str(percent) + " percent copied.")
             self.pbar.set_postfix(file=fname[-10:], refresh=False)
             self.pbar.update(size)
         fileQueue.task_done()
예제 #18
0
    def vbm_characters(self, characters: list) -> None:
        """
        Set vbm_characters 
        """
        try:
            if characters != None:
                for element in characters:
                    element[0] = element[0].capitalize()
                    element[1] = element[1].lower()
                    element[2] = float(element[2])
        except:
            loguru.logger.error("repalce_vbm incorrectly specified")
            raise Error("repalce_vbm incorrectly specified")

        self._vbm_characters = characters
예제 #19
0
    def divide_character(self, factors: int) -> None:
        """
        Set factor that divides the correction between atoms
        """
        try:
            if factors != None:
                for element in factors:
                    element[0] = element[0].capitalize()
                    element[1] = element[1].lower()
                    element[2] = int(element[2])
        except:
            loguru.logger.error("divide_character incorrectly specified")
            raise Error("divide_character incorrectly specified")

        self._divide_character = factors
예제 #20
0
    def conduction_cut_guess(self, cut_guess: list) -> None:
        """
        Set conduction_cut_guess
        """

        try:
            if cut_guess != None:
                for element in cut_guess:
                    element[0] = element[0].capitalize()
                    element[1] = element[1].lower()
                    element[2] = float(element[2])
        except:
            loguru.logger.error("conduction_cut_guess incorrectly specified")
            raise Error("conduction_cut_guess incorrectly specified")

        self._conduction_cut_guess = cut_guess
예제 #21
0
def parse_bool(message: str) -> bool:
    message = str(message)

    result = message.lower()

    if result == "y" or result == "1" or result == "t" or result == "true":
        return True
    if result == "n" or result == "0" or result == "f" or result == "false":
        return False

    if should_quit(result):
        raise QuitError(result)

    if should_restart(result):
        raise RestartError(result)

    raise Error(message)
예제 #22
0
def copyfiles(src, dst, ignore=None):
    """
   https://docs.python.org/2/library/shutil.html#copytree-example
   with some modifications (destination folder can exist)
   """
    names = os.listdir(src)
    files = []
    if ignore is not None:
        ignored_names = ignore(src, names)
    else:
        ignored_names = set()

    if not os.path.exists(dst):
        os.makedirs(dst)
    errors = []
    for name in names:
        if name in ignored_names:
            continue
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if os.path.isdir(srcname):
                files += copyfiles(srcname, dstname, ignore)
            else:
                copy2(srcname, dstname)
                files += [dstname]
            # XXX What about devices, sockets etc.?
        except (IOError, os.error) as why:
            errors.append((srcname, dstname, str(why)))
        # catch the Error from the recursive copytree so that we can
        # continue with other files
        except Error as err:
            errors.extend(err.args[0])
    try:
        copystat(src, dst)
    except WindowsError:
        # can't copy file access times on Windows
        pass
    except OSError as why:
        errors.extend((src, dst, str(why)))
    if errors:
        raise Error(errors)
    return files
예제 #23
0
def copyfile(src, dst):
    """Copy data from src to dst"""
    if _samefile(src, dst):
        raise Error("`%s` and `%s` are the same file" % (src, dst))

    for fn in [src, dst]:
        try:
            st = os.stat(fn)
        except OSError:
            # File most likely does not exist
            pass
        else:
            # XXX What about other special files? (sockets, devices...)
            if stat.S_ISFIFO(st.st_mode):
                raise SpecialFileError("`%s` is a named pipe" % fn)

    with open(src, 'rb') as fsrc:
        with open(dst, 'wb') as fdst:
            copyfileobj(fsrc, fdst)
예제 #24
0
def copyfile(src, dst):
    """Copy data from src to dst"""
    if _samefile(src, dst):
        raise Error("`%s` and `%s` are the same file" % (src, dst))

    for fn in [src, dst]:  # pylint: disable=invalid-name
        try:
            st = os.stat(fn)  # pylint: disable=invalid-name
        except OSError:
            # File most likely does not exist
            pass
        else:
            # XXX What about other special files? (sockets, devices...)
            if stat.S_ISFIFO(st.st_mode):
                raise SpecialFileError("`%s` is a named pipe" % fn)

    with open(src, "rb") as fsrc:
        with open(dst, "wb") as fdst:
            for done in copyfileobj(fsrc, fdst):
                yield done
예제 #25
0
    def CopyWorker(self):
        """Thread worker for file copying."""
        try:
            from urlparse import urljoin
        except ImportError:
            from urllib.parse import urljoin

        while True:
            fileItem = fileQueue.get()
            fileName, size = fileItem
            url = self.tserv_lb()
            srcname = urljoin(url, fileName)
            dstname = os.path.join(self.dst, fileName)
            self.FetchFile(srcname, dstname)
            if self.errors:
                raise Error(self.errors)
            with self.lock:
                self.copyCount += 1
                self.pbar.set_postfix(file=fileName[-10:], refresh=False)
                self.pbar.update(size)
            fileQueue.task_done()
예제 #26
0
    def run_rclone(self):
        from subprocess import check_output, CalledProcessError, STDOUT

        cmd = ['rclone']
        [cmd.append(f) for f in self.flags]
        cmd.append(self.command)
        cmd.append(self.src)
        cmd.append(self.dst)
        [cmd.append(a) for a in self.cmdargs]
        logger.debug('rclone command: {}'.format(" ".join(cmd)))
        
        logger.info('Starting rclone from %s to %s..' % (self.src, self.dst))
        try:
            output = check_output(cmd, stderr=STDOUT)
            logger.debug(output)
        except CalledProcessError as e:
            self.errors.append((self.src, self.dst, str(e.output), str(e.returncode)))
        if self.errors:
            raise Error(self.errors)

        logger.info('Done')
예제 #27
0
def copytree(src, dst, symlinks=True):
    """
    Modified from shutil.copytree docs code sample, merges files rather than
    requiring dst to not exist.
    """
    from shutil import copy2, Error, copystat

    names = os.listdir(src)

    if not Path(dst).exists():
        os.makedirs(dst)

    errors = []
    for name in names:
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcname):
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
            elif os.path.isdir(srcname):
                copytree(srcname, dstname, symlinks)
            else:
                copy2(srcname, dstname)
            # XXX What about devices, sockets etc.?
        except OSError as why:
            errors.append((srcname, dstname, str(why)))
        # catch the Error from the recursive copytree so that we can
        # continue with other files
        except Error as err:
            errors.extend(err.args[0])
    try:
        copystat(src, dst)
    except OSError as why:
        # can't copy file access times on Windows
        if why.winerror is None:
            errors.extend((src, dst, str(why)))
    if errors:
        raise Error(errors)
예제 #28
0
def copytree_multi(src, dst, symlinks=False, ignore=None):
    names = os.listdir(src)
    if ignore is not None:
        ignored_names = ignore(src, names)
    else:
        ignored_names = set()

    # -------- E D I T --------
    # os.path.isdir(dst)
    if not os.path.isdir(dst):
        os.makedirs(dst)
    # -------- E D I T --------

    errors = []
    for name in names:
        if name in ignored_names:
            continue
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcname):
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
            elif os.path.isdir(srcname):
                copytree_multi(srcname, dstname, symlinks, ignore)
            else:
                copy2(srcname, dstname)
        except (IOError, os.error) as why:
            errors.append((srcname, dstname, str(why)))
        except Error as err:
            errors.extend(err.args[0])
    try:
        copystat(src, dst)
    except WindowsError:
        pass
    except OSError as why:
        errors.extend((src, dst, str(why)))
    if errors:
        raise Error(errors)
예제 #29
0
 def my_copytree(self, src, dst, symlinks=False, ignore=None, preserve_attributes = 1, update=0):
      names = os.listdir(src)
      if ignore is not None:
           ignored_names = ignore(src, names)
      else:
           ignored_names = set()
      if not os.path.isdir(dst):
           os.makedirs(dst)
      errors = []
      for name in names:
           if name in ignored_names:
                continue
           srcname = os.path.join(src, name)
           dstname = os.path.join(dst, name)
           try:
                if symlinks and os.path.islink(srcname):
                     linkto = os.readlink(srcname)
                     os.symlink(linkto, dstname)
                elif os.path.isdir(srcname):
                     self.my_copytree(srcname, dstname, symlinks, ignore, preserve_attributes, update)
                else:
                     self.copyOptionChecking(srcname, dstname, preserve_attributes, update)
            # XXX What about devices, sockets etc.?
           except (IOError, os.error) as why:
                errors.append((srcname, dstname, str(why)))
        # catch the Error from the recursive copytree so that we can
        # continue with other files
           except Error as err:
                errors.extend(err.args[0])
      try:
           copystat(src, dst)
      except WindowsError:
           # can't copy file access times on Windows
           pass
      except OSError as why:
           errors.extend((src, dst, str(why)))
      if errors:
           raise Error(errors)
예제 #30
0
def copy_files(src, dst, symlinks=False):
    errors = []
    if os.path.isfile(src):
        copy_file(src, dst, symlinks, errors)
    else:
        names = os.listdir(src)
        if not os.path.exists(dst):
            os.makedirs(dst)

        for name in names:
            src_name = os.path.join(src, name)
            dst_name = os.path.join(dst, name)
            copy_file(src_name, dst_name, symlinks, errors)

        try:
            shutil.copystat(src, dst)
        except WindowsError:
            pass
        except OSError as why:
            if why.errno not in [5, 13]:
                errors.append((src, dst, str(why)))

    if errors:
        raise Error(errors)