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
#!/bin/python | |
import click | |
from os import walk, makedirs | |
from os.path import join | |
from shutil import copy | |
@click.command() | |
@click.option('--source', help="""Path to the grails 2 project""", type=file) | |
@click.option('--destination', | |
help="""Path to the bootstrapped grails 3 project""", type=file) | |
def migrate(source, destination): | |
pass | |
def migrate_folder(old_folder, new_folder, destination_root): | |
""" | |
Both old_folder and new_folder are expected to be paths (str) | |
destination_root has to be an absolute one. | |
will move files present in old_folder but not in new_folder. | |
""" | |
old_files, old_folders = gather_paths(old_folder) | |
new_files, new_folders = gather_paths(new_folder) | |
create_folders(old_folders, new_folders) | |
move_files(old_files, new_files) | |
def get_diff(paths1, paths2): | |
""" | |
Returns paths present in paths1 but absent in paths2 | |
""" | |
diff = [p for p in paths1 if not p in paths2] | |
return diff | |
def move_files(old_files, new_files, destination_root): | |
diff = get_diff(old_files, new_files) | |
diff = [(p,join(destination_root, p)) for p in diff] | |
for old_path, new_path in diff: | |
copy(old_path, new_path) | |
def create_folders(old_folders, new_folders, destination_root): | |
diff = get_diff(old_folders, new_folders) | |
diff = [join(destination_root, p) for p in diff] | |
for p in diff: | |
makedirs(p) | |
def gather_paths(folder): | |
""" | |
For given path returns tuple: | |
files, folders | |
each being a list of paths relative to folder | |
""" | |
files_present = [] | |
folders_present = [] | |
for root, dirs, files in walk(folder): | |
for name in files: | |
files_present.append(os.path.join(root, name)) | |
for name in dirs: | |
folders_present.append(os.path.join(root, name)) | |
return files_present, folders_present | |
if __name__ == "__main__": | |
migrate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment