call it file-version instead

This commit is contained in:
2018-07-10 11:19:52 +03:00
parent 162c10e846
commit 8ba01c110b
3 changed files with 75 additions and 27 deletions

View File

@@ -1 +0,0 @@
../files/cp-version.py

View File

@@ -9,7 +9,8 @@ function helpexit() {
echo "Usage: $BS [-z/-n/--rm] [folder name(s)]"
echo " -z Compress."
echo " -n No compression. [default]"
echo " --rm Remove source folders after archival"
echo " --nl Do not store a separate file list."
echo " --rm Remove source folders after archival."
echo " folder name: If given, archive only the named folder(s)."
echo " If not, for loop over all folders in the current directory."
@@ -63,11 +64,13 @@ SUFFIX=tar
DEPTH=1
FOLDERS=( )
REMOVE=0
KEEP_LIST=1
for ((i=1; i<=${#@}; i++)) {
[[ "${!i}" = "-h" ]] && helpexit
[[ "${!i}" = "--help" ]] && helpexit
[[ "${!i}" = "-n" ]] && { continue; } # obsolete behaviour
[[ "${!i}" = "-z" ]] && { COMPRESSION=1; NOCOMPRESSION=0; COMPRESSCMD="| gzip"; SUFFIX=tgz; continue; }
[[ "${!i}" = "--nl" ]] && { KEEP_LIST=0; continue; }
[[ "${!i}" = "--rm" ]] && { REMOVE=1; continue; }
[[ "${!i}" = "-"* ]] && helpexit
FOLDERS+=( "${!i%/}" )
@@ -79,6 +82,7 @@ export IFS=$'\n'
REALFOLDERS=( $( listfolders "${FOLDERS[@]}" ) )
echo $R'Existing files will be overwritten!'$Z
[[ "$REMOVE" -eq 1 ]] && echo 'Source folders will be deleted!'
[[ "$KEEP_LIST" -eq 0 ]] && echo 'File list will be removed.'
echo -n $S"Archive the following folders "
[[ "$NOCOMPRESSION" -eq 1 ]] && {
echo -n without compression;
@@ -98,6 +102,9 @@ for d in "${REALFOLDERS[@]}"; do
eval "tar cvv --index-file=\"$d.${SUFFIX}.lst\" \"$d\" | \
pv -s \"$SIZE\" $COMPRESSCMD > \"$d.${SUFFIX}\"" && {
# tar exists okay
if [ "$KEEP_LIST" -eq 0 ]; then
rm -f "$d.${SUFFIX}.lst"
fi
if [ "$REMOVE" -eq 1 ]; then
printf "${Y}Removing folder $d$Z\n"
which rm-progress &> /dev/null && rm-progress -f "$d"

View File

@@ -1,18 +1,38 @@
#!/usr/bin/env python
import os, argparse
import os, argparse, sys
from shutil import copyfile
from datetime import datetime
def get_options():
parser = argparse.ArgumentParser(
description = 'Copy file with version number'
description = 'Rename file with version number'
)
parser.add_argument('-m', action = "store_true", dest = "move",
help = "Move file instead of copying",
parser.add_argument(
'-c',
action = "store_false",
dest = "move",
default = True,
help = "Copy file instead of copying",
)
parser.add_argument(
'-d',
action = "store_true",
dest = "date",
help = "Use file's date+time as version number",
default = False
)
parser.add_argument('-d', action = "store_true", dest = "date",
help = "Use file date+time as version number",
parser.add_argument(
'-f',
action = "store_true",
dest = "force",
help = "Force overwrite",
default = False
)
parser.add_argument(
'-q',
action = "store_true",
dest = "quiet",
help = "Quiet operation",
default = False
)
parser.add_argument(action = "store", dest = "file")
@@ -20,7 +40,7 @@ def get_options():
def get_version_name(full_path):
""" Move file to versioned with integer """
""" New name versioned with integer """
file_dir = os.path.dirname(full_path)
file_name = os.path.basename(full_path)
basename, extension = os.path.splitext(file_name)
@@ -43,7 +63,7 @@ def get_version_name(full_path):
def get_date_name(full_path):
""" Move file to versioned with integer """
""" New name versioned with date of the file """
file_dir = os.path.dirname(full_path)
file_name = os.path.basename(full_path)
basename, extension = os.path.splitext(file_name)
@@ -60,9 +80,29 @@ def get_date_name(full_path):
extension
)
)
if full_path.endswith("%s%s"%(
time_formatted,
extension
)):
# file is already named with the date+time
return full_path
return new_name
def test_existing(name, overwrite):
if os.path.exists(name):
if overwrite:
os.remove(name)
else:
sys.stderr.write(
"'%s' already exists, exiting.\n"%(
name
)
)
sys.exit(1)
if __name__ == "__main__":
opts = get_options()
if os.path.isdir(opts.file):
@@ -71,10 +111,12 @@ if __name__ == "__main__":
new_name = get_date_name(opts.file)
else:
new_name = get_version_name(opts.file)
print("%s -> %s"%(
opts.file,
new_name
))
if not opts.quiet:
print("%s -> %s"%(
opts.file,
new_name
))
test_existing(new_name, opts.force)
if opts.move:
os.rename(opts.file, new_name)
else: