Example #1
0
def test_tools_variants(monkeypatch, tool_name, tool_variant_name,
                        is_expect_fail: bool):
    host, target, base = "mac", "desktop", "https://example.com"
    datafile = f"{host}-{target}-{tool_name}"
    update_xml = (Path(__file__).parent / "data" /
                  f"{datafile}-update.xml").read_text("utf-8")

    def _mock(self, *args):
        self.update_xml_text = update_xml

    monkeypatch.setattr(QtArchives, "_download_update_xml", _mock)

    if is_expect_fail:
        expect_help = [
            f"Please use 'aqt list-tool {host} {target} {tool_name}' to show tool variants available."
        ]
        with pytest.raises(NoPackageFound) as e:
            ToolArchives(host, target, tool_name, base, arch=tool_variant_name)
        assert e.type == NoPackageFound
        assert tool_variant_name in str(
            e.value), "Message should include the missing variant"
        assert e.value.suggested_action == expect_help
        return

    expect_json = json.loads((Path(__file__).parent / "data" /
                              f"{datafile}-expect.json").read_text("utf-8"))
    expect = next(
        filter(lambda x: x["Name"] == tool_variant_name,
               expect_json["variants_metadata"]))
    expected_7z_files = set(expect["DownloadableArchives"])
    qt_pkgs = ToolArchives(host,
                           target,
                           tool_name,
                           base,
                           arch=tool_variant_name).archives
    url_begin = posixpath.join(
        base, f"online/qtsdkrepository/mac_x64/{target}/{tool_name}")
    expected_archive_url_pattern = re.compile(r"^" + re.escape(url_begin) +
                                              "/(" + expect["Name"] + ")/" +
                                              re.escape(expect["Version"]) +
                                              r"(.+\.7z)$")

    for pkg in qt_pkgs:
        if not expect["Description"]:
            assert not pkg.package_desc
        else:
            assert pkg.package_desc == expect["Description"]
        url_match = expected_archive_url_pattern.match(pkg.archive_url)
        assert url_match
        actual_variant_name, archive_name = url_match.groups()
        assert actual_variant_name == tool_variant_name
        assert pkg.archive == archive_name
        assert pkg.hashurl == pkg.archive_url + ".sha1"
        assert archive_name in expected_7z_files
        expected_7z_files.remove(archive_name)
    assert len(expected_7z_files
               ) == 0, f"Failed to produce QtPackages for {expected_7z_files}"
Example #2
0
 def run_tool(self, args):
     """Run tool subcommand"""
     start_time = time.perf_counter()
     arch = args.arch
     tool_name = args.tool_name
     os_name = args.host
     output_dir = args.outputdir
     sevenzip = self._set_sevenzip(args)
     version = args.version
     if args.base is not None:
         base = args.base + '/online/qtsdkrepository/'
     else:
         base = BASE_URL
     if args.timeout is not None:
         timeout = (args.timeout, args.timeout)
     else:
         timeout = (5, 5)
     self._run_common_part(output_dir, base)
     if not self._check_tools_arg_combination(os_name, tool_name, arch):
         self.logger.warning(
             "Specified target combination is not valid: {} {} {}".format(
                 os_name, tool_name, arch))
     try:
         tool_archives = ToolArchives(os_name,
                                      tool_name,
                                      version,
                                      arch,
                                      base,
                                      logging=self.logger,
                                      timeout=timeout)
     except ArchiveConnectionError:
         try:
             self.logger.warning(
                 "Connection to the download site failed and fallback to mirror site."
             )
             tool_archives = ToolArchives(os_name,
                                          tool_name,
                                          version,
                                          arch,
                                          random.choice(FALLBACK_URLS),
                                          logging=self.logger,
                                          timeout=timeout)
         except Exception:
             self.logger.error(
                 "Connection to the download site failed. Aborted...")
             exit(1)
     except ArchiveDownloadError or ArchiveListError:
         exit(1)
     self.call_installer(tool_archives, output_dir, sevenzip)
     self.logger.info("Finished installation")
     self.logger.info("Time elapsed: {time:.8f} second".format(
         time=time.perf_counter() - start_time))
Example #3
0
 def run_tool(self, args):
     start_time = time.perf_counter()
     arch = args.arch
     tool_name = args.tool_name
     os_name = args.host
     output_dir = args.outputdir
     sevenzip = self._set_sevenzip(args)
     version = args.version
     mirror = args.base
     self._run_common_part(output_dir, mirror)
     if not self._check_tools_arg_combination(os_name, tool_name, arch):
         self.logger.warning(
             "Specified target combination is not valid: {} {} {}".format(
                 os_name, tool_name, arch))
     try:
         tool_archives = ToolArchives(os_name,
                                      tool_name,
                                      version,
                                      arch,
                                      mirror=mirror,
                                      logging=self.logger)
     except ArchiveDownloadError or ArchiveListError:
         exit(1)
     else:
         self.call_installer(tool_archives, output_dir, sevenzip)
     self.logger.info("Finished installation")
     self.logger.info("Time elasped: {time:.8f} second".format(
         time=time.perf_counter() - start_time))
