Created
July 17, 2015 05:17
-
-
Save rebolyte/2b5d4c314b78a467fc46 to your computer and use it in GitHub Desktop.
Playing around with Python
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
| """ | |
| title: nginx log rotator | |
| author: James Irwin | |
| date: 16-Jul-2015 | |
| purpose: A test to play around with Python. | |
| """ | |
| import os | |
| from datetime import datetime | |
| import shutil | |
| # import zipfile | |
| start_time = datetime.now() | |
| print("---Starting faux nginx log rotator") | |
| # print("directory: {}".format(os.getcwd())) | |
| # NOTE that these are not the only files that will be archived. Any *.log file | |
| # will be slurped below and archived. These are just for safety/sanity checks. | |
| expected_log_files = [ | |
| "access.log", | |
| "error.log", | |
| "https_access.log", | |
| "https_errs.log" | |
| ] | |
| def check_files_exist(files_list): | |
| res = True | |
| for f in files_list: | |
| if os.path.isfile(f) == False: | |
| res = False | |
| return res | |
| def all_empty_files(files_list): | |
| res = True | |
| sizes = [] | |
| for f in files_list: | |
| sizes.append(os.path.getsize(f)) | |
| for size in sizes: | |
| if size != 0: | |
| res = False | |
| return res | |
| today_dir = 'logs_{}'.format(datetime.now().strftime("%d-%b-%Y")) | |
| today_archive = today_dir + '.zip' | |
| # Perform safety checks | |
| if os.path.isfile(today_archive): | |
| print("Archive for today ({}) already exists. Quitting.".format(today_archive)) | |
| quit() | |
| if not check_files_exist(expected_log_files): | |
| print("You seem to be missing an expected log file. Quitting. (Are you running this in the nginx log dir?)") | |
| quit() | |
| if all_empty_files(expected_log_files): | |
| print("Looks like all log files are empty. Quitting. (Did you mean to run this right now?)") | |
| quit() | |
| # Create the directory that we'll zip up | |
| try: | |
| os.makedirs(today_dir, exist_ok=False) | |
| except OSError as exception: | |
| print("Temporary output directory already exists! Quitting. (Did the last run of this script fail?)") | |
| quit() | |
| # list() is just so we can find the length of the list later | |
| files_to_process = list(filter(lambda f: os.path.isfile(f) and f.endswith('.log'), os.listdir(os.curdir))) | |
| print("Copying {} log files to temp directory...".format(len(files_to_process)), end="") | |
| for f in files_to_process : | |
| # shutil.move(f, today_dir) # for production | |
| shutil.copy2(f, today_dir) # for testing | |
| print("done.") | |
| print("Zipping up the log files...", end="") | |
| # This is just one way to do it; we could also use zipfile and zlib to add | |
| # specific files. | |
| # make_archive(basename, format, dir to zip) | |
| shutil.make_archive(today_dir, 'zip', root_dir=today_dir) | |
| print("done.") | |
| print("Creating blank log files for nginx...", end="") | |
| # re-enable for production | |
| # for f in expected_log_files: | |
| # open(f, 'x').close() | |
| # print("done.") | |
| print("skipping.") | |
| print("Removing temporary directory...", end="") | |
| # os.rmdir() fails with files still in the dir | |
| shutil.rmtree(today_dir) | |
| print("done.") | |
| print("Completed in {}".format(datetime.now() - start_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment