def test_samefile(self):
            f = open(test_support.TESTFN + "1", "wb")
            try:
                f.write("foo")
                f.close()
                self.assertIs(posixpath.samefile(test_support.TESTFN + "1", test_support.TESTFN + "1"), True)
                # If we don't have links, assume that os.stat doesn't return resonable
                # inode information and thus, that samefile() doesn't work
                if hasattr(os, "symlink"):
                    os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
                    self.assertIs(posixpath.samefile(test_support.TESTFN + "1", test_support.TESTFN + "2"), True)
                    os.remove(test_support.TESTFN + "2")
                    f = open(test_support.TESTFN + "2", "wb")
                    f.write("bar")
                    f.close()
                    self.assertIs(posixpath.samefile(test_support.TESTFN + "1", test_support.TESTFN + "2"), False)
            finally:
                if not f.close():
                    f.close()
                try:
                    os.remove(test_support.TESTFN + "1")
                except os.error:
                    pass
                try:
                    os.remove(test_support.TESTFN + "2")
                except os.error:
                    pass

            self.assertRaises(TypeError, posixpath.samefile)
    def test_samefile(self):
        f = open(test_support.TESTFN + "1", "wb")
        try:
            f.write("foo")
            f.close()
            self.assertIs(
                posixpath.samefile(test_support.TESTFN + "1",
                                   test_support.TESTFN + "1"), True)

            # If we don't have links, assume that os.stat doesn't return
            # reasonable inode information and thus, that samefile() doesn't
            # work.
            if hasattr(os, "symlink"):
                os.symlink(test_support.TESTFN + "1",
                           test_support.TESTFN + "2")
                self.assertIs(
                    posixpath.samefile(test_support.TESTFN + "1",
                                       test_support.TESTFN + "2"), True)
                os.remove(test_support.TESTFN + "2")
                f = open(test_support.TESTFN + "2", "wb")
                f.write("bar")
                f.close()
                self.assertIs(
                    posixpath.samefile(test_support.TESTFN + "1",
                                       test_support.TESTFN + "2"), False)
        finally:
            if not f.close():
                f.close()
Example #3
0
    def test_samefile_on_links(self) -> None:
        test_fn1 = support.TESTFN + "1"
        test_fn2 = support.TESTFN + "2"
        self._create_file(test_fn1)

        os.symlink(test_fn1, test_fn2)
        self.assertTrue(posixpath.samefile(test_fn1, test_fn2))
        os.remove(test_fn2)

        self._create_file(test_fn2)
        self.assertFalse(posixpath.samefile(test_fn1, test_fn2))
Example #4
0
    def test_samefile_on_links(self):
        test_fn1 = support.TESTFN + "1"
        test_fn2 = support.TESTFN + "2"
        self._create_file(test_fn1)

        os.symlink(test_fn1, test_fn2)
        self.assertTrue(posixpath.samefile(test_fn1, test_fn2))
        os.remove(test_fn2)

        self._create_file(test_fn2)
        self.assertFalse(posixpath.samefile(test_fn1, test_fn2))
Example #5
0
    def assertNotSameFile(self, file1, file2):
        """Assert files are not the same.

        Assertion fails if first file does not exist.
        """
        self.assertTrue(posixpath.exists(file1))
        if posixpath.exists(file2):
            self.assertFalse(posixpath.samefile(file1, file2))
Example #6
0
def unload_dtags(rootpath, dirpath):
    """Remove symlinks using a directory's dtags."""
    tags = dtags.list_tags(dirpath)
    dirpath = pathlib.readlink(dirpath)
    for tagname in tags:
        tagpath = tagnames.tag2path(rootpath, tagname)
        if posixpath.samefile(dirpath, tagpath):
            os.unlink(tagpath)
Example #7
0
def untag(rootpath, path, directory):
    """Untag file from a directory.

    Args:
        rootpath: Rootpath to resolve tagnames.
        path: Path of file or directory to tag.
        directory: Directory path.
    """
    target = path
    to_unlink = []
    for filepath in pathlib.listdirpaths(directory):
        if posixpath.samefile(target, filepath):
            to_unlink.append(filepath)
    for filepath in to_unlink:
        base.unlink(rootpath, filepath)
Example #8
0
def untag(rootpath, path, directory):
    """Untag file from a directory.

    Args:
        rootpath: Rootpath to resolve tagnames.
        path: Path of file or directory to tag.
        directory: Directory path.
    """
    target = path
    to_unlink = []
    for filepath in pathlib.listdirpaths(directory):
        if posixpath.samefile(target, filepath):
            to_unlink.append(filepath)
    for filepath in to_unlink:
        base.unlink(rootpath, filepath)
