Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
213 lines (163 loc) · 6.41 KB
/
Copy pathtest.py
File metadata and controls
213 lines (163 loc) · 6.41 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import hashlib
import pwd
from myGit import createblob
#from simulation import createblobfromtree
def createEntries(directory):
"""not used because it is not yet finished"""
array = []
if os.path.isfile(directory):
print("make a node out of")
array.append((100644, os.path.basename(directory), createblob(directory)))
elif os.path.isdir(directory):
print("working")
for elt in os.listdir(directory):
elt_path = os.path.join(directory, elt)
array.append((600233, os.path.basename(elt_path)))
createEntries(elt_path)
print(elt_path)
return array
def tree_to_dictionary_recursive(directory=".", result=None):
"""turns a directory into a entries tuples that can be used in the createEntries function"""
if result is None:
result = {}
if os.path.isdir(directory):
if os.listdir(directory):
for elt in os.listdir(directory):
elt_path = os.path.relpath(os.path.join(directory, elt), ".")
# Add current node
tree_to_dictionary_recursive(elt_path, result) # Visit left
#
elif os.path.isfile(directory):
result.update({f"{directory}": f"{createblob(directory)}"})
return result
flat_dict = tree_to_dictionary_recursive("test")
def flat_to_tree(flat_dict):
"turn a flat dictionary into a nested tree"
tree = {}
for path, sha in flat_dict.items():
parts = path.split("/") # e.g., ['test', 'cool', 'good.txt']
current = tree
# 1. Traverse or create the directory folders
for folder in parts[:-1]:
current = current.setdefault(folder, {})
# 2. Place the file name and its hash at the final leaf node
file_name = parts[-1]
current[file_name] = sha
return tree
def flatten_tree(tree, current_path=""):
flat = {}
for key, value in tree.items():
# Build the path dynamically (e.g., "test/cool")
new_path = f"{current_path}/{key}" if current_path else key
if isinstance(value, dict):
# If it's a folder (dictionary), go deeper recursively
flat.update(flatten_tree(value, new_path))
else:
# If it's a file string, we hit the leaf node. Store it!
flat[new_path] = value
return flat
def blob_tree_from_dict(dictionary):
"creates blobs(sha values) for folders and files recursively out of a given nested dict."
tree = b""
blobValue = b""
destination_path = ".mgit/Dest"
if isinstance(dictionary, dict):
for key, value in dictionary.items():
if isinstance(value, dict):
tree += f"123000 tree {blob_tree_from_dict(item_path).decode('utf-8')} {item_path}\n".encode(
"utf-8"
)
blobValue = createblobfromtree(tree)
# print(tree.decode("utf-8"))
with open(destination_path, "a") as f:
f.write(tree.decode())
print(blobValue)
print("\n")
else:
tree += (
b"300444 blob "
+ f"{(createblob(item_path))} {item_path}\n".encode("utf-8")
)
print(item_path)
blobValue = createblobfromtree(tree)
with open(destination_path, "a") as f:
f.write(tree.decode())
# print(tree.decode("utf-8"))
print(blobValue)
print("\n")
else:
blobValue = value
return blobValue
def blob_tree_from_dict(node_directory):
"creates blobs(sha values) for folders and files recursively out of a given nested dict."
tree = b""
final = b""
t_path = ".mgit/Head"
# t_path= os.path.join(t_path, "file")
if os.listdir(node_directory) or os.path.isfile(node_directory):
for item in os.listdir(node_directory):
item_path = os.path.join(node_directory, item)
if os.path.isdir(item_path): # if dir is node, recurse
# tree.append(item_path)
tree += f"123000 tree {blob_tree_from_dict(item_path).decode('utf-8')} {item_path}\n".encode(
"utf-8"
)
final = createblobfromtree(tree)
# print(tree.decode("utf-8"))
with open(t_path, "a") as f:
f.write(tree.decode())
print(final)
print("\n")
elif os.path.isfile(item_path):
tree += (
b"300444 blob "
+ f"{(createblob(item_path))} {item_path}\n".encode("utf-8")
)
print(item_path)
final = createblobfromtree(tree)
with open(t_path, "a") as f:
f.write(tree.decode())
# print(tree.decode("utf-8"))
print(final)
print("\n")
return final
def createTreefromDict(nested_dict, current_path=""):
path = ""
tree = b""
for key, value in nested_dict.items():
new_path = os.path.join(current_path, key) if current_path else key
if isinstance(value, dict):
tree += f"110 tree {createTreefromDict(value, new_path).decode('utf-8')}\n".encode('utf-8')
else:
tree += b"300444 blob "+ f"{value}\n".encode("utf-8")
print(tree.decode('utf-8'))
return hashlib.sha1(tree).hexdigest().encode("utf-8")
nested_tree = {
"src": {
"components": {
"Button.js": "ann",
"Header.js": "hhaas",
},
"utils": {"helpers.js": "hhhs"},
"index.js": "hhsahh",
},
"package.json": "hallo",
}
def get_user_email():
email = os.environ.get('EMAIL')
print(email)
def get_linux_user_info():
# Obtain the numeric user ID of the current process
uid = os.getuid()
# Query system database for user details
user_entry = pwd.getpwuid(uid)
username = user_entry.pw_name
# The gecos field typically stores the user's Full Name
full_name = user_entry.pw_gecos.split(',')[0]
# System mail configurations default locally or look at $EMAIL variable
email = os.environ.get('EMAIL') or f"{username}@localhost"
print(f"Username: {username}")
print(f"Full Real Name: {full_name}")
print(f"System Email: {email}")
get_linux_user_info()