def set_base(self, base): """ :param str|system.FolderBase|None base: Folder to use as base for installations """ if not base: base = os.environ.get("PICKLEY_ROOT") if not base: base = runez.parent_folder(system.PICKLEY_PROGRAM_PATH) if DOT_PICKLEY in base: # Don't consider meta folder .pickley/... as installation base i = base.index(DOT_PICKLEY) base = base[:i].rstrip("/") elif ".venv" in base: # Convenience for development base = runez.parent_folder(base) base = os.path.join(base, "root") if isinstance(base, system.FolderBase): self.base = base else: self.base = system.FolderBase(base, name="base") if self.meta: runez.Anchored.pop(self.meta.path) self.meta = meta_folder(self.base.path) runez.Anchored.add(self.meta.path)
def _install(self, target, source): if os.path.isabs(source) and os.path.isabs(target): parent = runez.parent_folder(target) if runez.parent_folder(source).startswith(parent): # Use relative path if source is under target source = os.path.relpath(source, parent) os.symlink(source, target)
def test_parent_folder(): cwd = os.getcwd() assert runez.parent_folder(None) is None assert runez.parent_folder("././some-file") == cwd if not runez.SYS_INFO.platform_id.is_windows: parent = runez.parent_folder("/logs/foo") assert parent == "/logs" assert runez.parent_folder(parent) == "/" assert runez.parent_folder("/") == "/"
def test_pathlib(temp_folder): subfolder = Path("subfolder") assert runez.to_path(subfolder) is subfolder assert not subfolder.is_dir() runez.ensure_folder(subfolder) assert subfolder.is_dir() with pytest.raises(Exception): runez.to_path("foo bar", no_spaces=Exception) with runez.CurrentFolder(subfolder, anchor=True): path = Path("foo") assert runez.short(path) == "foo" assert runez.short(path.absolute()) == "foo" assert runez.resolved_path(path) assert runez.parent_folder(path) == os.path.join(temp_folder, "subfolder") assert runez.touch(path) == 1 assert runez.copy(path, Path("bar")) == 1 assert runez.copy(Path("bar"), Path("baz")) == 1 foo_json = Path("foo.json") runez.write(path, '{"a": "b"}') runez.symlink(path, foo_json) assert runez.read_json(foo_json) == {"a": "b"} assert list(runez.readlines(foo_json)) == ['{"a": "b"}'] assert runez.basename(foo_json.absolute()) == "foo"
def find_brew_name(target): """ Args: target (str): Path to executable file Returns: (str, str): Name of brew formula, if target was installed with brew """ if os.path.islink(target): path = os.path.realpath(target) folder = runez.parent_folder(target) cellar = os.path.join(runez.parent_folder(folder), "Cellar") if path.startswith(cellar): brew = os.path.join(folder, "brew") name, _, _ = path[len(cellar) + 1:].partition("/") return brew, name return None, None
def __init__(self, parent, path=None, name=None): """ :param Settings parent: Parent settings object :param str|None path: Path to settings file """ self.parent = parent self.path = path or name self.folder = runez.parent_folder(path) self._contents = None
def find_brew_name(target): """ :param str target: Path to executable file :return str, str: Name of brew formula, if target was installed with brew """ if not os.path.islink(target): return None, None path = os.path.realpath(target) folder = runez.parent_folder(target) cellar = os.path.join(runez.parent_folder(folder), "Cellar") if not path.startswith(cellar): return None, None brew = os.path.join(folder, "brew") if not runez.is_executable(brew): return None, None name, _, _ = path[len(cellar) + 1:].partition("/") return brew, name
def test_paths(temp_folder): assert runez.resolved_path(None) is None assert runez.resolved_path("some-file") == os.path.join(temp_folder, "some-file") assert runez.resolved_path("some-file", base="bar") == os.path.join(temp_folder, "bar", "some-file") assert runez.short(None) is None assert runez.short("") == "" assert runez.short(temp_folder) == temp_folder assert runez.short(temp_folder + "/some-file") == "some-file" assert runez.short(temp_folder + "/some-file") == "some-file" assert runez.parent_folder(None) is None assert runez.parent_folder(temp_folder + "/some-file") == temp_folder assert runez.represented_args(["ls", temp_folder + "/some-file bar", "-a"]) == 'ls "some-file bar" -a' # Don't crash for no-ops assert runez.ensure_folder(None) == 0 assert runez.ensure_folder("") == 0 assert runez.copy(None, None) == 0 assert runez.move(None, None) == 0 assert runez.symlink(None, None) == 0 assert runez.copy("some-file", "some-file") == 0 assert runez.move("some-file", "some-file") == 0 assert runez.symlink("some-file", "some-file") == 0 assert runez.ensure_folder("some-folder") == 0 # 'some-folder' would be in temp_folder, which already exists with runez.CaptureOutput(dryrun=True) as logged: assert runez.ensure_folder("some-folder", folder=True, fatal=False) == 1 assert "Would create" in logged.pop() assert runez.touch("some-file", logger=logging.debug) == 1 assert "Would touch some-file" in logged.pop() assert runez.copy("some-file", "bar") == 1 assert "Would copy some-file -> bar" in logged.pop() assert runez.move("some-file", "bar") == 1 assert "Would move some-file -> bar" in logged.pop() assert runez.symlink("some-file", "bar") == 1 assert "Would symlink some-file <- bar" in logged.pop() assert runez.delete(temp_folder) == 1 assert "Would delete" in logged.pop() assert runez.copy("some-folder/bar/baz", "some-folder", fatal=False) == -1 assert "source contained in destination" in logged.pop() assert runez.move("some-folder/bar/baz", "some-folder", fatal=False) == -1 assert "source contained in destination" in logged.pop() assert runez.symlink("some-folder/bar/baz", "some-folder", fatal=False) == -1 assert "source contained in destination" in logged.pop() with runez.CaptureOutput(): assert runez.touch("sample") == 1 assert "Can't create folder" in runez.verify_abort(runez.ensure_folder, "sample", folder=True) custom = runez.verify_abort(runez.ensure_folder, "sample", folder=True, fatal=SystemExit, expected_exception=SystemExit) assert "Can't create folder" in custom with pytest.raises(AssertionError): assert runez.verify_abort(runez.ensure_folder, None) assert runez.delete("sample") == 1 assert runez.ensure_folder("sample", folder=True) == 1 assert os.getcwd() == temp_folder with runez.CurrentFolder("sample", anchor=False): cwd = os.getcwd() sample = os.path.join(temp_folder, "sample") assert cwd == sample assert runez.short(os.path.join(cwd, "some-file")) == "sample/some-file" with runez.CurrentFolder("sample", anchor=True): cwd = os.getcwd() sample = os.path.join(temp_folder, "sample") assert cwd == sample assert runez.short(os.path.join(cwd, "some-file")) == "some-file" assert os.getcwd() == temp_folder assert runez.delete("sample") == 1 with runez.CaptureOutput() as logged: sample = os.path.join(os.path.dirname(__file__), "sample.txt") content = runez.get_lines(sample) assert runez.write("sample", "".join(content), fatal=False, logger=logging.debug) == 1 assert runez.get_lines("sample") == content assert "Writing 13 bytes" in logged.pop() assert runez.first_line("sample") == "Fred" assert runez.is_younger("sample", age=10) assert not runez.is_younger("sample", age=-1) assert runez.copy("bar", "baz", fatal=False) == -1 assert "does not exist" in logged.pop() assert runez.move("bar", "baz", fatal=False) == -1 assert "does not exist" in logged.pop() assert runez.symlink("bar", "baz", fatal=False) == -1 assert "does not exist" in logged.pop() # Creating dangling symlinks is possible assert runez.symlink("bar", "baz", fatal=False, must_exist=False) == 1 assert "Symlink bar <- baz" in logged.pop() assert os.path.islink("baz") assert not os.path.exists("baz") assert runez.copy("sample", "x/y/sample") == 1 assert runez.symlink("sample", "x/y/sample3", fatal=False) == 1 assert os.path.exists("sample") assert runez.move("sample", "x/y/sample2") == 1 assert not os.path.exists("sample") assert runez.copy("x/y", "x/z") == 1 assert os.path.exists("x/z/sample") assert os.path.exists("x/z/sample2") assert os.path.exists("x/z/sample3") assert os.path.islink("x/z/sample3") assert runez.touch(None) == 0 assert not runez.is_younger(None, 1) assert not runez.is_younger("/dev/null/not-there", 1) assert runez.first_line("/dev/null/not-there") is None assert runez.get_lines(None) is None
import os import pytest import runez from runez.conftest import cli from pickley.cli import main from pickley.settings import DOT_PICKLEY # noqa: imported to ensure that system.SETTINGS is set TESTS = runez.parent_folder(__file__) PROJECT = runez.parent_folder(TESTS) INEXISTING_FILE = "/dev/null/foo/bar" cli.default_main = main def sample_path(*relative): return os.path.join(TESTS, "samples", *relative) def verify_abort(func, *args, **kwargs): exception = kwargs.pop('exception', SystemExit) with runez.CaptureOutput() as logged: with pytest.raises(exception): func(*args, **kwargs) return str(logged) class TemporaryBase(runez.TempFolder): def __enter__(self): super(TemporaryBase, self).__enter__()