Python shutil.Error() Examples

The following are 30 code examples for showing how to use shutil.Error(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module shutil , or try the search function .

Example 1
Project: smbprotocol   Author: jborean93   File: test_smbclient_shutil.py    License: MIT License 6 votes vote down vote up
def test_copytree_with_broken_symlink_fail(smb_share):
    src_dirname = "%s\\source" % smb_share
    dst_dirname = "%s\\target" % smb_share

    mkdir(src_dirname)
    symlink("%s\\dir" % src_dirname, "%s\\link" % src_dirname, target_is_directory=True)
    symlink("%s\\file.txt" % src_dirname, "%s\\link.txt" % src_dirname)

    with pytest.raises(shutil.Error) as actual:
        copytree(src_dirname, dst_dirname)

    assert len(actual.value.args[0]) == 2
    err1 = actual.value.args[0][0]
    err2 = actual.value.args[0][1]

    assert err1[0] == "%s\\link" % src_dirname
    assert err1[1] == "%s\\link" % dst_dirname
    assert "No such file or directory" in err1[2]

    assert err2[0] == "%s\\link.txt" % src_dirname
    assert err2[1] == "%s\\link.txt" % dst_dirname
    assert "No such file or directory" in err2[2] 
Example 2
Project: glazier   Author: google   File: log_copy.py    License: Apache License 2.0 6 votes vote down vote up
def _ShareUpload(self, source_log: Text, share: Text):
    """Copy the log file to a network file share.

    Args:
      source_log: Path to the source log file to be copied.
      share: The destination share to copy the file to.

    Raises:
      LogCopyError: Failure to mount share and copy log.
    """
    creds = LogCopyCredentials()
    username = creds.GetUsername()
    password = creds.GetPassword()

    mapper = drive_map.DriveMap()
    result = mapper.MapDrive('l:', share, username, password)
    if result:
      destination = self._GetLogFileName()
      try:
        shutil.copy(source_log, destination)
      except shutil.Error:
        raise LogCopyError('Log copy failed.')
      mapper.UnmapDrive('l:')
    else:
      raise LogCopyError('Drive mapping failed.') 
Example 3
Project: glazier   Author: google   File: file_util.py    License: Apache License 2.0 6 votes vote down vote up
def CreateDirectories(path: Text):
  """Create directory if the path to a file doesn't exist.

  Args:
    path: The full file path to where a file will be placed.

  Raises:
    Error: Failure creating the requested directory.
  """
  dirname = os.path.dirname(path)
  if not os.path.isdir(dirname):
    logging.debug('Creating directory %s ', dirname)
    try:
      os.makedirs(dirname)
    except (shutil.Error, OSError):
      raise Error('Unable to make directory: %s' % dirname) 
Example 4
Project: glazier   Author: google   File: file_util.py    License: Apache License 2.0 6 votes vote down vote up
def Move(src: Text, dst: Text):
  """Move a file from src to dst.

  Python's os.rename doesn't support overwrite on Windows.

  Args:
    src: The full file path to the source.
    dst: The full file path to the destination.

  Raises:
    Error: Failure moving the file.
  """
  try:
    Remove(dst)
    os.rename(src, dst)
  except OSError as e:
    raise Error('Failure moving file from %s to %s. (%s)' % (src, dst, str(e))) 
Example 5
Project: MySQL-AutoXtraBackup   Author: ShahriyarR   File: prepare.py    License: MIT License 6 votes vote down vote up
def start_mysql_func(self, start_tool=None, options=None):
        # Starting MySQL
        logger.info("Starting MySQL server: ")
        if start_tool is None:
            args = self.start_mysql
        else:
            args = start_tool

        if options is not None:
            start_command = "{} {}".format(args, options)
        else:
            start_command = args
        status, output = subprocess.getstatusoutput(start_command)
        if status == 0:
            logger.info("Starting MySQL ...")
            logger.info(output)
            return True
        else:
            logger.error("Error occurred while starting MySQL!")
            logger.error(output)
            raise RuntimeError("Error occurred while starting MySQL!") 
Example 6
Project: MySQL-AutoXtraBackup   Author: ShahriyarR   File: prepare.py    License: MIT License 6 votes vote down vote up
def copy(self, options=None, datadir=None):
        """
        Function for running:
          xtrabackup --copy-back
          giving chown to datadir
          starting mysql
        :return: True if succeeded. Error if failed
        """
        logger.info("Copying Back Already Prepared Final Backup:")
        if len(os.listdir(self.datadir if datadir is None else datadir)) > 0:
            logger.info("MySQL Datadir is not empty!")
            return False
        else:
            self.run_xtra_copyback(datadir=datadir)
            self.giving_chown(datadir=datadir)
            self.start_mysql_func(options=options)
            return True 
Example 7
Project: Faraday-Software   Author: FaradayRF   File: proxy.py    License: GNU General Public License v3.0 6 votes vote down vote up
def saveProxyLog(name, config):
    '''
    Save proxy log database into a new file

    :param name: Name of file to save data into (should be .db)
    :param config: Proxy ConfigParser object from proxy.ini
    :return: None
    '''

    log = config.get("DATABASE", "filename")
    oldpath = os.path.join(faradayHelper.userPath, 'lib', log)
    newpath = os.path.join(faradayHelper.userPath, 'lib', name)

    try:
        shutil.move(oldpath, newpath)
        sys.exit(0)

    except shutil.Error as e:
        logger.error(e)
    except IOError as e:
        logger.error(e) 
Example 8
Project: Faraday-Software   Author: FaradayRF   File: telemetry.py    License: GNU General Public License v3.0 6 votes vote down vote up
def saveTelemetryLog(name, config):
    '''
    Save telemetry log database into a new file

    :param name: Name of file to save data into (should be .db)
    :param config: Telemetry ConfigParser object from telmetry.ini
    :return: None
    '''

    log = config.get("DATABASE", "filename")
    oldpath = os.path.join(faradayHelper.userPath, 'lib', log)
    newpath = os.path.join(faradayHelper.userPath, 'lib', name)
    try:
        shutil.move(oldpath, newpath)
        sys.exit(0)

    except shutil.Error as e:
        logger.error(e)
    except IOError as e:
        logger.error(e) 
Example 9
Project: ironpython2   Author: IronLanguages   File: test_shutil.py    License: Apache License 2.0 6 votes vote down vote up
def test_copytree_named_pipe(self):
        os.mkdir(TESTFN)
        try:
            subdir = os.path.join(TESTFN, "subdir")
            os.mkdir(subdir)
            pipe = os.path.join(subdir, "mypipe")
            os.mkfifo(pipe)
            try:
                shutil.copytree(TESTFN, TESTFN2)
            except shutil.Error as e:
                errors = e.args[0]
                self.assertEqual(len(errors), 1)
                src, dst, error_msg = errors[0]
                self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
            else:
                self.fail("shutil.Error should have been raised")
        finally:
            shutil.rmtree(TESTFN, ignore_errors=True)
            shutil.rmtree(TESTFN2, ignore_errors=True) 
Example 10
Project: indy-plenum   Author: hyperledger   File: util.py    License: Apache License 2.0 6 votes vote down vote up
def moveKeyFilesToCorrectLocations(keys_dir, pkdir, skdir):
    for key_file in os.listdir(keys_dir):
        if key_file.endswith(".key"):
            try:
                shutil.move(os.path.join(keys_dir, key_file),
                            os.path.join(pkdir, key_file))
            except shutil.Error as ex:
                # print(ex)
                pass
        if key_file.endswith(".key_secret"):
            try:
                shutil.move(os.path.join(keys_dir, key_file),
                            os.path.join(skdir, key_file))
            except shutil.Error as ex:
                # print(ex)
                pass 
Example 11
Project: indy-plenum   Author: hyperledger   File: util.py    License: Apache License 2.0 6 votes vote down vote up
def moveKeyFilesToCorrectLocations(keys_dir, pkdir, skdir):
    for key_file in os.listdir(keys_dir):
        if key_file.endswith(".key"):
            try:
                shutil.move(os.path.join(keys_dir, key_file),
                            os.path.join(pkdir, key_file))
            except shutil.Error as ex:
                # print(ex)
                pass
        if key_file.endswith(".key_secret"):
            try:
                shutil.move(os.path.join(keys_dir, key_file),
                            os.path.join(skdir, key_file))
            except shutil.Error as ex:
                # print(ex)
                pass 
Example 12
Project: BinderFilter   Author: dxwu   File: test_shutil.py    License: MIT License 6 votes vote down vote up
def test_copytree_named_pipe(self):
            os.mkdir(TESTFN)
            try:
                subdir = os.path.join(TESTFN, "subdir")
                os.mkdir(subdir)
                pipe = os.path.join(subdir, "mypipe")
                os.mkfifo(pipe)
                try:
                    shutil.copytree(TESTFN, TESTFN2)
                except shutil.Error as e:
                    errors = e.args[0]
                    self.assertEqual(len(errors), 1)
                    src, dst, error_msg = errors[0]
                    self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
                else:
                    self.fail("shutil.Error should have been raised")
            finally:
                shutil.rmtree(TESTFN, ignore_errors=True)
                shutil.rmtree(TESTFN2, ignore_errors=True) 
Example 13
Project: oss-ftp   Author: aliyun   File: test_shutil.py    License: MIT License 6 votes vote down vote up
def test_copytree_named_pipe(self):
        os.mkdir(TESTFN)
        try:
            subdir = os.path.join(TESTFN, "subdir")
            os.mkdir(subdir)
            pipe = os.path.join(subdir, "mypipe")
            os.mkfifo(pipe)
            try:
                shutil.copytree(TESTFN, TESTFN2)
            except shutil.Error as e:
                errors = e.args[0]
                self.assertEqual(len(errors), 1)
                src, dst, error_msg = errors[0]
                self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
            else:
                self.fail("shutil.Error should have been raised")
        finally:
            shutil.rmtree(TESTFN, ignore_errors=True)
            shutil.rmtree(TESTFN2, ignore_errors=True) 
Example 14
Project: signac   Author: glotzerlab   File: syncutil.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def copytree(src, dst, copy_function=shutil.copy2, symlinks=False):
    "Implementation adapted from https://docs.python.org/3/library/shutil.html#copytree-example'."
    os.makedirs(dst)
    names = os.listdir(src)
    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, copy_function, symlinks)
            else:
                copy_function(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 shutil.Error as err:
            errors.extend(err.args[0])
    if errors:
        raise shutil.Error(errors) 
Example 15
Project: signac   Author: glotzerlab   File: syncutil.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def create_backup(self, path):
        logger.debug("Create backup of '{}'.".format(os.path.relpath(path)))
        path_backup = path + '~'
        if os.path.isfile(path_backup):
            raise RuntimeError(
                "Failed to create backup, file already exists: '{}'.".format(
                    os.path.relpath(path_backup)))
        try:
            self._copy2(path, path_backup)
            yield path_backup
        except:     # noqa roll-back
            logger.more("Error occured, restoring backup...")
            self._copy2(path_backup, path)
            raise
        finally:
            logger.debug("Remove backup of '{}'.".format(os.path.relpath(path)))
            self._remove(path_backup) 
Example 16
Project: dax   Author: VUIIS   File: XnatUtils.py    License: MIT License 6 votes vote down vote up
def set_assessor_status(self, status):
        """
        Set the status of the assessor based on passed value

        :param status: Value to set the procstatus to
        :except: All catchable errors.
        :return: None
        """
        # Connection to Xnat
        try:
            with get_interface(host=self.host) as intf:
                assessor = self.assr_handler.select_assessor(intf)
                if assessor.exists():
                    dtype = DEFAULT_DATATYPE
                    if self.assr_handler.get_proctype() == 'FS':
                        dtype = DEFAULT_FS_DATATYPE
                    former_status = assessor.attrs.get('%s/procstatus' % dtype)
                    if former_status == JOB_RUNNING:
                        assessor.attrs.set('%s/procstatus' % dtype, status)
                        msg = '  - job status set to %s'
                        self.print_msg(msg % str(status))
        except XnatAuthentificationError as e:
            print(('Failed to connect to XNAT. Error: ', e))
            pass 
Example 17
Project: WordOps   Author: WordOps   File: fileutils.py    License: MIT License 6 votes vote down vote up
def remove(self, filelist):
        """remove files from given path"""
        for file in filelist:
            if os.path.isfile(file):
                Log.info(self, "Removing {0:65}".format(file), end=' ')
                os.remove(file)
                Log.info(self, "{0}".format("[" + Log.ENDC + "Done" +
                                            Log.OKBLUE + "]"))
                Log.debug(self, 'file Removed')
            if os.path.isdir(file):
                try:
                    Log.info(self, "Removing {0:65}".format(file), end=' ')
                    shutil.rmtree(file)
                    Log.info(self, "{0}".format("[" + Log.ENDC + "Done" +
                                                Log.OKBLUE + "]"))
                except shutil.Error as e:
                    Log.debug(self, "{err}".format(err=str(e.reason)))
                    Log.error(self, 'Unable to Remove file ') 
Example 18
Project: WordOps   Author: WordOps   File: fileutils.py    License: MIT License 6 votes vote down vote up
def copyfiles(self, src, dest):
        """
        Copies files:
            src : source path
            dest : destination path

            Recursively copy an entire directory tree rooted at src.
            The destination directory, named by dst, must not already exist;
            it will be created as well as missing parent directories.
        """
        try:
            Log.debug(self, "Copying files, Source:{0}, Dest:{1}"
                      .format(src, dest))
            shutil.copytree(src, dest)
        except shutil.Error as e:
            Log.debug(self, "{0}".format(e))
            Log.error(self, 'Unable to copy files from {0} to {1}'
                      .format(src, dest))
        except IOError as e:
            Log.debug(self, "{0}".format(e.strerror))
            Log.error(self, "Unable to copy files from {0} to {1}"
                      .format(src, dest), exit=False) 
Example 19
Project: WordOps   Author: WordOps   File: fileutils.py    License: MIT License 6 votes vote down vote up
def copyfile(self, src, dest):
        """
        Copy file:
            src : source path
            dest : destination path
        """
        try:
            Log.debug(self, "Copying file, Source:{0}, Dest:{1}"
                      .format(src, dest))
            shutil.copy2(src, dest)
        except shutil.Error as e:
            Log.debug(self, "{0}".format(e))
            Log.error(self, 'Unable to copy file from {0} to {1}'
                      .format(src, dest))
        except IOError as e:
            Log.debug(self, "{0}".format(e.strerror))
            Log.error(self, "Unable to copy file from {0} to {1}"
                      .format(src, dest), exit=False) 
Example 20
Project: WordOps   Author: WordOps   File: fileutils.py    License: MIT License 6 votes vote down vote up
def rm(self, path):
        """
            Remove files
        """
        Log.debug(self, "Removing {0}".format(path))
        if WOFileUtils.isexist(self, path):
            try:
                if os.path.isdir(path):
                    shutil.rmtree(path)
                else:
                    os.remove(path)
            except shutil.Error as e:
                Log.debug(self, "{0}".format(e))
                Log.error(self, "Unable to remove directory : {0} "
                          .format(path))
            except OSError as e:
                Log.debug(self, "{0}".format(e))
                Log.error(self, "Unable to remove file  : {0} "
                          .format(path)) 
Example 21
Project: Fluid-Designer   Author: Microvellum   File: test_shutil.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_copytree_named_pipe(self):
        os.mkdir(TESTFN)
        try:
            subdir = os.path.join(TESTFN, "subdir")
            os.mkdir(subdir)
            pipe = os.path.join(subdir, "mypipe")
            os.mkfifo(pipe)
            try:
                shutil.copytree(TESTFN, TESTFN2)
            except shutil.Error as e:
                errors = e.args[0]
                self.assertEqual(len(errors), 1)
                src, dst, error_msg = errors[0]
                self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
            else:
                self.fail("shutil.Error should have been raised")
        finally:
            shutil.rmtree(TESTFN, ignore_errors=True)
            shutil.rmtree(TESTFN2, ignore_errors=True) 
Example 22
Project: Fluid-Designer   Author: Microvellum   File: test_shutil.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_copytree_dangling_symlinks(self):

        # a dangling symlink raises an error at the end
        src_dir = self.mkdtemp()
        dst_dir = os.path.join(self.mkdtemp(), 'destination')
        os.symlink('IDONTEXIST', os.path.join(src_dir, 'test.txt'))
        os.mkdir(os.path.join(src_dir, 'test_dir'))
        write_file((src_dir, 'test_dir', 'test.txt'), '456')
        self.assertRaises(Error, shutil.copytree, src_dir, dst_dir)

        # a dangling symlink is ignored with the proper flag
        dst_dir = os.path.join(self.mkdtemp(), 'destination2')
        shutil.copytree(src_dir, dst_dir, ignore_dangling_symlinks=True)
        self.assertNotIn('test.txt', os.listdir(dst_dir))

        # a dangling symlink is copied if symlinks=True
        dst_dir = os.path.join(self.mkdtemp(), 'destination3')
        shutil.copytree(src_dir, dst_dir, symlinks=True)
        self.assertIn('test.txt', os.listdir(dst_dir)) 
Example 23
Project: smbprotocol   Author: jborean93   File: test_smbclient_shutil.py    License: MIT License 5 votes vote down vote up
def test_copytree_with_errors_raises(smb_share):
    src_dirname = "%s\\source" % smb_share
    dst_dirname = "%s\\target" % smb_share

    makedirs("%s\\dir1" % src_dirname)

    with open_file("%s\\file1.txt" % src_dirname, mode='w') as fd:
        fd.write(u"file1.txt")

    with open_file("%s\\dir1\\file2.txt" % src_dirname, mode='w') as fd:
        fd.write(u"file2.txt")

    actual = copytree(src_dirname, dst_dirname)
    assert actual == dst_dirname

    # Doing this will force a failure as we cannot run copyfile against a target with this flag (access is denied).
    _set_file_attributes("%s\\file1.txt" % dst_dirname, FileAttributes.FILE_ATTRIBUTE_READONLY)
    _set_file_attributes("%s\\dir1\\file2.txt" % dst_dirname, FileAttributes.FILE_ATTRIBUTE_READONLY)

    with pytest.raises(shutil.Error) as actual:
        copytree(src_dirname, dst_dirname, dirs_exist_ok=True)

    assert len(actual.value.args[0]) == 2
    err1 = actual.value.args[0][0]
    assert err1[0] == "%s\\dir1\\file2.txt" % src_dirname
    assert err1[1] == "%s\\dir1\\file2.txt" % dst_dirname
    assert "STATUS_ACCESS_DENIED" in err1[2]

    err2 = actual.value.args[0][1]
    assert err2[0] == "%s\\file1.txt" % src_dirname
    assert err2[1] == "%s\\file1.txt" % dst_dirname
    assert "STATUS_ACCESS_DENIED" in err2[2] 
Example 24
Project: harmony-ops   Author: harmony-one   File: testHmy.py    License: MIT License 5 votes vote down vote up
def delete_from_keystore_by_name(name):
    log(f"[KEY DELETE] Removing {name} from keystore at {KEYSTORE_PATH}", error=False)
    key_file_path = f"{KEYSTORE_PATH}/{name}"

    try:
        shutil.rmtree(key_file_path)
    except shutil.Error as e:
        log(f"[KEY DELETE] Failed to delete dir: {key_file_path}\n"
            f"Exception: {e}")
        return

    del ADDRESSES[name] 
Example 25
Project: glazier   Author: google   File: log_copy.py    License: Apache License 2.0 5 votes vote down vote up
def _GetLogFileName(self):
    """Creates the destination file name for a text log file.

    Returns:
      The full text file log name (string).
    """
    try:
      hostname = registry.get_value('name')
    except registry.Error as e:
      raise LogCopyError('Hostname could not be determined for log copy: %s' %
                         str(e))
    destination_file_date = gtime.now().replace(microsecond=0)
    destination_file_date = destination_file_date.isoformat()
    destination_file_date = destination_file_date.replace(':', '')
    return 'l:\\' + hostname + '-' + destination_file_date + '.log' 
Example 26
Project: glazier   Author: google   File: log_copy_test.py    License: Apache License 2.0 5 votes vote down vote up
def testGetLogFileNameError(self, gv):
    gv.side_effect = log_copy.registry.Error
    self.assertRaises(log_copy.LogCopyError, self.lc._GetLogFileName) 
Example 27
Project: glazier   Author: google   File: log_copy_test.py    License: Apache License 2.0 5 votes vote down vote up
def testShareUpload(self, map_drive, unmap_drive, copy, get_file_name, wpe):

    class TestCredProvider(log_copy.LogCopyCredentials):

      def GetUsername(self):
        wpe.return_value = False
        return 'test_user'

      def GetPassword(self):
        wpe.return_value = False
        return 'test_pass'

    log_copy.LogCopyCredentials = TestCredProvider

    log_host = 'log-host.example.com'
    get_file_name.return_value = 'log.txt'
    self.lc.ShareCopy(self.log_file, log_host)
    map_drive.assert_called_with(mock.ANY, 'l:', 'log-host.example.com',
                                 'test_user', 'test_pass')
    copy.assert_called_with(self.log_file, 'log.txt')
    unmap_drive.assert_called_with(mock.ANY, 'l:')
    # map error
    map_drive.return_value = None
    self.assertRaises(log_copy.LogCopyError, self.lc.ShareCopy, self.log_file,
                      log_host)
    # copy error
    map_drive.return_value = True
    copy.side_effect = shutil.Error()
    self.assertRaises(log_copy.LogCopyError, self.lc.ShareCopy, self.log_file,
                      log_host) 
Example 28
Project: glazier   Author: google   File: file_system.py    License: Apache License 2.0 5 votes vote down vote up
def _CreateDirectories(self, path):
    """Create a directory.

    Args:
      path: The full path to the directory to be created.

    Raises:
      ActionError: Failure creating the requested directory.
    """
    if not os.path.isdir(path):
      try:
        logging.debug('Creating directory: %s', path)
        os.makedirs(path)
      except (shutil.Error, OSError) as e:
        raise ActionError('Unable to create directory %s: %s' % (path, str(e))) 
Example 29
Project: glazier   Author: google   File: file_system.py    License: Apache License 2.0 5 votes vote down vote up
def Run(self):
    try:
      src = self._args[0]
      dst = self._args[1]
    except IndexError:
      raise ActionError('Unable to determine source and destination from %s.' %
                        str(self._args))
    try:
      path = os.path.dirname(dst)
      self._CreateDirectories(path)
      shutil.copy2(src, dst)
      logging.info('Copying: %s to %s', src, dst)
    except (shutil.Error, IOError) as e:
      raise ActionError('Unable to copy %s to %s: %s' % (src, dst, str(e))) 
Example 30
Project: glazier   Author: google   File: file_util.py    License: Apache License 2.0 5 votes vote down vote up
def Remove(path: Text):
  """Remove a file.

  Args:
    path: The full file path to the file to be removed.

  Raises:
    Error: Failure removing the file.
  """
  try:
    if os.path.exists(path):
      os.remove(path)
  except OSError as e:
    raise Error('Failure removing file %s. (%s)' % (path, str(e)))