Exemplo n.º 1
0
def download_binaries(release=None, url=None, verbose=False, platform="auto"):
    """
    Download IDAES solvers and libraries and put them in the right location. Need
    to supply either local or url argument.

    Args:
        url (str): a url to download binary files to install files

    Returns:
        None
    """
    if verbose:
        _log.setLevel(idaeslog.DEBUG)
    idaes._create_bin_dir()
    solvers_tar = os.path.join(idaes.bin_directory, "idaes-solvers.tar.gz")
    libs_tar = os.path.join(idaes.bin_directory, "idaes-lib.tar.gz")
    fd = FileDownloader()
    arch = fd.get_sysinfo()
    if arch[1] != 64:
        _log.error("IDAES Extensions currently only supports 64bit Python.")
        raise RuntimeError(
            "IDAES Extensions currently only supports 64bit Python.")
    if platform == "auto":
        platform = arch[0]
        if platform == "linux":
            linux_dist = fd.get_os_version().replace(".", "")
            if linux_dist in idaes.config.known_binary_platform:
                platform = linux_dist
    if platform not in idaes.config.known_binary_platform:
        raise Exception("Unknow platform {}".format(platform))
    if platform in idaes.config.binary_platform_map:
        platform = idaes.config.binary_platform_map[platform]

    checksum = {}
    if release is not None:
        # if a release is specified it takes precedence over a url
        url = "/".join([_release_base_url, release])
        # if we're downloading an official release check checksum
        check_to = os.path.join(idaes.bin_directory,
                                f"sha256sum_{release}.txt")
        check_from = f"https://raw.githubusercontent.com/IDAES/idaes-ext/main/releases/sha256sum_{release}.txt"
        _log.debug("Getting release {}\n  checksum file {}".format(
            release, check_from))
        fd.set_destination_filename(check_to)
        fd.get_binary_file(check_from)
        with open(check_to, 'r') as f:
            for i in range(1000):
                line = f.readline(1000)
                if line == "":
                    break
                line = line.split(sep="  ")
                checksum[line[1].strip()] = line[0].strip()
    if url is not None:
        if not url.endswith("/"):
            c = "/"
        else:
            c = ""
        solvers_from = c.join(
            [url, "idaes-solvers-{}-{}.tar.gz".format(platform, arch[1])])
        libs_from = c.join(
            [url, "idaes-lib-{}-{}.tar.gz".format(platform, arch[1])])
        _log.debug("URLs \n  {}\n  {}\n  {}".format(url, solvers_from,
                                                    libs_from))
        _log.debug("Destinations \n  {}\n  {}".format(solvers_tar, libs_tar))
        if platform == 'darwin':
            raise Exception('Mac OSX currently unsupported')
        fd.set_destination_filename(solvers_tar)
        fd.get_binary_file(solvers_from)
        fd.set_destination_filename(libs_tar)
        fd.get_binary_file(libs_from)
    else:
        raise Exception("Must provide a location to download binaries")

    if checksum:
        # if you are downloading a release and not a specific URL verify checksum
        fn_s = "idaes-solvers-{}-{}.tar.gz".format(platform, arch[1])
        fn_l = "idaes-lib-{}-{}.tar.gz".format(platform, arch[1])
        hash_s = _hash(solvers_tar)
        hash_l = _hash(libs_tar)
        _log.debug("Solvers Hash {}".format(hash_s))
        _log.debug("Libs Hash {}".format(hash_l))
        if checksum.get(fn_s, "") != hash_s:
            raise Exception("Solver files hash does not match expected")
        if checksum.get(fn_l, "") != hash_l:
            raise Exception("Library files hash does not match expected")

    _log.debug("Extracting files in {}".format(idaes.bin_directory))
    with tarfile.open(solvers_tar, 'r') as f:
        f.extractall(idaes.bin_directory)
    _log.debug("Extracting files in {}".format(idaes.bin_directory))
    with tarfile.open(libs_tar, 'r') as f:
        f.extractall(idaes.bin_directory)
Exemplo n.º 2
0
    def test_get_os_version(self):
        f = FileDownloader()
        _os, _ver = f.get_os_version(normalize=False)
        _norm = f.get_os_version(normalize=True)
        #print(_os,_ver,_norm)
        _sys = f.get_sysinfo()[0]
        if _sys == 'linux':
            dist, dist_ver = re.match('^([^0-9]+)(.*)', _norm).groups()
            self.assertNotIn('.', dist_ver)
            self.assertGreater(int(dist_ver), 0)
            if dist == 'ubuntu':
                self.assertEqual(dist_ver, ''.join(_ver.split('.')[:2]))
            else:
                self.assertEqual(dist_ver, _ver.split('.')[0])

            if distro_available:
                d, v = f._get_distver_from_distro()
                #print(d,v)
                self.assertEqual(_os, d)
                self.assertEqual(_ver, v)
                self.assertTrue(v.replace('.', '').startswith(dist_ver))

            if os.path.exists('/etc/redhat-release'):
                d, v = f._get_distver_from_redhat_release()
                #print(d,v)
                self.assertEqual(_os, d)
                self.assertEqual(_ver, v)
                self.assertTrue(v.replace('.', '').startswith(dist_ver))

            if subprocess.run(['lsb_release'],
                              stdout=subprocess.DEVNULL,
                              stderr=subprocess.DEVNULL).returncode == 0:
                d, v = f._get_distver_from_lsb_release()
                #print(d,v)
                self.assertEqual(_os, d)
                self.assertEqual(_ver, v)
                self.assertTrue(v.replace('.', '').startswith(dist_ver))

            if os.path.exists('/etc/os-release'):
                d, v = f._get_distver_from_os_release()
                #print(d,v)
                self.assertEqual(_os, d)
                # Note that (at least on centos), os_release is an
                # imprecise version string
                self.assertTrue(_ver.startswith(v))
                self.assertTrue(v.replace('.', '').startswith(dist_ver))

        elif _sys == 'darwin':
            dist, dist_ver = re.match('^([^0-9]+)(.*)', _norm).groups()
            self.assertEqual(_os, 'macos')
            self.assertEqual(dist, 'macos')
            self.assertNotIn('.', dist_ver)
            self.assertGreater(int(dist_ver), 0)
            self.assertEqual(_norm, _os + ''.join(_ver.split('.')[:2]))
        elif _sys == 'windows':
            self.assertEqual(_os, 'win')
            self.assertEqual(_norm, _os + ''.join(_ver.split('.')[:2]))
        else:
            self.assertEqual(ans, '')

        self.assertEqual((_os, _ver), FileDownloader._os_version)
        # Exercise the fetch from CACHE
        try:
            FileDownloader._os_version, tmp \
                = ("test", '2'), FileDownloader._os_version
            self.assertEqual(f.get_os_version(False), ("test", "2"))
            self.assertEqual(f.get_os_version(), "test2")
        finally:
            FileDownloader._os_version = tmp