Example #9
0
def list_links(top, path):
    """List all links to the target file.

    Args:
        top: Path to top of directory tree to search.
        path: Path of file.

    Returns:
        Generator yielding paths.
    """
    target = path
    for (dirpath, dirnames, filenames) in os.walk(top):
        for name in chain(dirnames, filenames):
            filepath = posixpath.join(dirpath, name)
            if posixpath.samefile(target, filepath):
                yield filepath
Example #10
0
 def test_samefile(self) -> None:
     test_fn = support.TESTFN + "1"
     self._create_file(test_fn)
     self.assertTrue(posixpath.samefile(test_fn, test_fn))
     self.assertRaises(TypeError, posixpath.samefile)
Example #11
0
def copyfile(
    originalfile,
    newfile,
    copy=False,
    create_new=False,
    use_hardlink=True,
    copy_related_files=True,
):
    """
    Copy or link files.

    If ``use_hardlink`` is True, and the file can be hard-linked, then a
    link is created, instead of copying the file.

    If a hard link is not created and ``copy`` is False, then a symbolic
    link is created.

    .. admonition:: Copy options for existing files

        * symlink

            * to regular file originalfile            (keep if symlinking)
            * to same dest as symlink originalfile    (keep if symlinking)
            * to other file                           (unlink)

        * regular file

            * hard link to originalfile               (keep)
            * copy of file (same hash)                (keep)
            * different file (diff hash)              (unlink)

    .. admonition:: Copy options for new files

        * ``use_hardlink`` & ``can_hardlink`` => hardlink
        * ``~hardlink`` & ``~copy`` & ``can_symlink`` => symlink
        * ``~hardlink`` & ``~symlink`` => copy

    Parameters
    ----------
    originalfile : :obj:`str`
        full path to original file
    newfile : :obj:`str`
        full path to new file
    copy : Bool
        specifies whether to copy or symlink files
        (default=False) but only for POSIX systems
    use_hardlink : Bool
        specifies whether to hard-link files, when able
        (Default=False), taking precedence over copy
    copy_related_files : Bool
        specifies whether to also operate on related files, as defined in
        ``related_filetype_sets``

    Returns
    -------
    None

    """
    newhash = None
    orighash = None
    logger.debug(newfile)

    if create_new:
        while op.exists(newfile):
            base, fname, ext = split_filename(newfile)
            s = re.search("_c[0-9]{4,4}$", fname)
            i = 0
            if s:
                i = int(s.group()[2:]) + 1
                fname = fname[:-6] + "_c%04d" % i
            else:
                fname += "_c%04d" % i
            newfile = base + os.sep + fname + ext

    # Don't try creating symlinks on CIFS
    if copy is False and on_cifs(newfile):
        copy = True

    keep = False
    if op.lexists(newfile):
        if op.islink(newfile):
            if all(
                (
                    os.readlink(newfile) == op.realpath(originalfile),
                    not use_hardlink,
                    not copy,
                )
            ):
                keep = True
        elif posixpath.samefile(newfile, originalfile):
            keep = True
        else:
            newhash = hash_file(newfile)
            logger.debug("File: %s already exists,%s, copy:%d", newfile, newhash, copy)
            orighash = hash_file(originalfile)
            keep = newhash == orighash
        if keep:
            logger.debug(
                "File: %s already exists, not overwriting, copy:%d", newfile, copy
            )
        else:
            os.unlink(newfile)

    if not keep and use_hardlink:
        try:
            logger.debug("Linking File: %s->%s", newfile, originalfile)
            # Use realpath to avoid hardlinking symlinks
            os.link(op.realpath(originalfile), newfile)
        except OSError:
            use_hardlink = False  # Disable hardlink for associated files
        else:
            keep = True

    if not keep and not copy and os.name == "posix":
        try:
            logger.debug("Symlinking File: %s->%s", newfile, originalfile)
            os.symlink(originalfile, newfile)
        except OSError:
            copy = True  # Disable symlink for associated files
        else:
            keep = True

    if not keep:
        try:
            logger.debug("Copying File: %s->%s", newfile, originalfile)
            shutil.copyfile(originalfile, newfile)
        except shutil.Error as e:
            logger.warning(e.message)

    # Associated files
    if copy_related_files:
        related_file_pairs = (
            get_related_files(f, include_this_file=False)
            for f in (originalfile, newfile)
        )
        for alt_ofile, alt_nfile in zip(*related_file_pairs):
            if op.exists(alt_ofile):
                copyfile(
                    alt_ofile,
                    alt_nfile,
                    copy,
                    use_hardlink=use_hardlink,
                    copy_related_files=False,
                )

    return newfile
