-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_scripts.py
54 lines (48 loc) · 2.06 KB
/
make_scripts.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
import os
import subprocess
import sys
def convert_qmd_files(folder_path):
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith(".qmd"):
qmd_file = os.path.join(root, file)
ipynb_file = qmd_file.replace(".qmd", ".ipynb")
command = f"quarto convert {qmd_file} --output {ipynb_file}"
try:
subprocess.run(command, check=True, shell=True)
print(f"Converted {qmd_file} to {ipynb_file}")
except subprocess.CalledProcessError as e:
print(f"Failed to convert {qmd_file}: {e}")
py_file = qmd_file.replace(".qmd", ".py")
command = f"jupytext --to script {ipynb_file}"
try:
subprocess.run(command, check=True, shell=True)
print(f"Converted {ipynb_file} to {py_file}")
except subprocess.CalledProcessError as e:
print(f"Failed to convert {py_file}: {e}")
def clean_ipynb_files(folder_path):
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith(".ipynb") or file.endswith(".py"):
script_file = os.path.join(root, file)
try:
os.remove(script_file)
print(f"Deleted {script_file}")
except OSError as e:
print(f"Failed to delete {script_file}: {e}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python make_scripts.py [--convert | --clean] <folder_path>")
sys.exit(1)
action = sys.argv[1]
folder_path = sys.argv[2]
if not os.path.isdir(folder_path):
print(f"{folder_path} is not a valid directory.")
sys.exit(1)
if action == "--convert":
convert_qmd_files(folder_path)
elif action == "--clean":
clean_ipynb_files(folder_path)
else:
print("Invalid option. Use --convert to convert files or --clean to delete .ipynb files.")
sys.exit(1)