Skip to content

Commit

Permalink
Get scripts from core scripts repo
Browse files Browse the repository at this point in the history
Instead of getting them as a sub module from the docs repo, get them directly from the core scrips repo.
  • Loading branch information
NeutraChikara committed Nov 27, 2024
1 parent 474a980 commit 35a8c0d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import django
from django.core.management.base import BaseCommand
from system.models import Script, ScriptTag, Input # TODO-script remove Input if it is not used
from system.models import Script, ScriptTag, Input
from system.script_fetcher import fetch_scripts

class Command(BaseCommand):
Expand Down
53 changes: 26 additions & 27 deletions admin_site/system/script_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
from typing import List, Optional

def fetch_scripts(versionTag, commitHash): # TODO-script use args
repo_url = "https://github.com/OS2borgerPC/os2borgerpc-docs.git"
repo_url = "https://github.com/OS2borgerPC/os2borgerpc-core-scripts.git"
clone_path = Path("downloaded_core_scripts") / get_repo_name(repo_url) / commitHash

# Perform a shallow clone if the repository isn't already cloned
if not (clone_path).exists():
if versionTag:
# Check that the commitHash matches the versionTag
subprocess.run(
["git", "clone", "--depth", "1", "--branch", versionTag, "--recurse-submodules", repo_url, str(clone_path)],
["git", "clone", "--depth", "1", "--branch", versionTag, repo_url, str(clone_path)],
check=True
)

Expand All @@ -32,36 +32,35 @@ def fetch_scripts(versionTag, commitHash): # TODO-script use args
else:
# Clone the repository and then check out the specific commit hash
subprocess.run(
["git", "clone", "--filter=blob:none", "--recurse-submodules", repo_url, str(clone_path)],
["git", "clone", "--filter=blob:none", repo_url, str(clone_path)],
check=True
)
subprocess.run(
["git", "-C", str(clone_path), "checkout", commitHash, "--recurse-submodules"],
["git", "-C", str(clone_path), "checkout", commitHash],
check=True
)

# Retrieve content of all markdown (.md) files, and convert them to Scripts
scripts = []
md_dir_path = clone_path / "docs/scripts"
if md_dir_path.exists():
for md_file in md_dir_path.glob("**/*.md"):
if md_file.is_file():
with open(md_file, 'r') as file:
file_content = file.read()
try:
script = parse_md_to_script(file_content, md_file.parent.name, clone_path)
except Exception as e:
print("Warning: Skipping script file '" + str(md_file.relative_to(md_dir_path)) + "' because it's content is not formatted correctly:")
print(file_content)
print(f"Exception: {e}")
continue
if not Path(script.sourcePath).exists():
# TODO-script print warning
print("Warning: Skipping " + str(md_file) + " because the 'source' attribute does not point to a valid file.")
continue
scripts.append(script)
else:
print("Warning: Path '/docs/scripts' not found at " + repo_url) # TODO-script print warning
md_dir_path = clone_path
for md_file in md_dir_path.glob("**/*.md"):
if md_file.is_file():
if md_file.name == "README.md":
continue
with open(md_file, 'r') as file:
file_content = file.read()
try:
script = parse_md_to_script(file_content, clone_path)
except Exception as e:
print("Warning: Skipping script file '" + str(md_file.relative_to(md_dir_path)) + "' because it's content is not formatted correctly:")
print(file_content)
print(f"Exception: {e}")
continue
if not Path(script.sourcePath).exists():
# TODO-script print warning properly
print("Warning: Skipping " + str(md_file.relative_to(md_dir_path)) + " because the 'source' attribute does not point to a valid file.")
continue
scripts.append(script)

return scripts

Expand All @@ -84,7 +83,7 @@ class Script:
partners: Optional[str]
parameters: List[Parameter] = field(default_factory=list)

def parse_md_to_script(content: str, parentFolderName: str, clone_path: Path) -> Script:
def parse_md_to_script(content: str, clone_path: Path) -> Script:
# Split YAML and Markdown content
yamlBegin, yaml_string, markdown_string = content.split("---", 2)
yaml_string = yaml_string.strip()
Expand All @@ -110,11 +109,11 @@ def parse_md_to_script(content: str, parentFolderName: str, clone_path: Path) ->
return Script(
title=yaml_content.get('title'),
parent=yaml_content.get('parent'),
sourcePath=str(clone_path) + yaml_content.get('source'),
sourcePath=str(clone_path / yaml_content.get('source')),
compatible_versions=yaml_content.get('compatible_versions'),
compatible_images=yaml_content.get('compatible_images', []),
description=markdown_string,
tag=parentFolderName,
tag=yaml_content.get('parent'),
partners=yaml_content.get('partners'),
parameters=parameters
)
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ services:
DB_PASSWORD: bpc
DB_PORT: ""
ALLOWED_HOSTS: "*"
CORE_SCRIPT_COMMIT_HASH: e0376ca2758372a80ebaa7804e18bbbc4c198453
CORE_SCRIPT_COMMIT_HASH: bcd012570ce43536c3bc7b837e57d6e4e41a56f1
DEBUG: True
SECRET_KEY: v3rys1kr3t
# Admin contact - fill in your own name and email as desired.
Expand Down

0 comments on commit 35a8c0d

Please sign in to comment.