Example #4
0
 def run_tool(self, args):
     start_time = time.perf_counter()
     arch = args.arch
     tool_name = args.tool_name
     os_name = args.host
     output_dir = args.outputdir
     sevenzip = self._set_sevenzip(args)
     version = args.version
     mirror = args.base
     self._check_mirror(mirror)
     if not self._check_tools_arg_combination(os_name, tool_name, arch):
         self.logger.warning(
             "Specified target combination is not valid: {} {} {}".format(
                 os_name, tool_name, arch))
     QtInstaller(ToolArchives(os_name,
                              tool_name,
                              version,
                              arch,
                              mirror=mirror,
                              logging=self.logger),
                 logging=self.logger,
                 command=sevenzip,
                 target_dir=output_dir).install()
     self.logger.info("Time elasped: {time:.8f} second".format(
         time=time.perf_counter() - start_time))
Example #5
0
 def run_tool(self, args):
     arch = args.arch
     tool_name = args.tool_name
     os_name = args.host
     output_dir = args.outputdir
     sevenzip = self._set_sevenzip(args)
     version = args.version
     mirror = self._check_mirror(args)
     if not self._check_tools_arg_combination(os_name, tool_name, arch):
         self.logger.error("Specified target combination is not valid: {} {} {}".format(os_name, tool_name, arch))
         exit(1)
     QtInstaller(ToolArchives(os_name, tool_name, version, arch, mirror=mirror, logging=self.logger),
                 logging=self.logger).install(command=sevenzip, target_dir=output_dir)
Example #6
0
def test_tool_archive_wrong_version(monkeypatch, tool_name, variant_name,
                                    version, actual_version):
    def _mock(self, *args):
        return to_xml([dict(Name=variant_name, Version=actual_version)])

    monkeypatch.setattr(QtArchives, "_download_update_xml", _mock)

    host, target, base = "mac", "desktop", "https://example.com"
    with pytest.raises(NoPackageFound) as e:
        ToolArchives(host,
                     target,
                     tool_name,
                     base,
                     version_str=version,
                     arch=variant_name)
    assert e.type == NoPackageFound
Example #7
0
    def run_install_tool(self, args):
        """Run tool subcommand"""
        start_time = time.perf_counter()
        self.show_aqt_version()
        if args.is_legacy:
            self._warn_on_deprecated_command("tool", "install-tool")
        tool_name = args.tool_name  # such as tools_openssl_x64
        os_name = args.host  # windows, linux and mac
        target = "desktop" if args.is_legacy else args.target  # desktop, android and ios
        output_dir = args.outputdir
        if output_dir is None:
            base_dir = os.getcwd()
        else:
            base_dir = output_dir
        sevenzip = self._set_sevenzip(args.external)
        if EXT7Z and sevenzip is None:
            # override when py7zr is not exist
            sevenzip = self._set_sevenzip(Settings.zipcmd)
        version = getattr(args, "version", None)  # for legacy aqt tool
        keep = args.keep
        if args.base is not None:
            base = args.base
        else:
            base = Settings.baseurl
        if args.timeout is not None:
            timeout = (args.timeout, args.timeout)
        else:
            timeout = (Settings.connection_timeout, Settings.response_timeout)
        if args.tool_variant is None:
            archive_id = ArchiveId("tools", os_name, target, "")
            meta = MetadataFactory(archive_id,
                                   is_latest_version=True,
                                   tool_name=tool_name)
            try:
                archs = meta.getList()
            except ArchiveDownloadError as e:
                msg = f"Failed to locate XML data for the tool '{tool_name}'."
                raise ArchiveListError(
                    msg, suggested_action=suggested_follow_up(meta)) from e

        else:
            archs = [args.tool_variant]

        for arch in archs:
            if not self._check_tools_arg_combination(os_name, tool_name, arch):
                self.logger.warning(
                    "Specified target combination is not valid: {} {} {}".
                    format(os_name, tool_name, arch))

            tool_archives: ToolArchives = self.retry_on_bad_connection(
                lambda base_url: ToolArchives(
                    os_name=os_name,
                    tool_name=tool_name,
                    target=target,
                    base=base_url,
                    version_str=version,
                    arch=arch,
                    timeout=timeout,
                ),
                base,
            )
            run_installer(tool_archives.get_packages(), base_dir, sevenzip,
                          keep)
        self.logger.info("Finished installation")
        self.logger.info("Time elapsed: {time:.8f} second".format(
            time=time.perf_counter() - start_time))