From 6f296a31adefb1dcb7dd308e67702b1c29feed0d Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 1 Apr 2022 02:24:01 +0200 Subject: [PATCH] tools: move gap_exec to utils.py --- tools/scan_for_updates.py | 17 +++-------------- tools/tests/test_scan_for_updates.py | 3 +-- tools/utils.py | 16 ++++++++++++++-- tools/validate_package.py | 5 +++-- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/tools/scan_for_updates.py b/tools/scan_for_updates.py index 857f530a4..281af3aca 100755 --- a/tools/scan_for_updates.py +++ b/tools/scan_for_updates.py @@ -27,15 +27,15 @@ import json import os import requests -import subprocess from os.path import join from multiprocessing.pool import ThreadPool from download_packages import download_archive +import utils from utils import notice, error, warning, all_packages, metadata, metadata_fname, sha256, archive_name -from typing import Dict, Optional, Tuple +from typing import Dict, Optional archive_dir = "_archives" pkginfos_dir = "_pkginfos" @@ -54,17 +54,6 @@ def download_pkg_info(pkg_name: str) -> Optional[bytes]: return response.content -def gap_exec(commands: str, args="") -> Tuple[int, bytes]: - with subprocess.Popen( - "gap -A -b --quitonbreak -q " + args, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - shell=True, - ) as GAP: - out, err = GAP.communicate(input=commands.encode('utf-8')) - return GAP.returncode, out - - def scan_for_one_update(pkginfos_dir: str, pkg_name: str) -> Optional[str]: pkg_json = metadata(pkg_name) try: @@ -98,7 +87,7 @@ def scan_for_updates(pkginfos_dir = pkginfos_dir, disable_threads = False): def output_json(updated_pkgs, pkginfos_dir = pkginfos_dir): dir_of_this_file = os.path.dirname(os.path.realpath(__file__)) str = '", "'.join(updated_pkgs) - result, _ = gap_exec( + result, _ = utils.gap_exec( 'OutputJson(["{}"], "{}");'.format(str, pkginfos_dir), args="{}/pkginfo_to_json.g".format(dir_of_this_file), ) diff --git a/tools/tests/test_scan_for_updates.py b/tools/tests/test_scan_for_updates.py index 6fcda4d9a..bf98877a6 100644 --- a/tools/tests/test_scan_for_updates.py +++ b/tools/tests/test_scan_for_updates.py @@ -19,14 +19,13 @@ from scan_for_updates import ( download_pkg_info, - gap_exec, main, output_json, scan_for_one_update, scan_for_updates, - sha256, ) +from utils import gap_exec, sha256 @pytest.fixture def ensure_in_tests_dir(): diff --git a/tools/utils.py b/tools/utils.py index dd5d79578..052d31bcb 100644 --- a/tools/utils.py +++ b/tools/utils.py @@ -10,13 +10,14 @@ import hashlib import json import os -import sys import requests +import subprocess +import sys import tempfile from os.path import join -from typing import NoReturn +from typing import NoReturn, Tuple # print notices in green def notice(msg): @@ -128,3 +129,14 @@ def symlink(target, link_name, overwrite=False): if os.path.islink(temp_link_name): os.remove(temp_link_name) raise + + +def gap_exec(commands: str, args: str = "") -> Tuple[int, bytes]: + with subprocess.Popen( + "gap -A -b --quitonbreak -q " + args, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + shell=True, + ) as GAP: + out, err = GAP.communicate(input=commands.encode('utf-8')) + return GAP.returncode, out diff --git a/tools/validate_package.py b/tools/validate_package.py index f02e6afee..e7b50d19b 100755 --- a/tools/validate_package.py +++ b/tools/validate_package.py @@ -42,8 +42,9 @@ from tempfile import TemporaryDirectory from download_packages import download_archive -from scan_for_updates import download_pkg_info, gap_exec +from scan_for_updates import download_pkg_info +import utils from utils import notice, warning, error, normalize_pkg_name, archive_name, metadata, sha256 @@ -105,7 +106,7 @@ def main(pkgs): pkgdir = join(tempdir, validate_tarball(archive_fname)) shutil.unpack_archive(archive_fname, tempdir) validate_package(archive_fname, pkgdir, pkg_name) - result, _ = gap_exec( + result, _ = utils.gap_exec( "ValidatePackagesArchive(\"{}\", \"{}\");".format(pkgdir, pkg_name), args="{}/validate_package.g".format(dir_of_this_file), )