spcd/cd/__init__.py
Marc Beninca 34ed31e3aa
All checks were successful
/ job (push) Successful in 1m18s
args
2024-06-02 00:51:21 +02:00

107 lines
2.4 KiB
Python

import os
from cd.project import Project
from cd.projects import Projects
from rwx import fs
from rwx import ps
COMMANDS_PREFIX = 'cd-'
projects = Projects(os.environ)
project = Project(projects)
def cd_browse_workspace():
print(f'''\
{project.root}
''', end=str())
paths = []
for directory, directories, files in os.walk(project.root):
for file in files:
absolute_path = os.path.join(directory, file)
relative_path = os.path.relpath(absolute_path, start=project.root)
paths.append(relative_path)
for path in sorted(paths):
print(path)
def cd_build_project():
for extension in ['py', 'sh']:
path = os.path.join(project.root, f'build.{extension}')
if os.path.exists(path):
ps.run(path)
break
else:
pass
def cd_clone_branch():
print(f'''\
{project.url}
''', end=str(), flush=True)
ps.run('git',
'clone',
'--branch', project.branch,
'--',
project.url,
project.root,
)
def cd_list_environment():
for variable, value in sorted(projects.environment.items()):
print(variable, '=', value)
def cd_synchronize():
sync_host = 'rwx.work'
sync_source = 'out'
sync_user = 'cd'
#
sync_root = f'/{sync_user}/{project.branch}/{project.name}'
#
sync_target = f'{sync_user}@{sync_host}:{sync_root}'
ps.run('rsync',
'--archive',
'--delete-before',
'--verbose',
f'{sync_source}/',
f'{sync_target}/',
'--dry-run',
)
def install_commands(path):
user = '/usr/local/bin'
for command in [
'browse-workspace',
'build-project',
'clone-branch',
'list-environment',
'synchronize',
]:
print(command)
os.symlink(path, os.path.join(user, f'{COMMANDS_PREFIX}{command}'))
def set_ssh(*arguments):
ssh_key, ssh_hosts = arguments
ssh_type = projects.environment.get('CD_SSH_TYPE', 'ed25519')
#
home = os.path.expanduser('~')
#
ssh = os.path.join(home, '.ssh')
os.makedirs(ssh, exist_ok=True)
os.chmod(ssh, 0o700)
#
key = os.path.join(ssh, f'id_{ssh_type}')
if ssh_key:
fs.write(key, ssh_key)
os.chmod(key, 0o400)
#
known = os.path.join(ssh, 'known_hosts')
if ssh_hosts:
fs.write(known, ssh_hosts)
os.chmod(known, 0o400)