From a1db1d0577e5a1748ac95226203a0af835c1cc90 Mon Sep 17 00:00:00 2001 From: Peng Zhou Date: Fri, 31 Dec 2021 11:01:30 +0800 Subject: [PATCH 01/29] add install quingoc module --- src/quingo/core/install_quingoc.py | 221 +++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 src/quingo/core/install_quingoc.py diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py new file mode 100644 index 0000000..f4a7f6b --- /dev/null +++ b/src/quingo/core/install_quingoc.py @@ -0,0 +1,221 @@ +import re +import requests +import json +import distutils.spawn +import subprocess +import platform +import zipfile +import shutil +import tempfile +from pathlib import Path +from setuptools import setup +from setuptools.command.develop import develop +from setuptools.command.install import install + + +def set_path_env_on_Linux(install_path): + """Set path environment to contain the milr quingo compiler on Linux. + """ + quingoc_dir = install_path / 'bin' + + ret_value = subprocess.run("echo $PATH", stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + + if ret_value.returncode != 0: + raise RuntimeError( + "Failed to retrieve system path using 'echo $PATH'.") + + if str(quingoc_dir.absolute()) in ret_value.stdout: + return + + bash_profile = Path.home() / '.bash_profile' + bashrc = Path.home() / '.bashrc' + shell_cmd = "if [ -f \"{bashrc_path}\" ]\nthen\n \ + echo 'export PATH={quingoc_dir_path}:$PATH' >> {bashrc_path}\n \ + elif [ -f \"{bash_profile_path}\" ]\nthen\n \ + echo 'export PATH={quingoc_dir_path}:$PATH' >> {bash_profile_path}\n \ + fi\n" + set_env_cmd = shell_cmd.format( + bashrc_path=bashrc, bash_profile_path=bash_profile, quingoc_dir_path=quingoc_dir) + + ret_value = subprocess.run(set_env_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + + if(ret_value.returncode != 0): + raise RuntimeError("Failed add \"{}\" to path environment with the" + "following error: {}".format(quingoc_dir, ret_value.stderr)) + + print("Installed mlir quingo compiler at directory:{}".format(quingoc_dir)) + + +def install_on_Linux(mlir_compiler_path): + """Install quingo compiler on Linux. + """ + mlir_compiler_path.chmod(0o744) + quingoc_install_dir = Path.home() / '.local' + + mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( + mlir_compiler_path, quingoc_install_dir) + + print("mlir_compiler_install_cmd: ", mlir_compiler_install_cmd) + + ret_value = subprocess.run(mlir_compiler_install_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to install lastest quingo compiler with the" + "following error: {}".format(ret_value.stderr)) + + set_path_env_on_Linux(quingoc_install_dir) + + +def set_path_on_Windows(install_path): + """Set path environment to contain the milr quingo compiler on Windows. + """ + # Nothing to do, quingo-runtime will search the default install path + # Default path is Path.home() / '.quingo' + + return + + +def install_on_Windows(mlir_compiler_path): + """Install quingo compiler on Windows. + """ + + mlir_compiler_install_path = Path.home() / '.quingo' + + zip_file = zipfile.ZipFile(mlir_compiler_path) + zip_list = zip_file.namelist() + zip_file.extractall(mlir_compiler_install_path) + zip_file.close() + + files = [file for file in mlir_compiler_install_path.glob( + "*/*") if file.is_file()] + for file in files: + file.replace(mlir_compiler_install_path / file.name) + + set_path_on_Windows(mlir_compiler_install_path) + + +def install_compiler(os_name, mlir_compiler_path, old_version_path=None): + assert os_name in ['Linux', 'Windows', 'Darwin'] + if(os_name == 'Windows'): + install_on_Windows(mlir_compiler_path, old_version_path) + if(os_name == 'Linux'): + install_on_Linux(mlir_compiler_path, old_version_path) + if(os_name == 'Linux'): + install_on_Linux(mlir_compiler_path, old_version_path) + + +def download_compiler(os_name, tmp_dir_name): + assert os_name in ['Linux', 'Windows', 'Darwin'] + os_dl_suffix = { + 'Linux': '.sh', + 'Windows': '.zip', + 'Darwin': '.dmg' + } + dl_file_suffix = os_dl_suffix[os_name] + + latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( + owner="hpcl_quanta", repo="quingo-runtime") + + try: + latest_release = requests.get(latest_release_url).text + release_info = json.loads(latest_release) + assets = release_info['assets'] + quingoc_asset = None + for asset in assets: + if 'name' in asset and asset['name'].endswith(dl_file_suffix): + quingoc_asset = asset + break + if quingoc_asset is None: + raise RuntimeError( + "Failed to download quingo compiler from gitee.") + + quingoc_url = quingoc_asset['browser_download_url'] + \ + '/' + quingoc_asset['name'] + quingoc_response = requests.get(quingoc_url) + + mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] + with mlir_compiler_path.open('wb') as tmp_dl_file: + num_bytes = tmp_dl_file.write(quingoc_response.content) + print("installation file has been downloaded to tmp file {} ({} bytes). ".format( + mlir_compiler_path, num_bytes)) + return mlir_compiler_path + + except Exception as e: + raise RuntimeError("Failed to parse information retrieved from gitee with the " + "following error: {}".format(e)) + + +def download_and_install_latest_quingoc(old_version_path=None): + """Download the latest mlir quingo compiler. + """ + os_name = platform.system() + if os_name not in ['Linux', 'Windows', 'Darwin']: + raise SystemError( + "Currently pip installation does not support {}".format(os_name)) + + tmp_dir = tempfile.TemporaryDirectory() + tmp_dir_path = Path(tmp_dir.name) + + mlir_compiler_path = download_compiler(os_name, tmp_dir_path) + install_compiler(os_name, mlir_compiler_path, old_version_path) + + shutil.rmtree(tmp_dir_path) + + +def check_update(quingoc_path): + """Check local quingo compiler whether is latest version + """ + + latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( + owner="hpcl_quanta", repo="quingo-runtime") + + try: + latest_release = requests.get(latest_release_url).text + release_info = json.loads(latest_release) + find_version = re.search(r'\d+\.\d+\.\d+', release_info['tag_name']) + if find_version is not None: + lastest_version = find_version.group() + else: + raise RuntimeError( + "Failed to get lastest version of quingo compiler from gitee.") + + except Exception as e: + raise RuntimeError("Failed to parse information retrieved from gitee with the " + "following error: {}".format(e)) + + get_current_version_cmd = "{} --version".format(quingoc_path) + + ret_value = subprocess.run(get_current_version_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to get local quingo compiler version with the" + "following error: {}".format(ret_value.stderr)) + + find_version = re.search(r'\d+\.\d+\.\d+', ret_value.stdout.split()[0]) + if find_version is not None: + current_version = find_version.group() + else: + raise RuntimeError( + "Failed to get version of local quingo compiler from {}.".format(quingoc_path)) + + if current_version == lastest_version: + return False + else: + return True + + +if __name__ == "__main__": + default_path = Path.home() / '.quingo' + quingoc_path = distutils.spawn.find_executable( + 'quingoc', str(default_path)) + + if quingoc_path is None: + quingoc_path = distutils.spawn.find_executable('quingoc') + + if quingoc_path is None: + download_and_install_latest_quingoc() + else: + if check_update(quingoc_path): + download_and_install_latest_quingoc(quingoc_path) -- Gitee From 2e0863175c1bc0a56f38afa840a57ac78f5d80f4 Mon Sep 17 00:00:00 2001 From: Peng Zhou Date: Wed, 5 Jan 2022 14:21:04 +0800 Subject: [PATCH 02/29] add install on darwin --- src/quingo/core/install_quingoc.py | 106 +++++++++++++++++++++++++---- 1 file changed, 92 insertions(+), 14 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index f4a7f6b..d717efb 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -1,3 +1,4 @@ +import pathlib import re import requests import json @@ -7,16 +8,29 @@ import platform import zipfile import shutil import tempfile +import datetime from pathlib import Path from setuptools import setup from setuptools.command.develop import develop from setuptools.command.install import install +def backup_compiler(quingoc_path): + + backup_path = quingoc_path.parent / "quingoc_backup" + + if not backup_path.exists(): + backup_path.mkdir(exist_ok=True) + + backup_file = quingoc_path.name + \ + datetime.datetime.now().strftime('%F') + + shutil.move(quingoc_path, backup_path/backup_file) + + def set_path_env_on_Linux(install_path): """Set path environment to contain the milr quingo compiler on Linux. """ - quingoc_dir = install_path / 'bin' ret_value = subprocess.run("echo $PATH", stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True) @@ -25,7 +39,7 @@ def set_path_env_on_Linux(install_path): raise RuntimeError( "Failed to retrieve system path using 'echo $PATH'.") - if str(quingoc_dir.absolute()) in ret_value.stdout: + if str(install_path.absolute()) in ret_value.stdout: return bash_profile = Path.home() / '.bash_profile' @@ -36,26 +50,33 @@ def set_path_env_on_Linux(install_path): echo 'export PATH={quingoc_dir_path}:$PATH' >> {bash_profile_path}\n \ fi\n" set_env_cmd = shell_cmd.format( - bashrc_path=bashrc, bash_profile_path=bash_profile, quingoc_dir_path=quingoc_dir) + bashrc_path=bashrc, bash_profile_path=bash_profile, quingoc_dir_path=install_path) ret_value = subprocess.run(set_env_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True) if(ret_value.returncode != 0): raise RuntimeError("Failed add \"{}\" to path environment with the" - "following error: {}".format(quingoc_dir, ret_value.stderr)) + "following error: {}".format(install_path, ret_value.stderr)) - print("Installed mlir quingo compiler at directory:{}".format(quingoc_dir)) + print("Installed mlir quingo compiler at directory:{}".format(install_path)) -def install_on_Linux(mlir_compiler_path): +def install_on_Linux(mlir_compiler_path, old_version_path=None): """Install quingo compiler on Linux. """ + mlir_compiler_path.chmod(0o744) - quingoc_install_dir = Path.home() / '.local' + + if old_version_path is not True: + mlir_compiler_install_dir = old_version_path.parent + mlir_compiler_exec_path = mlir_compiler_install_dir + else: + mlir_compiler_install_dir = Path.home() / '.local' + mlir_compiler_exec_path = mlir_compiler_install_dir / 'bin' mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( - mlir_compiler_path, quingoc_install_dir) + mlir_compiler_path, mlir_compiler_install_dir) print("mlir_compiler_install_cmd: ", mlir_compiler_install_cmd) @@ -65,7 +86,7 @@ def install_on_Linux(mlir_compiler_path): raise RuntimeError("Failed to install lastest quingo compiler with the" "following error: {}".format(ret_value.stderr)) - set_path_env_on_Linux(quingoc_install_dir) + set_path_env_on_Linux(mlir_compiler_exec_path) def set_path_on_Windows(install_path): @@ -77,11 +98,14 @@ def set_path_on_Windows(install_path): return -def install_on_Windows(mlir_compiler_path): +def install_on_Windows(mlir_compiler_path, old_version_path=None): """Install quingo compiler on Windows. """ - mlir_compiler_install_path = Path.home() / '.quingo' + if old_version_path is not None: + mlir_compiler_install_path = old_version_path.parent + else: + mlir_compiler_install_path = Path.home() / '.quingo' zip_file = zipfile.ZipFile(mlir_compiler_path) zip_list = zip_file.namelist() @@ -96,14 +120,68 @@ def install_on_Windows(mlir_compiler_path): set_path_on_Windows(mlir_compiler_install_path) +def set_path_env_on_Darwin(install_path): + """Set path environment to contain the milr quingo compiler on Darwin. + """ + + set_path_env_on_Linux(install_path) + + +def install_on_Darwin(mlir_compiler_path, old_version_path=None): + """Install quingo compiler on Darwin. + """ + + if old_version_path is not None: + mlir_compiler_install_path = old_version_path.parent + mlir_compiler_exec_path = mlir_compiler_install_path + else: + mlir_compiler_install_path = Path.home() / '.local' + mlir_compiler_exec_path = mlir_compiler_install_path / 'bin' + + if not mlir_compiler_exec_path.exists(): + mlir_compiler_exec_path.mkdir(exist_ok=True) + + # mount dmg file + mount_cmd = "hdiutil attach " + str(mlir_compiler_path) + + ret_value = subprocess.run(mount_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to mount lastest quingo compiler dmg file with the" + "following error: {}".format(ret_value.stderr)) + + mlir_compiler_exec_file = pathlib.path( + "/Volumes/" + mlir_compiler_path.name) / 'quingoc.app' / 'Contents' / 'Resources' / 'bin' / 'quingoc' + + shutil.move(mlir_compiler_exec_file, mlir_compiler_exec_path) + + # umount dmg file + umount_cmd = "hdiutil detach " + str("/Volumes/" + mlir_compiler_path.name) + + ret_value = subprocess.run(umount_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to umount lastest quingo compiler dmg file with the" + "following error: {}".format(ret_value.stderr)) + + set_path_env_on_Darwin(mlir_compiler_exec_path) + + def install_compiler(os_name, mlir_compiler_path, old_version_path=None): assert os_name in ['Linux', 'Windows', 'Darwin'] + + if old_version_path is not None: + backup_compiler(old_version_path) + if(os_name == 'Windows'): - install_on_Windows(mlir_compiler_path, old_version_path) + install_on_Windows(mlir_compiler_path, + old_version_path) if(os_name == 'Linux'): - install_on_Linux(mlir_compiler_path, old_version_path) + install_on_Linux(mlir_compiler_path, + old_version_path) if(os_name == 'Linux'): - install_on_Linux(mlir_compiler_path, old_version_path) + install_on_Linux(mlir_compiler_path, + old_version_path) def download_compiler(os_name, tmp_dir_name): -- Gitee From 4cf5b9c7b030240582ef557a3151da19231f51f4 Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Wed, 5 Jan 2022 15:36:56 +0800 Subject: [PATCH 03/29] fixed bugs on windows --- src/quingo/core/install_quingoc.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index d717efb..eb87be8 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -10,10 +10,6 @@ import shutil import tempfile import datetime from pathlib import Path -from setuptools import setup -from setuptools.command.develop import develop -from setuptools.command.install import install - def backup_compiler(quingoc_path): @@ -112,11 +108,6 @@ def install_on_Windows(mlir_compiler_path, old_version_path=None): zip_file.extractall(mlir_compiler_install_path) zip_file.close() - files = [file for file in mlir_compiler_install_path.glob( - "*/*") if file.is_file()] - for file in files: - file.replace(mlir_compiler_install_path / file.name) - set_path_on_Windows(mlir_compiler_install_path) @@ -296,4 +287,4 @@ if __name__ == "__main__": download_and_install_latest_quingoc() else: if check_update(quingoc_path): - download_and_install_latest_quingoc(quingoc_path) + download_and_install_latest_quingoc(pathlib.Path(quingoc_path)) -- Gitee From 5d5d1f2c0fa30ded5698de5e2a6f94f0a341d5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E6=9C=8B?= Date: Wed, 5 Jan 2022 19:41:41 +0800 Subject: [PATCH 04/29] fix bugs on macos --- src/quingo/core/install_quingoc.py | 44 +++++++++++++++++------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index eb87be8..d0e116a 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -132,28 +132,34 @@ def install_on_Darwin(mlir_compiler_path, old_version_path=None): if not mlir_compiler_exec_path.exists(): mlir_compiler_exec_path.mkdir(exist_ok=True) - # mount dmg file - mount_cmd = "hdiutil attach " + str(mlir_compiler_path) + try: + # mount dmg file + mount_cmd = "hdiutil attach " + str(mlir_compiler_path) - ret_value = subprocess.run(mount_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - if(ret_value.returncode != 0): - raise RuntimeError("Failed to mount lastest quingo compiler dmg file with the" - "following error: {}".format(ret_value.stderr)) + ret_value = subprocess.run(mount_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to mount lastest quingo compiler dmg file with the" + "following error: {}".format(ret_value.stderr)) - mlir_compiler_exec_file = pathlib.path( - "/Volumes/" + mlir_compiler_path.name) / 'quingoc.app' / 'Contents' / 'Resources' / 'bin' / 'quingoc' + mlir_compiler_exec_file = pathlib.Path( + "/Volumes/" + mlir_compiler_path.stem) / 'quingoc.app' / 'Contents' / 'Resources' / 'bin' / 'quingoc' - shutil.move(mlir_compiler_exec_file, mlir_compiler_exec_path) + shutil.copy(str(mlir_compiler_exec_file), str(mlir_compiler_exec_path)) + + except Exception as e: + raise RuntimeError("Failed to copy quingo compiler {} to '{}' with the " + "following error: {}".format(mlir_compiler_exec_file, mlir_compiler_exec_path, e)) - # umount dmg file - umount_cmd = "hdiutil detach " + str("/Volumes/" + mlir_compiler_path.name) + finally: + # umount dmg file + umount_cmd = "hdiutil detach " + str("/Volumes/" + mlir_compiler_path.stem) - ret_value = subprocess.run(umount_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - if(ret_value.returncode != 0): - raise RuntimeError("Failed to umount lastest quingo compiler dmg file with the" - "following error: {}".format(ret_value.stderr)) + ret_value = subprocess.run(umount_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to umount lastest quingo compiler dmg file with the" + "following error: {}".format(ret_value.stderr)) set_path_env_on_Darwin(mlir_compiler_exec_path) @@ -170,8 +176,8 @@ def install_compiler(os_name, mlir_compiler_path, old_version_path=None): if(os_name == 'Linux'): install_on_Linux(mlir_compiler_path, old_version_path) - if(os_name == 'Linux'): - install_on_Linux(mlir_compiler_path, + if(os_name == 'Darwin'): + install_on_Darwin(mlir_compiler_path, old_version_path) -- Gitee From 9f064b65ba720d93379ba5e17190f147629134ff Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Wed, 5 Jan 2022 20:51:32 +0800 Subject: [PATCH 05/29] fix bugs in ubuntu --- src/quingo/core/install_quingoc.py | 32 +++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index d0e116a..58a2241 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -64,12 +64,11 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): mlir_compiler_path.chmod(0o744) - if old_version_path is not True: + if old_version_path is not None: mlir_compiler_install_dir = old_version_path.parent - mlir_compiler_exec_path = mlir_compiler_install_dir else: mlir_compiler_install_dir = Path.home() / '.local' - mlir_compiler_exec_path = mlir_compiler_install_dir / 'bin' + mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( mlir_compiler_path, mlir_compiler_install_dir) @@ -81,6 +80,12 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): if(ret_value.returncode != 0): raise RuntimeError("Failed to install lastest quingo compiler with the" "following error: {}".format(ret_value.stderr)) + + if old_version_path is not None: + mlir_compiler_exec_path = mlir_compiler_install_dir + shutil.copy(str(mlir_compiler_install_dir/ 'bin' / 'quingoc'), str(mlir_compiler_exec_path)) + else: + mlir_compiler_exec_path = mlir_compiler_install_dir / 'bin' set_path_env_on_Linux(mlir_compiler_exec_path) @@ -238,9 +243,8 @@ def download_and_install_latest_quingoc(old_version_path=None): shutil.rmtree(tmp_dir_path) - -def check_update(quingoc_path): - """Check local quingo compiler whether is latest version +def get_lastest_version(): + """Get lastest quingo compiler version """ latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( @@ -260,6 +264,13 @@ def check_update(quingoc_path): raise RuntimeError("Failed to parse information retrieved from gitee with the " "following error: {}".format(e)) + return lastest_version + + +def get_current_version(): + """Get current quingo compiler version + """ + get_current_version_cmd = "{} --version".format(quingoc_path) ret_value = subprocess.run(get_current_version_cmd, stdout=subprocess.PIPE, @@ -275,7 +286,14 @@ def check_update(quingoc_path): raise RuntimeError( "Failed to get version of local quingo compiler from {}.".format(quingoc_path)) - if current_version == lastest_version: + return current_version + + +def check_update(quingoc_path): + """Check local quingo compiler whether is latest version + """ + return True + if get_current_version() == get_lastest_version(): return False else: return True -- Gitee From fc38a8e453d549de8ddbde025cb05b3c0b3686c4 Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Wed, 5 Jan 2022 22:19:45 +0800 Subject: [PATCH 06/29] fix bugs for windows --- src/quingo/core/install_quingoc.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index 58a2241..fe12ace 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -113,6 +113,15 @@ def install_on_Windows(mlir_compiler_path, old_version_path=None): zip_file.extractall(mlir_compiler_install_path) zip_file.close() + mlir_compiler_extract_path = mlir_compiler_install_path / mlir_compiler_path.stem + + files = [file for file in mlir_compiler_extract_path.glob( + "bin/*") if file.is_file()] + for file in files: + shutil.copy(file, mlir_compiler_install_path) + + shutil.rmtree(mlir_compiler_extract_path) + set_path_on_Windows(mlir_compiler_install_path) @@ -243,6 +252,7 @@ def download_and_install_latest_quingoc(old_version_path=None): shutil.rmtree(tmp_dir_path) + def get_lastest_version(): """Get lastest quingo compiler version """ @@ -292,11 +302,12 @@ def get_current_version(): def check_update(quingoc_path): """Check local quingo compiler whether is latest version """ - return True - if get_current_version() == get_lastest_version(): - return False - else: - return True + + #if get_current_version() == get_lastest_version(): + # return False + #else: + # return True + return True # set true while quingoc can not get version at this time if __name__ == "__main__": -- Gitee From bc54e95e5a3c19e6ec450d2fe95160a65b54d0d6 Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Wed, 5 Jan 2022 22:47:40 +0800 Subject: [PATCH 07/29] tempdir will auto delete --- src/quingo/core/install_quingoc.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index fe12ace..5468c01 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -250,8 +250,6 @@ def download_and_install_latest_quingoc(old_version_path=None): mlir_compiler_path = download_compiler(os_name, tmp_dir_path) install_compiler(os_name, mlir_compiler_path, old_version_path) - shutil.rmtree(tmp_dir_path) - def get_lastest_version(): """Get lastest quingo compiler version -- Gitee From 3a5dc7cadd03af86178f31387efeb3b9acec417e Mon Sep 17 00:00:00 2001 From: Peng Zhou Date: Thu, 6 Jan 2022 16:42:55 +0800 Subject: [PATCH 08/29] add print information during install --- src/quingo/core/install_quingoc.py | 40 +++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index 5468c01..a8a795b 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -11,6 +11,7 @@ import tempfile import datetime from pathlib import Path + def backup_compiler(quingoc_path): backup_path = quingoc_path.parent / "quingoc_backup" @@ -23,6 +24,9 @@ def backup_compiler(quingoc_path): shutil.move(quingoc_path, backup_path/backup_file) + print("Local quingoc has been backup at \"{}\".".format( + backup_path)) + def set_path_env_on_Linux(install_path): """Set path environment to contain the milr quingo compiler on Linux. @@ -55,8 +59,6 @@ def set_path_env_on_Linux(install_path): raise RuntimeError("Failed add \"{}\" to path environment with the" "following error: {}".format(install_path, ret_value.stderr)) - print("Installed mlir quingo compiler at directory:{}".format(install_path)) - def install_on_Linux(mlir_compiler_path, old_version_path=None): """Install quingo compiler on Linux. @@ -68,7 +70,6 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): mlir_compiler_install_dir = old_version_path.parent else: mlir_compiler_install_dir = Path.home() / '.local' - mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( mlir_compiler_path, mlir_compiler_install_dir) @@ -80,15 +81,19 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): if(ret_value.returncode != 0): raise RuntimeError("Failed to install lastest quingo compiler with the" "following error: {}".format(ret_value.stderr)) - + if old_version_path is not None: mlir_compiler_exec_path = mlir_compiler_install_dir - shutil.copy(str(mlir_compiler_install_dir/ 'bin' / 'quingoc'), str(mlir_compiler_exec_path)) + shutil.copy(str(mlir_compiler_install_dir / 'bin' / + 'quingoc'), str(mlir_compiler_exec_path)) else: mlir_compiler_exec_path = mlir_compiler_install_dir / 'bin' set_path_env_on_Linux(mlir_compiler_exec_path) + print("Lastest quingoc has been installed at \"{}\".".format( + mlir_compiler_exec_path)) + def set_path_on_Windows(install_path): """Set path environment to contain the milr quingo compiler on Windows. @@ -124,6 +129,9 @@ def install_on_Windows(mlir_compiler_path, old_version_path=None): set_path_on_Windows(mlir_compiler_install_path) + print("Lastest quingoc has been installed at \"{}\".".format( + mlir_compiler_install_path)) + def set_path_env_on_Darwin(install_path): """Set path environment to contain the milr quingo compiler on Darwin. @@ -160,14 +168,15 @@ def install_on_Darwin(mlir_compiler_path, old_version_path=None): "/Volumes/" + mlir_compiler_path.stem) / 'quingoc.app' / 'Contents' / 'Resources' / 'bin' / 'quingoc' shutil.copy(str(mlir_compiler_exec_file), str(mlir_compiler_exec_path)) - + except Exception as e: raise RuntimeError("Failed to copy quingo compiler {} to '{}' with the " "following error: {}".format(mlir_compiler_exec_file, mlir_compiler_exec_path, e)) finally: # umount dmg file - umount_cmd = "hdiutil detach " + str("/Volumes/" + mlir_compiler_path.stem) + umount_cmd = "hdiutil detach " + \ + str("/Volumes/" + mlir_compiler_path.stem) ret_value = subprocess.run(umount_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True) @@ -177,6 +186,9 @@ def install_on_Darwin(mlir_compiler_path, old_version_path=None): set_path_env_on_Darwin(mlir_compiler_exec_path) + print("Lastest quingoc has been installed at \"{}\".".format( + mlir_compiler_exec_path)) + def install_compiler(os_name, mlir_compiler_path, old_version_path=None): assert os_name in ['Linux', 'Windows', 'Darwin'] @@ -192,7 +204,7 @@ def install_compiler(os_name, mlir_compiler_path, old_version_path=None): old_version_path) if(os_name == 'Darwin'): install_on_Darwin(mlir_compiler_path, - old_version_path) + old_version_path) def download_compiler(os_name, tmp_dir_name): @@ -275,7 +287,7 @@ def get_lastest_version(): return lastest_version -def get_current_version(): +def get_current_version(quingoc_path): """Get current quingo compiler version """ @@ -301,11 +313,15 @@ def check_update(quingoc_path): """Check local quingo compiler whether is latest version """ - #if get_current_version() == get_lastest_version(): + lastest_version = get_lastest_version() + # current_version = get_current_version(quingoc_path) + # if current_version == lastest_version: + # print("Local quingoc is already the lastest version.") # return False - #else: + # else: + # print("Local quingoc version ({}) is behind lastest version ({}). Update now.".format(current_version, lastest_version)) # return True - return True # set true while quingoc can not get version at this time + return True # set true while quingoc can not get version at this time if __name__ == "__main__": -- Gitee From 95736c547333ed1ccd2855ff5cc259ed0139f08c Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Thu, 6 Jan 2022 17:30:16 +0800 Subject: [PATCH 09/29] add progress bar --- src/quingo/core/install_quingoc.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index a8a795b..d5d34d5 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -1,5 +1,6 @@ import pathlib import re +import math import requests import json import distutils.spawn @@ -9,6 +10,7 @@ import zipfile import shutil import tempfile import datetime +import tqdm from pathlib import Path @@ -234,13 +236,17 @@ def download_compiler(os_name, tmp_dir_name): quingoc_url = quingoc_asset['browser_download_url'] + \ '/' + quingoc_asset['name'] - quingoc_response = requests.get(quingoc_url) + quingoc_response = requests.get(quingoc_url, stream=True) + data_size = math.ceil(int(quingoc_response.headers['Content-Length'])/1024/1024) mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] with mlir_compiler_path.open('wb') as tmp_dl_file: - num_bytes = tmp_dl_file.write(quingoc_response.content) + for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024*1024), total=data_size, desc='Download', unit='MB'): + tmp_dl_file.write(data) + print("installation file has been downloaded to tmp file {} ({} bytes). ".format( - mlir_compiler_path, num_bytes)) + mlir_compiler_path, quingoc_response.headers['Content-Length'])) + return mlir_compiler_path except Exception as e: -- Gitee From cc387997b7e6a7db2ef11151a545c5f8c237b8a4 Mon Sep 17 00:00:00 2001 From: Peng Zhou Date: Fri, 7 Jan 2022 09:30:18 +0800 Subject: [PATCH 10/29] update repo url to newest --- README.md | 4 ++-- README_ZH.md | 4 ++-- setup.cfg | 4 ++-- setup.py | 20 +++++++++++++------- src/quingo/core/compiler_config.py | 2 +- src/quingo/core/manager.py | 26 +++++++++++++++++--------- 6 files changed, 37 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 19d0d3f..25c4f8b 100755 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The Quingo installation comprises of two main steps: ### Install the Runtime system and simulator Install Quingo runtime system with required simulators using the following command: ```sh -git clone https://gitee.com/hpcl_quanta/quingo-runtime.git +git clone https://gitee.com/quingo/quingo-runtime.git cd /your/download/path/quingo-runtime/ pip install -e . ``` @@ -72,4 +72,4 @@ The `Quingo_interface` class expose the following methods: - For QCIS-based backend, the result format is defined by PyQCISim. Please refer to the docstring of `quingo.if_backend.non_arch_backend.pyqcisim_quantumsim.PyQCISim_quantumsim::execute()` ## Quingo programming tutorial -At present, Qingguo runtime system has included sample programs such as `Bell_state`, `GHZ`, `VQE`, etc. Details can be found [here](https://gitee.com/hpcl_quanta/quingo-runtime/tree/master/src/examples). \ No newline at end of file +At present, Qingguo runtime system has included sample programs such as `Bell_state`, `GHZ`, `VQE`, etc. Details can be found [here](https://gitee.com/quingo/quingo-runtime/tree/master/src/examples). \ No newline at end of file diff --git a/README_ZH.md b/README_ZH.md index 27afb41..955697d 100755 --- a/README_ZH.md +++ b/README_ZH.md @@ -10,7 +10,7 @@ 依次执行以下命令便可以安装青果运行时系统、PyQCAS模拟器以及PyQCISim模拟器。 ```sh -git clone https://gitee.com/hpcl_quanta/quingo-runtime.git +git clone https://gitee.com/quingo/quingo-runtime.git cd /your/download/path/quingo-runtime/ pip install -e . ``` @@ -71,4 +71,4 @@ The result of bell_state is: - 对于能够执行QCIS指令的后端,结果的格式由PyQCISim进行定义。详情请参考`quingo.if_backend.non_arch_backend.pyqcisim_quantumsim.PyQCISim_quantumsim::execute()`中的文档描述。 ## 青果示例程序 -目前青果运行时系统中已经包含了`Bell_state`、`GHZ`、`VQE`等示例程序,详情可见[此处](https://gitee.com/hpcl_quanta/quingo-runtime/tree/master/src/examples)。 \ No newline at end of file +目前青果运行时系统中已经包含了`Bell_state`、`GHZ`、`VQE`等示例程序,详情可见[此处](https://gitee.com/quingo/quingo-runtime/tree/master/src/examples)。 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 7b48b30..b4678be 100644 --- a/setup.cfg +++ b/setup.cfg @@ -7,9 +7,9 @@ use_2to3 = False description = Quingo Runtime System long_description = file: README.md long_description_content_type = text/markdown -url = https://gitee.com/hpcl_quanta/quingo-runtime +url = https://gitee.com/quingo/quingo-runtime project_urls = - Bug Tracker = https://gitee.com/hpcl_quanta/quingo-runtime/issues + Bug Tracker = https://gitee.com/quingo/quingo-runtime/issues classifiers = Programming Language :: Python :: 3 :: Only License :: OSI Approved :: Apache Software License diff --git a/setup.py b/setup.py index 65db303..0c25b09 100755 --- a/setup.py +++ b/setup.py @@ -21,7 +21,8 @@ def set_path_env_on_Linux(install_path): stderr=subprocess.PIPE, text=True, shell=True) if ret_value.returncode != 0: - raise RuntimeError("Failed to retrieve system path using 'echo $PATH'.") + raise RuntimeError( + "Failed to retrieve system path using 'echo $PATH'.") if str(quingoc_dir.absolute()) in ret_value.stdout: return @@ -86,7 +87,8 @@ def install_on_Windows(mlir_compiler_path): zip_file.extractall(mlir_compiler_install_path) zip_file.close() - files = [file for file in mlir_compiler_install_path.glob("*/*") if file.is_file()] + files = [file for file in mlir_compiler_install_path.glob( + "*/*") if file.is_file()] for file in files: file.replace(mlir_compiler_install_path / file.name) @@ -110,7 +112,7 @@ def download_compiler(os_name, tmp_dir_name): dl_file_suffix = os_dl_suffix[os_name] latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( - owner="hpcl_quanta", repo="quingo-runtime") + owner="quingo", repo="quingo-runtime") try: latest_release = requests.get(latest_release_url).text @@ -122,9 +124,11 @@ def download_compiler(os_name, tmp_dir_name): quingoc_asset = asset break if quingoc_asset is None: - raise RuntimeError("Failed to download quingo compiler from gitee.") + raise RuntimeError( + "Failed to download quingo compiler from gitee.") - quingoc_url = quingoc_asset['browser_download_url'] + '/' + quingoc_asset['name'] + quingoc_url = quingoc_asset['browser_download_url'] + \ + '/' + quingoc_asset['name'] quingoc_response = requests.get(quingoc_url) mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] @@ -144,7 +148,8 @@ def download_and_install_latest_quingoc(): """ os_name = platform.system() if os_name not in ['Linux', 'Windows']: - raise SystemError("Currently pip installation does not support {}".format(os_name)) + raise SystemError( + "Currently pip installation does not support {}".format(os_name)) tmp_dir = tempfile.TemporaryDirectory() tmp_dir_path = Path(tmp_dir.name) @@ -164,7 +169,8 @@ def friendly(command_subclass): def modified_run(self): default_path = Path.home() / '.quingo' - quingoc_path = distutils.spawn.find_executable('quingoc', str(default_path)) + quingoc_path = distutils.spawn.find_executable( + 'quingoc', str(default_path)) if quingoc_path is None: quingoc_path = distutils.spawn.find_executable('quingoc') diff --git a/src/quingo/core/compiler_config.py b/src/quingo/core/compiler_config.py index f0a66d8..8d1bee8 100644 --- a/src/quingo/core/compiler_config.py +++ b/src/quingo/core/compiler_config.py @@ -32,7 +32,7 @@ def set_compiler_path(path_str, is_mlir=False): def download_latest_quingoc(): latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( - owner="hpcl_quanta", repo="quingo-runtime") + owner="quingo", repo="quingo-runtime") try: latest_release = get_text(latest_release_url) diff --git a/src/quingo/core/manager.py b/src/quingo/core/manager.py index d0013f1..d9c8a59 100755 --- a/src/quingo/core/manager.py +++ b/src/quingo/core/manager.py @@ -135,7 +135,8 @@ class Runtime_system_manager(): - mlir """ if (compiler_name not in self.supported_compilers): - raise ValueError("Found unsupported compiler: {}".format(compiler_name)) + raise ValueError( + "Found unsupported compiler: {}".format(compiler_name)) self.compiler_name = compiler_name @@ -182,7 +183,8 @@ class Runtime_system_manager(): try: self.backend = backend_hub.get_instance(backend_name) except Exception as e: - quingo_err("Cannot connect backend '{}' with the following error:".format(backend_name)) + quingo_err( + "Cannot connect backend '{}' with the following error:".format(backend_name)) quingo_err("{}".format(e)) quingo_info("To fix this problem, you could explicitly connect another " "backend use the the following method: \n" @@ -318,13 +320,16 @@ class Runtime_system_manager(): "The backend {} is not available.".format(backend.name())) if self.mode == 'state_vector' and not backend.is_simulator(): - raise ValueError("Cannot retrieve state vector from a non-simulator backend.") + raise ValueError( + "Cannot retrieve state vector from a non-simulator backend.") if self.verbose: - quingo_msg("Uploading the program to the backend {}...".format(backend.name())) + quingo_msg( + "Uploading the program to the backend {}...".format(backend.name())) if not backend.upload_program(self.qasm_file_path): - quingo_err("Failed to upload the program to the backend {}.".format(backend.name())) + quingo_err( + "Failed to upload the program to the backend {}.".format(backend.name())) quingo_info(" Suggestion: are you uploading QCIS program to an eQASM backend " "or eQASM program to a QCIS backend?\n" " If so, please specify the compiler and backend accordingly.") @@ -359,10 +364,11 @@ class Runtime_system_manager(): if compiler_name == 'mlir': quingoc_path = get_mlir_path() if quingoc_path is None: - quingo_err("Cannot find the mlir-based quingoc compiler in the system path.") + quingo_err( + "Cannot find the mlir-based quingoc compiler in the system path.") quingo_info( "To resolve this problem, you can download quingoc from " - "https://gitee.com/hpcl_quanta/quingo-runtime/releases and save " + "https://gitee.com/quingo/quingo-runtime/releases and save " "it at a directory in the system path \n" "or configure its path by calling this method inside python:\n" " `quingo.quingo_interface.set_mlir_compiler_path()`") @@ -432,8 +438,10 @@ class Runtime_system_manager(): compile_files.extend(user_files) compile_files.extend(default_files) - logger.debug(self.compose_xtext_cmd(compile_cmd_head, compile_files, print=True)) - compile_cmd = self.compose_xtext_cmd(compile_cmd_head, compile_files, False) + logger.debug(self.compose_xtext_cmd( + compile_cmd_head, compile_files, print=True)) + compile_cmd = self.compose_xtext_cmd( + compile_cmd_head, compile_files, False) else: raise ValueError("Found undefined compiler to use.") -- Gitee From 25157b418680dfbd0b187280b5f600f12789c6ac Mon Sep 17 00:00:00 2001 From: Peng Zhou Date: Fri, 31 Dec 2021 11:01:30 +0800 Subject: [PATCH 11/29] add install quingoc module --- src/quingo/core/install_quingoc.py | 221 +++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 src/quingo/core/install_quingoc.py diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py new file mode 100644 index 0000000..f4a7f6b --- /dev/null +++ b/src/quingo/core/install_quingoc.py @@ -0,0 +1,221 @@ +import re +import requests +import json +import distutils.spawn +import subprocess +import platform +import zipfile +import shutil +import tempfile +from pathlib import Path +from setuptools import setup +from setuptools.command.develop import develop +from setuptools.command.install import install + + +def set_path_env_on_Linux(install_path): + """Set path environment to contain the milr quingo compiler on Linux. + """ + quingoc_dir = install_path / 'bin' + + ret_value = subprocess.run("echo $PATH", stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + + if ret_value.returncode != 0: + raise RuntimeError( + "Failed to retrieve system path using 'echo $PATH'.") + + if str(quingoc_dir.absolute()) in ret_value.stdout: + return + + bash_profile = Path.home() / '.bash_profile' + bashrc = Path.home() / '.bashrc' + shell_cmd = "if [ -f \"{bashrc_path}\" ]\nthen\n \ + echo 'export PATH={quingoc_dir_path}:$PATH' >> {bashrc_path}\n \ + elif [ -f \"{bash_profile_path}\" ]\nthen\n \ + echo 'export PATH={quingoc_dir_path}:$PATH' >> {bash_profile_path}\n \ + fi\n" + set_env_cmd = shell_cmd.format( + bashrc_path=bashrc, bash_profile_path=bash_profile, quingoc_dir_path=quingoc_dir) + + ret_value = subprocess.run(set_env_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + + if(ret_value.returncode != 0): + raise RuntimeError("Failed add \"{}\" to path environment with the" + "following error: {}".format(quingoc_dir, ret_value.stderr)) + + print("Installed mlir quingo compiler at directory:{}".format(quingoc_dir)) + + +def install_on_Linux(mlir_compiler_path): + """Install quingo compiler on Linux. + """ + mlir_compiler_path.chmod(0o744) + quingoc_install_dir = Path.home() / '.local' + + mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( + mlir_compiler_path, quingoc_install_dir) + + print("mlir_compiler_install_cmd: ", mlir_compiler_install_cmd) + + ret_value = subprocess.run(mlir_compiler_install_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to install lastest quingo compiler with the" + "following error: {}".format(ret_value.stderr)) + + set_path_env_on_Linux(quingoc_install_dir) + + +def set_path_on_Windows(install_path): + """Set path environment to contain the milr quingo compiler on Windows. + """ + # Nothing to do, quingo-runtime will search the default install path + # Default path is Path.home() / '.quingo' + + return + + +def install_on_Windows(mlir_compiler_path): + """Install quingo compiler on Windows. + """ + + mlir_compiler_install_path = Path.home() / '.quingo' + + zip_file = zipfile.ZipFile(mlir_compiler_path) + zip_list = zip_file.namelist() + zip_file.extractall(mlir_compiler_install_path) + zip_file.close() + + files = [file for file in mlir_compiler_install_path.glob( + "*/*") if file.is_file()] + for file in files: + file.replace(mlir_compiler_install_path / file.name) + + set_path_on_Windows(mlir_compiler_install_path) + + +def install_compiler(os_name, mlir_compiler_path, old_version_path=None): + assert os_name in ['Linux', 'Windows', 'Darwin'] + if(os_name == 'Windows'): + install_on_Windows(mlir_compiler_path, old_version_path) + if(os_name == 'Linux'): + install_on_Linux(mlir_compiler_path, old_version_path) + if(os_name == 'Linux'): + install_on_Linux(mlir_compiler_path, old_version_path) + + +def download_compiler(os_name, tmp_dir_name): + assert os_name in ['Linux', 'Windows', 'Darwin'] + os_dl_suffix = { + 'Linux': '.sh', + 'Windows': '.zip', + 'Darwin': '.dmg' + } + dl_file_suffix = os_dl_suffix[os_name] + + latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( + owner="hpcl_quanta", repo="quingo-runtime") + + try: + latest_release = requests.get(latest_release_url).text + release_info = json.loads(latest_release) + assets = release_info['assets'] + quingoc_asset = None + for asset in assets: + if 'name' in asset and asset['name'].endswith(dl_file_suffix): + quingoc_asset = asset + break + if quingoc_asset is None: + raise RuntimeError( + "Failed to download quingo compiler from gitee.") + + quingoc_url = quingoc_asset['browser_download_url'] + \ + '/' + quingoc_asset['name'] + quingoc_response = requests.get(quingoc_url) + + mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] + with mlir_compiler_path.open('wb') as tmp_dl_file: + num_bytes = tmp_dl_file.write(quingoc_response.content) + print("installation file has been downloaded to tmp file {} ({} bytes). ".format( + mlir_compiler_path, num_bytes)) + return mlir_compiler_path + + except Exception as e: + raise RuntimeError("Failed to parse information retrieved from gitee with the " + "following error: {}".format(e)) + + +def download_and_install_latest_quingoc(old_version_path=None): + """Download the latest mlir quingo compiler. + """ + os_name = platform.system() + if os_name not in ['Linux', 'Windows', 'Darwin']: + raise SystemError( + "Currently pip installation does not support {}".format(os_name)) + + tmp_dir = tempfile.TemporaryDirectory() + tmp_dir_path = Path(tmp_dir.name) + + mlir_compiler_path = download_compiler(os_name, tmp_dir_path) + install_compiler(os_name, mlir_compiler_path, old_version_path) + + shutil.rmtree(tmp_dir_path) + + +def check_update(quingoc_path): + """Check local quingo compiler whether is latest version + """ + + latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( + owner="hpcl_quanta", repo="quingo-runtime") + + try: + latest_release = requests.get(latest_release_url).text + release_info = json.loads(latest_release) + find_version = re.search(r'\d+\.\d+\.\d+', release_info['tag_name']) + if find_version is not None: + lastest_version = find_version.group() + else: + raise RuntimeError( + "Failed to get lastest version of quingo compiler from gitee.") + + except Exception as e: + raise RuntimeError("Failed to parse information retrieved from gitee with the " + "following error: {}".format(e)) + + get_current_version_cmd = "{} --version".format(quingoc_path) + + ret_value = subprocess.run(get_current_version_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to get local quingo compiler version with the" + "following error: {}".format(ret_value.stderr)) + + find_version = re.search(r'\d+\.\d+\.\d+', ret_value.stdout.split()[0]) + if find_version is not None: + current_version = find_version.group() + else: + raise RuntimeError( + "Failed to get version of local quingo compiler from {}.".format(quingoc_path)) + + if current_version == lastest_version: + return False + else: + return True + + +if __name__ == "__main__": + default_path = Path.home() / '.quingo' + quingoc_path = distutils.spawn.find_executable( + 'quingoc', str(default_path)) + + if quingoc_path is None: + quingoc_path = distutils.spawn.find_executable('quingoc') + + if quingoc_path is None: + download_and_install_latest_quingoc() + else: + if check_update(quingoc_path): + download_and_install_latest_quingoc(quingoc_path) -- Gitee From adc6a6fb01462d1d9816a9716aafd5cd7e111814 Mon Sep 17 00:00:00 2001 From: Peng Zhou Date: Wed, 5 Jan 2022 14:21:04 +0800 Subject: [PATCH 12/29] add install on darwin --- src/quingo/core/install_quingoc.py | 106 +++++++++++++++++++++++++---- 1 file changed, 92 insertions(+), 14 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index f4a7f6b..d717efb 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -1,3 +1,4 @@ +import pathlib import re import requests import json @@ -7,16 +8,29 @@ import platform import zipfile import shutil import tempfile +import datetime from pathlib import Path from setuptools import setup from setuptools.command.develop import develop from setuptools.command.install import install +def backup_compiler(quingoc_path): + + backup_path = quingoc_path.parent / "quingoc_backup" + + if not backup_path.exists(): + backup_path.mkdir(exist_ok=True) + + backup_file = quingoc_path.name + \ + datetime.datetime.now().strftime('%F') + + shutil.move(quingoc_path, backup_path/backup_file) + + def set_path_env_on_Linux(install_path): """Set path environment to contain the milr quingo compiler on Linux. """ - quingoc_dir = install_path / 'bin' ret_value = subprocess.run("echo $PATH", stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True) @@ -25,7 +39,7 @@ def set_path_env_on_Linux(install_path): raise RuntimeError( "Failed to retrieve system path using 'echo $PATH'.") - if str(quingoc_dir.absolute()) in ret_value.stdout: + if str(install_path.absolute()) in ret_value.stdout: return bash_profile = Path.home() / '.bash_profile' @@ -36,26 +50,33 @@ def set_path_env_on_Linux(install_path): echo 'export PATH={quingoc_dir_path}:$PATH' >> {bash_profile_path}\n \ fi\n" set_env_cmd = shell_cmd.format( - bashrc_path=bashrc, bash_profile_path=bash_profile, quingoc_dir_path=quingoc_dir) + bashrc_path=bashrc, bash_profile_path=bash_profile, quingoc_dir_path=install_path) ret_value = subprocess.run(set_env_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True) if(ret_value.returncode != 0): raise RuntimeError("Failed add \"{}\" to path environment with the" - "following error: {}".format(quingoc_dir, ret_value.stderr)) + "following error: {}".format(install_path, ret_value.stderr)) - print("Installed mlir quingo compiler at directory:{}".format(quingoc_dir)) + print("Installed mlir quingo compiler at directory:{}".format(install_path)) -def install_on_Linux(mlir_compiler_path): +def install_on_Linux(mlir_compiler_path, old_version_path=None): """Install quingo compiler on Linux. """ + mlir_compiler_path.chmod(0o744) - quingoc_install_dir = Path.home() / '.local' + + if old_version_path is not True: + mlir_compiler_install_dir = old_version_path.parent + mlir_compiler_exec_path = mlir_compiler_install_dir + else: + mlir_compiler_install_dir = Path.home() / '.local' + mlir_compiler_exec_path = mlir_compiler_install_dir / 'bin' mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( - mlir_compiler_path, quingoc_install_dir) + mlir_compiler_path, mlir_compiler_install_dir) print("mlir_compiler_install_cmd: ", mlir_compiler_install_cmd) @@ -65,7 +86,7 @@ def install_on_Linux(mlir_compiler_path): raise RuntimeError("Failed to install lastest quingo compiler with the" "following error: {}".format(ret_value.stderr)) - set_path_env_on_Linux(quingoc_install_dir) + set_path_env_on_Linux(mlir_compiler_exec_path) def set_path_on_Windows(install_path): @@ -77,11 +98,14 @@ def set_path_on_Windows(install_path): return -def install_on_Windows(mlir_compiler_path): +def install_on_Windows(mlir_compiler_path, old_version_path=None): """Install quingo compiler on Windows. """ - mlir_compiler_install_path = Path.home() / '.quingo' + if old_version_path is not None: + mlir_compiler_install_path = old_version_path.parent + else: + mlir_compiler_install_path = Path.home() / '.quingo' zip_file = zipfile.ZipFile(mlir_compiler_path) zip_list = zip_file.namelist() @@ -96,14 +120,68 @@ def install_on_Windows(mlir_compiler_path): set_path_on_Windows(mlir_compiler_install_path) +def set_path_env_on_Darwin(install_path): + """Set path environment to contain the milr quingo compiler on Darwin. + """ + + set_path_env_on_Linux(install_path) + + +def install_on_Darwin(mlir_compiler_path, old_version_path=None): + """Install quingo compiler on Darwin. + """ + + if old_version_path is not None: + mlir_compiler_install_path = old_version_path.parent + mlir_compiler_exec_path = mlir_compiler_install_path + else: + mlir_compiler_install_path = Path.home() / '.local' + mlir_compiler_exec_path = mlir_compiler_install_path / 'bin' + + if not mlir_compiler_exec_path.exists(): + mlir_compiler_exec_path.mkdir(exist_ok=True) + + # mount dmg file + mount_cmd = "hdiutil attach " + str(mlir_compiler_path) + + ret_value = subprocess.run(mount_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to mount lastest quingo compiler dmg file with the" + "following error: {}".format(ret_value.stderr)) + + mlir_compiler_exec_file = pathlib.path( + "/Volumes/" + mlir_compiler_path.name) / 'quingoc.app' / 'Contents' / 'Resources' / 'bin' / 'quingoc' + + shutil.move(mlir_compiler_exec_file, mlir_compiler_exec_path) + + # umount dmg file + umount_cmd = "hdiutil detach " + str("/Volumes/" + mlir_compiler_path.name) + + ret_value = subprocess.run(umount_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to umount lastest quingo compiler dmg file with the" + "following error: {}".format(ret_value.stderr)) + + set_path_env_on_Darwin(mlir_compiler_exec_path) + + def install_compiler(os_name, mlir_compiler_path, old_version_path=None): assert os_name in ['Linux', 'Windows', 'Darwin'] + + if old_version_path is not None: + backup_compiler(old_version_path) + if(os_name == 'Windows'): - install_on_Windows(mlir_compiler_path, old_version_path) + install_on_Windows(mlir_compiler_path, + old_version_path) if(os_name == 'Linux'): - install_on_Linux(mlir_compiler_path, old_version_path) + install_on_Linux(mlir_compiler_path, + old_version_path) if(os_name == 'Linux'): - install_on_Linux(mlir_compiler_path, old_version_path) + install_on_Linux(mlir_compiler_path, + old_version_path) def download_compiler(os_name, tmp_dir_name): -- Gitee From 4023e099355947e3cbeeff48feb8205df5769e81 Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Wed, 5 Jan 2022 15:36:56 +0800 Subject: [PATCH 13/29] fixed bugs on windows --- src/quingo/core/install_quingoc.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index d717efb..eb87be8 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -10,10 +10,6 @@ import shutil import tempfile import datetime from pathlib import Path -from setuptools import setup -from setuptools.command.develop import develop -from setuptools.command.install import install - def backup_compiler(quingoc_path): @@ -112,11 +108,6 @@ def install_on_Windows(mlir_compiler_path, old_version_path=None): zip_file.extractall(mlir_compiler_install_path) zip_file.close() - files = [file for file in mlir_compiler_install_path.glob( - "*/*") if file.is_file()] - for file in files: - file.replace(mlir_compiler_install_path / file.name) - set_path_on_Windows(mlir_compiler_install_path) @@ -296,4 +287,4 @@ if __name__ == "__main__": download_and_install_latest_quingoc() else: if check_update(quingoc_path): - download_and_install_latest_quingoc(quingoc_path) + download_and_install_latest_quingoc(pathlib.Path(quingoc_path)) -- Gitee From 0f509e6c30c7d261a6c4aa9d8065d7590dd680fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E6=9C=8B?= Date: Wed, 5 Jan 2022 19:41:41 +0800 Subject: [PATCH 14/29] fix bugs on macos --- src/quingo/core/install_quingoc.py | 44 +++++++++++++++++------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index eb87be8..d0e116a 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -132,28 +132,34 @@ def install_on_Darwin(mlir_compiler_path, old_version_path=None): if not mlir_compiler_exec_path.exists(): mlir_compiler_exec_path.mkdir(exist_ok=True) - # mount dmg file - mount_cmd = "hdiutil attach " + str(mlir_compiler_path) + try: + # mount dmg file + mount_cmd = "hdiutil attach " + str(mlir_compiler_path) - ret_value = subprocess.run(mount_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - if(ret_value.returncode != 0): - raise RuntimeError("Failed to mount lastest quingo compiler dmg file with the" - "following error: {}".format(ret_value.stderr)) + ret_value = subprocess.run(mount_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to mount lastest quingo compiler dmg file with the" + "following error: {}".format(ret_value.stderr)) - mlir_compiler_exec_file = pathlib.path( - "/Volumes/" + mlir_compiler_path.name) / 'quingoc.app' / 'Contents' / 'Resources' / 'bin' / 'quingoc' + mlir_compiler_exec_file = pathlib.Path( + "/Volumes/" + mlir_compiler_path.stem) / 'quingoc.app' / 'Contents' / 'Resources' / 'bin' / 'quingoc' - shutil.move(mlir_compiler_exec_file, mlir_compiler_exec_path) + shutil.copy(str(mlir_compiler_exec_file), str(mlir_compiler_exec_path)) + + except Exception as e: + raise RuntimeError("Failed to copy quingo compiler {} to '{}' with the " + "following error: {}".format(mlir_compiler_exec_file, mlir_compiler_exec_path, e)) - # umount dmg file - umount_cmd = "hdiutil detach " + str("/Volumes/" + mlir_compiler_path.name) + finally: + # umount dmg file + umount_cmd = "hdiutil detach " + str("/Volumes/" + mlir_compiler_path.stem) - ret_value = subprocess.run(umount_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - if(ret_value.returncode != 0): - raise RuntimeError("Failed to umount lastest quingo compiler dmg file with the" - "following error: {}".format(ret_value.stderr)) + ret_value = subprocess.run(umount_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to umount lastest quingo compiler dmg file with the" + "following error: {}".format(ret_value.stderr)) set_path_env_on_Darwin(mlir_compiler_exec_path) @@ -170,8 +176,8 @@ def install_compiler(os_name, mlir_compiler_path, old_version_path=None): if(os_name == 'Linux'): install_on_Linux(mlir_compiler_path, old_version_path) - if(os_name == 'Linux'): - install_on_Linux(mlir_compiler_path, + if(os_name == 'Darwin'): + install_on_Darwin(mlir_compiler_path, old_version_path) -- Gitee From 23665a381d8cdb225cd8d5e64b265cb152ee627a Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Wed, 5 Jan 2022 20:51:32 +0800 Subject: [PATCH 15/29] fix bugs in ubuntu --- src/quingo/core/install_quingoc.py | 32 +++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index d0e116a..58a2241 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -64,12 +64,11 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): mlir_compiler_path.chmod(0o744) - if old_version_path is not True: + if old_version_path is not None: mlir_compiler_install_dir = old_version_path.parent - mlir_compiler_exec_path = mlir_compiler_install_dir else: mlir_compiler_install_dir = Path.home() / '.local' - mlir_compiler_exec_path = mlir_compiler_install_dir / 'bin' + mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( mlir_compiler_path, mlir_compiler_install_dir) @@ -81,6 +80,12 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): if(ret_value.returncode != 0): raise RuntimeError("Failed to install lastest quingo compiler with the" "following error: {}".format(ret_value.stderr)) + + if old_version_path is not None: + mlir_compiler_exec_path = mlir_compiler_install_dir + shutil.copy(str(mlir_compiler_install_dir/ 'bin' / 'quingoc'), str(mlir_compiler_exec_path)) + else: + mlir_compiler_exec_path = mlir_compiler_install_dir / 'bin' set_path_env_on_Linux(mlir_compiler_exec_path) @@ -238,9 +243,8 @@ def download_and_install_latest_quingoc(old_version_path=None): shutil.rmtree(tmp_dir_path) - -def check_update(quingoc_path): - """Check local quingo compiler whether is latest version +def get_lastest_version(): + """Get lastest quingo compiler version """ latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( @@ -260,6 +264,13 @@ def check_update(quingoc_path): raise RuntimeError("Failed to parse information retrieved from gitee with the " "following error: {}".format(e)) + return lastest_version + + +def get_current_version(): + """Get current quingo compiler version + """ + get_current_version_cmd = "{} --version".format(quingoc_path) ret_value = subprocess.run(get_current_version_cmd, stdout=subprocess.PIPE, @@ -275,7 +286,14 @@ def check_update(quingoc_path): raise RuntimeError( "Failed to get version of local quingo compiler from {}.".format(quingoc_path)) - if current_version == lastest_version: + return current_version + + +def check_update(quingoc_path): + """Check local quingo compiler whether is latest version + """ + return True + if get_current_version() == get_lastest_version(): return False else: return True -- Gitee From 461050779ed5670f2b3c4118385ebcb2ce777aea Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Wed, 5 Jan 2022 22:19:45 +0800 Subject: [PATCH 16/29] fix bugs for windows --- src/quingo/core/install_quingoc.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index 58a2241..fe12ace 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -113,6 +113,15 @@ def install_on_Windows(mlir_compiler_path, old_version_path=None): zip_file.extractall(mlir_compiler_install_path) zip_file.close() + mlir_compiler_extract_path = mlir_compiler_install_path / mlir_compiler_path.stem + + files = [file for file in mlir_compiler_extract_path.glob( + "bin/*") if file.is_file()] + for file in files: + shutil.copy(file, mlir_compiler_install_path) + + shutil.rmtree(mlir_compiler_extract_path) + set_path_on_Windows(mlir_compiler_install_path) @@ -243,6 +252,7 @@ def download_and_install_latest_quingoc(old_version_path=None): shutil.rmtree(tmp_dir_path) + def get_lastest_version(): """Get lastest quingo compiler version """ @@ -292,11 +302,12 @@ def get_current_version(): def check_update(quingoc_path): """Check local quingo compiler whether is latest version """ - return True - if get_current_version() == get_lastest_version(): - return False - else: - return True + + #if get_current_version() == get_lastest_version(): + # return False + #else: + # return True + return True # set true while quingoc can not get version at this time if __name__ == "__main__": -- Gitee From 0440d6932142659fb73c25c2757c81e8620dae48 Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Wed, 5 Jan 2022 22:47:40 +0800 Subject: [PATCH 17/29] tempdir will auto delete --- src/quingo/core/install_quingoc.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index fe12ace..5468c01 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -250,8 +250,6 @@ def download_and_install_latest_quingoc(old_version_path=None): mlir_compiler_path = download_compiler(os_name, tmp_dir_path) install_compiler(os_name, mlir_compiler_path, old_version_path) - shutil.rmtree(tmp_dir_path) - def get_lastest_version(): """Get lastest quingo compiler version -- Gitee From 04c005df0b5fd732ca565c3f6d10a4fab6a75ac8 Mon Sep 17 00:00:00 2001 From: Peng Zhou Date: Thu, 6 Jan 2022 16:42:55 +0800 Subject: [PATCH 18/29] add print information during install --- src/quingo/core/install_quingoc.py | 40 +++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index 5468c01..a8a795b 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -11,6 +11,7 @@ import tempfile import datetime from pathlib import Path + def backup_compiler(quingoc_path): backup_path = quingoc_path.parent / "quingoc_backup" @@ -23,6 +24,9 @@ def backup_compiler(quingoc_path): shutil.move(quingoc_path, backup_path/backup_file) + print("Local quingoc has been backup at \"{}\".".format( + backup_path)) + def set_path_env_on_Linux(install_path): """Set path environment to contain the milr quingo compiler on Linux. @@ -55,8 +59,6 @@ def set_path_env_on_Linux(install_path): raise RuntimeError("Failed add \"{}\" to path environment with the" "following error: {}".format(install_path, ret_value.stderr)) - print("Installed mlir quingo compiler at directory:{}".format(install_path)) - def install_on_Linux(mlir_compiler_path, old_version_path=None): """Install quingo compiler on Linux. @@ -68,7 +70,6 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): mlir_compiler_install_dir = old_version_path.parent else: mlir_compiler_install_dir = Path.home() / '.local' - mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( mlir_compiler_path, mlir_compiler_install_dir) @@ -80,15 +81,19 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): if(ret_value.returncode != 0): raise RuntimeError("Failed to install lastest quingo compiler with the" "following error: {}".format(ret_value.stderr)) - + if old_version_path is not None: mlir_compiler_exec_path = mlir_compiler_install_dir - shutil.copy(str(mlir_compiler_install_dir/ 'bin' / 'quingoc'), str(mlir_compiler_exec_path)) + shutil.copy(str(mlir_compiler_install_dir / 'bin' / + 'quingoc'), str(mlir_compiler_exec_path)) else: mlir_compiler_exec_path = mlir_compiler_install_dir / 'bin' set_path_env_on_Linux(mlir_compiler_exec_path) + print("Lastest quingoc has been installed at \"{}\".".format( + mlir_compiler_exec_path)) + def set_path_on_Windows(install_path): """Set path environment to contain the milr quingo compiler on Windows. @@ -124,6 +129,9 @@ def install_on_Windows(mlir_compiler_path, old_version_path=None): set_path_on_Windows(mlir_compiler_install_path) + print("Lastest quingoc has been installed at \"{}\".".format( + mlir_compiler_install_path)) + def set_path_env_on_Darwin(install_path): """Set path environment to contain the milr quingo compiler on Darwin. @@ -160,14 +168,15 @@ def install_on_Darwin(mlir_compiler_path, old_version_path=None): "/Volumes/" + mlir_compiler_path.stem) / 'quingoc.app' / 'Contents' / 'Resources' / 'bin' / 'quingoc' shutil.copy(str(mlir_compiler_exec_file), str(mlir_compiler_exec_path)) - + except Exception as e: raise RuntimeError("Failed to copy quingo compiler {} to '{}' with the " "following error: {}".format(mlir_compiler_exec_file, mlir_compiler_exec_path, e)) finally: # umount dmg file - umount_cmd = "hdiutil detach " + str("/Volumes/" + mlir_compiler_path.stem) + umount_cmd = "hdiutil detach " + \ + str("/Volumes/" + mlir_compiler_path.stem) ret_value = subprocess.run(umount_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True) @@ -177,6 +186,9 @@ def install_on_Darwin(mlir_compiler_path, old_version_path=None): set_path_env_on_Darwin(mlir_compiler_exec_path) + print("Lastest quingoc has been installed at \"{}\".".format( + mlir_compiler_exec_path)) + def install_compiler(os_name, mlir_compiler_path, old_version_path=None): assert os_name in ['Linux', 'Windows', 'Darwin'] @@ -192,7 +204,7 @@ def install_compiler(os_name, mlir_compiler_path, old_version_path=None): old_version_path) if(os_name == 'Darwin'): install_on_Darwin(mlir_compiler_path, - old_version_path) + old_version_path) def download_compiler(os_name, tmp_dir_name): @@ -275,7 +287,7 @@ def get_lastest_version(): return lastest_version -def get_current_version(): +def get_current_version(quingoc_path): """Get current quingo compiler version """ @@ -301,11 +313,15 @@ def check_update(quingoc_path): """Check local quingo compiler whether is latest version """ - #if get_current_version() == get_lastest_version(): + lastest_version = get_lastest_version() + # current_version = get_current_version(quingoc_path) + # if current_version == lastest_version: + # print("Local quingoc is already the lastest version.") # return False - #else: + # else: + # print("Local quingoc version ({}) is behind lastest version ({}). Update now.".format(current_version, lastest_version)) # return True - return True # set true while quingoc can not get version at this time + return True # set true while quingoc can not get version at this time if __name__ == "__main__": -- Gitee From 9cb04464349fb27ea00d239f161c93f0624dedc5 Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Thu, 6 Jan 2022 17:30:16 +0800 Subject: [PATCH 19/29] add progress bar --- src/quingo/core/install_quingoc.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index a8a795b..d5d34d5 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -1,5 +1,6 @@ import pathlib import re +import math import requests import json import distutils.spawn @@ -9,6 +10,7 @@ import zipfile import shutil import tempfile import datetime +import tqdm from pathlib import Path @@ -234,13 +236,17 @@ def download_compiler(os_name, tmp_dir_name): quingoc_url = quingoc_asset['browser_download_url'] + \ '/' + quingoc_asset['name'] - quingoc_response = requests.get(quingoc_url) + quingoc_response = requests.get(quingoc_url, stream=True) + data_size = math.ceil(int(quingoc_response.headers['Content-Length'])/1024/1024) mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] with mlir_compiler_path.open('wb') as tmp_dl_file: - num_bytes = tmp_dl_file.write(quingoc_response.content) + for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024*1024), total=data_size, desc='Download', unit='MB'): + tmp_dl_file.write(data) + print("installation file has been downloaded to tmp file {} ({} bytes). ".format( - mlir_compiler_path, num_bytes)) + mlir_compiler_path, quingoc_response.headers['Content-Length'])) + return mlir_compiler_path except Exception as e: -- Gitee From 833dbc8f4d8294c9668b00f36268212bc427a7d4 Mon Sep 17 00:00:00 2001 From: Peng Zhou Date: Fri, 7 Jan 2022 14:19:26 +0800 Subject: [PATCH 20/29] update repo url --- src/quingo/core/install_quingoc.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index d5d34d5..921a0ea 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -219,7 +219,7 @@ def download_compiler(os_name, tmp_dir_name): dl_file_suffix = os_dl_suffix[os_name] latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( - owner="hpcl_quanta", repo="quingo-runtime") + owner="quingo", repo="quingo-runtime") try: latest_release = requests.get(latest_release_url).text @@ -237,7 +237,8 @@ def download_compiler(os_name, tmp_dir_name): quingoc_url = quingoc_asset['browser_download_url'] + \ '/' + quingoc_asset['name'] quingoc_response = requests.get(quingoc_url, stream=True) - data_size = math.ceil(int(quingoc_response.headers['Content-Length'])/1024/1024) + data_size = math.ceil( + int(quingoc_response.headers['Content-Length'])/1024/1024) mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] with mlir_compiler_path.open('wb') as tmp_dl_file: @@ -274,7 +275,7 @@ def get_lastest_version(): """ latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( - owner="hpcl_quanta", repo="quingo-runtime") + owner="quingo", repo="quingo-runtime") try: latest_release = requests.get(latest_release_url).text -- Gitee From f3ab7e52bb42967a04c119f65670553c000f8604 Mon Sep 17 00:00:00 2001 From: Peng Zhou Date: Fri, 7 Jan 2022 14:37:17 +0800 Subject: [PATCH 21/29] remove quingoc install during pip install --- setup.py | 200 +------------------------------------------------------ 1 file changed, 2 insertions(+), 198 deletions(-) diff --git a/setup.py b/setup.py index 0c25b09..b908cbe 100755 --- a/setup.py +++ b/setup.py @@ -1,199 +1,3 @@ -import requests -import json -import distutils.spawn -import subprocess -import platform -import zipfile -import shutil -import tempfile -from pathlib import Path -from setuptools import setup -from setuptools.command.develop import develop -from setuptools.command.install import install +import setuptools - -def set_path_env_on_Linux(install_path): - """Set path environment to contain the milr quingo compiler on Linux. - """ - quingoc_dir = install_path / 'bin' - - ret_value = subprocess.run("echo $PATH", stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - - if ret_value.returncode != 0: - raise RuntimeError( - "Failed to retrieve system path using 'echo $PATH'.") - - if str(quingoc_dir.absolute()) in ret_value.stdout: - return - - bash_profile = Path.home() / '.bash_profile' - bashrc = Path.home() / '.bashrc' - shell_cmd = "if [ -f \"{bashrc_path}\" ]\nthen\n \ - echo 'export PATH={quingoc_dir_path}:$PATH' >> {bashrc_path}\n \ - elif [ -f \"{bash_profile_path}\" ]\nthen\n \ - echo 'export PATH={quingoc_dir_path}:$PATH' >> {bash_profile_path}\n \ - fi\n" - set_env_cmd = shell_cmd.format( - bashrc_path=bashrc, bash_profile_path=bash_profile, quingoc_dir_path=quingoc_dir) - - ret_value = subprocess.run(set_env_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - - if(ret_value.returncode != 0): - raise RuntimeError("Failed add \"{}\" to path environment with the" - "following error: {}".format(quingoc_dir, ret_value.stderr)) - - print("Installed mlir quingo compiler at directory:{}".format(quingoc_dir)) - - -def install_on_Linux(mlir_compiler_path): - """Install quingo compiler on Linux. - """ - mlir_compiler_path.chmod(0o744) - quingoc_install_dir = Path.home() / '.local' - - mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( - mlir_compiler_path, quingoc_install_dir) - - print("mlir_compiler_install_cmd: ", mlir_compiler_install_cmd) - - ret_value = subprocess.run(mlir_compiler_install_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - if(ret_value.returncode != 0): - raise RuntimeError("Failed to install lastest quingo compiler with the" - "following error: {}".format(ret_value.stderr)) - - set_path_env_on_Linux(quingoc_install_dir) - - -def set_path_on_Windows(install_path): - """Set path environment to contain the milr quingo compiler on Windows. - """ - # Nothing to do, quingo-runtime will search the default install path - # Default path is Path.home() / '.quingo' - - return - - -def install_on_Windows(mlir_compiler_path): - """Install quingo compiler on Windows. - """ - - mlir_compiler_install_path = Path.home() / '.quingo' - - zip_file = zipfile.ZipFile(mlir_compiler_path) - zip_list = zip_file.namelist() - zip_file.extractall(mlir_compiler_install_path) - zip_file.close() - - files = [file for file in mlir_compiler_install_path.glob( - "*/*") if file.is_file()] - for file in files: - file.replace(mlir_compiler_install_path / file.name) - - set_path_on_Windows(mlir_compiler_install_path) - - -def install_compiler(os_name, mlir_compiler_path): - assert os_name in ['Linux', 'Windows'] - if(os_name == 'Windows'): - install_on_Windows(mlir_compiler_path) - if(os_name == 'Linux'): - install_on_Linux(mlir_compiler_path) - - -def download_compiler(os_name, tmp_dir_name): - assert os_name in ['Linux', 'Windows'] - os_dl_suffix = { - 'Linux': '.sh', - 'Windows': '.zip' - } - dl_file_suffix = os_dl_suffix[os_name] - - latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( - owner="quingo", repo="quingo-runtime") - - try: - latest_release = requests.get(latest_release_url).text - release_info = json.loads(latest_release) - assets = release_info['assets'] - quingoc_asset = None - for asset in assets: - if 'name' in asset and asset['name'].endswith(dl_file_suffix): - quingoc_asset = asset - break - if quingoc_asset is None: - raise RuntimeError( - "Failed to download quingo compiler from gitee.") - - quingoc_url = quingoc_asset['browser_download_url'] + \ - '/' + quingoc_asset['name'] - quingoc_response = requests.get(quingoc_url) - - mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] - with mlir_compiler_path.open('wb') as tmp_dl_file: - num_bytes = tmp_dl_file.write(quingoc_response.content) - print("installation file has been downloaded to tmp file {} ({} bytes). ".format( - mlir_compiler_path, num_bytes)) - return mlir_compiler_path - - except Exception as e: - raise RuntimeError("Failed to parse information retrieved from gitee with the " - "following error: {}".format(e)) - - -def download_and_install_latest_quingoc(): - """Download the latest mlir quingo compiler. - """ - os_name = platform.system() - if os_name not in ['Linux', 'Windows']: - raise SystemError( - "Currently pip installation does not support {}".format(os_name)) - - tmp_dir = tempfile.TemporaryDirectory() - tmp_dir_path = Path(tmp_dir.name) - - mlir_compiler_path = download_compiler(os_name, tmp_dir_path) - install_compiler(os_name, mlir_compiler_path) - - shutil.rmtree(tmp_dir_path) - - -def friendly(command_subclass): - """A decorator for classes subclassing one of the setuptools commands. - - It modifies the run() method so that it prints a friendly greeting. - """ - orig_run = command_subclass.run - - def modified_run(self): - default_path = Path.home() / '.quingo' - quingoc_path = distutils.spawn.find_executable( - 'quingoc', str(default_path)) - - if quingoc_path is None: - quingoc_path = distutils.spawn.find_executable('quingoc') - - if quingoc_path is None: - download_and_install_latest_quingoc() - - orig_run(self) - - command_subclass.run = modified_run - return command_subclass - - -@friendly -class CustomDevelopCommand(develop): - pass - - -@friendly -class CustomInstallCommand(install): - pass - - -setup(cmdclass={ - 'install': CustomInstallCommand, - 'develop': CustomDevelopCommand, }) +setuptools.setup() -- Gitee From 16780484db17116ac4babe39d9bfec72f17b0e3b Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Fri, 7 Jan 2022 15:33:09 +0800 Subject: [PATCH 22/29] fix bug when get local quingoc version --- src/quingo/core/install_quingoc.py | 41 ++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index 921a0ea..971a7fe 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -245,7 +245,7 @@ def download_compiler(os_name, tmp_dir_name): for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024*1024), total=data_size, desc='Download', unit='MB'): tmp_dl_file.write(data) - print("installation file has been downloaded to tmp file {} ({} bytes). ".format( + print("installation file has been downloaded to tmp file \"{}\" ({} bytes). ".format( mlir_compiler_path, quingoc_response.headers['Content-Length'])) return mlir_compiler_path @@ -274,13 +274,32 @@ def get_lastest_version(): """Get lastest quingo compiler version """ + os_name = platform.system() + assert os_name in ['Linux', 'Windows', 'Darwin'] + os_dl_suffix = { + 'Linux': '.sh', + 'Windows': '.zip', + 'Darwin': '.dmg' + } + dl_file_suffix = os_dl_suffix[os_name] + latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( owner="quingo", repo="quingo-runtime") try: latest_release = requests.get(latest_release_url).text release_info = json.loads(latest_release) - find_version = re.search(r'\d+\.\d+\.\d+', release_info['tag_name']) + assets = release_info['assets'] + quingoc_asset = None + for asset in assets: + if 'name' in asset and asset['name'].endswith(dl_file_suffix): + quingoc_asset = asset + break + if quingoc_asset is None: + raise RuntimeError( + "Failed to download quingo compiler from gitee.") + + find_version = re.search(r'\d+\.\d+\.\d+', quingoc_asset['name']) if find_version is not None: lastest_version = find_version.group() else: @@ -306,7 +325,7 @@ def get_current_version(quingoc_path): raise RuntimeError("Failed to get local quingo compiler version with the" "following error: {}".format(ret_value.stderr)) - find_version = re.search(r'\d+\.\d+\.\d+', ret_value.stdout.split()[0]) + find_version = re.search(r'\d+\.\d+\.\d+', ret_value.stdout.split('\n')[0]) if find_version is not None: current_version = find_version.group() else: @@ -320,15 +339,15 @@ def check_update(quingoc_path): """Check local quingo compiler whether is latest version """ + current_version = get_current_version(quingoc_path) lastest_version = get_lastest_version() - # current_version = get_current_version(quingoc_path) - # if current_version == lastest_version: - # print("Local quingoc is already the lastest version.") - # return False - # else: - # print("Local quingoc version ({}) is behind lastest version ({}). Update now.".format(current_version, lastest_version)) - # return True - return True # set true while quingoc can not get version at this time + + if current_version == lastest_version: + print("Local quingoc is already the lastest version.") + return False + else: + print("Local quingoc version ({}) is behind lastest version ({}). Update now.".format(current_version, lastest_version)) + return True if __name__ == "__main__": -- Gitee From 34e8ad035ded56cb55bf37e5b8a53b6269213441 Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Fri, 7 Jan 2022 16:01:20 +0800 Subject: [PATCH 23/29] print quingoc version --- src/quingo/core/install_quingoc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index 971a7fe..1e9e474 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -343,7 +343,7 @@ def check_update(quingoc_path): lastest_version = get_lastest_version() if current_version == lastest_version: - print("Local quingoc is already the lastest version.") + print("Local quingoc is already the lastest version {}.".format(lastest_version)) return False else: print("Local quingoc version ({}) is behind lastest version ({}). Update now.".format(current_version, lastest_version)) -- Gitee From 8d408a1d8dfa12cf01c86bccca6245080ea5da11 Mon Sep 17 00:00:00 2001 From: Xiang Fu Date: Fri, 7 Jan 2022 17:25:09 +0800 Subject: [PATCH 24/29] update dependency and install msg --- setup.cfg | 1 + src/quingo/core/install_quingoc.py | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/setup.cfg b/setup.cfg index b4678be..6d84803 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,6 +27,7 @@ install_requires = colorama termcolor requests + tqdm [options.packages.find] where = src \ No newline at end of file diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index 1e9e474..b06b3a9 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -76,7 +76,7 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( mlir_compiler_path, mlir_compiler_install_dir) - print("mlir_compiler_install_cmd: ", mlir_compiler_install_cmd) + print("installing...") ret_value = subprocess.run(mlir_compiler_install_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True) @@ -93,7 +93,7 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): set_path_env_on_Linux(mlir_compiler_exec_path) - print("Lastest quingoc has been installed at \"{}\".".format( + print("Successfully installed the lastest quingoc at \"{}\".".format( mlir_compiler_exec_path)) @@ -242,7 +242,7 @@ def download_compiler(os_name, tmp_dir_name): mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] with mlir_compiler_path.open('wb') as tmp_dl_file: - for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024*1024), total=data_size, desc='Download', unit='MB'): + for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024*1024), total=data_size, desc='Downloading quingoc from {}'.format(quingoc_url), unit='MB'): tmp_dl_file.write(data) print("installation file has been downloaded to tmp file \"{}\" ({} bytes). ".format( @@ -336,7 +336,7 @@ def get_current_version(quingoc_path): def check_update(quingoc_path): - """Check local quingo compiler whether is latest version + """Check local quingo compiler whether is latest version """ current_version = get_current_version(quingoc_path) -- Gitee From 7f2299a16f09ec4c809e08be91a90bd8a1adccb3 Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Fri, 7 Jan 2022 18:40:33 +0800 Subject: [PATCH 25/29] refine try exception - refine try exception code - update repo to quingoc-release --- src/quingo/core/compiler_config.py | 2 +- src/quingo/core/install_quingoc.py | 136 +++++++++++++++-------------- src/quingo/core/manager.py | 5 +- 3 files changed, 73 insertions(+), 70 deletions(-) diff --git a/src/quingo/core/compiler_config.py b/src/quingo/core/compiler_config.py index 8d1bee8..f670c52 100644 --- a/src/quingo/core/compiler_config.py +++ b/src/quingo/core/compiler_config.py @@ -32,7 +32,7 @@ def set_compiler_path(path_str, is_mlir=False): def download_latest_quingoc(): latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( - owner="quingo", repo="quingo-runtime") + owner="quingo", repo="quingoc-release") try: latest_release = get_text(latest_release_url) diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py index b06b3a9..71f95a6 100644 --- a/src/quingo/core/install_quingoc.py +++ b/src/quingo/core/install_quingoc.py @@ -13,6 +13,17 @@ import datetime import tqdm from pathlib import Path +quingoc_owner = "quingo" +quingoc_release_repo = "quingoc-release" +lastest_quingoc_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( + owner=quingoc_owner, repo=quingoc_release_repo) + +os_name_list = ['Linux', 'Windows', 'Darwin'] +os_dl_suffix = { + 'Linux': '.sh', + 'Windows': '.zip', + 'Darwin': '.dmg' + } def backup_compiler(quingoc_path): @@ -76,8 +87,6 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( mlir_compiler_path, mlir_compiler_install_dir) - print("installing...") - ret_value = subprocess.run(mlir_compiler_install_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True) if(ret_value.returncode != 0): @@ -93,7 +102,7 @@ def install_on_Linux(mlir_compiler_path, old_version_path=None): set_path_env_on_Linux(mlir_compiler_exec_path) - print("Successfully installed the lastest quingoc at \"{}\".".format( + print("Successfully installed the Lastest quingoc at \"{}\".".format( mlir_compiler_exec_path)) @@ -116,7 +125,6 @@ def install_on_Windows(mlir_compiler_path, old_version_path=None): mlir_compiler_install_path = Path.home() / '.quingo' zip_file = zipfile.ZipFile(mlir_compiler_path) - zip_list = zip_file.namelist() zip_file.extractall(mlir_compiler_install_path) zip_file.close() @@ -131,7 +139,7 @@ def install_on_Windows(mlir_compiler_path, old_version_path=None): set_path_on_Windows(mlir_compiler_install_path) - print("Lastest quingoc has been installed at \"{}\".".format( + print("Successfully installed the Lastest quingoc at \"{}\".".format( mlir_compiler_install_path)) @@ -188,12 +196,11 @@ def install_on_Darwin(mlir_compiler_path, old_version_path=None): set_path_env_on_Darwin(mlir_compiler_exec_path) - print("Lastest quingoc has been installed at \"{}\".".format( + print("Successfully installed the Lastest quingoc at \"{}\".".format( mlir_compiler_exec_path)) def install_compiler(os_name, mlir_compiler_path, old_version_path=None): - assert os_name in ['Linux', 'Windows', 'Darwin'] if old_version_path is not None: backup_compiler(old_version_path) @@ -210,49 +217,50 @@ def install_compiler(os_name, mlir_compiler_path, old_version_path=None): def download_compiler(os_name, tmp_dir_name): - assert os_name in ['Linux', 'Windows', 'Darwin'] - os_dl_suffix = { - 'Linux': '.sh', - 'Windows': '.zip', - 'Darwin': '.dmg' - } - dl_file_suffix = os_dl_suffix[os_name] + assert os_name in os_name_list - latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( - owner="quingo", repo="quingo-runtime") + dl_file_suffix = os_dl_suffix[os_name] try: - latest_release = requests.get(latest_release_url).text + latest_release = requests.get(lastest_quingoc_release_url).text release_info = json.loads(latest_release) - assets = release_info['assets'] - quingoc_asset = None - for asset in assets: - if 'name' in asset and asset['name'].endswith(dl_file_suffix): - quingoc_asset = asset - break - if quingoc_asset is None: - raise RuntimeError( - "Failed to download quingo compiler from gitee.") - - quingoc_url = quingoc_asset['browser_download_url'] + \ - '/' + quingoc_asset['name'] - quingoc_response = requests.get(quingoc_url, stream=True) - data_size = math.ceil( - int(quingoc_response.headers['Content-Length'])/1024/1024) - mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] - with mlir_compiler_path.open('wb') as tmp_dl_file: - for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024*1024), total=data_size, desc='Downloading quingoc from {}'.format(quingoc_url), unit='MB'): - tmp_dl_file.write(data) + except Exception as e: + raise RuntimeError("Failed to parse information retrieved from gitee with the " + "following error: {}".format(e)) - print("installation file has been downloaded to tmp file \"{}\" ({} bytes). ".format( - mlir_compiler_path, quingoc_response.headers['Content-Length'])) + assets = release_info['assets'] + quingoc_asset = None + for asset in assets: + if 'name' in asset and asset['name'].endswith(dl_file_suffix): + quingoc_asset = asset + break + if quingoc_asset is None: + raise RuntimeError( + "Failed to get quingo compiler release package for {} platform.".format(os_name)) - return mlir_compiler_path + quingoc_url = quingoc_asset['browser_download_url'] + \ + '/' + quingoc_asset['name'] + try: + quingoc_response = requests.get(quingoc_url, stream=True) except Exception as e: - raise RuntimeError("Failed to parse information retrieved from gitee with the " - "following error: {}".format(e)) + raise RuntimeError("Failed to download package \"{}\" from gitee with the " + "following error: {}".format(quingoc_asset['name'], e)) + + data_size = math.ceil( + int(quingoc_response.headers['Content-Length'])/1024/1024) + + mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] + with mlir_compiler_path.open('wb') as tmp_dl_file: + print('Downloading quingoc from {}'.format(quingoc_url)) + for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024*1024), total=data_size, desc='', unit='MB'): + tmp_dl_file.write(data) + + print("installation file has been downloaded to tmp file \"{}\" ({} bytes). ".format( + mlir_compiler_path, quingoc_response.headers['Content-Length'])) + + return mlir_compiler_path def download_and_install_latest_quingoc(old_version_path=None): @@ -275,41 +283,35 @@ def get_lastest_version(): """ os_name = platform.system() - assert os_name in ['Linux', 'Windows', 'Darwin'] - os_dl_suffix = { - 'Linux': '.sh', - 'Windows': '.zip', - 'Darwin': '.dmg' - } + assert os_name in os_name_list + dl_file_suffix = os_dl_suffix[os_name] - latest_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( - owner="quingo", repo="quingo-runtime") - try: - latest_release = requests.get(latest_release_url).text + latest_release = requests.get(lastest_quingoc_release_url).text release_info = json.loads(latest_release) - assets = release_info['assets'] - quingoc_asset = None - for asset in assets: - if 'name' in asset and asset['name'].endswith(dl_file_suffix): - quingoc_asset = asset - break - if quingoc_asset is None: - raise RuntimeError( - "Failed to download quingo compiler from gitee.") - - find_version = re.search(r'\d+\.\d+\.\d+', quingoc_asset['name']) - if find_version is not None: - lastest_version = find_version.group() - else: - raise RuntimeError( - "Failed to get lastest version of quingo compiler from gitee.") except Exception as e: raise RuntimeError("Failed to parse information retrieved from gitee with the " "following error: {}".format(e)) + assets = release_info['assets'] + quingoc_asset = None + for asset in assets: + if 'name' in asset and asset['name'].endswith(dl_file_suffix): + quingoc_asset = asset + break + if quingoc_asset is None: + raise RuntimeError( + "Failed to get quingo compiler release package for {} platform.".format(os_name)) + + find_version = re.search(r'\d+\.\d+\.\d+', quingoc_asset['name']) + if find_version is not None: + lastest_version = find_version.group() + else: + raise RuntimeError( + "Failed to get lastest version of quingo compiler from package \"{}\".".format(quingoc_asset['name'])) + return lastest_version diff --git a/src/quingo/core/manager.py b/src/quingo/core/manager.py index d9c8a59..b4ec740 100755 --- a/src/quingo/core/manager.py +++ b/src/quingo/core/manager.py @@ -367,8 +367,9 @@ class Runtime_system_manager(): quingo_err( "Cannot find the mlir-based quingoc compiler in the system path.") quingo_info( - "To resolve this problem, you can download quingoc from " - "https://gitee.com/quingo/quingo-runtime/releases and save " + "To resolve this problem, you can install quingoc with two ways:\n" + "1. run the following command \"python -m quingo.install_quingoc\"\n" + "2. Dowload quingoc from https://gitee.com/quingo/quingoc-release/releases and save " "it at a directory in the system path \n" "or configure its path by calling this method inside python:\n" " `quingo.quingo_interface.set_mlir_compiler_path()`") -- Gitee From bbe234218054274a96079085b162ec054cf5a0af Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Fri, 7 Jan 2022 19:07:05 +0800 Subject: [PATCH 26/29] move install quingoc to upper directory --- src/quingo/core/install_quingoc.py | 367 ----------------------------- 1 file changed, 367 deletions(-) delete mode 100644 src/quingo/core/install_quingoc.py diff --git a/src/quingo/core/install_quingoc.py b/src/quingo/core/install_quingoc.py deleted file mode 100644 index 71f95a6..0000000 --- a/src/quingo/core/install_quingoc.py +++ /dev/null @@ -1,367 +0,0 @@ -import pathlib -import re -import math -import requests -import json -import distutils.spawn -import subprocess -import platform -import zipfile -import shutil -import tempfile -import datetime -import tqdm -from pathlib import Path - -quingoc_owner = "quingo" -quingoc_release_repo = "quingoc-release" -lastest_quingoc_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( - owner=quingoc_owner, repo=quingoc_release_repo) - -os_name_list = ['Linux', 'Windows', 'Darwin'] -os_dl_suffix = { - 'Linux': '.sh', - 'Windows': '.zip', - 'Darwin': '.dmg' - } - -def backup_compiler(quingoc_path): - - backup_path = quingoc_path.parent / "quingoc_backup" - - if not backup_path.exists(): - backup_path.mkdir(exist_ok=True) - - backup_file = quingoc_path.name + \ - datetime.datetime.now().strftime('%F') - - shutil.move(quingoc_path, backup_path/backup_file) - - print("Local quingoc has been backup at \"{}\".".format( - backup_path)) - - -def set_path_env_on_Linux(install_path): - """Set path environment to contain the milr quingo compiler on Linux. - """ - - ret_value = subprocess.run("echo $PATH", stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - - if ret_value.returncode != 0: - raise RuntimeError( - "Failed to retrieve system path using 'echo $PATH'.") - - if str(install_path.absolute()) in ret_value.stdout: - return - - bash_profile = Path.home() / '.bash_profile' - bashrc = Path.home() / '.bashrc' - shell_cmd = "if [ -f \"{bashrc_path}\" ]\nthen\n \ - echo 'export PATH={quingoc_dir_path}:$PATH' >> {bashrc_path}\n \ - elif [ -f \"{bash_profile_path}\" ]\nthen\n \ - echo 'export PATH={quingoc_dir_path}:$PATH' >> {bash_profile_path}\n \ - fi\n" - set_env_cmd = shell_cmd.format( - bashrc_path=bashrc, bash_profile_path=bash_profile, quingoc_dir_path=install_path) - - ret_value = subprocess.run(set_env_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - - if(ret_value.returncode != 0): - raise RuntimeError("Failed add \"{}\" to path environment with the" - "following error: {}".format(install_path, ret_value.stderr)) - - -def install_on_Linux(mlir_compiler_path, old_version_path=None): - """Install quingo compiler on Linux. - """ - - mlir_compiler_path.chmod(0o744) - - if old_version_path is not None: - mlir_compiler_install_dir = old_version_path.parent - else: - mlir_compiler_install_dir = Path.home() / '.local' - - mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( - mlir_compiler_path, mlir_compiler_install_dir) - - ret_value = subprocess.run(mlir_compiler_install_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - if(ret_value.returncode != 0): - raise RuntimeError("Failed to install lastest quingo compiler with the" - "following error: {}".format(ret_value.stderr)) - - if old_version_path is not None: - mlir_compiler_exec_path = mlir_compiler_install_dir - shutil.copy(str(mlir_compiler_install_dir / 'bin' / - 'quingoc'), str(mlir_compiler_exec_path)) - else: - mlir_compiler_exec_path = mlir_compiler_install_dir / 'bin' - - set_path_env_on_Linux(mlir_compiler_exec_path) - - print("Successfully installed the Lastest quingoc at \"{}\".".format( - mlir_compiler_exec_path)) - - -def set_path_on_Windows(install_path): - """Set path environment to contain the milr quingo compiler on Windows. - """ - # Nothing to do, quingo-runtime will search the default install path - # Default path is Path.home() / '.quingo' - - return - - -def install_on_Windows(mlir_compiler_path, old_version_path=None): - """Install quingo compiler on Windows. - """ - - if old_version_path is not None: - mlir_compiler_install_path = old_version_path.parent - else: - mlir_compiler_install_path = Path.home() / '.quingo' - - zip_file = zipfile.ZipFile(mlir_compiler_path) - zip_file.extractall(mlir_compiler_install_path) - zip_file.close() - - mlir_compiler_extract_path = mlir_compiler_install_path / mlir_compiler_path.stem - - files = [file for file in mlir_compiler_extract_path.glob( - "bin/*") if file.is_file()] - for file in files: - shutil.copy(file, mlir_compiler_install_path) - - shutil.rmtree(mlir_compiler_extract_path) - - set_path_on_Windows(mlir_compiler_install_path) - - print("Successfully installed the Lastest quingoc at \"{}\".".format( - mlir_compiler_install_path)) - - -def set_path_env_on_Darwin(install_path): - """Set path environment to contain the milr quingo compiler on Darwin. - """ - - set_path_env_on_Linux(install_path) - - -def install_on_Darwin(mlir_compiler_path, old_version_path=None): - """Install quingo compiler on Darwin. - """ - - if old_version_path is not None: - mlir_compiler_install_path = old_version_path.parent - mlir_compiler_exec_path = mlir_compiler_install_path - else: - mlir_compiler_install_path = Path.home() / '.local' - mlir_compiler_exec_path = mlir_compiler_install_path / 'bin' - - if not mlir_compiler_exec_path.exists(): - mlir_compiler_exec_path.mkdir(exist_ok=True) - - try: - # mount dmg file - mount_cmd = "hdiutil attach " + str(mlir_compiler_path) - - ret_value = subprocess.run(mount_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - if(ret_value.returncode != 0): - raise RuntimeError("Failed to mount lastest quingo compiler dmg file with the" - "following error: {}".format(ret_value.stderr)) - - mlir_compiler_exec_file = pathlib.Path( - "/Volumes/" + mlir_compiler_path.stem) / 'quingoc.app' / 'Contents' / 'Resources' / 'bin' / 'quingoc' - - shutil.copy(str(mlir_compiler_exec_file), str(mlir_compiler_exec_path)) - - except Exception as e: - raise RuntimeError("Failed to copy quingo compiler {} to '{}' with the " - "following error: {}".format(mlir_compiler_exec_file, mlir_compiler_exec_path, e)) - - finally: - # umount dmg file - umount_cmd = "hdiutil detach " + \ - str("/Volumes/" + mlir_compiler_path.stem) - - ret_value = subprocess.run(umount_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - if(ret_value.returncode != 0): - raise RuntimeError("Failed to umount lastest quingo compiler dmg file with the" - "following error: {}".format(ret_value.stderr)) - - set_path_env_on_Darwin(mlir_compiler_exec_path) - - print("Successfully installed the Lastest quingoc at \"{}\".".format( - mlir_compiler_exec_path)) - - -def install_compiler(os_name, mlir_compiler_path, old_version_path=None): - - if old_version_path is not None: - backup_compiler(old_version_path) - - if(os_name == 'Windows'): - install_on_Windows(mlir_compiler_path, - old_version_path) - if(os_name == 'Linux'): - install_on_Linux(mlir_compiler_path, - old_version_path) - if(os_name == 'Darwin'): - install_on_Darwin(mlir_compiler_path, - old_version_path) - - -def download_compiler(os_name, tmp_dir_name): - assert os_name in os_name_list - - dl_file_suffix = os_dl_suffix[os_name] - - try: - latest_release = requests.get(lastest_quingoc_release_url).text - release_info = json.loads(latest_release) - - except Exception as e: - raise RuntimeError("Failed to parse information retrieved from gitee with the " - "following error: {}".format(e)) - - assets = release_info['assets'] - quingoc_asset = None - for asset in assets: - if 'name' in asset and asset['name'].endswith(dl_file_suffix): - quingoc_asset = asset - break - if quingoc_asset is None: - raise RuntimeError( - "Failed to get quingo compiler release package for {} platform.".format(os_name)) - - quingoc_url = quingoc_asset['browser_download_url'] + \ - '/' + quingoc_asset['name'] - - try: - quingoc_response = requests.get(quingoc_url, stream=True) - except Exception as e: - raise RuntimeError("Failed to download package \"{}\" from gitee with the " - "following error: {}".format(quingoc_asset['name'], e)) - - data_size = math.ceil( - int(quingoc_response.headers['Content-Length'])/1024/1024) - - mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] - with mlir_compiler_path.open('wb') as tmp_dl_file: - print('Downloading quingoc from {}'.format(quingoc_url)) - for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024*1024), total=data_size, desc='', unit='MB'): - tmp_dl_file.write(data) - - print("installation file has been downloaded to tmp file \"{}\" ({} bytes). ".format( - mlir_compiler_path, quingoc_response.headers['Content-Length'])) - - return mlir_compiler_path - - -def download_and_install_latest_quingoc(old_version_path=None): - """Download the latest mlir quingo compiler. - """ - os_name = platform.system() - if os_name not in ['Linux', 'Windows', 'Darwin']: - raise SystemError( - "Currently pip installation does not support {}".format(os_name)) - - tmp_dir = tempfile.TemporaryDirectory() - tmp_dir_path = Path(tmp_dir.name) - - mlir_compiler_path = download_compiler(os_name, tmp_dir_path) - install_compiler(os_name, mlir_compiler_path, old_version_path) - - -def get_lastest_version(): - """Get lastest quingo compiler version - """ - - os_name = platform.system() - assert os_name in os_name_list - - dl_file_suffix = os_dl_suffix[os_name] - - try: - latest_release = requests.get(lastest_quingoc_release_url).text - release_info = json.loads(latest_release) - - except Exception as e: - raise RuntimeError("Failed to parse information retrieved from gitee with the " - "following error: {}".format(e)) - - assets = release_info['assets'] - quingoc_asset = None - for asset in assets: - if 'name' in asset and asset['name'].endswith(dl_file_suffix): - quingoc_asset = asset - break - if quingoc_asset is None: - raise RuntimeError( - "Failed to get quingo compiler release package for {} platform.".format(os_name)) - - find_version = re.search(r'\d+\.\d+\.\d+', quingoc_asset['name']) - if find_version is not None: - lastest_version = find_version.group() - else: - raise RuntimeError( - "Failed to get lastest version of quingo compiler from package \"{}\".".format(quingoc_asset['name'])) - - return lastest_version - - -def get_current_version(quingoc_path): - """Get current quingo compiler version - """ - - get_current_version_cmd = "{} --version".format(quingoc_path) - - ret_value = subprocess.run(get_current_version_cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, shell=True) - if(ret_value.returncode != 0): - raise RuntimeError("Failed to get local quingo compiler version with the" - "following error: {}".format(ret_value.stderr)) - - find_version = re.search(r'\d+\.\d+\.\d+', ret_value.stdout.split('\n')[0]) - if find_version is not None: - current_version = find_version.group() - else: - raise RuntimeError( - "Failed to get version of local quingo compiler from {}.".format(quingoc_path)) - - return current_version - - -def check_update(quingoc_path): - """Check local quingo compiler whether is latest version - """ - - current_version = get_current_version(quingoc_path) - lastest_version = get_lastest_version() - - if current_version == lastest_version: - print("Local quingoc is already the lastest version {}.".format(lastest_version)) - return False - else: - print("Local quingoc version ({}) is behind lastest version ({}). Update now.".format(current_version, lastest_version)) - return True - - -if __name__ == "__main__": - default_path = Path.home() / '.quingo' - quingoc_path = distutils.spawn.find_executable( - 'quingoc', str(default_path)) - - if quingoc_path is None: - quingoc_path = distutils.spawn.find_executable('quingoc') - - if quingoc_path is None: - download_and_install_latest_quingoc() - else: - if check_update(quingoc_path): - download_and_install_latest_quingoc(pathlib.Path(quingoc_path)) -- Gitee From 95361f74aa0a40ed8464852a43d401f561fc3923 Mon Sep 17 00:00:00 2001 From: ZhouPeng Date: Fri, 7 Jan 2022 19:16:41 +0800 Subject: [PATCH 27/29] add missing file --- src/quingo/install_quingoc.py | 367 ++++++++++++++++++++++++++++++++++ 1 file changed, 367 insertions(+) create mode 100644 src/quingo/install_quingoc.py diff --git a/src/quingo/install_quingoc.py b/src/quingo/install_quingoc.py new file mode 100644 index 0000000..71f95a6 --- /dev/null +++ b/src/quingo/install_quingoc.py @@ -0,0 +1,367 @@ +import pathlib +import re +import math +import requests +import json +import distutils.spawn +import subprocess +import platform +import zipfile +import shutil +import tempfile +import datetime +import tqdm +from pathlib import Path + +quingoc_owner = "quingo" +quingoc_release_repo = "quingoc-release" +lastest_quingoc_release_url = "https://gitee.com/api/v5/repos/{owner}/{repo}/releases/latest".format( + owner=quingoc_owner, repo=quingoc_release_repo) + +os_name_list = ['Linux', 'Windows', 'Darwin'] +os_dl_suffix = { + 'Linux': '.sh', + 'Windows': '.zip', + 'Darwin': '.dmg' + } + +def backup_compiler(quingoc_path): + + backup_path = quingoc_path.parent / "quingoc_backup" + + if not backup_path.exists(): + backup_path.mkdir(exist_ok=True) + + backup_file = quingoc_path.name + \ + datetime.datetime.now().strftime('%F') + + shutil.move(quingoc_path, backup_path/backup_file) + + print("Local quingoc has been backup at \"{}\".".format( + backup_path)) + + +def set_path_env_on_Linux(install_path): + """Set path environment to contain the milr quingo compiler on Linux. + """ + + ret_value = subprocess.run("echo $PATH", stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + + if ret_value.returncode != 0: + raise RuntimeError( + "Failed to retrieve system path using 'echo $PATH'.") + + if str(install_path.absolute()) in ret_value.stdout: + return + + bash_profile = Path.home() / '.bash_profile' + bashrc = Path.home() / '.bashrc' + shell_cmd = "if [ -f \"{bashrc_path}\" ]\nthen\n \ + echo 'export PATH={quingoc_dir_path}:$PATH' >> {bashrc_path}\n \ + elif [ -f \"{bash_profile_path}\" ]\nthen\n \ + echo 'export PATH={quingoc_dir_path}:$PATH' >> {bash_profile_path}\n \ + fi\n" + set_env_cmd = shell_cmd.format( + bashrc_path=bashrc, bash_profile_path=bash_profile, quingoc_dir_path=install_path) + + ret_value = subprocess.run(set_env_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + + if(ret_value.returncode != 0): + raise RuntimeError("Failed add \"{}\" to path environment with the" + "following error: {}".format(install_path, ret_value.stderr)) + + +def install_on_Linux(mlir_compiler_path, old_version_path=None): + """Install quingo compiler on Linux. + """ + + mlir_compiler_path.chmod(0o744) + + if old_version_path is not None: + mlir_compiler_install_dir = old_version_path.parent + else: + mlir_compiler_install_dir = Path.home() / '.local' + + mlir_compiler_install_cmd = '"{}" --prefix="{}" --exclude-subdir'.format( + mlir_compiler_path, mlir_compiler_install_dir) + + ret_value = subprocess.run(mlir_compiler_install_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to install lastest quingo compiler with the" + "following error: {}".format(ret_value.stderr)) + + if old_version_path is not None: + mlir_compiler_exec_path = mlir_compiler_install_dir + shutil.copy(str(mlir_compiler_install_dir / 'bin' / + 'quingoc'), str(mlir_compiler_exec_path)) + else: + mlir_compiler_exec_path = mlir_compiler_install_dir / 'bin' + + set_path_env_on_Linux(mlir_compiler_exec_path) + + print("Successfully installed the Lastest quingoc at \"{}\".".format( + mlir_compiler_exec_path)) + + +def set_path_on_Windows(install_path): + """Set path environment to contain the milr quingo compiler on Windows. + """ + # Nothing to do, quingo-runtime will search the default install path + # Default path is Path.home() / '.quingo' + + return + + +def install_on_Windows(mlir_compiler_path, old_version_path=None): + """Install quingo compiler on Windows. + """ + + if old_version_path is not None: + mlir_compiler_install_path = old_version_path.parent + else: + mlir_compiler_install_path = Path.home() / '.quingo' + + zip_file = zipfile.ZipFile(mlir_compiler_path) + zip_file.extractall(mlir_compiler_install_path) + zip_file.close() + + mlir_compiler_extract_path = mlir_compiler_install_path / mlir_compiler_path.stem + + files = [file for file in mlir_compiler_extract_path.glob( + "bin/*") if file.is_file()] + for file in files: + shutil.copy(file, mlir_compiler_install_path) + + shutil.rmtree(mlir_compiler_extract_path) + + set_path_on_Windows(mlir_compiler_install_path) + + print("Successfully installed the Lastest quingoc at \"{}\".".format( + mlir_compiler_install_path)) + + +def set_path_env_on_Darwin(install_path): + """Set path environment to contain the milr quingo compiler on Darwin. + """ + + set_path_env_on_Linux(install_path) + + +def install_on_Darwin(mlir_compiler_path, old_version_path=None): + """Install quingo compiler on Darwin. + """ + + if old_version_path is not None: + mlir_compiler_install_path = old_version_path.parent + mlir_compiler_exec_path = mlir_compiler_install_path + else: + mlir_compiler_install_path = Path.home() / '.local' + mlir_compiler_exec_path = mlir_compiler_install_path / 'bin' + + if not mlir_compiler_exec_path.exists(): + mlir_compiler_exec_path.mkdir(exist_ok=True) + + try: + # mount dmg file + mount_cmd = "hdiutil attach " + str(mlir_compiler_path) + + ret_value = subprocess.run(mount_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to mount lastest quingo compiler dmg file with the" + "following error: {}".format(ret_value.stderr)) + + mlir_compiler_exec_file = pathlib.Path( + "/Volumes/" + mlir_compiler_path.stem) / 'quingoc.app' / 'Contents' / 'Resources' / 'bin' / 'quingoc' + + shutil.copy(str(mlir_compiler_exec_file), str(mlir_compiler_exec_path)) + + except Exception as e: + raise RuntimeError("Failed to copy quingo compiler {} to '{}' with the " + "following error: {}".format(mlir_compiler_exec_file, mlir_compiler_exec_path, e)) + + finally: + # umount dmg file + umount_cmd = "hdiutil detach " + \ + str("/Volumes/" + mlir_compiler_path.stem) + + ret_value = subprocess.run(umount_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to umount lastest quingo compiler dmg file with the" + "following error: {}".format(ret_value.stderr)) + + set_path_env_on_Darwin(mlir_compiler_exec_path) + + print("Successfully installed the Lastest quingoc at \"{}\".".format( + mlir_compiler_exec_path)) + + +def install_compiler(os_name, mlir_compiler_path, old_version_path=None): + + if old_version_path is not None: + backup_compiler(old_version_path) + + if(os_name == 'Windows'): + install_on_Windows(mlir_compiler_path, + old_version_path) + if(os_name == 'Linux'): + install_on_Linux(mlir_compiler_path, + old_version_path) + if(os_name == 'Darwin'): + install_on_Darwin(mlir_compiler_path, + old_version_path) + + +def download_compiler(os_name, tmp_dir_name): + assert os_name in os_name_list + + dl_file_suffix = os_dl_suffix[os_name] + + try: + latest_release = requests.get(lastest_quingoc_release_url).text + release_info = json.loads(latest_release) + + except Exception as e: + raise RuntimeError("Failed to parse information retrieved from gitee with the " + "following error: {}".format(e)) + + assets = release_info['assets'] + quingoc_asset = None + for asset in assets: + if 'name' in asset and asset['name'].endswith(dl_file_suffix): + quingoc_asset = asset + break + if quingoc_asset is None: + raise RuntimeError( + "Failed to get quingo compiler release package for {} platform.".format(os_name)) + + quingoc_url = quingoc_asset['browser_download_url'] + \ + '/' + quingoc_asset['name'] + + try: + quingoc_response = requests.get(quingoc_url, stream=True) + except Exception as e: + raise RuntimeError("Failed to download package \"{}\" from gitee with the " + "following error: {}".format(quingoc_asset['name'], e)) + + data_size = math.ceil( + int(quingoc_response.headers['Content-Length'])/1024/1024) + + mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] + with mlir_compiler_path.open('wb') as tmp_dl_file: + print('Downloading quingoc from {}'.format(quingoc_url)) + for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024*1024), total=data_size, desc='', unit='MB'): + tmp_dl_file.write(data) + + print("installation file has been downloaded to tmp file \"{}\" ({} bytes). ".format( + mlir_compiler_path, quingoc_response.headers['Content-Length'])) + + return mlir_compiler_path + + +def download_and_install_latest_quingoc(old_version_path=None): + """Download the latest mlir quingo compiler. + """ + os_name = platform.system() + if os_name not in ['Linux', 'Windows', 'Darwin']: + raise SystemError( + "Currently pip installation does not support {}".format(os_name)) + + tmp_dir = tempfile.TemporaryDirectory() + tmp_dir_path = Path(tmp_dir.name) + + mlir_compiler_path = download_compiler(os_name, tmp_dir_path) + install_compiler(os_name, mlir_compiler_path, old_version_path) + + +def get_lastest_version(): + """Get lastest quingo compiler version + """ + + os_name = platform.system() + assert os_name in os_name_list + + dl_file_suffix = os_dl_suffix[os_name] + + try: + latest_release = requests.get(lastest_quingoc_release_url).text + release_info = json.loads(latest_release) + + except Exception as e: + raise RuntimeError("Failed to parse information retrieved from gitee with the " + "following error: {}".format(e)) + + assets = release_info['assets'] + quingoc_asset = None + for asset in assets: + if 'name' in asset and asset['name'].endswith(dl_file_suffix): + quingoc_asset = asset + break + if quingoc_asset is None: + raise RuntimeError( + "Failed to get quingo compiler release package for {} platform.".format(os_name)) + + find_version = re.search(r'\d+\.\d+\.\d+', quingoc_asset['name']) + if find_version is not None: + lastest_version = find_version.group() + else: + raise RuntimeError( + "Failed to get lastest version of quingo compiler from package \"{}\".".format(quingoc_asset['name'])) + + return lastest_version + + +def get_current_version(quingoc_path): + """Get current quingo compiler version + """ + + get_current_version_cmd = "{} --version".format(quingoc_path) + + ret_value = subprocess.run(get_current_version_cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, shell=True) + if(ret_value.returncode != 0): + raise RuntimeError("Failed to get local quingo compiler version with the" + "following error: {}".format(ret_value.stderr)) + + find_version = re.search(r'\d+\.\d+\.\d+', ret_value.stdout.split('\n')[0]) + if find_version is not None: + current_version = find_version.group() + else: + raise RuntimeError( + "Failed to get version of local quingo compiler from {}.".format(quingoc_path)) + + return current_version + + +def check_update(quingoc_path): + """Check local quingo compiler whether is latest version + """ + + current_version = get_current_version(quingoc_path) + lastest_version = get_lastest_version() + + if current_version == lastest_version: + print("Local quingoc is already the lastest version {}.".format(lastest_version)) + return False + else: + print("Local quingoc version ({}) is behind lastest version ({}). Update now.".format(current_version, lastest_version)) + return True + + +if __name__ == "__main__": + default_path = Path.home() / '.quingo' + quingoc_path = distutils.spawn.find_executable( + 'quingoc', str(default_path)) + + if quingoc_path is None: + quingoc_path = distutils.spawn.find_executable('quingoc') + + if quingoc_path is None: + download_and_install_latest_quingoc() + else: + if check_update(quingoc_path): + download_and_install_latest_quingoc(pathlib.Path(quingoc_path)) -- Gitee From b367004a844226217fda70fb5e1d6574dcdf8828 Mon Sep 17 00:00:00 2001 From: Xiang Fu Date: Fri, 7 Jan 2022 20:34:10 +0800 Subject: [PATCH 28/29] fix downloading progress bar --- src/quingo/install_quingoc.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/quingo/install_quingoc.py b/src/quingo/install_quingoc.py index 71f95a6..862eb4e 100644 --- a/src/quingo/install_quingoc.py +++ b/src/quingo/install_quingoc.py @@ -7,6 +7,7 @@ import distutils.spawn import subprocess import platform import zipfile +import sys import shutil import tempfile import datetime @@ -249,17 +250,14 @@ def download_compiler(os_name, tmp_dir_name): "following error: {}".format(quingoc_asset['name'], e)) data_size = math.ceil( - int(quingoc_response.headers['Content-Length'])/1024/1024) + int(quingoc_response.headers['Content-Length'])/1024) mlir_compiler_path = tmp_dir_name / quingoc_asset['name'] with mlir_compiler_path.open('wb') as tmp_dl_file: - print('Downloading quingoc from {}'.format(quingoc_url)) - for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024*1024), total=data_size, desc='', unit='MB'): + print('Downloading quingoc from {} ({} KiB)'.format(quingoc_url, data_size)) + for data in tqdm.tqdm(iterable=quingoc_response.iter_content(1024), total=data_size, desc='progress', unit='KB'): tmp_dl_file.write(data) - print("installation file has been downloaded to tmp file \"{}\" ({} bytes). ".format( - mlir_compiler_path, quingoc_response.headers['Content-Length'])) - return mlir_compiler_path @@ -284,7 +282,7 @@ def get_lastest_version(): os_name = platform.system() assert os_name in os_name_list - + dl_file_suffix = os_dl_suffix[os_name] try: -- Gitee From 0b97d857f1bd377b1d7abc70d4749cb2c6793d66 Mon Sep 17 00:00:00 2001 From: Xiang Fu Date: Fri, 7 Jan 2022 20:35:07 +0800 Subject: [PATCH 29/29] update version number to 0.1.4 --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 6d84803..639f349 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = quingo -version = 0.1.3 +version = 0.1.4 author = Xiang Fu author_email = gtaifu@gmail.com use_2to3 = False -- Gitee