Last active
June 30, 2019 15:09
-
-
Save dusekdan/e13465a194c8965af2fb6cb094163c10 to your computer and use it in GitHub Desktop.
Fragments files in the directory so they can be pushed into Github.
This file contains hidden or 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
# Recursively goes through the TARGET directory and zips files that are | |
# over SIZE_LIMIT bytes into PART_SIZE_7z parts, deleting the originals. | |
# | |
# Uses 7zip utility that needs to be ADDED INTO THE $PATH. | |
# | |
# Results in a directory structure that can be pushed into | |
# the GitHub without issues with oversized files. | |
import os, sys, subprocess | |
from time import sleep | |
# Path to the folder that should be fragmented. | |
TARGET = "X:\\_" | |
# Maximum allowed filesize in bytes. | |
SIZE_LIMIT = 95000000 | |
# 7-zip size of the part file. | |
PART_SIZE_7z = "95m" | |
def obtain_all_filepaths(target_dir): | |
files = [] | |
for r, d, f in os.walk(target_dir): | |
for file in f: | |
files.append(os.path.join(r, file)) | |
print("Discovered: %s files." % len(files)) | |
return files | |
def get_files_over_limit(files): | |
large = [] | |
for f in files: | |
size = os.stat(f).st_size | |
if size >= SIZE_LIMIT: | |
large.append(f) | |
print("Discovered: %s files over the size limit." % len(large)) | |
return large | |
def zip_file_into_parts(path): | |
print("Splitting: %s" % path) | |
command = ["7z", "a", "%s_part" % path, path, "-v%" % PART_SIZE_7z] | |
print(command) | |
system = subprocess.Popen(command, shell=True) | |
returncode = system.wait() | |
sleep(3) # Just to be sure 7zip releases the file. | |
print("File split. Deleting the original.") | |
return True | |
files = obtain_all_filepaths(TARGET) | |
large = get_files_over_limit(files) | |
for file in large: | |
zip_file_into_parts(file) | |
try: | |
os.remove(file) | |
except PermissionError as e: | |
print("PermissionError: %s" % e) | |
print("Deleting failed for: %s" % file) | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment