Skip to content

Instantly share code, notes, and snippets.

@Gnuxie
Created April 13, 2025 16:03
Show Gist options
  • Select an option

  • Save Gnuxie/f27a6efc8a0d55918e25167add19fe8f to your computer and use it in GitHub Desktop.

Select an option

Save Gnuxie/f27a6efc8a0d55918e25167add19fe8f to your computer and use it in GitHub Desktop.
draupnir dependencies script
#!/bin/python3
import os
import sys
import subprocess
TypeScriptResult = '@gnuxie/typescript-result'
MatrixBasicTypes = '@the-draupnir-project/matrix-basic-types'
InterfaceManager = '@the-draupnir-project/interface-manager'
YARN_PATH = '/usr/bin/yarn'
# Sample projects dictionary
projects = {
'experiments/typescript-result': [],
'experiments/neightrix-basic-types': [TypeScriptResult],
'experiments/matrix-protection-suite': [TypeScriptResult, MatrixBasicTypes],
'experiments/matrix-protection-suite-for-matrix-bot-sdk': [TypeScriptResult, MatrixBasicTypes, 'matrix-protection-suite'],
'experiments/interface-manager': [TypeScriptResult, MatrixBasicTypes],
'experiments/Draupnir': [TypeScriptResult, MatrixBasicTypes, InterfaceManager, 'matrix-protection-suite', 'matrix-protection-suite-for-matrix-bot-sdk']
}
def subprocess_run(command_parts):
try:
subprocess.run(command_parts, check=True, capture_output=True, text=True)
print(f'Successfully ran {command_parts}')
except subprocess.CalledProcessError as e:
print(f'Error running {command_parts}\n{e.stderr}')
def run_command(command, directory, project_list):
# Change to the target directory
os.chdir(directory)
# run the command just once to link itself or unlink itself.
subprocess_run([YARN_PATH, command])
try:
# Run the yarn link or unlink command for all projects in one call
if len(project_list) != 0:
subprocess.run([YARN_PATH, command] + project_list, check=True, capture_output=True, text=True)
print(f'Successfully ran {YARN_PATH} {command} for {", ".join(project_list)} in {directory}')
except subprocess.CalledProcessError as e:
print(f'Error running {YARN_PATH} {command} for {", ".join(project_list)} in {directory}:\n{e.stderr}')
# refresh the dependencies
if (command == 'unlink'):
subprocess_run([YARN_PATH, "install", "--force"])
def main(action):
# Determine the yarn command based on the action
if action not in ['link', 'unlink']:
print(f'Invalid action: {action}. Must be "link" or "unlink".')
sys.exit(1)
# Get the user's home directory
home_dir = os.path.expanduser('~')
# Iterate over the projects dictionary
for relative_directory, project_list in projects.items():
# Construct the full path by joining the home directory with the relative directory
full_directory = os.path.join(home_dir, relative_directory)
# Call yarn link/unlink once with all projects in the list
run_command(action, full_directory, project_list)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: script.py <link|unlink>")
sys.exit(1)
action = sys.argv[1]
main(action)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment