"""install.py: copy files from to_system into system directories """ import os import filecmp import shutil from os.path import join, getmtime, exists os.chdir('to_system') DEL = '__to_delete__' def walk(action): for dirpath, _, files in os.walk('.'): syspath = dirpath[1:] # remove leading '.' action.dirpath = dirpath action.syspath = syspath if files: match, mismatch, missing = filecmp.cmpfiles(dirpath, syspath, files) if mismatch: newer = [f for f in mismatch if getmtime(join(syspath, f)) > getmtime(join(dirpath, f))] if newer: action.newer(newer) if len(newer) < len(mismatch): newer = set(newer) action.older([f for f in mismatch if f not in newer]) if missing: if DEL in missing: missing.remove(DEL) with open(join(dirpath, DEL)) as fil: to_delete = [] for f in fil: f = f.strip() if f and exists(join(syspath, f)): if exists(join(dirpath, f)): print('ERROR: %s in %s, but also in repo -> ignored' % (f, DEL)) else: to_delete.append(f) action.delete(to_delete) if missing: action.missing(missing) class Show: def show(self, title, files): print('%s %s:\n %s' % (title, self.syspath, ' '.join(files))) def newer(self, files): self.show('get from', files) def older(self, files): self.show('replace in', files) def missing(self, files): self.show('install in', files) def delete(self, files): self.show('remove from', files) class Do: def newer(self, files): for file in files: shutil.copy(join(self.syspath, file), join(self.dirpath, file)) def older(self, files): for file in files: shutil.copy(join(self.dirpath, file), join(self.syspath, file)) def missing(self, files): self.older(files) def delete(self, files): for file in files: os.remove(join(self.syspath, file)) walk(Show()) answer = input('do above?') if answer.lower().startswith('y'): walk(Do())