From 660cdbde117572559f83112fae146858233e9f9e Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Sun, 9 Jun 2024 18:09:53 +0200 Subject: [PATCH] Path --- cd/__init__.py | 30 +++++++++++++++--------------- cd/__main__.py | 4 ++-- cd/project.py | 6 +++--- cd/projects.py | 4 ++-- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/cd/__init__.py b/cd/__init__.py index da2943e..4d15ce0 100644 --- a/cd/__init__.py +++ b/cd/__init__.py @@ -1,4 +1,5 @@ import os +from pathlib import Path import env from rwx import fs, ps @@ -18,8 +19,8 @@ def cd_browse_workspace() -> None: def cd_build_project() -> None: for extension in ["py", "sh"]: - path = os.path.join(project.root, f"build.{extension}") - if os.path.exists(path): + path = Path(project.root) / f"build.{extension}" + if path.exists(): ps.run(path) break else: @@ -50,8 +51,7 @@ def cd_synchronize() -> None: source = "out" user = "cd" # - root = os.sep.join(["", - user, project.branch, projects.group, project.name]) + root = Path(os.sep) / user / project.branch / projects.group / project.name # target = f"{user}@{host}:{root}" ps.run("rsync", @@ -68,7 +68,7 @@ def browse(root: str) -> None: paths = [] for directory, _, files in os.walk(root): for file in files: - absolute_path = os.path.join(directory, file) + absolute_path = Path(directory) / file relative_path = os.path.relpath(absolute_path, start=root) paths.append(relative_path) open(root) @@ -85,7 +85,7 @@ def cat(file: str) -> None: def install_commands(path) -> None: step("Install commands") - user = "/usr/local/bin" + user = Path("/usr/local/bin") for command in [ "browse-workspace", "build-project", @@ -94,7 +94,7 @@ def install_commands(path) -> None: "synchronize", ]: print(command) - os.symlink(path, os.path.join(user, f"{COMMANDS_PREFIX}{command}")) + os.symlink(path, user / f"{COMMANDS_PREFIX}{command}") def set_ssh(*arguments) -> None: @@ -104,21 +104,21 @@ def set_ssh(*arguments) -> None: # ssh_type = "ed25519" # - home = os.path.expanduser("~") + home = Path("~").expanduser() # - ssh = os.path.join(home, ".ssh") - os.makedirs(ssh, exist_ok=True) - os.chmod(ssh, 0o700) + ssh = home / ".ssh" + ssh.mkdir(parents=True) + ssh.chmod(0o700) # - key = os.path.join(ssh, f"id_{ssh_type}") + key = ssh / f"id_{ssh_type}" if ssh_key: fs.write(key, ssh_key) - os.chmod(key, 0o400) + key.chmod(0o400) # - known = os.path.join(ssh, "known_hosts") + known = ssh / "known_hosts" if ssh_hosts: fs.write(known, ssh_hosts) - os.chmod(known, 0o400) + known.chmod(0o400) # browse(ssh) cat(known) diff --git a/cd/__main__.py b/cd/__main__.py index af676cd..891e6aa 100755 --- a/cd/__main__.py +++ b/cd/__main__.py @@ -1,13 +1,13 @@ #! /usr/bin/env python3 -import os import sys +from pathlib import Path import cd if __name__ == "__main__": command, *arguments = sys.argv - command = os.path.basename(command) + command = Path(command).name if command == "__main__.py": cd.set_ssh(*arguments) cd.install_commands(__file__) diff --git a/cd/project.py b/cd/project.py index 3ff02cd..51971d1 100644 --- a/cd/project.py +++ b/cd/project.py @@ -1,4 +1,4 @@ -from os import path +from pathlib import Path BRANCH = [ "GITHUB_REF_NAME", @@ -24,10 +24,10 @@ class Project: # name for variable in NAME: if value := projects.environment.get(variable, None): - self.name = path.basename(value) + self.name = Path(value).name # root for variable in ROOT: if value := projects.environment.get(variable, None): self.root = value # url - self.url = path.join(projects.url, self.name) + self.url = Path(projects.url) / self.name diff --git a/cd/projects.py b/cd/projects.py index 6f7c96a..616ff2d 100644 --- a/cd/projects.py +++ b/cd/projects.py @@ -1,4 +1,4 @@ -from os import path +from pathlib import Path GROUP_AND_NAME = [ "GITHUB_REPOSITORY", @@ -20,4 +20,4 @@ class Projects: # url for variable in SERVER_URL: if value := self.environment.get(variable, None): - self.url = path.join(value, self.group) + self.url = Path(value) / self.group