viery simple file versionizer
This commit is contained in:
1
bin/cp-version
Symbolic link
1
bin/cp-version
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../files/cp-version.py
|
||||||
48
files/cp-version.py
Executable file
48
files/cp-version.py
Executable 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)
|
||||||
Reference in New Issue
Block a user