spcd/cd/__init__.py

146 lines
2.9 KiB
Python
Raw Normal View History

2024-04-26 19:58:35 +00:00
import os
2024-04-27 13:32:33 +00:00
from cd.project import Project
from cd.projects import Projects
2024-06-03 01:36:56 +00:00
import env
2024-05-30 22:29:12 +00:00
from rwx import fs
2024-04-26 23:10:30 +00:00
from rwx import ps
2024-06-07 08:26:18 +00:00
COMMANDS_PREFIX = "cd-"
2024-04-26 21:55:20 +00:00
2024-04-27 15:09:51 +00:00
projects = Projects(os.environ)
project = Project(projects)
2024-04-27 13:32:33 +00:00
2024-04-26 21:55:20 +00:00
2024-05-29 22:21:42 +00:00
def cd_browse_workspace():
2024-06-06 08:17:02 +00:00
browse(project.root)
2024-05-29 22:21:42 +00:00
2024-05-30 10:35:05 +00:00
def cd_build_project():
2024-06-07 08:26:18 +00:00
for extension in ["py", "sh"]:
path = os.path.join(project.root, f"build.{extension}")
2024-05-30 10:35:05 +00:00
if os.path.exists(path):
ps.run(path)
break
else:
pass
2024-04-26 23:10:30 +00:00
def cd_clone_branch():
2024-06-07 08:26:18 +00:00
print(f"""\
2024-04-28 16:36:34 +00:00
{project.url}
2024-06-07 08:26:18 +00:00
""", end=str(), flush=True)
ps.run("git",
"clone",
"--branch", project.branch,
"--",
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-04-26 21:58:06 +00:00
def cd_list_environment():
2024-05-30 22:29:12 +00:00
for variable, value in sorted(projects.environment.items()):
2024-06-07 08:26:18 +00:00
print(variable, "=", value)
2024-04-26 21:55:20 +00:00
2024-04-26 19:58:35 +00:00
2024-06-01 13:25:23 +00:00
def cd_synchronize():
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-02 10:41:46 +00:00
root = os.sep.join([str(),
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}"
ps.run("rsync",
"--archive",
"--delete-before",
"--verbose",
f"{source}/",
f"{target}/",
"--dry-run",
2024-06-01 13:25:23 +00:00
)
2024-06-06 08:17:02 +00:00
def browse(root: str):
paths = []
for directory, _, files in os.walk(root):
for file in files:
absolute_path = os.path.join(directory, file)
relative_path = os.path.relpath(absolute_path, start=root)
paths.append(relative_path)
2024-06-06 08:53:19 +00:00
open(root)
2024-06-06 08:17:02 +00:00
for path in sorted(paths):
print(path)
2024-06-06 08:53:19 +00:00
shut(root)
2024-06-06 08:17:02 +00:00
2024-06-06 12:41:06 +00:00
def cat(file: str):
open(file)
2024-06-06 14:01:59 +00:00
print(fs.read_file_text(file).rstrip())
2024-06-06 12:41:06 +00:00
shut(file)
2024-04-26 21:24:46 +00:00
def install_commands(path):
2024-06-07 08:26:18 +00:00
step("Install commands")
user = "/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-04-28 11:00:18 +00:00
print(command)
2024-06-07 08:26:18 +00:00
os.symlink(path, os.path.join(user, f"{COMMANDS_PREFIX}{command}"))
2024-05-30 22:29:12 +00:00
2024-06-01 18:14:06 +00:00
def set_ssh(*arguments):
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-07 08:26:18 +00:00
home = os.path.expanduser("~")
2024-05-30 22:29:12 +00:00
#
2024-06-07 08:26:18 +00:00
ssh = os.path.join(home, ".ssh")
2024-05-30 22:29:12 +00:00
os.makedirs(ssh, exist_ok=True)
os.chmod(ssh, 0o700)
#
2024-06-07 08:26:18 +00:00
key = os.path.join(ssh, f"id_{ssh_type}")
2024-05-30 22:29:12 +00:00
if ssh_key:
fs.write(key, ssh_key)
os.chmod(key, 0o400)
#
2024-06-07 08:26:18 +00:00
known = os.path.join(ssh, "known_hosts")
2024-05-30 22:29:12 +00:00
if ssh_hosts:
fs.write(known, ssh_hosts)
os.chmod(known, 0o400)
2024-06-06 12:41:06 +00:00
cat(known)
2024-06-06 08:17:02 +00:00
#
2024-06-06 09:36:06 +00:00
browse(ssh)
2024-06-05 09:41:47 +00:00
2024-06-05 10:45:54 +00:00
def open(*arguments):
print(env.CD_OPEN, end=str())
print(*arguments, flush=True)
def shut(*arguments):
print(env.CD_SHUT, end=str())
print(*arguments, flush=True)
2024-06-05 09:45:41 +00:00
def split():
2024-06-05 12:46:56 +00:00
print(env.CD_SPLT, flush=True)
2024-06-05 09:45:41 +00:00
2024-06-05 09:41:47 +00:00
def step(*arguments):
2024-06-05 10:45:54 +00:00
env.CD_STEP += 1
print(env.CD_DOWN)
2024-06-05 12:48:19 +00:00
print(env.CD_VERT, env.CD_STEP, *arguments)
2024-06-05 12:49:24 +00:00
print(env.CD___UP, flush=True)