Created
December 13, 2014 22:25
-
-
Save cauethenorio/855041ed0e1fbc6c2e84 to your computer and use it in GitHub Desktop.
save and restore mtime from all files in a dir
This file contains hidden or 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/env python | |
# coding: utf-8 | |
import hashlib | |
from os import listdir, path, utime | |
import time | |
import pickle | |
import sys | |
mtimes = {} | |
def save_mtimes(): | |
for file in listdir('.'): | |
print('Saving {} mtime...'.format(file)) | |
mtimes[calc_file_md5(file)] = path.getmtime(file) | |
pickle.dump(mtimes, open('mtimes.pickle', 'wb')) | |
def restore_mtimes(): | |
mtimes = pickle.load(open('mtimes.pickle', 'rb')) | |
for file in listdir('.'): | |
file_md5 = calc_file_md5(file) | |
if file_md5 in mtimes: | |
print 'Restauring {} mtime...'.format(file) | |
utime(file, (mtimes[file_md5], mtimes[file_md5])) | |
def calc_file_md5(filename): | |
return hashlib.md5(open(filename).read()).hexdigest() | |
if __name__ == '__main__': | |
args = sys.argv | |
if len(args) != 2 or args[1].lower() not in ('save', 'restore'): | |
print('Arg must be \'save\' or \'restore\'') | |
action = args[1].lower() | |
{ | |
'save': save_mtimes, | |
'restore': restore_mtimes | |
}[action]() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment