Skip to content

Instantly share code, notes, and snippets.

@MahdiNazemi
Created January 21, 2022 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MahdiNazemi/df653b99a70c9cac43d1b8fb4d68c951 to your computer and use it in GitHub Desktop.
Save MahdiNazemi/df653b99a70c9cac43d1b8fb4d68c951 to your computer and use it in GitHub Desktop.
Rename files of a Cryptomator vault after a Google Drive migration.
#!/usr/bin/env python3
import argparse
import filecmp
import os
import stat
class dircmpdiff(filecmp.dircmp):
def phase0(self):
super(dircmpdiff, self).phase0()
def phase1(self):
super(dircmpdiff, self).phase1()
def phase2(self):
self.common_dirs = []
for x in self.common:
a_path = os.path.join(self.left, x)
b_path = os.path.join(self.right, x)
ok = 1
try:
a_stat = os.stat(a_path)
except OSError:
ok = 0
if ok:
a_type = stat.S_IFMT(a_stat.st_mode)
if stat.S_ISDIR(a_type):
self.common_dirs.append(x)
def phase3(self):
raise NotImplementedError
def phase4(self):
self.subdirs = {}
for x in self.common_dirs:
a_x = os.path.join(self.left, x)
b_x = os.path.join(self.right, x)
self.subdirs[x] = self.__class__(a_x, b_x, self.ignore, self.hide)
def diff_in_dir(self):
if self.left_only:
self.left_only.sort()
if self.right_only:
self.right_only.sort()
for original, new in zip(self.left_only, self.right_only):
print("Replacing \n{} with \n{}".format(os.path.join(self.right, new), os.path.join(self.right, original)))
os.replace(os.path.join(self.right, new), os.path.join(self.right, original))
def diff_in_all_dirs(self):
self.diff_in_dir()
for sd in self.subdirs.values():
sd.diff_in_all_dirs()
methodmap = dict(subdirs=phase4, common_dirs=phase2, common=phase1,
left_only=phase1, right_only=phase1,
left_list=phase0, right_list=phase0)
def rename(args):
comparator = dircmpdiff(args.src, args.dst)
comparator.diff_in_all_dirs()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Fix file names after Google Drive migration.")
parser.add_argument('src', help="Path to the original vault")
parser.add_argument('dst', help="Path to the new vault")
args = parser.parse_args()
rename(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment