diff --git a/bin/cp-version b/bin/cp-version deleted file mode 120000 index bc9549d..0000000 --- a/bin/cp-version +++ /dev/null @@ -1 +0,0 @@ -../files/cp-version.py \ No newline at end of file diff --git a/files/archive-subfolders b/files/archive-subfolders index cb50333..0b94437 100755 --- a/files/archive-subfolders +++ b/files/archive-subfolders @@ -9,17 +9,18 @@ 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." - + exit } function listfolders() { - find "$@" -mindepth $DEPTH -maxdepth $DEPTH -type d | sort + find "$@" -mindepth $DEPTH -maxdepth $DEPTH -type d | sort } function count_size() { - + cat - | python -c "import sys def sizeof_fmt(num, suffix='B'): for unit in ['','K','M','G','T','P','E','Z']: @@ -42,11 +43,11 @@ print(sum) } function exitokay() { - [[ "$REMOVE" -eq 1 ]] && { exit; } + [[ "$REMOVE" -eq 1 ]] && { exit; } echo -ne "\n${G}If all looks okay, delete source folders with: ${Z}# rm -r " - for folder in "${REALFOLDERS[@]}"; do echo -n "'$folder' "; done + for folder in "${REALFOLDERS[@]}"; do echo -n "'$folder' "; done echo '' - exit + exit } _qCol(){ true; } # incase qolop missing @@ -63,41 +64,47 @@ 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%/}" ) DEPTH=0 } [[ ${#FOLDERS[@]} -eq 0 ]] && FOLDERS+=( "." ) -which pv &>/dev/null || { echo pv command missing; exit 1; } +which pv &>/dev/null || { echo pv command missing; exit 1; } export IFS=$'\n' REALFOLDERS=( $( listfolders "${FOLDERS[@]}" ) ) echo $R'Existing files will be overwritten!'$Z -[[ "$REMOVE" -eq 1 ]] && echo 'Source folders will be deleted!' +[[ "$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; -} || { +[[ "$NOCOMPRESSION" -eq 1 ]] && { + echo -n without compression; +} || { [[ "$COMPRESSION" -eq 1 ]] && echo -n with gzip compression } echo ':'$Z -for folder in "${REALFOLDERS[@]}"; do echo -n "'$folder', "; done | sed -e "s/..$//" +for folder in "${REALFOLDERS[@]}"; do echo -n "'$folder', "; done | sed -e "s/..$//" printf "\n$S to quit$Z\n" read foo set -o pipefail -for d in "${REALFOLDERS[@]}"; do +for d in "${REALFOLDERS[@]}"; do printf "$d" SIZE=$( find "$d" -type f -printf %s"\n" | count_size "$d" ) printf "\n" 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" diff --git a/files/cp-version.py b/files/file_version.py similarity index 54% rename from files/cp-version.py rename to files/file_version.py index 5befafe..06fc656 100755 --- a/files/cp-version.py +++ b/files/file_version.py @@ -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: