2024-06-21 13:01:29 +00:00
|
|
|
"""Python Integration, Delivery & Deployment."""
|
2024-06-09 21:15:19 +00:00
|
|
|
|
2024-06-14 12:25:21 +00:00
|
|
|
__version__ = "0.0.1"
|
|
|
|
|
2024-04-26 19:58:35 +00:00
|
|
|
import os
|
2024-06-21 10:10:09 +00:00
|
|
|
import sys
|
2024-06-09 16:09:53 +00:00
|
|
|
from pathlib import Path
|
2024-04-26 19:58:35 +00:00
|
|
|
|
2024-06-08 22:01:38 +00:00
|
|
|
import env
|
|
|
|
from rwx import fs, ps
|
2024-06-13 10:34:05 +00:00
|
|
|
from rwx.log import stream as log
|
2024-06-08 22:01:38 +00:00
|
|
|
|
2024-06-21 12:57:57 +00:00
|
|
|
import pidd
|
|
|
|
from pidd.project import Project
|
|
|
|
from pidd.projects import Projects
|
2024-04-26 23:10:30 +00:00
|
|
|
|
2024-06-21 12:57:57 +00:00
|
|
|
COMMANDS_PREFIX = "pidd-"
|
2024-04-26 21:55:20 +00:00
|
|
|
|
2024-06-10 12:22:07 +00:00
|
|
|
projects = Projects()
|
2024-04-27 15:09:51 +00:00
|
|
|
project = Project(projects)
|
2024-04-27 13:32:33 +00:00
|
|
|
|
2024-04-26 21:55:20 +00:00
|
|
|
|
2024-06-21 12:57:57 +00:00
|
|
|
def pidd_browse_workspace() -> None:
|
2024-06-06 08:17:02 +00:00
|
|
|
browse(project.root)
|
2024-05-29 22:21:42 +00:00
|
|
|
|
|
|
|
|
2024-06-21 12:57:57 +00:00
|
|
|
def pidd_build_project() -> None:
|
2024-06-07 08:26:18 +00:00
|
|
|
for extension in ["py", "sh"]:
|
2024-06-09 16:09:53 +00:00
|
|
|
path = Path(project.root) / f"build.{extension}"
|
|
|
|
if path.exists():
|
2024-05-30 10:35:05 +00:00
|
|
|
ps.run(path)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2024-06-21 12:57:57 +00:00
|
|
|
def pidd_clone_branch() -> None:
|
2024-06-13 11:44:59 +00:00
|
|
|
log.info(projects)
|
2024-06-10 19:29:11 +00:00
|
|
|
split()
|
2024-06-13 11:44:59 +00:00
|
|
|
log.info(project)
|
2024-06-10 19:29:11 +00:00
|
|
|
split()
|
2024-06-22 10:39:06 +00:00
|
|
|
log.info(f"""\
|
2024-04-28 16:36:34 +00:00
|
|
|
{project.url}
|
|
|
|
↓
|
2024-06-22 10:39:06 +00:00
|
|
|
""")
|
2024-06-12 12:40:39 +00:00
|
|
|
ps.run(
|
|
|
|
"git",
|
2024-06-07 08:26:18 +00:00
|
|
|
"clone",
|
2024-06-12 12:40:39 +00:00
|
|
|
"--branch",
|
|
|
|
project.branch,
|
2024-06-07 08:26:18 +00:00
|
|
|
"--",
|
2024-04-27 13:32:33 +00:00
|
|
|
project.url,
|
2024-05-30 10:03:15 +00:00
|
|
|
project.root,
|
2024-04-26 23:10:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-06-21 12:57:57 +00:00
|
|
|
def pidd_list_environment() -> None:
|
2024-05-30 22:29:12 +00:00
|
|
|
for variable, value in sorted(projects.environment.items()):
|
2024-06-13 11:44:59 +00:00
|
|
|
log.info(f"{variable} = {value}")
|
2024-04-26 21:55:20 +00:00
|
|
|
|
2024-04-26 19:58:35 +00:00
|
|
|
|
2024-06-21 12:57:57 +00:00
|
|
|
def pidd_synchronize() -> None:
|
2024-06-07 08:26:18 +00:00
|
|
|
host = "rwx.work"
|
|
|
|
source = "out"
|
|
|
|
user = "cd"
|
2024-06-01 13:25:23 +00:00
|
|
|
#
|
2024-06-09 16:09:53 +00:00
|
|
|
root = Path(os.sep) / user / project.branch / projects.group / project.name
|
2024-06-01 13:25:23 +00:00
|
|
|
#
|
2024-06-07 08:26:18 +00:00
|
|
|
target = f"{user}@{host}:{root}"
|
2024-06-12 12:40:39 +00:00
|
|
|
ps.run(
|
|
|
|
"rsync",
|
2024-06-07 08:26:18 +00:00
|
|
|
"--archive",
|
|
|
|
"--delete-before",
|
|
|
|
"--verbose",
|
|
|
|
f"{source}/",
|
|
|
|
f"{target}/",
|
|
|
|
"--dry-run",
|
2024-06-01 13:25:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-06-09 15:23:57 +00:00
|
|
|
def browse(root: str) -> None:
|
2024-06-06 08:17:02 +00:00
|
|
|
paths = []
|
|
|
|
for directory, _, files in os.walk(root):
|
|
|
|
for file in files:
|
2024-06-09 16:09:53 +00:00
|
|
|
absolute_path = Path(directory) / file
|
2024-06-06 08:17:02 +00:00
|
|
|
relative_path = os.path.relpath(absolute_path, start=root)
|
|
|
|
paths.append(relative_path)
|
2024-06-09 19:00:01 +00:00
|
|
|
frame(root)
|
2024-06-06 08:17:02 +00:00
|
|
|
for path in sorted(paths):
|
2024-06-13 11:44:59 +00:00
|
|
|
log.info(path)
|
2024-06-06 08:53:19 +00:00
|
|
|
shut(root)
|
2024-06-06 08:17:02 +00:00
|
|
|
|
|
|
|
|
2024-06-09 15:23:57 +00:00
|
|
|
def cat(file: str) -> None:
|
2024-06-09 19:00:01 +00:00
|
|
|
frame(file)
|
2024-06-13 11:44:59 +00:00
|
|
|
log.info(fs.read_file_text(file).rstrip())
|
2024-06-06 12:41:06 +00:00
|
|
|
shut(file)
|
|
|
|
|
|
|
|
|
2024-06-09 18:28:09 +00:00
|
|
|
def install_commands(path: str) -> None:
|
2024-06-07 08:26:18 +00:00
|
|
|
step("Install commands")
|
2024-06-09 16:09:53 +00:00
|
|
|
user = Path("/usr/local/bin")
|
2024-04-26 23:10:30 +00:00
|
|
|
for command in [
|
2024-06-07 08:26:18 +00:00
|
|
|
"browse-workspace",
|
|
|
|
"build-project",
|
|
|
|
"clone-branch",
|
|
|
|
"list-environment",
|
|
|
|
"synchronize",
|
2024-04-26 23:10:30 +00:00
|
|
|
]:
|
2024-06-13 11:44:59 +00:00
|
|
|
log.info(command)
|
2024-06-09 18:28:09 +00:00
|
|
|
(user / f"{COMMANDS_PREFIX}{command}").symlink_to(path)
|
2024-05-30 22:29:12 +00:00
|
|
|
|
|
|
|
|
2024-06-21 14:26:33 +00:00
|
|
|
def main(main: str) -> None:
|
2024-06-21 10:10:09 +00:00
|
|
|
path, *arguments = sys.argv
|
|
|
|
name = Path(path).name
|
|
|
|
if name == "__main__.py":
|
2024-06-21 12:57:57 +00:00
|
|
|
pidd.set_ssh(*arguments)
|
2024-06-21 14:26:33 +00:00
|
|
|
pidd.install_commands(main)
|
2024-06-21 10:10:09 +00:00
|
|
|
else:
|
2024-06-21 12:57:57 +00:00
|
|
|
function = getattr(pidd, name.replace("-", "_"))
|
2024-06-21 10:10:09 +00:00
|
|
|
function(*arguments)
|
|
|
|
|
|
|
|
|
2024-06-09 18:55:32 +00:00
|
|
|
def set_ssh(*arguments: str) -> None:
|
2024-06-07 08:26:18 +00:00
|
|
|
step("Set SSH")
|
2024-06-05 15:04:06 +00:00
|
|
|
#
|
2024-06-01 22:51:21 +00:00
|
|
|
ssh_key, ssh_hosts = arguments
|
2024-06-05 09:55:25 +00:00
|
|
|
#
|
2024-06-07 08:26:18 +00:00
|
|
|
ssh_type = "ed25519"
|
2024-05-30 22:29:12 +00:00
|
|
|
#
|
2024-06-09 16:09:53 +00:00
|
|
|
home = Path("~").expanduser()
|
2024-05-30 22:29:12 +00:00
|
|
|
#
|
2024-06-09 16:09:53 +00:00
|
|
|
ssh = home / ".ssh"
|
2024-06-09 16:52:29 +00:00
|
|
|
ssh.mkdir(exist_ok=True, parents=True)
|
2024-06-09 16:09:53 +00:00
|
|
|
ssh.chmod(0o700)
|
2024-05-30 22:29:12 +00:00
|
|
|
#
|
2024-06-09 16:09:53 +00:00
|
|
|
key = ssh / f"id_{ssh_type}"
|
2024-05-30 22:29:12 +00:00
|
|
|
if ssh_key:
|
|
|
|
fs.write(key, ssh_key)
|
2024-06-09 16:09:53 +00:00
|
|
|
key.chmod(0o400)
|
2024-05-30 22:29:12 +00:00
|
|
|
#
|
2024-06-09 16:09:53 +00:00
|
|
|
known = ssh / "known_hosts"
|
2024-05-30 22:29:12 +00:00
|
|
|
if ssh_hosts:
|
|
|
|
fs.write(known, ssh_hosts)
|
2024-06-09 16:09:53 +00:00
|
|
|
known.chmod(0o400)
|
2024-06-06 08:17:02 +00:00
|
|
|
#
|
2024-06-06 09:36:06 +00:00
|
|
|
browse(ssh)
|
2024-06-08 21:51:37 +00:00
|
|
|
cat(known)
|
2024-06-05 09:41:47 +00:00
|
|
|
|
|
|
|
|
2024-06-13 11:44:59 +00:00
|
|
|
def frame(text: str) -> None:
|
2024-06-21 12:57:57 +00:00
|
|
|
log.info(f"{env.PIDD_OPEN}{text}")
|
2024-06-05 10:45:54 +00:00
|
|
|
|
|
|
|
|
2024-06-13 11:44:59 +00:00
|
|
|
def shut(text: str) -> None:
|
2024-06-21 12:57:57 +00:00
|
|
|
log.info(f"{env.PIDD_SHUT}{text}")
|
2024-06-05 10:45:54 +00:00
|
|
|
|
|
|
|
|
2024-06-09 15:23:57 +00:00
|
|
|
def split() -> None:
|
2024-06-21 12:57:57 +00:00
|
|
|
log.info(env.PIDD_SPLT)
|
2024-06-05 09:45:41 +00:00
|
|
|
|
|
|
|
|
2024-06-13 11:44:59 +00:00
|
|
|
def step(text: str) -> None:
|
2024-06-21 12:57:57 +00:00
|
|
|
env.PIDD_STEP += 1
|
|
|
|
log.info(env.PIDD_DOWN)
|
|
|
|
log.info(f"{env.PIDD_VERT} {env.PIDD_STEP} {text}")
|
|
|
|
log.info(env.PIDD___UP)
|