""" By Brian Tomasik (https://briantomasik.com/). First published: 2019-11-10. Last update of any kind: 2019-11-10T02-30. To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. This script was built with Python 3.6. Hopefully it should work for later versions of Python 3 also. """ import argparse import os import shutil from datetime import datetime NAME_OF_VERSION_HISTORY_FOLDER = "vxhx" def parse_input(): parser = argparse.ArgumentParser(description="Make a version snapshot of a file to the local {}/ folder.".format(NAME_OF_VERSION_HISTORY_FOLDER)) parser.add_argument("filename", help="The name of the file to make a snapshot of.") return parser.parse_args() def check_that_the_file_is_in_the_current_dir(filename): head, tail = os.path.split(filename) assert head == "", "The file provided isn't in the current directory." assert tail == filename def get_versioned_filename_with_path(original_filename): root, extension = os.path.splitext(original_filename) versioned_filename = root + "_vao" + datetime.now().strftime("%Y-%m-%dT%H-%M") + extension return os.path.join(NAME_OF_VERSION_HISTORY_FOLDER, versioned_filename) def main(): args = parse_input() assert os.path.isfile(args.filename), "Your file '{}' doesn't exist.".format(args.filename) check_that_the_file_is_in_the_current_dir(args.filename) if not os.path.isdir(NAME_OF_VERSION_HISTORY_FOLDER): os.mkdir(NAME_OF_VERSION_HISTORY_FOLDER) versioned_filename_with_path = get_versioned_filename_with_path(args.filename) assert not os.path.isfile(versioned_filename_with_path), "Versioned file already exists for some reason." shutil.copy(args.filename, versioned_filename_with_path) assert os.path.isfile(versioned_filename_with_path), "Versioned file doesn't exist for some reason." if __name__ == "__main__": main()