Created
November 10, 2013 09:26
-
-
Save akaihola/7395955 to your computer and use it in GitHub Desktop.
Move photo from one tree to another in a git repository
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
#!/usr/bin/python3.3 | |
import os | |
NEWDIR = '/home/akaihol/Kuvat_' | |
OLDDIR = '/media/akaihol/tasku/Valokuvat' | |
def get_file_fingerprint(path): | |
return os.path.getsize(path), os.path.basename(path) | |
def scan_paths(root_directory): | |
new_paths = {} | |
for root, _dirs, files in os.walk(root_directory): | |
for filename in files: | |
absolute_path = os.path.join(root, filename) | |
fingerprint = get_file_fingerprint(absolute_path) | |
relative_path = os.path.relpath(absolute_path, root_directory) | |
if not relative_path.startswith('.git/'): | |
new_paths[fingerprint] = relative_path | |
print(relative_path) | |
return new_paths | |
def move_to_new_path(from_path, to_path): | |
print('git mv', from_path, to_path) | |
def main(): | |
new_paths = scan_paths(NEWDIR) | |
old_paths = scan_paths(OLDDIR) | |
for fingerprint, path in old_paths.items(): | |
if fingerprint in new_paths and new_paths[fingerprint] != path: | |
move_to_new_path(path, new_paths[fingerprint]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment