-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhelper.py
More file actions
106 lines (93 loc) · 2.98 KB
/
Copy pathhelper.py
File metadata and controls
106 lines (93 loc) · 2.98 KB
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
import os
import subprocess
from dotenv import load_dotenv
def get_system_username():
"""
Get the current system username.
---
Parameters:
---
Returns:
str: System username.
"""
return os.getlogin()
def get_system_node_name():
"""
Get the system node name.
---
Parameters:
---
Returns:
str: System node name
"""
return os.uname().nodename
def get_ip_address():
try:
result = subprocess.run(['hostname', '-I'], capture_output=True, text=True, check=True)
ip_address = result.stdout.split()[0]
return ip_address
except (IndexError, subprocess.CalledProcessError) as e:
return None
def check_installation_information():
# Output dictionary to store results
output = {
"is_git_repo": False,
"git_branch": None,
"git_commit": None,
"git_repo": None,
"last_commit_date": None,
"last_commit_message": None,
"update_available": False
}
# Check if .git directory exists
if not os.path.isdir(".git"):
return output
# Read the HEAD file to get the current branch
try:
with open(".git/HEAD", "r") as f:
head = f.read().strip()
except IOError:
return output
# Check if HEAD is a branch
if head.startswith("ref: refs/heads/"):
branch = head.replace("ref: refs/heads/", "")
output["is_git_repo"] = True
output["git_branch"] = branch
# Get the last commit information
try:
result = subprocess.run(["git", "log", "-1", "--pretty=format:%H|%ad|%s", "--date=short"], capture_output=True, text=True, check=True)
commit_data = result.stdout.split("|")
output["git_commit"] = commit_data[0]
output["last_commit_date"] = commit_data[1]
output["last_commit_message"] = commit_data[2]
except subprocess.CalledProcessError:
pass
# Check for updates
try:
result = subprocess.run(["git", "status", "-uno"], capture_output=True, text=True, check=True)
if "Your branch is up to date" in result.stdout:
output["update_available"] = False
else:
output["update_available"] = True
except subprocess.CalledProcessError:
pass
return output
def load_secret_key():
"""Load the secret key for the application."""
load_dotenv()
try:
secret_key = os.getenv('SYSTEMGUARD_KEY')
if secret_key:
return secret_key
else:
try:
with open('secret.key', 'rb') as key_file:
secret_key = key_file.read()
return secret_key
except FileNotFoundError:
raise FileNotFoundError("The secret key file 'secret.key' was not found.")
except Exception as e:
raise RuntimeError(f"An error occurred while reading the secret key: {e}")
except Exception as e:
# temporary fix
return "1234567890123456"