copying from private repo

This commit is contained in:
Q
2024-11-26 20:09:34 +02:00
parent 045f87ed7e
commit 6245fbcba0
14 changed files with 784 additions and 0 deletions

88
files/archive-to-datefolders Executable file
View File

@@ -0,0 +1,88 @@
#!/bin/bash
set -e
_help() {
self=$( readlink -f "$0" )
grep " # " "$self" | grep -v [\)]
exit
# Moves files to subfolders YYYY-MM-DD/
# Give filenames as arguments e.x.: *
#
}
function gen_unique() {
if [[ ! -e "$1" ]]; then
echo "$1"
return
fi
pathdir=$( dirname "$1" )
fullname="${1##*/}"
extension="${fullname##*.}"
filename="${fullname%.*}"
aZ=( {a..z} {A..Z} )
idx=0
while true; do
newname="${filename}.${aZ[$idx]}.${extension}"
idx=$(( idx + 1 ))
if [[ ! -e "$pathdir/$newname" ]]; then
echo "$pathdir/$newname"
return
fi
if [[ -z "${aZ[$idx]}" ]]; then
echo "Cannot find unique filename for: $1" >&2
exit 1
fi
done
}
function move_to_dir() {
pathdir=$( dirname "$1" )
fullname="${1##*/}"
date=$( find "$1" -printf '%TY-%Tm-%Td' )
#date '+%Y-%m-%d' -d "$( date '+%Y/%m/%d %H:%M:%S' -r "$1" ) + 4" )
mkdir -p "$pathdir/$date"
tgt_name=$( gen_unique "$pathdir/$date/$fullname" )
mv -vi "$1" "$tgt_name"
}
if [[ -z "$1" ]]; then
_help
fi
for arg in "$@"; do
case "$arg" in
--debug)
# --debug turn on set -x
set -x
;;
--help)
# --help This help message
_help
;;
-h) # This help message
_help
;;
esac
done
shopt -s nullglob
for file in "$@"; do
if [[ "$file" = "-"* ]]; then
continue
fi
if [[ -d "$file" ]]; then
continue
fi
if [[ "$file" = "*" ]]; then
continue
fi
move_to_dir "$file"
done

31
files/gpg-decrypt Executable file
View File

@@ -0,0 +1,31 @@
#!/bin/bash
if [[ -z "$1" ]]; then
echo "File missing or use -"
exit 1
fi
if [[ "$1" = "-" ]]; then
pv "$1" | gpg -d
exit $?
fi
if [[ -e "${1%.gpg}" ]]; then
echo "Target already exists"
exit 1
fi
echo Creating "${1%.gpg}"
if [[ -z "$GPGPASS" ]]; then
pv "$1" | gpg -d -o "${1%.gpg}" || {
echo ""
echo "If agent doesnt work, then:"
echo "Give password with env GPGPASS ( GPGPASS=secret gpg-decrypt $1 )"
}
else
{
echo "$GPGPASS";
pv "$1";
} | \
gpg -o "${1%.gpg}" -d --passphrase-fd 0 --batch --yes
fi

31
files/gpg-encrypt Executable file
View File

@@ -0,0 +1,31 @@
#!/bin/bash
if [[ "$1" = "-" ]]; then
pv "$1" | gpg -c
exit $?
fi
if [[ -z "$1" ]]; then
echo "File missing, or use -"
exit 1
fi
if [[ -e "$1".gpg ]]; then
echo "Target already exists"
exit 1
fi
echo Creating "$1".gpg
if [[ -z "$GPGPASS" ]]; then
pv "$1" | gpg --symmetric -o "$1".gpg || {
echo ""
echo "If agent doesnt work, then:"
echo "Give password with env GPGPASS ( GPGPASS=secret gpg-encrypt $1 )"
}
else
{
echo "$GPGPASS";
pv "$1";
} | \
gpg -o "$1".gpg --passphrase-fd 0 --batch --yes --symmetric
fi

79
files/zip2tar Executable file
View File

@@ -0,0 +1,79 @@
#!/usr/bin/env python3
"""zip2tar """
import argparse
import sys
import os
from zipfile import ZipFile
import tarfile
import time
def get_opts():
parser = argparse.ArgumentParser()
parser.add_argument(
"-q",
dest="quiet",
action="store_true",
default=False,
help="Quiet operation",
)
parser.add_argument(
"-f",
dest="force",
action="store_true",
default=False,
help="Overwrite target",
)
parser.add_argument(
"zip",
action="store",
help="Zip to convert",
)
parser.add_argument(
"tar",
action="store",
help="Output filename, defaults to [zip].tgz. Recognizes extensios: .tar, .tgz, .gz, .bz2",
default=None,
nargs="?",
)
parsed = parser.parse_args()
if parsed.tar == None:
parsed.tar = os.path.splitext(parsed.zip)[0] + ".tgz"
return parsed
def main():
opts = get_opts()
if not opts.force and os.path.exists(opts.tar):
raise FileExistsError(opts.tar)
mode = "w"
if opts.tar.endswith(".tgz") or opts.tar.endswith(".gz"):
mode = "w:gz"
if opts.tar.endswith(".bz2"):
mode = "w:bz2"
with ZipFile(opts.zip) as zipf:
with tarfile.open(opts.tar, mode) as tarf:
for zip_info in zipf.infolist():
if not opts.quiet:
print(zip_info.filename, zip_info.file_size)
tar_info = tarfile.TarInfo(name=zip_info.filename)
tar_info.size = zip_info.file_size
tar_info.mtime = time.mktime(zip_info.date_time + (-1, -1, -1))
if zip_info.is_dir():
tar_info.type = tarfile.DIRTYPE
tarf.addfile(tarinfo=tar_info)
else:
tarf.addfile(tarinfo=tar_info, fileobj=zipf.open(zip_info.filename))
if __name__ == "__main__":
main()