Example #12
0
 def test_samefile(self):
     test_fn = support.TESTFN + "1"
     self._create_file(test_fn)
     self.assertTrue(posixpath.samefile(test_fn, test_fn))
     self.assertRaises(TypeError, posixpath.samefile)
Example #13
0
 def assertSameFile(self, file1, file2):
     """Assert both files are the same by inode."""
     self.assertTrue(posixpath.samefile(file1, file2))
Example #14
0
def copyfile(originalfile, newfile, copy=False, create_new=False,
             hashmethod=None, use_hardlink=False,
             copy_related_files=True):
    """Copy or link ``originalfile`` to ``newfile``.

    If ``use_hardlink`` is True, and the file can be hard-linked, then a
    link is created, instead of copying the file.

    If a hard link is not created and ``copy`` is False, then a symbolic
    link is created.

    Parameters
    ----------
    originalfile : str
        full path to original file
    newfile : str
        full path to new file
    copy : Bool
        specifies whether to copy or symlink files
        (default=False) but only for POSIX systems
    use_hardlink : Bool
        specifies whether to hard-link files, when able
        (Default=False), taking precedence over copy
    copy_related_files : Bool
        specifies whether to also operate on related files, as defined in
        ``related_filetype_sets``

    Returns
    -------
    None

    """
    newhash = None
    orighash = None
    fmlogger.debug(newfile)

    if create_new:
        while os.path.exists(newfile):
            base, fname, ext = split_filename(newfile)
            s = re.search('_c[0-9]{4,4}$', fname)
            i = 0
            if s:
                i = int(s.group()[2:]) + 1
                fname = fname[:-6] + "_c%04d" % i
            else:
                fname += "_c%04d" % i
            newfile = base + os.sep + fname + ext

    if hashmethod is None:
        hashmethod = config.get('execution', 'hash_method').lower()

    # Don't try creating symlinks on CIFS
    if copy is False and on_cifs(newfile):
        copy = True

    # Existing file
    # -------------
    # Options:
    #   symlink
    #       to regular file originalfile            (keep if symlinking)
    #       to same dest as symlink originalfile    (keep if symlinking)
    #       to other file                           (unlink)
    #   regular file
    #       hard link to originalfile               (keep)
    #       copy of file (same hash)                (keep)
    #       different file (diff hash)              (unlink)
    keep = False
    if os.path.lexists(newfile):
        if os.path.islink(newfile):
            if all((os.readlink(newfile) == os.path.realpath(originalfile),
                    not use_hardlink, not copy)):
                keep = True
        elif posixpath.samefile(newfile, originalfile):
            keep = True
        else:
            if hashmethod == 'timestamp':
                hashfn = hash_timestamp
            elif hashmethod == 'content':
                hashfn = hash_infile
            newhash = hashfn(newfile)
            fmlogger.debug("File: %s already exists,%s, copy:%d" %
                           (newfile, newhash, copy))
            orighash = hashfn(originalfile)
            keep = newhash == orighash
        if keep:
            fmlogger.debug("File: %s already exists, not overwriting, copy:%d"
                           % (newfile, copy))
        else:
            os.unlink(newfile)

    # New file
    # --------
    # use_hardlink & can_hardlink => hardlink
    # ~hardlink & ~copy & can_symlink => symlink
    # ~hardlink & ~symlink => copy
    if not keep and use_hardlink:
        try:
            fmlogger.debug("Linking File: %s->%s" % (newfile, originalfile))
            # Use realpath to avoid hardlinking symlinks
            os.link(os.path.realpath(originalfile), newfile)
        except OSError:
            use_hardlink = False  # Disable hardlink for associated files
        else:
            keep = True

    if not keep and not copy and os.name == 'posix':
        try:
            fmlogger.debug("Symlinking File: %s->%s" % (newfile, originalfile))
            os.symlink(originalfile, newfile)
        except OSError:
            copy = True  # Disable symlink for associated files
        else:
            keep = True

    if not keep:
        try:
            fmlogger.debug("Copying File: %s->%s" % (newfile, originalfile))
            shutil.copyfile(originalfile, newfile)
        except shutil.Error as e:
            fmlogger.warn(e.message)

    # Associated files
    if copy_related_files:
        related_file_pairs = (get_related_files(f, include_this_file=False)
                              for f in (originalfile, newfile))
        for alt_ofile, alt_nfile in zip(*related_file_pairs):
            if os.path.exists(alt_ofile):
                copyfile(alt_ofile, alt_nfile, copy, hashmethod=hashmethod,
                         use_hardlink=use_hardlink, copy_related_files=False)

    return newfile
