forked from Crypto-TII/claasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_bash_script.py
72 lines (59 loc) · 2.23 KB
/
create_bash_script.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
import os
import sys
with open("docker/Dockerfile", "r") as f:
dockerfile_lines = f.readlines()
claasp_directory = os.path.abspath(".")
bash_instructions = ["#!/bin/bash"]
environment_variables = []
docker_command = ""
for line in dockerfile_lines:
line = line.strip()
if line.startswith("FROM claasp-base AS claasp-lib"):
break
is_a_comment = line.startswith("#")
if not line or is_a_comment:
continue
is_split_command = line.endswith("\\")
if is_split_command:
docker_command += line[:-1]
continue
docker_command += line
bash_instruction = ""
match docker_command.split()[0]:
case "RUN":
bash_instruction = docker_command.split("RUN")[1].strip()
case "ENV":
command = docker_command.split("ENV")[1].strip()
environment_variable = command.split("=")[0]
if environment_variable not in environment_variables:
environment_variables.append(environment_variable)
bash_instruction = f"export {command}"
case "ARG":
command = docker_command.split("ARG")[1].strip()
bash_instruction = f"export {command}"
case "WORKDIR":
directory = docker_command.split("WORKDIR")[1].strip()
if directory != '/home/sage/tii-claasp':
if os.path.exists(directory):
bash_instruction = f"cd {directory}"
else:
bash_instruction = f"mkdir {directory} && cd {directory}"
case "COPY":
command = docker_command.split("COPY")[1].strip()
src, dst = command.split()
src = f"{claasp_directory}{os.sep}{src}"
bash_instruction = f"cp {src} {dst}"
if os.path.isdir(src):
bash_instruction += " -r"
case _:
docker_command = ""
continue
if bash_instruction:
bash_instructions.append(bash_instruction)
docker_command = ""
for environment_variable in environment_variables:
bash_instructions.append(
f"echo 'export {environment_variable}='${environment_variable} >> ~/.bashrc"
)
with open("dependencies_script.sh", "w") as f:
f.write("\n\n".join(bash_instructions))