Last active
September 22, 2020 17:38
-
-
Save aregee/abe0dda20343d71ed922d7567dcfcc7a to your computer and use it in GitHub Desktop.
This file contains 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
import shutil | |
import os | |
import argparse | |
import datetime | |
import argparse | |
''' | |
on bash execute with --path to directory containing logs | |
$ python moverename.py --path <somepath> | |
To simulate without actual move pass --dry-run true opts | |
$ python moverename.py --dry-run true --path <somepath> | |
To move the files in subfolder created yesterday | |
$ python moverename.py --move-by date --path <somepath> | |
Function: move | |
Parameters: | |
@currentDir: The path to the folder you want to travers | |
''' | |
DRY = False | |
today = datetime.date.today() | |
yesterday = f"{today.year}{today.month:02d}{today.day - 1:02d}" | |
def move_for_yesterday(currentDir, depth=None): | |
filesList = os.walk(currentDir) | |
ind = 0 | |
for rootDir, folders, files in filesList: | |
for f in files: | |
if (rootDir != currentDir): | |
toMove = os.path.join(rootDir, f) | |
date_folder = os.path.join(rootDir, f).split('/')[-2:-1][0] | |
local_root = os.path.join(rootDir, f).split('/')[-3:-2][0] | |
extension = os.path.splitext(os.path.join(currentDir, f))[1] | |
newFilename = local_root + ' - '+"{0:0=2d}".format(ind) + extension | |
newpath = os.path.join(currentDir, newFilename) | |
# If a file exists in the new path we defined, we probably do not want to move that file | |
if not os.path.exists(newpath) and yesterday == date_folder: | |
if extension in ('.gz', '.0'): | |
print(">>>>>moving<<<<") | |
print(toMove) | |
print(newpath) | |
if not DRY: | |
shutil.copy(toMove, newpath) | |
ind += 1 | |
def move_all(currentDir): | |
filesList = os.walk(currentDir) | |
ind = 0 | |
for rootDir, folders, files in filesList: | |
for f in files: | |
if (rootDir != currentDir): | |
toMove = os.path.join(rootDir, f) | |
local_root = os.path.join(rootDir, f).split('/')[-3:-2][0] | |
extension = os.path.splitext(os.path.join(currentDir, f))[1] | |
newFilename = local_root + ' - '+"{0:0=2d}".format(ind) + extension | |
newpath = os.path.join(currentDir, newFilename) | |
# If a file exists in the new path we defined, we probably do not want | |
# to over write it. So we will redefine the new path until we get a unique name | |
while os.path.exists(newpath): | |
ind += 1 | |
newFilename = local_root + ' - '+"{0:0=2d}".format(ind) + "_" + str(ind) + extension | |
newpath = os.path.join(currentDir, newFilename) | |
if extension in ('.gz', '.0'): | |
print(">>>>>moving<<<<") | |
print(toMove) | |
print(newpath) | |
if not DRY: | |
shutil.copy(toMove, newpath) | |
ind += 1 | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"-dry", "--dry-run", type=str, default="false", help="Recursively Move all files to root directory of the path provided") | |
parser.add_argument("-move", "--move-by", type=str, default="all", help="If supplied, move by date") | |
parser.add_argument("-p", "--path", type=str, help="Path to directory") | |
args = parser.parse_args() | |
truthy = ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh'] | |
if args.move_by in ['date']: | |
move = move_for_yesterday | |
else: | |
move = move_all | |
if args.path: | |
if(args.dry_run in truthy): | |
DRY = True | |
move(args.path) | |
else: | |
move(args.path) | |
else: | |
print("Invalid Choice") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment