spcd/cd/__init__.py

109 lines
2.4 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-05-30 22:29:12 +00:00
from rwx import fs
2024-04-26 23:10:30 +00:00
from rwx import ps
2024-04-26 21:55:20 +00:00
COMMANDS_PREFIX = 'cd-'
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():
print(f'''\
{project.root}
''', end=str())
2024-05-29 22:46:05 +00:00
paths = []
2024-05-29 22:21:42 +00:00
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)
2024-05-29 22:46:05 +00:00
paths.append(relative_path)
2024-05-29 22:49:23 +00:00
for path in sorted(paths):
2024-05-29 22:46:05 +00:00
print(path)
2024-05-29 22:21:42 +00:00
2024-05-30 10:35:05 +00:00
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
2024-04-26 23:10:30 +00:00
def cd_clone_branch():
2024-04-28 16:36:34 +00:00
print(f'''\
{project.url}
2024-04-28 16:40:31 +00:00
''', end=str(), flush=True)
2024-04-26 23:10:30 +00:00
ps.run('git',
'clone',
2024-04-27 13:32:33 +00:00
'--branch', project.branch,
2024-04-26 23:10:30 +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-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-04-26 22:06:41 +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():
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',
)
2024-04-26 21:24:46 +00:00
def install_commands(path):
user = '/usr/local/bin'
2024-04-26 23:10:30 +00:00
for command in [
2024-05-29 22:21:42 +00:00
'browse-workspace',
2024-05-30 10:35:05 +00:00
'build-project',
2024-04-26 23:10:30 +00:00
'clone-branch',
'list-environment',
2024-06-01 13:25:23 +00:00
'synchronize',
2024-04-26 23:10:30 +00:00
]:
2024-04-28 11:00:18 +00:00
print(command)
2024-04-26 21:55:20 +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):
ssh_hosts = arguments[0]
2024-05-30 22:29:12 +00:00
ssh_key = projects.environment.get('CD', None)
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)