-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: the function of installing and checking third-party libraries
- Loading branch information
Showing
2 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from typing import Optional, Union, Iterable | ||
import subprocess | ||
|
||
|
||
def net_online() -> bool: | ||
""" | ||
Check whether the current device is connected to the Internet. | ||
""" | ||
import socket | ||
|
||
for host in '1.1.1.1', '8.8.8.8', '223.5.5.5': # Cloudflare, Google, AliDNS: | ||
try: | ||
test_connection = socket.create_connection(address=(host, 53), timeout=2) | ||
except (socket.timeout, socket.gaierror, OSError): | ||
continue | ||
else: | ||
# If the connection was successful, close it to avoid a ResourceWarning | ||
test_connection.close() | ||
return True | ||
return False | ||
|
||
|
||
def install_lib(name: Union[str, Iterable[str]], version: Optional[Union[str, Iterable[str]]] = None) -> bool: | ||
"""Install python third-party libraries.""" | ||
if isinstance(name, str): | ||
name = [name] | ||
if version is not None: | ||
if isinstance(version, str): | ||
name = [i + version for i in name] | ||
else: | ||
name = [i + version[idx] for idx, i in enumerate(name)] | ||
|
||
if version is not None: | ||
name = '=='.join(name) | ||
try: | ||
assert net_online(), "Network Connection Failure!" | ||
print(f"Third-party library {name} being installed") | ||
subprocess.check_output(f'pip install --no-cache {name}', shell=True).decode() | ||
return True | ||
except Exception as e: | ||
print(f"Warning ⚠️: Installation of {name} has failed, the installation process has been skipped") | ||
return False | ||
|
||
|
||
def check_lib(name: str, version: Optional[str] = None, install: bool = True) -> bool: | ||
""" | ||
Check if the third-party libraries have been installed. | ||
""" | ||
import pkg_resources as pkg | ||
|
||
flag = True | ||
try: | ||
pkg.require(name) | ||
except pkg.DistributionNotFound: | ||
try: | ||
import importlib | ||
|
||
importlib.import_module(next(pkg.parse_requirements(name)).name) | ||
except ImportError: | ||
flag = False | ||
except pkg.VersionConflict: | ||
pass | ||
|
||
if install and not flag: | ||
return install_lib(name, version=version) | ||
return flag |