def mkstemp_rename(destination,  **kwargs):
    """For writing to a temporary file and then move it ontop of a
    (possibly) existing file only when finished.  This enables us to
    perform long running operations on a file that other people might
    be using and let everyone else see a consistent version.

    * other args are passed to tempfile.mkstemp

    Example::

        with mkstemp_rename('foobar.txt') as f:
            f.write('stuff\n')

    """
    log = kwargs.pop('log', None)
    kwargs.setdefault('dir', os.path.dirname(destination))

    (fd, path) = tempfile.mkstemp(**kwargs)
    path = Path(path)
    try:
        filelike = os.fdopen(fd, 'wb')
        yield filelike
        filelike.close()
        path.chmod(0o0644)
        path.rename(destination)
    finally:
        path.remove_p()
Esempio n. 2
0
    def doRenameDir(self, newname):
        if newname:
            src = self.SOURCELIST.getFilename()
            dst = self.SOURCELIST.getCurrentDirectory() + newname
            
            try:
                os.rename(src, dst)
            except:
                pass

            self.doRefresh()
Esempio n. 3
0
    def move_single_file(self, path, dry=True):
        # type: (Path, bool) -> bool
        """ Finds torrent which matches `path` and move file to the correct location.
		"""

        info_hash = self.path_to_info_hash(path)
        torrentpath = self.get_fastresume_path(info_hash)

        if path == torrentpath or dry:
            return False
        else:
            path.rename(torrentpath)
            return True
Esempio n. 4
0
def normalize_event_filename(filename: Union[str, Path],
                             action: bool = True) -> Path:
    """
    Normalize a file name.

    Note: we cannot decorate the function with lru_cache() because it as
    several side effects. We need to find a way to decouple the normalization
    and actions to do on the OS.

    :param unicode filename: The file name to normalize.
    :param bool action: Apply changes on the file system.
    :return Path: The normalized file name.
    """

    import unicodedata

    path = Path(filename)

    # NXDRIVE-688: Ensure the name is stripped for a file
    stripped = Path(str(path).strip())
    if all([
            not WINDOWS,  # Windows does not allow files/folders ending with space(s)
            action,
            path != stripped,
            path.exists(),
            not path.is_dir(),
    ]):
        # We can have folders ending with spaces
        log.info(f"Forcing space normalization: {path!r} -> {stripped!r}")
        path.rename(stripped)
        path = stripped

    # NXDRIVE-188: Normalize name on the file system, if needed
    normalized = Path(unicodedata.normalize("NFC", str(path)))
    normalized = normalized.with_name(safe_filename(normalized.name))

    if WINDOWS and path.exists():
        path = normalized_path(path).with_name(path.name)

    if not MAC and action and path != normalized and path.exists():
        log.info(f"Forcing normalization: {path!r} -> {normalized!r}")
        safe_rename(path, normalized)

    return normalized
Esempio n. 5
0
def normalize_event_filename(filename: Union[str, Path], action: bool = True) -> Path:
    """
    Normalize a file name.

    :param unicode filename: The file name to normalize.
    :param bool action: Apply changes on the file system.
    :return Path: The normalized file name.
    """

    import unicodedata

    path = Path(filename)

    # NXDRIVE-688: Ensure the name is stripped for a file
    stripped = Path(str(path).strip())
    if all(
        [
            not WINDOWS,  # Windows does not allow files/folders ending with space(s)
            action,
            path != stripped,
            path.exists(),
            not path.is_dir(),
        ]
    ):
        # We can have folders ending with spaces
        log.info(f"Forcing space normalization: {path!r} -> {stripped!r}")
        path.rename(stripped)
        path = stripped

    # NXDRIVE-188: Normalize name on the file system, if needed
    normalized = Path(unicodedata.normalize("NFC", str(path)))
    normalized = normalized.with_name(safe_os_filename(normalized.name))

    if WINDOWS and path.exists():
        path = normalized_path(path).with_name(path.name)

    if not MAC and action and path != normalized and path.exists():
        log.info(f"Forcing normalization: {path!r} -> {normalized!r}")
        path.rename(normalized)

    return normalized
Esempio n. 6
0
 def replace_files(self):
     for path in self.output_files:
         path.rename(path.parent / path.stem)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'ipetrash'

import os.path
os.makedirs('./dir1/dir2/dir3/dir4', exist_ok=True)

from pathlib import Path

n = 'dir3'
m = 'new_dir3'

# Рекурсивный поиск
items = list(Path('.').rglob(n))

# Если нашли папку
if items:
    path = items[0]
    path.rename(path.parent / m)
Esempio n. 8
0
    import unicodedata

    path = Path(filename)

    # NXDRIVE-688: Ensure the name is stripped for a file
    stripped = Path(str(path).strip())
    if all([
            not WINDOWS,  # Windows does not allow files/folders ending with space(s)
            action,
            path != stripped,
            path.exists(),
            not path.is_dir(),
    ]):
        # We can have folders ending with spaces
        log.info(f"Forcing space normalization: {path!r} -> {stripped!r}")
        path.rename(stripped)
        path = stripped

    # NXDRIVE-188: Normalize name on the file system, if needed
    normalized = Path(unicodedata.normalize("NFC", str(path)))
    normalized = normalized.with_name(safe_filename(normalized.name))

    if WINDOWS and path.exists():
        path = normalized_path(path).with_name(path.name)

    if not MAC and action and path != normalized and path.exists():
        log.info(f"Forcing normalization: {path!r} -> {normalized!r}")
        safe_rename(path, normalized)

    return normalized