-
Notifications
You must be signed in to change notification settings - Fork 21
/
runpodctl-docs.py
109 lines (91 loc) · 3.55 KB
/
runpodctl-docs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
from helpers import install_steps
"""
The above Python script performs the following tasks:
1. Checks if a Git module exists and adds it if not.
2. Moves Markdown files from one directory to another.
3. Updates each Markdown file by adding a metadata header and removing unnecessary lines.
4. Cleans up by removing the Git module
:param command: The `command` parameter in the `run_command` function is the shell command that you
want to execute. It can be any valid shell command, such as `ls`, `git clone`, or `wget`. The
function will run the command and return the output as a string
:return: The script does not explicitly return any values. It performs various operations such as
checking for an existing Git module, moving Markdown files, editing Markdown files, and cleaning up.
The script prints messages to indicate the progress and completion of each operation.
"""
import glob
import re
import subprocess
# Function to run shell commands
def run_command(command):
process = subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
out, err = process.communicate()
if process.returncode != 0:
print(f"Error: {err}")
return out.decode().strip()
# Function to generate metadata header
def generate_metadata_header(title):
return f'---\ntitle: "{title}"\n---\n\n'
# 1. Check if Git module already exists and add if not
print("Checking for existing Git module...")
module_path = "runpodctl"
if not os.path.exists(module_path):
print("Adding Git module...")
run_command("gh repo clone runpod/runpodctl runpodctl")
else:
print("Git module already exists. Skipping addition.")
# 2. Move Markdown files
print("Moving Markdown files...")
source_dir = "runpodctl/docs/"
target_dir = "docs/runpodctl/reference/"
os.makedirs(target_dir, exist_ok=True)
for md_file in glob.glob(f"{source_dir}*.md"):
os.rename(md_file, f"{target_dir}{os.path.basename(md_file)}")
# 3. Update each Markdown file
print("Editing Markdown files...")
for md_file in glob.glob(f"{target_dir}*.md"):
title = (
os.path.basename(md_file)
.replace("runpodctl", "")
.replace(".md", "")
.replace("-", " ")
.replace("_", " ")
.strip()
.title()
)
if title == "":
title = "runpodctl"
helper_function = ""
with open(md_file, "r") as file:
lines = file.readlines()
with open(md_file, "w") as file:
file.write(generate_metadata_header(title).strip() + "\n")
for line in lines:
if "###### Auto generated by" not in line:
file.write(line)
def modify_markdown(file_path, title):
# Read the contents of the markdown file
with open(file_path, "r") as file:
content = file.read()
# Check if the title is blank or 'runpodctl'
if title == "" or title.lower() == "runpodctl":
# Insert the installation instructions after the metadata
metadata_end_index = content.find("---", content.find("---") + 1) + 3
# Formatting the install steps with a header
formatted_install_steps = "\n\n" + str(install_steps.install_steps)
content = (
content[:metadata_end_index]
+ formatted_install_steps
+ content[metadata_end_index:]
)
# Write the modified content back to the file
with open(file_path, "w") as file:
file.write(content)
# Example usage
modify_markdown("docs/runpodctl/reference/runpodctl.md", "runpodctl")
# 4. Clean up
print("Cleaning up...")
run_command("rm -rf runpodctl")
print("Script completed.")