Add a load of handy ~/bin scripts

This commit is contained in:
2020-02-23 15:07:02 +01:00
parent 99fbfc42fe
commit 8032cee29c
13 changed files with 323 additions and 0 deletions

34
bin/ascii Executable file
View File

@@ -0,0 +1,34 @@
#!/usr/bin/env python3
import sys
import os.path
SEP = '='*10
decode = False
tohex = False
if os.path.basename(sys.argv[0]) == 'unascii':
decode = True
if os.path.basename(sys.argv[0]) == 'asciihex':
tohex = True
if len(sys.argv) < 2 or sys.argv[1] == '-':
text = sys.stdin.readlines()
else:
text = sys.argv[1:]
for i in range(len(text)):
if decode:
line = text[i]
if line.strip() == SEP:
pass
else:
print(chr(int(line)), end='')
else:
for char in text[i]:
if tohex:
print('0x' + format(ord(char), '02x'))
else:
print(ord(char))
if i != len(text) - 1:
print(SEP)

1
bin/asciihex Symbolic link
View File

@@ -0,0 +1 @@
ascii

43
bin/days-until Executable file
View File

@@ -0,0 +1,43 @@
#!/usr/bin/env python3
formats = (
'%Y-%m-%d',
'%d.%m.%Y',
'%Y-%m',
'%d.%m')
import datetime
import sys
for format in formats:
try:
day = datetime.datetime.strptime(sys.argv[1], format).date()
except ValueError:
continue
diff = (day - datetime.datetime.now().date()).days
print(str(diff) + " days")
print()
if abs(diff) > 14:
print("{} weeks and {} days".format(
diff // 7,
diff % 7))
if abs(diff) > 365:
print("{} years, {} weeks and {} days".format(
diff // 365,
(diff % 365) // 7,
(diff % 365 % 7)))
print()
for i in range(abs(diff) // 7):
print("X X X X X X X")
if (i+1) % 4 == 0:
print()
for _ in range(abs(diff) % 7):
print("X ", end="")
if abs(diff) % 7 != 0:
print()
sys.exit(0)
print("No format matched:")
print()
sys.exit(1)

24
bin/git-rewrite-author Executable file
View File

@@ -0,0 +1,24 @@
#!/bin/bash
(( $# != 4 )) && { echo "$0 <repo> <oldmail> <newauthor> <newmail>" >&1 ; exit 1 ; }
_repo="$1"
_oldmail="$2"
_newauthor="$3"
_newmail="$4"
cd "$_repo" || exit $?
git filter-branch --force --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "'"$_oldmail"'" ];
then
export GIT_AUTHOR_NAME="'"$_newauthor"'";
export GIT_AUTHOR_EMAIL="'"$_newmail"'";
git commit-tree -S "$@"
elif [ "$GIT_COMMITTER_EMAIL" = "'"$_oldmail"'" ];
then
export GIT_COMMITTER_NAME="'"$_newauthor"'";
export GIT_COMMITTER_EMAIL="'"$_newmail"'";
git commit-tree -S "$@"
fi' \
--tag-name-filter cat \
-- --branches --tags

20
bin/kw Executable file
View File

@@ -0,0 +1,20 @@
#!/usr/bin/env python3
import datetime
import sys
formats = (
'%Y-%m-%d',
'%d.%m.%Y')
for format in formats:
try:
day = datetime.datetime.strptime(sys.argv[1], format).date()
except ValueError:
continue
print(day.isocalendar()[1])
sys.exit(0)
print("no format matched:")
print("\n".join(" {}".format(f) for f in formats))
sys.exit(1)

2
bin/mousespam Executable file
View File

@@ -0,0 +1,2 @@
#!/usr/bin/env bash
xdotool click --repeat 1000000000 --delay 20ms 1

76
bin/mypass Executable file
View File

@@ -0,0 +1,76 @@
#!/usr/bin/env python3
import sys
import subprocess
import yaml
PASS = 'pass'
def run(args):
return(subprocess.run(
args,
stdin=sys.stdin,
stdout=sys.stdout,
stderr=sys.stderr).returncode)
def get_stdout(args):
return(subprocess.run(
args,
stdout=subprocess.PIPE).stdout)
def extract_from_dict(d, k, last=None):
if '.' in k:
k1, k2 = k.split('.', 1)
try:
d1 = d[k1]
return extract_from_dict(d1, k2, last=k1)
except KeyError:
print("\"{}\" was not found in the data".format(k))
sys.exit(3)
else:
if not isinstance(d, dict):
print("element \"{}\" is not a dictionary".format(last))
sys.exit(3)
try:
return d[k]
except KeyError:
print("\"{}\" was not found in the data".format(k))
sys.exit(3)
def cmd_get(*args):
if len(args) == 1:
print(get_stdout(['pass', 'show', args[0]]).split(b'\n')[0].decode())
return 0
if len(args) != 2:
print("what do you want to get?")
return 1
# get the pass output
out = get_stdout(['pass', 'show', args[0]])
try:
data = list(yaml.safe_load_all(out))[-1]
except yaml.scanner.ScannerError as e:
print("invalid YAML data: {}".format(str(e)))
return 2
except IndexError:
print("invalid YAML data: no YAML document found")
return 2
if not isinstance(data, dict):
print("invalid YAML data, not a dict")
return 2
keydata = extract_from_dict(data, args[1])
if isinstance(keydata, list) or isinstance(keydata, dict):
print(yaml.safe_dump(keydata, default_flow_style=False).strip())
else:
print(keydata)
cmds = ['get']
if len(sys.argv) == 1 or sys.argv[1] not in cmds:
sys.exit(run([PASS] + sys.argv[1:]))
else:
sys.exit(globals()['cmd_' + sys.argv[1]](*sys.argv[2:]))

62
bin/nato Executable file
View File

@@ -0,0 +1,62 @@
#!/usr/bin/env python3
import sys
hash = {
'A': 'Alpha',
'B': 'Bravo',
'C': 'Charlie',
'D': 'Delta',
'E': 'Echo',
'F': 'Foxtrot',
'G': 'Golf',
'H': 'Hotel',
'I': 'India',
'J': 'Juliett',
'K': 'Kilo',
'L': 'Lima',
'M': 'Mike',
'N': 'November',
'O': 'Oscar',
'P': 'Papa',
'Q': 'Quebec',
'R': 'Romeo',
'S': 'Sierra',
'T': 'Tango',
'U': 'Uniform',
'V': 'Victor',
'W': 'Whiskey',
'X': 'Xray',
'Y': 'Yankee',
'Z': 'Zulu',
'0': 'Zero',
'1': 'One',
'2': 'Two',
'3': 'Tree',
'4': 'Fower',
'5': 'Fife',
'6': 'Six',
'7': 'Seven',
'8': 'Eight',
'9': 'Niner',
'-': 'Dash',
'.': 'Stop',
}
if len(sys.argv) < 2 or sys.argv[1] == '-':
text = [line.strip() for line in sys.stdin.readlines()]
else:
text = sys.argv[1:]
for i in range(len(text)):
for char in text[i].upper():
if char in hash.keys():
print(hash[char])
elif char == ' ':
print()
else:
print(">> " + char)
if i != len(text) - 1:
print()
print("===")
print()

6
bin/pprint Executable file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/env python
import sys
import pprint
pprint.pprint(eval(sys.stdin.read()))

2
bin/pw Executable file
View File

@@ -0,0 +1,2 @@
#!/usr/bin/env bash
pwgen -s 25 1

8
bin/randmac Executable file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
_max=${1:-6}
for i in $(seq $_max) ; do
printf '%02X' $[RANDOM%256]
(( $i != $_max )) && printf ':'
done
printf '\n'

44
bin/since Executable file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/env python3
import datetime
import sys
USAGE="""Usage: {program} PATH_TO_FILE""".format(program=sys.argv[0])
def die(message, code=1):
print(message)
sys.exit(code)
def die_usage(code=1):
die(USAGE, code)
def read_date_from_file(path):
text = open(path).readline().strip()
try:
time = datetime.datetime.strptime(text, '%Y-%m-%d')
except ValueError as e:
die(e)
return time
def main(argv):
if len(argv) < 2:
die_usage()
last = read_date_from_file(argv[1])
now = datetime.datetime.now()
diff = now - last
days = diff.days
#hours = diff.seconds // (60 * 60)
if days == 1:
msg = "1 day"
else:
msg = "{days} days".format(days=days)
#msg += " "
#if hours == 1:
# msg += "1 hour "
#else:
# msg += "{hours} hours ".format(hours=hours)
print(msg)
main(sys.argv)

1
bin/unascii Symbolic link
View File

@@ -0,0 +1 @@
ascii