Example #15
0
def copyfile(originalfile,
             newfile,
             copy=False,
             create_new=False,
             hashmethod=None,
             use_hardlink=False):
    """Copy or link ``originalfile`` to ``newfile``.

    If ``use_hardlink`` is True, and the file can be hard-linked, then a
    link is created, instead of copying the file.

    If a hard link is not created and ``copy`` is False, then a symbolic
    link is created.

    Parameters
    ----------
    originalfile : str
        full path to original file
    newfile : str
        full path to new file
    copy : Bool
        specifies whether to copy or symlink files
        (default=False) but only for POSIX systems
    use_hardlink : Bool
        specifies whether to hard-link files, when able
        (Default=False), taking precedence over copy

    Returns
    -------
    None

    """
    newhash = None
    orighash = None
    fmlogger.debug(newfile)

    if create_new:
        while os.path.exists(newfile):
            base, fname, ext = split_filename(newfile)
            s = re.search('_c[0-9]{4,4}$', fname)
            i = 0
            if s:
                i = int(s.group()[2:]) + 1
                fname = fname[:-6] + "_c%04d" % i
            else:
                fname += "_c%04d" % i
            newfile = base + os.sep + fname + ext

    if hashmethod is None:
        hashmethod = config.get('execution', 'hash_method').lower()

    # Existing file
    # -------------
    # Options:
    #   symlink
    #       to originalfile             (keep if not (use_hardlink or copy))
    #       to other file               (unlink)
    #   regular file
    #       hard link to originalfile   (keep)
    #       copy of file (same hash)    (keep)
    #       different file (diff hash)  (unlink)
    keep = False
    if os.path.lexists(newfile):
        if os.path.islink(newfile):
            if all(
                    os.path.readlink(newfile) == originalfile,
                    not use_hardlink, not copy):
                keep = True
        elif posixpath.samefile(newfile, originalfile):
            keep = True
        else:
            if hashmethod == 'timestamp':
                hashfn = hash_timestamp
            elif hashmethod == 'content':
                hashfn = hash_infile
            newhash = hashfn(newfile)
            fmlogger.debug("File: %s already exists,%s, copy:%d" %
                           (newfile, newhash, copy))
            orighash = hashfn(originalfile)
            keep = newhash == orighash
        if keep:
            fmlogger.debug(
                "File: %s already exists, not overwriting, copy:%d" %
                (newfile, copy))
        else:
            os.unlink(newfile)

    # New file
    # --------
    # use_hardlink & can_hardlink => hardlink
    # ~hardlink & ~copy & can_symlink => symlink
    # ~hardlink & ~symlink => copy
    if not keep and use_hardlink:
        try:
            fmlogger.debug("Linking File: %s->%s" % (newfile, originalfile))
            # Use realpath to avoid hardlinking symlinks
            os.link(os.path.realpath(originalfile), newfile)
        except OSError:
            use_hardlink = False  # Disable hardlink for associated files
        else:
            keep = True

    if not keep and not copy and os.name == 'posix':
        try:
            fmlogger.debug("Symlinking File: %s->%s" % (newfile, originalfile))
            os.symlink(originalfile, newfile)
        except OSError:
            copy = True  # Disable symlink for associated files
        else:
            keep = True

    if not keep:
        try:
            fmlogger.debug("Copying File: %s->%s" % (newfile, originalfile))
            shutil.copyfile(originalfile, newfile)
        except shutil.Error as e:
            fmlogger.warn(e.message)

    # Associated files
    if originalfile.endswith(".img"):
        hdrofile = originalfile[:-4] + ".hdr"
        hdrnfile = newfile[:-4] + ".hdr"
        matofile = originalfile[:-4] + ".mat"
        if os.path.exists(matofile):
            matnfile = newfile[:-4] + ".mat"
            copyfile(matofile,
                     matnfile,
                     copy,
                     hashmethod=hashmethod,
                     use_hardlink=use_hardlink)
        copyfile(hdrofile,
                 hdrnfile,
                 copy,
                 hashmethod=hashmethod,
                 use_hardlink=use_hardlink)
    elif originalfile.endswith(".BRIK"):
        hdrofile = originalfile[:-5] + ".HEAD"
        hdrnfile = newfile[:-5] + ".HEAD"
        copyfile(hdrofile,
                 hdrnfile,
                 copy,
                 hashmethod=hashmethod,
                 use_hardlink=use_hardlink)

    return newfile
