diff --git a/superide/exception.py b/superide/exception.py index ad400e48e48f08e6db6debfa3e5f970029eb5cfe..94aa7c37d97bdb06ef37b09c352938caa2a02b61 100644 --- a/superide/exception.py +++ b/superide/exception.py @@ -10,6 +10,7 @@ # IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR # PURPOSE. # See the AGPL-3.0 for more details. +from superide import __container_engine__ class SuperIDEException(Exception): @@ -35,9 +36,13 @@ class AbortedByUser(UserSideException): MESSAGE = "Aborted by user" class ContainerEngineNotFound(UserSideException): - MESSAGE = "ContainerEngine not to be found" + MESSAGE = f"ContainerEngine({__container_engine__}) not to be found" +class ImageNotGet(UserSideException): + MESSAGE = "Failed to get image." +class InitProjectError(UserSideException): + MESSAGE = "Failed to init project." # # UDEV Rules # diff --git a/superide/toolchain/toolchain.py b/superide/toolchain/toolchain.py index 3f9607e6d7e528425101bd22b343b8245afe86dd..6e76d4f7e2a5e25e2e48591fcf4b7e5197592f59 100644 --- a/superide/toolchain/toolchain.py +++ b/superide/toolchain/toolchain.py @@ -3,9 +3,11 @@ import subprocess import os import shutil import sys -from superide import __container_engine__ +from superide import __container_engine__, exception class Toolchain: + check_image_flag = False + def __init__(self, image_name, project_directory): check_container_engine_availability() self.image_name = image_name @@ -15,51 +17,37 @@ class Toolchain: def check_image(self): + if(Toolchain.check_image_flag): + return try: - output = subprocess.check_output([__container_engine__, "images", "-q", self.image_name]) + print('check image...') + output = subprocess.check_output(["docker", "images", "-q", self.image_name]) if output: + Toolchain.check_image_flag = True return - else: - print(f"The image '{self.image_name}' does not exist locally, will be pulled.") - # 拉取镜像 - subprocess.run([__container_engine__, 'pull', self.image_name]) - + subprocess.run([__container_engine__, 'pull', self.image_name], stderr=subprocess.DEVNULL) + Toolchain.check_image_flag = True except subprocess.CalledProcessError: - print("Failed to get image.") - sys.exit(1) + raise exception.ImageNotGet() def init_project(self): # 文件夹非空则不能创建项目 if(len(os.listdir(self.project_directory)) != 0): print("can't init project in Non empty folder") - sys.exit(1) - container_name = 'CopyExampleProjectDemo' - source_path = '/app/ExampleProject' - destination_path = self.project_directory + raise exception.InitProjectError() + + source_path = '/app/ExampleProject/.' + destination_path = self.contain_project_directory try: - # 创建容器 - create_command = [__container_engine__, 'create', '--name', container_name, self.image_name] - subprocess.run(create_command) - - # 复制文件 - copy_command = [__container_engine__, 'cp', f'{container_name}:{source_path}', destination_path] - subprocess.run(copy_command) - - # 删除容器(可选) - delete_command = [__container_engine__, 'rm', container_name] - subprocess.run(delete_command) - - # 移动到项目目录 - source_path = destination_path + '/ExampleProject/' - for file in os.listdir(source_path): - src_file = os.path.join(source_path, file) - dst_file = os.path.join(destination_path, file) - shutil.move(src_file, dst_file) - clear_command = ['rm', '-rf', source_path] - subprocess.run(clear_command) + command = [__container_engine__, "run","-it","--rm", "-v", self.project_directory+":"+self.contain_project_directory, self.image_name, + 'cp', '-r', source_path, destination_path]; + subprocess.run(command) + command = [__container_engine__, "run","-it","--rm", "-v", self.project_directory+":"+self.contain_project_directory, self.image_name, + 'chmod', '-R', '755', destination_path + '/.']; + subprocess.run(command) except Exception: print("init project failed") - sys.exit(1) + raise exception.InitProjectError() def _get_tools(self): with open(f'{self.project_directory}/.vscode/tasks.json') as file: @@ -104,8 +92,6 @@ def check_container_engine_availability(): try: subprocess.run([path, '--version'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except subprocess.CalledProcessError: - print(f"{__container_engine__} is not available.") - sys.exit(1) + raise exception.ContainerEngineNotFound() else: - print(f"{__container_engine__} not found on the system.") - sys.exit(1) \ No newline at end of file + raise exception.ContainerEngineNotFound() \ No newline at end of file