Skip to content

Commit dc61380

Browse files
author
Konstantin Pankratov
committed
Final state. Main is a mess but it works.
1 parent 62aa8da commit dc61380

File tree

1,140 files changed

+77
-201944
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,140 files changed

+77
-201944
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# Compiled Python files
3+
__pycache__/
4+
*.py[cod]
5+
6+
# PyInstaller outputs
7+
build/
8+
*.spec
9+
10+
# Virtual environment
11+
pipe2/
12+
13+
# OS generated files
14+
.DS_Store
15+
Thumbs.db

dist/pipe

7.81 MB
Binary file not shown.

origin.zip

-1.49 KB
Binary file not shown.

pipe.py

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ def check_given_arguments(dest: str, origin: str, verb: bool) -> str:
7777
if checkExistence(dest):
7878
if not os.path.isdir(dest):
7979
raise InvalidPathError(f"Specified location {dest} is a file. Destination should be of type: directory")
80-
else:
80+
elif not os.path.isdir(origin):
8181
dest = os.path.join(dest, os.path.splitext(os.path.basename(origin))[0]) # Hell awaits me
82-
print(f"called {dest}")
82+
return dest
83+
else:
8384
return dest
8485
else: # non existent
8586
if askUser(f"{dest} does not exist. Do you want to create it?") == False:
@@ -90,32 +91,57 @@ def check_given_arguments(dest: str, origin: str, verb: bool) -> str:
9091
print(f"Creating {dest}")
9192
return dest
9293

93-
def copy_file(origin: str, dest: str):
94+
def copy_file(src, dst):
9495
""" Copy a file from src to dst with a progress bar and checksum verification. """
96+
if os.path.isdir(src):
97+
print(f"Skipped copying directory: {src}")
98+
return None # Return None or an appropriate value for directories
99+
95100
buffer_size = 1024 * 1024 # 1MB
96101
sha256_hash = hashlib.sha256()
102+
total_size = os.path.getsize(src)
103+
104+
if checkExistence(dst):
105+
if askUser(f"{dst} already exists. Do you want to overwrite it?"):
106+
if verb:
107+
print("removing {dst}")
108+
os.remove(dst)
109+
else:
110+
print(f"skipping {src}")
111+
return 'skipped'
97112

98-
total_size = os.path.getsize(origin)
99-
dest_file_path = os.path.join(dest, os.path.basename(origin))
100-
with open(origin, 'rb') as forigin, open(dest, 'wb') as fdest, tqdm(
101-
total=total_size, unit='B', unit_scale=True, desc=f"Copying {os.path.basename(origin)} to {dest_file_path}") as pbar:
113+
with open(src, 'rb') as forigin, open(dst, 'wb') as fdest, tqdm(
114+
total=total_size, unit='B', unit_scale=True, desc=f"Copying {os.path.basename(src)} to {dst}") as pbar:
102115
while True:
103116
buffer = forigin.read(buffer_size)
104117
if not buffer:
105118
break
106119
fdest.write(buffer)
107120
sha256_hash.update(buffer)
108121
pbar.update(len(buffer))
122+
109123
return sha256_hash.hexdigest()
110124

111-
def compare_hashes(original: str, copy: str) -> bool:
112-
""" Verify the integrity of the copied file by comparing hashes """
113-
original_hash = copy_file(original, copy)
114-
copied_hash = calculate_file_hash(copy)
115-
125+
def compare_hashes(src, dst):
126+
""" Compares hashes of the original file and the copied file. """
127+
if os.path.isdir(src):
128+
print(f"Skipping directory: {src}")
129+
return True # Skip directories
130+
131+
copied_hash = copy_file(src, dst)
132+
if copied_hash is None or copied_hash == 'skipped':
133+
return True # Handle directories or skipped files gracefully
134+
135+
original_hash = calculate_file_hash(src)
136+
116137
if original_hash == copied_hash:
138+
if verb:
139+
print(f"Integrity verified for {src}")
117140
return True
118141
else:
142+
os.remove(dst)
143+
if verb:
144+
print(f"Hashes did not match. Removing {dst}")
119145
return False
120146

121147
def calculate_file_hash(filepath: str):
@@ -161,6 +187,21 @@ def containsRar(dest: str) -> bool:
161187
return True
162188
return False
163189

190+
def copy_directory_with_integrity(src, dst):
191+
""" Recursively copies a directory and its contents with integrity checks for each file """
192+
if not os.path.exists(dst):
193+
os.makedirs(dst)
194+
if verb:
195+
print(f"Created directory {dst}")
196+
197+
for item in os.listdir(src):
198+
src_item = os.path.join(src, item)
199+
dst_item = os.path.join(dst, item)
200+
if os.path.isdir(src_item):
201+
copy_directory_with_integrity(src_item, dst_item) # Recursive call for directories
202+
else:
203+
if not compare_hashes(src_item, dst_item):
204+
print(f"File {src_item} is possibly corrupted. It was deleted automatically.")
164205
def main():
165206
global verb
166207

@@ -180,7 +221,7 @@ def main():
180221
if origin.endswith(".rar"):
181222
unrarFile(origin, dest)
182223
if origin.endswith(".zip"):
183-
unzipFile(origin, dest) # WARNING: Check if it is even a zip file
224+
unzipFile(origin, dest)
184225

185226
if not containsRar(dest):
186227
return
@@ -192,7 +233,13 @@ def main():
192233
else:
193234
if verb:
194235
print("Starting copy...")
195-
compare_hashes(origin, dest)
236+
if os.path.isdir(origin):
237+
if verb:
238+
print(f"{origin} is of type folder. Copying it's content to {dest}")
239+
copy_directory_with_integrity(origin, dest)
240+
else:
241+
if not compare_hashes(origin, dest):
242+
print(f"File {origin} is possibly corrupted. It was deleted automatically")
196243

197244
if __name__ == "__main__":
198245
main()

pipe/bin/Activate.ps1

Lines changed: 0 additions & 247 deletions
This file was deleted.

0 commit comments

Comments
 (0)