def _get_directory_stash(self, path): # type: (str) -> str """Stashes a directory. Directories are stashed adjacent to their original location if possible, or else moved/copied into the user's temp dir.""" try: save_dir = AdjacentTempDirectory(path) # type: TempDirectory
def test_adjacent_directory_exists(self, name, tmpdir): block_name, expect_name = itertools.islice( AdjacentTempDirectory._generate_names(name), 2) original = os.path.join(tmpdir, name) blocker = os.path.join(tmpdir, block_name) ensure_dir(original) ensure_dir(blocker) with AdjacentTempDirectory(original) as atmp_dir: assert expect_name == os.path.split(atmp_dir.path)[1]
def _stash(self, path): best = None for save_dir in self._save_dirs: if not path.startswith(save_dir.original + os.sep): continue if not best or len(save_dir.original) > len(best.original): best = save_dir if best is None: best = AdjacentTempDirectory(os.path.dirname(path)) best.create() self._save_dirs.append(best) return os.path.join(best.path, os.path.relpath(path, best.original))
def _get_directory_stash(self, path: str) -> str: """Stashes a directory. Directories are stashed adjacent to their original location if possible, or else moved/copied into the user's temp dir.""" try: save_dir: TempDirectory = AdjacentTempDirectory(path) except OSError: save_dir = TempDirectory(kind="uninstall") self._save_dirs[os.path.normcase(path)] = save_dir return save_dir.path
def test_adjacent_directory_permission_error(self, monkeypatch): name = "ABC" def raising_mkdir(*args, **kwargs): raise OSError("Unknown OSError") with TempDirectory() as tmp_dir: original = os.path.join(tmp_dir.path, name) ensure_dir(original) monkeypatch.setattr("os.mkdir", raising_mkdir) with pytest.raises(OSError): with AdjacentTempDirectory(original): pass