Compare commits
1 Commits
2019-03-20
...
2019-03-17
Author | SHA1 | Date | |
---|---|---|---|
db8b70dd8f |
75
fedit.py
75
fedit.py
@ -14,31 +14,27 @@ import shutil
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
# ========== Constants ==========
|
# ========== Constants ==========
|
||||||
# ----- Paths -----
|
# Paths
|
||||||
BOOT_DIR = "/boot"
|
BOOT_DIR = "/boot"
|
||||||
ETC_DIR = "/etc"
|
ETC_DIR = "/etc"
|
||||||
|
|
||||||
# ----- Exit Codes -----
|
# Exit Codes
|
||||||
E_NOEDITORFOUND = 2
|
E_NOEDITORFOUND = 2
|
||||||
E_NOFILESELECTED = 3
|
E_NOFILESELECTED = 3
|
||||||
|
|
||||||
# ----- Commands -----
|
# Commands
|
||||||
FIND_CMD = "/usr/bin/fd"
|
FIND_CMD = "/usr/bin/fd"
|
||||||
FIND_OPTS = ["--hidden", "--print0", "--type", "f"]
|
FIND_OPTS = ["--hidden", "--print0", "--type", "f", "--no-ignore-vcs"]
|
||||||
EXTRA_FIND_OPTS = {"no_ignore_vcs": "--no-ignore", "no_ignore": "--no-ignore-vcs"}
|
FZF_CMD = "/usr/bin/fzf"
|
||||||
|
FZF_OPTS = ["--read0", "--select-1", "--exit-0", "--print0"]
|
||||||
LOCATE_CMD = "/usr/bin/locate"
|
LOCATE_CMD = "/usr/bin/locate"
|
||||||
LOCATE_OPTS = ["--all", "--ignore-case", "--null"]
|
LOCATE_OPTS = ["--all", "--ignore-case", "--null"]
|
||||||
|
|
||||||
FZF_CMD = "/usr/bin/fzf"
|
|
||||||
FZF_OPTS = ["--read0", "--select-1", "--exit-0", "--print0"]
|
|
||||||
|
|
||||||
# ----- Misc. -----
|
|
||||||
LOCALE = "utf-8"
|
LOCALE = "utf-8"
|
||||||
|
|
||||||
|
|
||||||
# ========== Functions ==========
|
# ========== Functions ==========
|
||||||
def select_editor(override=None):
|
def select_editor(editor_override=None):
|
||||||
"""Return a possible canonical path to an editor.
|
"""Return a possible canonical path to an editor.
|
||||||
Select an editor from one of:
|
Select an editor from one of:
|
||||||
* -e, --editor
|
* -e, --editor
|
||||||
@ -49,15 +45,15 @@ def select_editor(override=None):
|
|||||||
|
|
||||||
If an editor cannot be resolved, then an Error is raised instead.
|
If an editor cannot be resolved, then an Error is raised instead.
|
||||||
|
|
||||||
:param override: argument to override an editor
|
:param editor_override: argument to override an editor
|
||||||
:returns: path to one of these editors
|
:returns: path to one of these editors
|
||||||
:rtype: str
|
:rtype: str
|
||||||
:raises: FileNotFoundError if an editor could not be resolved
|
:raises: FileNotFoundError if an editor could not be resolved
|
||||||
"""
|
"""
|
||||||
editor = None
|
editor = None
|
||||||
|
|
||||||
if override is not None:
|
if editor_override is not None:
|
||||||
editor = shutil.which(override)
|
editor = shutil.which(editor_override)
|
||||||
elif "EDITOR" in os.environ:
|
elif "EDITOR" in os.environ:
|
||||||
editor = shutil.which(os.environ.get("EDITOR"))
|
editor = shutil.which(os.environ.get("EDITOR"))
|
||||||
elif shutil.which("vim") is not None:
|
elif shutil.which("vim") is not None:
|
||||||
@ -73,16 +69,12 @@ def gen_editor_cmd(filename):
|
|||||||
"""Generate a command line to run for editing a file based on
|
"""Generate a command line to run for editing a file based on
|
||||||
permissions.
|
permissions.
|
||||||
|
|
||||||
This command does not pass extra options to the editor, hence
|
|
||||||
there are no arguments to pass for options.
|
|
||||||
|
|
||||||
:param filename: name of file to edit
|
:param filename: name of file to edit
|
||||||
:type filename: str or path-like object
|
:type filename: str or path-like object
|
||||||
:returns: command to execute to edit file
|
:returns: command to execute to edit file
|
||||||
:rtype: list
|
:rtype: list
|
||||||
"""
|
"""
|
||||||
# Possible for a race condition to occur here
|
# possible for a race condition to occur here
|
||||||
# What happens if the file or its metadata changes?
|
|
||||||
if os.access(filename, os.W_OK):
|
if os.access(filename, os.W_OK):
|
||||||
return [editor, filename]
|
return [editor, filename]
|
||||||
else:
|
else:
|
||||||
@ -98,24 +90,21 @@ def run_fzf(files):
|
|||||||
:rtype: str
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
selected_file = subprocess.run(
|
selected_file = subprocess.run(
|
||||||
[FZF_CMD, *FZF_OPTS], input=files, stdout=subprocess.PIPE
|
[FZF_CMD] + FZF_OPTS, input=files, stdout=subprocess.PIPE
|
||||||
).stdout
|
).stdout
|
||||||
|
|
||||||
return selected_file.decode(LOCALE).strip("\x00")
|
return selected_file.decode(LOCALE).strip("\x00")
|
||||||
|
|
||||||
|
|
||||||
def find_files(opts, directory=None):
|
def find_files(directory=None):
|
||||||
"""Use a find-based program to locate files, then pass to fzf.
|
"""Use a find-based program to locate files, then pass to fzf.
|
||||||
|
|
||||||
:param opts: options to pass to the find program
|
|
||||||
:type opts: list of str
|
|
||||||
:param directory: directory to search for files
|
:param directory: directory to search for files
|
||||||
:type directory: str
|
:type directory: str
|
||||||
:returns: path of user-selected file
|
:returns: path of user-selected file
|
||||||
:rtype: bytes
|
:rtype: bytes
|
||||||
"""
|
"""
|
||||||
cmd = [FIND_CMD, *opts]
|
cmd = [FIND_CMD] + FIND_OPTS
|
||||||
|
|
||||||
if directory is not None:
|
if directory is not None:
|
||||||
cmd.extend(["--", ".", directory])
|
cmd.extend(["--", ".", directory])
|
||||||
|
|
||||||
@ -137,7 +126,6 @@ def locate_files(patterns):
|
|||||||
|
|
||||||
|
|
||||||
# ========== Main Script ==========
|
# ========== Main Script ==========
|
||||||
if __name__ == "__main__":
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-b",
|
"-b",
|
||||||
@ -158,32 +146,12 @@ if __name__ == "__main__":
|
|||||||
dest="dir",
|
dest="dir",
|
||||||
help="edit a file in /etc",
|
help="edit a file in /etc",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
|
||||||
"-I",
|
|
||||||
"--no-ignore",
|
|
||||||
action="append_const",
|
|
||||||
const="--no-ignore",
|
|
||||||
dest="extra_find_opts",
|
|
||||||
help="do not respect .(git|fd)ignore files",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"-i",
|
|
||||||
"--no-ignore-vcs",
|
|
||||||
action="append_const",
|
|
||||||
const="--no-ignore-vcs",
|
|
||||||
dest="extra_find_opts",
|
|
||||||
help="do not respect .gitignore files",
|
|
||||||
)
|
|
||||||
parser.add_argument("-e", "--editor", help="use a given editor")
|
parser.add_argument("-e", "--editor", help="use a given editor")
|
||||||
parser.add_argument(
|
parser.add_argument("patterns", type=str, nargs="*", help="patterns to pass to locate")
|
||||||
"patterns", type=str, nargs="*", help="patterns to pass to locate"
|
|
||||||
)
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
user_opts = [] if args.extra_find_opts is None else args.extra_find_opts
|
final_find_cmd = [FIND_CMD] + FIND_OPTS
|
||||||
user_opts.extend(FIND_OPTS)
|
|
||||||
|
|
||||||
editor = ""
|
editor = ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -194,15 +162,14 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
# If patterns were passed, use locate
|
# If patterns were passed, use locate
|
||||||
# Otherwise check for -d and use fd
|
# Otherwise check for -d and use fd
|
||||||
files = (
|
if not args.patterns == []:
|
||||||
find_files(user_opts, args.dir)
|
files = locate_files(args.patterns)
|
||||||
if not args.patterns
|
else:
|
||||||
else locate_files(args.patterns)
|
files = find_files(args.dir)
|
||||||
)
|
|
||||||
|
|
||||||
selected_file = run_fzf(files)
|
selected_file = run_fzf(files)
|
||||||
|
|
||||||
if selected_file != "":
|
if not selected_file == "":
|
||||||
cmd = gen_editor_cmd(selected_file)
|
cmd = gen_editor_cmd(selected_file)
|
||||||
subprocess.run(cmd)
|
subprocess.run(cmd)
|
||||||
else:
|
else:
|
||||||
|
53
quickdel.py
53
quickdel.py
@ -14,7 +14,6 @@ Command-Line Arguments
|
|||||||
* -e, --empty-only
|
* -e, --empty-only
|
||||||
* -E, --extension
|
* -E, --extension
|
||||||
* -f, --files-only
|
* -f, --files-only
|
||||||
* -F, --force-directory-delete
|
|
||||||
* -i, --no-ignore
|
* -i, --no-ignore
|
||||||
* -I, --no-ignore-vcs
|
* -I, --no-ignore-vcs
|
||||||
* -l, --links-only
|
* -l, --links-only
|
||||||
@ -24,21 +23,18 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
import re
|
||||||
import shutil
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from termcolor import colored
|
from termcolor import colored
|
||||||
|
|
||||||
# ========== Constants ==========
|
# ========== Constants ==========
|
||||||
FD_BIN = "/usr/bin/fd"
|
FD_BIN = "/usr/bin/fd"
|
||||||
FD_OPTS = ["--hidden"]
|
FD_OPTS = []
|
||||||
# Matches 'y' or 'yes' only, ignoring case
|
# Matches 'y' or 'yes' only, ignoring case
|
||||||
USER_RESPONSE_YES = "^[Yy]{1}([Ee]{1}[Ss]{1})?$"
|
USER_RESPONSE_YES = "^[Yy]{1}([Ee]{1}[Ss]{1})?$"
|
||||||
|
|
||||||
E_NO_RESULTS = 1
|
E_USER_RESPONSE_NO = 1
|
||||||
E_USER_RESPONSE_NO = 2
|
E_INPUT_INTERRUPTED = 2
|
||||||
E_INPUT_INTERRUPTED = 3
|
|
||||||
|
|
||||||
|
|
||||||
# ========== Functions ==========
|
# ========== Functions ==========
|
||||||
def color_file(filename):
|
def color_file(filename):
|
||||||
@ -69,7 +65,7 @@ if __name__ == "__main__":
|
|||||||
"-d",
|
"-d",
|
||||||
"--directories-only",
|
"--directories-only",
|
||||||
action="store_const",
|
action="store_const",
|
||||||
const=["--type", "directory"],
|
const=["--type", "directories"],
|
||||||
dest="fd_extra_opts",
|
dest="fd_extra_opts",
|
||||||
help="filter results to directories",
|
help="filter results to directories",
|
||||||
)
|
)
|
||||||
@ -87,7 +83,7 @@ if __name__ == "__main__":
|
|||||||
action="append",
|
action="append",
|
||||||
dest="extensions",
|
dest="extensions",
|
||||||
help="file extension",
|
help="file extension",
|
||||||
metavar="ext",
|
metavar='ext',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-f",
|
"-f",
|
||||||
@ -97,12 +93,6 @@ if __name__ == "__main__":
|
|||||||
dest="fd_extra_opts",
|
dest="fd_extra_opts",
|
||||||
help="filter results to files",
|
help="filter results to files",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
|
||||||
"-F",
|
|
||||||
"--force-directory-delete",
|
|
||||||
action="store_true",
|
|
||||||
help="do not ignore non-empty directories, delete anyways",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-I",
|
"-I",
|
||||||
"--no-ignore-vcs",
|
"--no-ignore-vcs",
|
||||||
@ -137,23 +127,19 @@ if __name__ == "__main__":
|
|||||||
for ext in args.extensions:
|
for ext in args.extensions:
|
||||||
FD_OPTS.extend(["--extension", ext])
|
FD_OPTS.extend(["--extension", ext])
|
||||||
|
|
||||||
files = {}
|
files = []
|
||||||
for pattern in args.patterns:
|
for pattern in args.patterns:
|
||||||
cmd = [FD_BIN, *FD_OPTS, pattern]
|
cmd = [FD_BIN, *FD_OPTS, pattern]
|
||||||
files.update(
|
files.extend(
|
||||||
subprocess.run(cmd, capture_output=True, text=True).stdout.splitlines()
|
subprocess.run(
|
||||||
|
cmd, capture_output=True, text=True
|
||||||
|
).stdout.splitlines()
|
||||||
)
|
)
|
||||||
files = sorted(files)
|
files.sort()
|
||||||
|
|
||||||
if files == []:
|
|
||||||
print(f"No results found, exiting")
|
|
||||||
exit(E_NO_RESULTS)
|
|
||||||
|
|
||||||
# Pretty print all filenames
|
# Pretty print all filenames
|
||||||
for index, filename in enumerate([color_file(f) for f in files], 1):
|
for index, filename in enumerate([color_file(f) for f in files], 1):
|
||||||
print(f"{index}. {filename}")
|
print(f"{index}. {filename}")
|
||||||
# Padding line
|
|
||||||
print()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
user_response = input("Would you like to delete these files? ")
|
user_response = input("Would you like to delete these files? ")
|
||||||
@ -164,19 +150,8 @@ if __name__ == "__main__":
|
|||||||
print("Operation cancelled")
|
print("Operation cancelled")
|
||||||
exit(E_USER_RESPONSE_NO)
|
exit(E_USER_RESPONSE_NO)
|
||||||
|
|
||||||
# Remove files first
|
|
||||||
for f in [fi for fi in files if os.path.isfile(fi)]:
|
for f in files:
|
||||||
os.remove(f)
|
os.remove(f)
|
||||||
|
|
||||||
# Check -f, --force-directory-delete option
|
print("All files deleted")
|
||||||
rmdir_func = shutil.rmtree if args.force_directory_delete else os.rmdir
|
|
||||||
|
|
||||||
for d in filter(os.path.isdir, files):
|
|
||||||
try:
|
|
||||||
rmdir_func(d)
|
|
||||||
except OSError:
|
|
||||||
print(
|
|
||||||
f"{colored('Warning', 'yellow')}: {colored(d, 'blue')} is not empty, not deleting directory"
|
|
||||||
)
|
|
||||||
|
|
||||||
print(colored("\nDeletions complete", "green"))
|
|
||||||
|
@ -7,8 +7,6 @@ arguments=(
|
|||||||
{-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]'
|
||||||
{-I,--no-ignore}'[do not respect .(git|fd)ignore files]'
|
|
||||||
{-i,--no-ignore-vcs}'[do not respect .gitignore files]'
|
|
||||||
'*:filename:_files'
|
'*:filename:_files'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ arguments=(
|
|||||||
{-d,--directories-only}'[filter results to directories]'
|
{-d,--directories-only}'[filter results to directories]'
|
||||||
{-e,--empty-only}'[filter results to empty files and directories]'
|
{-e,--empty-only}'[filter results to empty files and directories]'
|
||||||
{-f,--files-only}'[filter results to files]'
|
{-f,--files-only}'[filter results to files]'
|
||||||
{-F,--force-directory-delete}'[do not ignore non-empty directories, delete anyways]'
|
|
||||||
{-E,--extension}'[file extension]'
|
{-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]'
|
||||||
|
Reference in New Issue
Block a user