"""Continuous Integration, Delivery & Deployment.""" __version__ = "0.0.1" import os import sys from pathlib import Path import env from rwx import fs, ps from rwx.log import stream as log import cidd from cidd.project import Project from cidd.projects import Projects COMMANDS_PREFIX = "cidd-" projects = Projects() project = Project(projects) def cidd_browse_workspace() -> None: browse(project.root) def cidd_build_project() -> None: for extension in ["py", "sh"]: path = Path(project.root) / f"build.{extension}" if path.exists(): ps.run(path) break else: pass def cidd_clone_branch() -> None: log.info(projects) split() log.info(project) split() log.info( f"""\ {project.url} ↓ """, end="", flush=True, ) ps.run( "git", "clone", "--branch", project.branch, "--", project.url, project.root, ) def cidd_list_environment() -> None: for variable, value in sorted(projects.environment.items()): log.info(f"{variable} = {value}") def cidd_synchronize() -> None: host = "rwx.work" source = "out" user = "cd" # root = Path(os.sep) / user / project.branch / projects.group / project.name # target = f"{user}@{host}:{root}" ps.run( "rsync", "--archive", "--delete-before", "--verbose", f"{source}/", f"{target}/", "--dry-run", ) def browse(root: str) -> None: paths = [] for directory, _, files in os.walk(root): for file in files: absolute_path = Path(directory) / file relative_path = os.path.relpath(absolute_path, start=root) paths.append(relative_path) frame(root) for path in sorted(paths): log.info(path) shut(root) def cat(file: str) -> None: frame(file) log.info(fs.read_file_text(file).rstrip()) shut(file) def install_commands(path: str) -> None: step("Install commands") user = Path("/usr/local/bin") for command in [ "browse-workspace", "build-project", "clone-branch", "list-environment", "synchronize", ]: log.info(command) (user / f"{COMMANDS_PREFIX}{command}").symlink_to(path) def main() -> None: path, *arguments = sys.argv name = Path(path).name if name == "__main__.py": cidd.set_ssh(*arguments) cidd.install_commands(__file__) else: function = getattr(cidd, name.replace("-", "_")) function(*arguments) def set_ssh(*arguments: str) -> None: step("Set SSH") # ssh_key, ssh_hosts = arguments # ssh_type = "ed25519" # home = Path("~").expanduser() # ssh = home / ".ssh" ssh.mkdir(exist_ok=True, parents=True) ssh.chmod(0o700) # key = ssh / f"id_{ssh_type}" if ssh_key: fs.write(key, ssh_key) key.chmod(0o400) # known = ssh / "known_hosts" if ssh_hosts: fs.write(known, ssh_hosts) known.chmod(0o400) # browse(ssh) cat(known) def frame(text: str) -> None: log.info(f"{env.CIDD_OPEN}{text}") def shut(text: str) -> None: log.info(f"{env.CIDD_SHUT}{text}") def split() -> None: log.info(env.CIDD_SPLT) def step(text: str) -> None: env.CIDD_STEP += 1 log.info(env.CIDD_DOWN) log.info(f"{env.CIDD_VERT} {env.CIDD_STEP} {text}") log.info(env.CIDD___UP)