viery simple file versionizer

This commit is contained in:
ville rantanen
2018-03-10 17:56:28 +02:00
parent 324b666c1a
commit 953fa35806
2 changed files with 49 additions and 0 deletions

48
files/cp-version.py Executable file
View File

@@ -0,0 +1,48 @@
#!/usr/bin/env python
import os, argparse
from shutil import copyfile
def get_options():
parser = argparse.ArgumentParser(
description = 'Copy file with version number'
)
parser.add_argument('-m', action = "store_true", dest = "move",
help = "Move file instead of copying",
default = False
)
parser.add_argument(action = "store", dest = "file")
return parser.parse_args()
def file_versionize(full_path, move = False):
""" Move file to versioned with integer """
file_dir = os.path.dirname(full_path)
file_name = os.path.basename(full_path)
basename, extension = os.path.splitext(file_name)
version = 1
while True:
new_name = os.path.join(
file_dir,
"%s.v%02d%s"%(
basename,
version,
extension
)
)
if os.path.exists(new_name):
version += 1
else:
break
print("%s -> %s"%(
full_path,
new_name
))
if move:
os.rename(full_path, new_name)
else:
copyfile(full_path, new_name)
if __name__ == "__main__":
opts = get_options()
file_versionize(opts.file, opts.move)