Example #16
0
def copyfile(originalfile,
             newfile,
             copy=False,
             create_new=False,
             hashmethod=None,
             use_hardlink=False,
             copy_related_files=True):
    """Copy or link ``originalfile`` to ``newfile``.

    If ``use_hardlink`` is True, and the file can be hard-linked, then a
    link is created, instead of copying the file.

    If a hard link is not created and ``copy`` is False, then a symbolic
    link is created.

    Parameters
    ----------
    originalfile : str
        full path to original file
    newfile : str
        full path to new file
    copy : Bool
        specifies whether to copy or symlink files
        (default=False) but only for POSIX systems
    use_hardlink : Bool
        specifies whether to hard-link files, when able
        (Default=False), taking precedence over copy
    copy_related_files : Bool
        specifies whether to also operate on related files, as defined in
        ``related_filetype_sets``

    Returns
    -------
    None

    """
    newhash = None
    orighash = None
    fmlogger.debug(newfile)

    if create_new:
        while os.path.exists(newfile):
            base, fname, ext = split_filename(newfile)
            s = re.search('_c[0-9]{4,4}$', fname)
            i = 0
            if s:
                i = int(s.group()[2:]) + 1
                fname = fname[:-6] + "_c%04d" % i
            else:
                fname += "_c%04d" % i
            newfile = base + os.sep + fname + ext

    if hashmethod is None:
        hashmethod = config.get('execution', 'hash_method').lower()

    # Don't try creating symlinks on CIFS
    if copy is False and on_cifs(newfile):
        copy = True

    # Existing file
    # -------------
    # Options:
    #   symlink
    #       to regular file originalfile            (keep if symlinking)
    #       to same dest as symlink originalfile    (keep if symlinking)
    #       to other file                           (unlink)
    #   regular file
    #       hard link to originalfile               (keep)
    #       copy of file (same hash)                (keep)
    #       different file (diff hash)              (unlink)
    keep = False
    if os.path.lexists(newfile):
        if os.path.islink(newfile):
            if all((os.readlink(newfile) == os.path.realpath(originalfile),
                    not use_hardlink, not copy)):
                keep = True
        elif posixpath.samefile(newfile, originalfile):
            keep = True
        else:
            if hashmethod == 'timestamp':
                hashfn = hash_timestamp
            elif hashmethod == 'content':
                hashfn = hash_infile
            newhash = hashfn(newfile)
            fmlogger.debug('File: %s already exists,%s, copy:%d', newfile,
                           newhash, copy)
            orighash = hashfn(originalfile)
            keep = newhash == orighash
        if keep:
            fmlogger.debug('File: %s already exists, not overwriting, copy:%d',
                           newfile, copy)
        else:
            os.unlink(newfile)

    # New file
    # --------
    # use_hardlink & can_hardlink => hardlink
    # ~hardlink & ~copy & can_symlink => symlink
    # ~hardlink & ~symlink => copy
    if not keep and use_hardlink:
        try:
            fmlogger.debug('Linking File: %s->%s', newfile, originalfile)
            # Use realpath to avoid hardlinking symlinks
            os.link(os.path.realpath(originalfile), newfile)
        except OSError:
            use_hardlink = False  # Disable hardlink for associated files
        else:
            keep = True

    if not keep and not copy and os.name == 'posix':
        try:
            fmlogger.debug('Symlinking File: %s->%s', newfile, originalfile)
            os.symlink(originalfile, newfile)
        except OSError:
            copy = True  # Disable symlink for associated files
        else:
            keep = True

    if not keep:
        try:
            fmlogger.debug('Copying File: %s->%s', newfile, originalfile)
            shutil.copyfile(originalfile, newfile)
        except shutil.Error as e:
            fmlogger.warn(e.message)

    # Associated files
    if copy_related_files:
        related_file_pairs = (get_related_files(f, include_this_file=False)
                              for f in (originalfile, newfile))
        for alt_ofile, alt_nfile in zip(*related_file_pairs):
            if os.path.exists(alt_ofile):
                copyfile(alt_ofile,
                         alt_nfile,
                         copy,
                         hashmethod=hashmethod,
                         use_hardlink=use_hardlink,
                         copy_related_files=False)

    return newfile
Example #17
0
 def update_event(self, inp=-1):
     self.set_output_val(0, posixpath.samefile(self.input(0),
                                               self.input(1)))