Last active
December 21, 2019 12:31
-
-
Save breezewish/8b3328fae820b9ac59fa227dd2ad75af to your computer and use it in GitHub Desktop.
Fun
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/python | |
from __future__ import print_function | |
import argparse | |
import json | |
import os | |
import sys | |
def collect_mtime_list(path): | |
dirs = os.walk(path) | |
mtime_list = [] | |
for (dirpath, dirnames, filenames) in dirs: | |
files = dirnames + filenames | |
for file in files: | |
path = os.path.join(dirpath, file) | |
file_info = os.lstat(path) | |
mtime_list.append([path, file_info.st_mtime]) | |
return mtime_list | |
def apply_mtime_list(mtime_list): | |
n = 0 | |
for item in mtime_list: | |
path = item[0] | |
mtime = item[1] | |
try: | |
if os.path.exists(path): | |
os.utime(path, (mtime, mtime)) | |
n = n + 1 | |
except: | |
pass | |
return n | |
def main(): | |
DEFAULT_META_FILE_NAME = 'mtime.json' | |
DEFAULT_DIRECTORY = os.getenv('CARGO_HOME', os.path.expanduser('~/.cargo')) | |
parser = argparse.ArgumentParser() | |
subparsers = parser.add_subparsers(dest='mode') | |
save_parser = subparsers.add_parser('save') | |
save_parser.add_argument('--meta-file', default=DEFAULT_META_FILE_NAME) | |
save_parser.add_argument('--directory', default=DEFAULT_DIRECTORY) | |
restore_parser = subparsers.add_parser('restore') | |
restore_parser.add_argument('--meta-file', default=DEFAULT_META_FILE_NAME) | |
args = parser.parse_args() | |
if args.mode == 'save': | |
print('Saving mtime from files in {} to {}'.format(args.directory, args.meta_file)) | |
mtime_list = collect_mtime_list(args.directory) | |
mtime_file = open(args.meta_file, 'w') | |
json.dump(mtime_list, mtime_file) | |
print('Collected {} files'.format(len(mtime_list))) | |
elif args.mode == 'restore': | |
print('Restoring mtime from {}'.format(args.meta_file)) | |
if not os.path.exists(args.meta_file): | |
print('{} not found'.format(args.meta_file), file=sys.stderr) | |
sys.exit(1) | |
mtime_file = open(args.meta_file, 'r') | |
mtime_list = json.load(mtime_file) | |
applied = apply_mtime_list(mtime_list) | |
print('Applied mtime for {} of {} files'.format(applied, len(mtime_list))) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment