-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_labbook.py
144 lines (129 loc) · 3.54 KB
/
create_labbook.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/python3
from __future__ import unicode_literals
from sys import argv
from sys import platform
import subprocess
import json
import os.path
import os
import glob
# Parse CLI args
if len(argv) < 6:
print("Usage:")
print("\tcreate_labbook.py directory title author editor command_name")
exit(1)
directory = argv[1]
title = argv[2]
author = argv[3]
editor = argv[4]
command_name = argv[5]
# Get home
home_dir = os.path.expanduser("~")
resources = home_dir + "/.labbook"
# Handle directory edgecase
if directory[-1] == "/":
directory = directory[:-1]
# Create general path reference for bash commands
if platform == "linux":
directory_bash = (
directory.replace("~/Documents", r"$(xdg-user-dir DOCUMENTS)")
.replace("~/documents", r"$(xdg-user-dir DOCUMENTS)")
.replace("~", r"$HOME")
)
else:
directory_bash = directory.replace("~", r"$HOME")
# Set up directory structure
subprocess.run(["mkdir", "-p", directory_bash])
subprocess.run(["mkdir", "-p", directory_bash + "/templates"])
subprocess.run(["mkdir", "-p", directory_bash + "/scripts"])
subprocess.run(["mkdir", "-p", directory_bash + "/entries"])
# Save config
config_data = {
"directory": directory_bash,
"title": title,
"author": author,
"editor": editor,
"command_name": command_name,
}
with open(directory + "/config.json", "w") as f:
json.dump(config_data, f)
# Move templates and apply author parameter
for template in glob.glob(resources + "/templates/*.md"):
file_name = template.split("/")[-1]
subprocess.run(
["cp", template, directory_bash + "/templates/" + file_name]
)
# cmd = " ".join(
# [
# "sed",
# "-i",
# "'s/AUTHOR/{}/g'".format(author),
# directory_bash + "/templates/" + file_name,
# ])
# print(cmd)
# subprocess.run(cmd)
subprocess.run(
[
"sed",
"-i",
"s/AUTHOR/{}/g".format(author),
directory_bash + "/templates/" + file_name,
]
)
# Move Python scripts
for script in glob.glob(resources + "/scripts/*.py"):
file_name = script.split("/")[-1]
subprocess.run(["cp", script, directory_bash + "/scripts/" + file_name])
# Move main bash script
subprocess.run(
[
"cp",
resources + "/scripts/editing_script.sh",
directory_bash + "/scripts/{}".format(command_name),
]
)
subprocess.run(
[
"sed",
"-i",
"s/DIRECTORY/{}/ ; s/EDITOR/{}/".format(
directory_bash.replace("/", "\\/"), editor
),
directory_bash + "/scripts/{}".format(command_name),
]
)
os.chmod(directory_bash + "/scripts/{}".format(command_name), 0o775)
# Copy main bash script to .local/bin
subprocess.run(
[
"cp",
directory_bash + "/scripts/{}".format(command_name),
home_dir + "/.local/bin/{}".format(command_name),
]
)
os.chmod(home_dir + "/.local/bin/{}".format(command_name), 0o775)
# Copy labbook template and apply author/title
subprocess.run(
[
"cp",
resources + "/entries/labbook.md",
directory_bash + "/entries/labbook.md",
]
)
subprocess.run(
[
"sed",
"-i",
"s/TITLE/{}/ ; s/AUTHOR/{}/".format(title, author),
directory_bash + "/entries/labbook.md",
]
)
# Copy Makefile and latex template
subprocess.run(["cp", resources + "/Makefile", directory_bash + "/Makefile"])
subprocess.run(
[
"cp",
resources + "/eisvogel_physics.tex",
directory_bash + "/eisvogel_physics.tex",
]
)