Compare commits
6 Commits
2019-03-08
...
2019-03-17
Author | SHA1 | Date | |
---|---|---|---|
db8b70dd8f | |||
e165d6768f | |||
5660ef32ad | |||
a671d8d1a9 | |||
366601d014 | |||
6aa5024811 |
@ -1,3 +1,3 @@
|
|||||||
## File for excluding particular files from being written to
|
# File for excluding block devices from being written to
|
||||||
## Write one device per line, shell-style globs are accepted i.e. /dev/sda?
|
# One rule per line, regular expressions are accepted i.e. /dev/sda[0-9]?
|
||||||
## Lines beginning with "#" and ";" are ignored
|
# Lines beginning with "#" and ";" are ignored
|
||||||
|
55
ddusb.py
55
ddusb.py
@ -1,16 +1,20 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
"""Write an ISO image to a usb drive using dd."""
|
"""Wrapper script for using dd to write to a USB drive."""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import glob
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
# ========== Constants ==========
|
# ========== Constants ==========
|
||||||
COMMENT_PATTERN = "[#;]"
|
COMMENT_PATTERN = "^[#;]"
|
||||||
EXCLUDE_FILE = "/etc/helper-scripts/ddusb-exclude.conf"
|
EXCLUDE_FILE = "/etc/helper-scripts/ddusb-exclude.conf"
|
||||||
|
|
||||||
|
E_BLOCKDEVICE_ERROR = 1
|
||||||
|
E_EXCLUDE_ERROR = 2
|
||||||
|
E_DD_ERROR = 3
|
||||||
|
|
||||||
|
|
||||||
# ========== Functions ==========
|
# ========== Functions ==========
|
||||||
def read_exclude_file(exclude_file):
|
def read_exclude_file(exclude_file):
|
||||||
@ -33,23 +37,6 @@ def read_exclude_file(exclude_file):
|
|||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
|
||||||
def expand_globs(*globs):
|
|
||||||
"""For each glob given, expand these globs and return a list
|
|
||||||
containing each glob expanded.
|
|
||||||
|
|
||||||
:param globs: patterns to expand
|
|
||||||
:type globs: str
|
|
||||||
:returns: all expanded globs aggregated together
|
|
||||||
:rtype: list
|
|
||||||
"""
|
|
||||||
expanded_globs = []
|
|
||||||
|
|
||||||
for line in globs:
|
|
||||||
expanded_globs.extend(glob.glob(line, recursive=True))
|
|
||||||
|
|
||||||
return expanded_globs
|
|
||||||
|
|
||||||
|
|
||||||
# ========== Main Script ==========
|
# ========== Main Script ==========
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-b", "--bs", default=512, help="block size", metavar="bs")
|
parser.add_argument("-b", "--bs", default=512, help="block size", metavar="bs")
|
||||||
@ -59,23 +46,23 @@ args = parser.parse_args()
|
|||||||
|
|
||||||
block_size = args.bs
|
block_size = args.bs
|
||||||
input_file = args.input_file
|
input_file = args.input_file
|
||||||
block_device = args.output_file
|
block_path = args.output_file
|
||||||
|
|
||||||
# Ensure that block_device is really a block device
|
# Ensure that block_path is really a block device
|
||||||
if not pathlib.Path(block_device).is_block_device():
|
if not pathlib.Path(block_path).is_block_device():
|
||||||
print(f'Error: "{block_device}" is not a block device')
|
print(f'Error: "{block_path}" is not a block device')
|
||||||
exit(1)
|
exit(E_BLOCKDEVICE_ERROR)
|
||||||
|
|
||||||
# Check if block_device is excluded
|
# Check if block_path is excluded
|
||||||
exclude_patterns = read_exclude_file(EXCLUDE_FILE)
|
exclude_patterns = read_exclude_file(EXCLUDE_FILE)
|
||||||
device_blacklist = expand_globs(*exclude_patterns)
|
|
||||||
|
|
||||||
if block_device in device_blacklist:
|
for pattern in exclude_patterns:
|
||||||
print(f'Error: "{block_device}" is blacklisted from running dd')
|
if re.fullmatch(pattern, block_path):
|
||||||
exit(2)
|
print(f'Error: "{block_path}" is blacklisted from running dd')
|
||||||
|
exit(E_EXCLUDE_ERROR)
|
||||||
|
|
||||||
print(f"Input file: {input_file}")
|
print(f"Input file: {input_file}")
|
||||||
print(f"Block device: {block_device}")
|
print(f"Block device: {block_path}")
|
||||||
print(f"Block size: {block_size}")
|
print(f"Block size: {block_size}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -83,13 +70,13 @@ try:
|
|||||||
[
|
[
|
||||||
"dd",
|
"dd",
|
||||||
f"if={input_file}",
|
f"if={input_file}",
|
||||||
f"of={block_device}",
|
f"of={block_path}",
|
||||||
f"bs={block_size}",
|
f"bs={block_size}",
|
||||||
"status=progress",
|
"status=progress",
|
||||||
],
|
],
|
||||||
check=True,
|
check=True,
|
||||||
)
|
)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
exit(3)
|
exit(E_DD_ERROR)
|
||||||
else:
|
else:
|
||||||
subprocess.run("sync")
|
os.sync()
|
||||||
|
157
quickdel.py
Normal file
157
quickdel.py
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
"""
|
||||||
|
quickdel - delete any file matching a query
|
||||||
|
|
||||||
|
Dependencies
|
||||||
|
============
|
||||||
|
* fd
|
||||||
|
* python-termcolor
|
||||||
|
|
||||||
|
Command-Line Arguments
|
||||||
|
======================
|
||||||
|
* -d, --directories-only
|
||||||
|
* -D, --directory TODO: implement this
|
||||||
|
* -e, --empty-only
|
||||||
|
* -E, --extension
|
||||||
|
* -f, --files-only
|
||||||
|
* -i, --no-ignore
|
||||||
|
* -I, --no-ignore-vcs
|
||||||
|
* -l, --links-only
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import os.path
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from termcolor import colored
|
||||||
|
|
||||||
|
# ========== Constants ==========
|
||||||
|
FD_BIN = "/usr/bin/fd"
|
||||||
|
FD_OPTS = []
|
||||||
|
# Matches 'y' or 'yes' only, ignoring case
|
||||||
|
USER_RESPONSE_YES = "^[Yy]{1}([Ee]{1}[Ss]{1})?$"
|
||||||
|
|
||||||
|
E_USER_RESPONSE_NO = 1
|
||||||
|
E_INPUT_INTERRUPTED = 2
|
||||||
|
|
||||||
|
# ========== Functions ==========
|
||||||
|
def color_file(filename):
|
||||||
|
"""Return correct color code for filetype of filename.
|
||||||
|
|
||||||
|
Example
|
||||||
|
-------
|
||||||
|
>>> color_file('Test File', 'red')
|
||||||
|
'\x1b[31mTest String\x1b[0m'
|
||||||
|
|
||||||
|
:param filename: file to determine color output for
|
||||||
|
:type filename: str
|
||||||
|
:return: filename with ANSII escape codes for color
|
||||||
|
:rtype: str
|
||||||
|
"""
|
||||||
|
if os.path.isdir(filename):
|
||||||
|
return colored(filename, "blue")
|
||||||
|
elif os.path.islink(filename):
|
||||||
|
return colored(filename, "green")
|
||||||
|
else:
|
||||||
|
return filename
|
||||||
|
|
||||||
|
|
||||||
|
# ========== Main Script ==========
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument(
|
||||||
|
"-d",
|
||||||
|
"--directories-only",
|
||||||
|
action="store_const",
|
||||||
|
const=["--type", "directories"],
|
||||||
|
dest="fd_extra_opts",
|
||||||
|
help="filter results to directories",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-e",
|
||||||
|
"--empty-only",
|
||||||
|
action="store_const",
|
||||||
|
const=["--type", "empty"],
|
||||||
|
dest="fd_extra_opts",
|
||||||
|
help="filter results to empty files and directories",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-E",
|
||||||
|
"--extension",
|
||||||
|
action="append",
|
||||||
|
dest="extensions",
|
||||||
|
help="file extension",
|
||||||
|
metavar='ext',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-f",
|
||||||
|
"--files-only",
|
||||||
|
action="store_const",
|
||||||
|
const=["--type", "file"],
|
||||||
|
dest="fd_extra_opts",
|
||||||
|
help="filter results to files",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-I",
|
||||||
|
"--no-ignore-vcs",
|
||||||
|
action="store_const",
|
||||||
|
const="--no-ignore-vcs",
|
||||||
|
dest="fd_extra_opts",
|
||||||
|
help="do not ignore .gitignore",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-i",
|
||||||
|
"--no-ignore",
|
||||||
|
action="store_const",
|
||||||
|
const="--no-ignore",
|
||||||
|
dest="fd_extra_opts",
|
||||||
|
help="do not ignore .gitignore and .fdignore",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-l",
|
||||||
|
"--links-only",
|
||||||
|
action="store_const",
|
||||||
|
const=["--type", "symlink"],
|
||||||
|
dest="fd_extra_opts",
|
||||||
|
help="filter results to symlinks",
|
||||||
|
)
|
||||||
|
parser.add_argument("patterns", nargs="+", help="file matching patterns")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.fd_extra_opts is not None:
|
||||||
|
FD_OPTS.extend(args.fd_extra_opts)
|
||||||
|
if args.extensions is not None:
|
||||||
|
for ext in args.extensions:
|
||||||
|
FD_OPTS.extend(["--extension", ext])
|
||||||
|
|
||||||
|
files = []
|
||||||
|
for pattern in args.patterns:
|
||||||
|
cmd = [FD_BIN, *FD_OPTS, pattern]
|
||||||
|
files.extend(
|
||||||
|
subprocess.run(
|
||||||
|
cmd, capture_output=True, text=True
|
||||||
|
).stdout.splitlines()
|
||||||
|
)
|
||||||
|
files.sort()
|
||||||
|
|
||||||
|
# Pretty print all filenames
|
||||||
|
for index, filename in enumerate([color_file(f) for f in files], 1):
|
||||||
|
print(f"{index}. {filename}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
user_response = input("Would you like to delete these files? ")
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
exit(E_INPUT_INTERRUPTED)
|
||||||
|
|
||||||
|
if re.match(USER_RESPONSE_YES, user_response) is None:
|
||||||
|
print("Operation cancelled")
|
||||||
|
exit(E_USER_RESPONSE_NO)
|
||||||
|
|
||||||
|
|
||||||
|
for f in files:
|
||||||
|
os.remove(f)
|
||||||
|
|
||||||
|
print("All files deleted")
|
@ -1,7 +1,6 @@
|
|||||||
#compdef ddusb
|
#compdef ddusb
|
||||||
|
|
||||||
# zsh completions for 'ddusb'
|
# zsh completions for 'ddusb'
|
||||||
# automatically generated with http://github.com/RobSis/zsh-completion-generator
|
|
||||||
local arguments
|
local arguments
|
||||||
|
|
||||||
arguments=(
|
arguments=(
|
||||||
|
@ -2,12 +2,11 @@
|
|||||||
local arguments
|
local arguments
|
||||||
|
|
||||||
arguments=(
|
arguments=(
|
||||||
$argument_list
|
{-h,--help}'[show this help message and exit]'
|
||||||
{-h, --help}'[show this help message and exit]'
|
{-b,--boot}'[edit a file in /boot]'
|
||||||
{-b, --boot}'[edit a file in /boot]'
|
{-d,--dir}'[edit a file in a given directory]'
|
||||||
{-d, --dir}'[edit a file in a given directory]'
|
{-E,--etc}'[edit a file in /etc]'
|
||||||
{-E, --etc}'[edit a file in /etc]'
|
{-e,--editor}'[use a given editor]'
|
||||||
{-e, --editor}'[use a given editor]'
|
|
||||||
'*:filename:_files'
|
'*:filename:_files'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -4,11 +4,16 @@
|
|||||||
local arguments
|
local arguments
|
||||||
|
|
||||||
arguments=(
|
arguments=(
|
||||||
{-d,--directories-only}'[only delete directories]'
|
{-d,--directories-only}'[filter results to directories]'
|
||||||
|
{-e,--empty-only}'[filter results to empty files and directories]'
|
||||||
|
{-f,--files-only}'[filter results to files]'
|
||||||
|
{-E,--extension}'[file extension]'
|
||||||
{-h,--help}'[print this help page]'
|
{-h,--help}'[print this help page]'
|
||||||
{-i,--no-ignore}'[do not ignore .gitignore and .fdignore]'
|
{-i,--no-ignore}'[do not ignore .gitignore and .fdignore]'
|
||||||
{-I,--no-ignore-vcs}'[do not ignore .gitignore]'
|
{-I,--no-ignore-vcs}'[do not ignore .gitignore]'
|
||||||
|
{-l,--links-only}'[filter results to symlinks]'
|
||||||
'*:filename:_files'
|
'*:filename:_files'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
_arguments -s $arguments
|
_arguments -s $arguments
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
# Fuzzy find a file and then edit it
|
# Fuzzy find a file and then edit it
|
||||||
|
|
||||||
_fedit() {
|
_fedit() {
|
||||||
/usr/bin/fedit
|
/usr/bin/fedit && zle reset-prompt
|
||||||
zle reset-prompt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_etcedit() {
|
_etcedit() {
|
||||||
/usr/bin/fedit --etc
|
/usr/bin/fedit --etc && zle reset-prompt
|
||||||
zle reset-prompt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zle -N _fedit
|
zle -N _fedit
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
# Fuzzy-find a file and open it in less
|
# Fuzzy-find a file and open it in less
|
||||||
fless() {
|
fless() {
|
||||||
/usr/bin/fless
|
/usr/bin/fless && zle reset-prompt
|
||||||
zle reset-prompt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zle -N fless
|
zle -N fless
|
||||||
|
Reference in New Issue
Block a user