-
Notifications
You must be signed in to change notification settings - Fork 0
/
00_check_core.py
117 lines (89 loc) · 3.25 KB
/
00_check_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
======================= NW DIAGNOSTIC UTILITY ==================================
https://github.com/denisecase/nw-diagnostics-python/
================================================================================
PURPOSE:
Generate diagnostic information about the local machine and Python installation.
ORIGIN:
This is an instructor-generated script. You do not need to edit or understand
the code in this file.
USAGE:
In the terminal, run the following command:
python 00_check_core.py
OUTPUT:
See the new file named `00_check_core.txt` in your local repository.
REQUIREMENTS:
An active internet connection is required to fetch the diagnostic utility from
the GitHub repository.
CAUTION:
This script fetches and executes Python code from a remote source using
the `exec` function. While efforts have been made to ensure the security and
integrity of the hosted code, always be cautious and aware of the potential
risks associated with executing remote code. Ensure that the URL
(https://github.com/denisecase/nw-diagnostics-python/) is trusted before running the script.
================================================================================
"""
# Python Standard Library
import os
import urllib.request
# The web addresses (URLs) of the code
URLS = [
"https://raw.githubusercontent.com/denisecase/nw-diagnostics-python/main/basic/nw_check_core.py",
]
# List of output files generated by the remote code
report_files = [
"00_report_core.txt",
]
def delete_report_files():
"""
Delete the report files from the current directory.
"""
for file in report_files:
try:
os.remove(file)
except FileNotFoundError:
pass
def fetch_code(url):
"""
Fetches the code from the URL but doesn't execute it.
Args:
- url (str): The URL to fetch the Python code from.
Returns:
- str: The fetched code as a string.
"""
try:
with urllib.request.urlopen(url) as response:
return response.read().decode("utf-8")
except Exception as e:
print(f"ERROR: Failed to fetch code from {url}. Reason: {e}")
return None
def execute_diagnostic(url, function_name):
"""
Fetches, executes, and runs the diagnostic function from the given URL.
Args:
- url (str): The URL to fetch the Python code from.
- function_name (str): The name of the diagnostic function to call.
Returns:
- bool: True if successful, False otherwise.
"""
code = fetch_code(url)
if code is None:
return False
namespace = {}
exec(code, globals(), namespace)
for key in list(namespace.keys()):
globals()[key] = namespace[key]
run_diagnostic = namespace.get(function_name)
if callable(run_diagnostic):
run_diagnostic(namespace)
return True
else:
print(f"ERROR: Failed to find {function_name} in {url}.")
return False
# ---------------------------------------------------------------------------
# If this is the script we are running, then call some functions and execute code!
# ---------------------------------------------------------------------------
if __name__ == "__main__":
delete_report_files()
if not execute_diagnostic(URLS[0], "run_diagnostic_core"):
exit(1)