-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from Crypto-TII/fix/fix-create-bash-script-an…
…d-documentation Fix create_bash_script.py and documentation
- Loading branch information
Showing
4 changed files
with
61 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,72 @@ | ||
import os | ||
import sys | ||
|
||
with open('docker/Dockerfile', 'r') as f: | ||
|
||
with open("docker/Dockerfile", "r") as f: | ||
dockerfile_lines = f.readlines() | ||
|
||
bash_instructions = ['#!/bin/bash'] | ||
docker_command = '' | ||
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('#') | ||
is_split_command = line.endswith('\\') | ||
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 = '' | ||
if docker_command.startswith('RUN'): | ||
bash_instruction = docker_command.split('RUN')[1].strip() | ||
elif docker_command.startswith('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}' | ||
elif docker_command.startswith('ARG'): | ||
command = docker_command.split('ARG')[1].strip() | ||
bash_instruction = f'export {command}' | ||
elif docker_command.startswith('WORKDIR'): | ||
directory = docker_command.split('WORKDIR')[1].strip() | ||
if directory != '/home/sage/tii-claasp': | ||
bash_instruction = f'cd {directory}' | ||
elif docker_command.startswith('COPY'): | ||
command = docker_command.split('COPY')[1].strip() | ||
bash_instruction = f'cp {command}' | ||
else: | ||
docker_command = '' | ||
continue | ||
|
||
bash_instructions.append(bash_instruction) | ||
docker_command = '' | ||
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") | ||
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)) | ||
with open("dependencies_script.sh", "w") as f: | ||
f.write("\n\n".join(bash_instructions)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters