merge png dirs

This commit is contained in:
guidotheelen 2020-04-15 13:07:09 +02:00
parent 42d256157a
commit bfc6b1c304

View file

@ -2,10 +2,12 @@
from __future__ import print_function from __future__ import print_function
import pickle import pickle
from os import path, makedirs from os import path, makedirs, walk, listdir
import io import io
import fire import fire
import sys import sys
import shutil
from distutils.dir_util import copy_tree
from googleapiclient.discovery import build from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request from google.auth.transport.requests import Request
@ -15,10 +17,10 @@ from googleapiclient.http import MediaIoBaseDownload
SCOPES = ["https://www.googleapis.com/auth/drive"] SCOPES = ["https://www.googleapis.com/auth/drive"]
# Use this file like this: # Use this file like this:
# python get_png_files_from_drive.py main "Drive_folder_name" "Output_dir_name" # python get_png_files_from_drive.py main "Drive_folder_name" "Output_dir_name" --reporting
def main(folder_name, output_dir): def main(folder_name, output_dir, reporting=False):
# Create a token.pickle file to store the users session # Create a token.pickle file to store the users session
service = get_service() service = get_service()
@ -35,6 +37,13 @@ def main(folder_name, output_dir):
# Download the files from the drive and put them in the output_dir # Download the files from the drive and put them in the output_dir
download_files(service, file_list, output_dir) download_files(service, file_list, output_dir)
if reporting:
# Generate a report on the downloaded files
report_on_download(output_dir)
# Create a combined PNG dir. Files in the png dir are the default
merge_png_dirs(output_dir)
def get_service(): def get_service():
@ -145,5 +154,39 @@ def download_files(service, file_list, output_dir):
f.write(fh.getbuffer()) f.write(fh.getbuffer())
def report_on_download(output_dir):
path, dirs, files = next(walk("./png/128"))
file_count = len(files)
print(files)
print(f"The original PNG directory contains {file_count} files.")
out_path, out_dirs, out_files = next(walk(output_dir))
downloaded_file_count = len(out_files)
print(f"The downloaded PNG directory contains {downloaded_file_count} files.")
overlap_count = 0
new_file_count = 0
for file in out_files:
if file in files:
overlap_count += 1
else:
new_file_count += 1
print(f"{overlap_count} files in the {output_dir} are also in the original PNG folder")
print(f"{new_file_count} files are new files that dont excist in the original PNG folder")
print("These folders are now being combined in the combined_png directory")
def merge_png_dirs(output_dir):
copy_tree("./png/128", "./combined_png")
src_files = listdir(output_dir)
for file_name in src_files:
full_file_name = path.join(output_dir, file_name)
if path.isfile(full_file_name):
shutil.copy(full_file_name, "./combined_png")
shutil.rmtree(output_dir)
if __name__ == "__main__": if __name__ == "__main__":
fire.Fire() fire.Fire()