[file_utils] unln: handle options with argparse
Remove the now useless error() function. Add options --verbose and --sync.
This commit is contained in:
parent
eb37c52e8c
commit
4533f40d58
|
@ -29,10 +29,10 @@
|
|||
# inodes by doing the equivalent of (cp -p file tmp && mv tmp file).
|
||||
#
|
||||
# TODO:
|
||||
# - Option verbose
|
||||
# - Option to follow symlinks
|
||||
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import os
|
||||
import tempfile
|
||||
|
@ -45,7 +45,7 @@ def verbose(*message, **args):
|
|||
This function is a simple wrapper around print(), and you can use any
|
||||
keyword argument you would use with print().
|
||||
"""
|
||||
if options["verbose"]:
|
||||
if options.verbose:
|
||||
print(*message, **args)
|
||||
|
||||
def warn(*message, prefix="Warning!", **args):
|
||||
|
@ -60,46 +60,33 @@ def warn(*message, prefix="Warning!", **args):
|
|||
sys.stdout.flush()
|
||||
print(prefix, *message, file=sys.stderr, **args)
|
||||
|
||||
def error(error_code, *message, **args):
|
||||
"""Prints the message on the error output and exits with 'error_code'.
|
||||
|
||||
This function is a simple wrapper around warn(), and you can use any
|
||||
keyword argument you would use with warn() or print().
|
||||
"""
|
||||
warn(*message, prefix="", **args)
|
||||
sys.exit(error_code)
|
||||
|
||||
def quote(string):
|
||||
""" Quotes a string. """
|
||||
return "``{}´´".format(string)
|
||||
|
||||
|
||||
# Default options
|
||||
options = {
|
||||
"verbose": True
|
||||
}
|
||||
# Parse command-line arguments
|
||||
arg_parser = argparse.ArgumentParser(
|
||||
description="Separate a file name from its other hard links",
|
||||
epilog="For more information about this program, see the README file \
|
||||
provided with the distribution.")
|
||||
arg_parser.add_argument("-s", "--sync", action="store_true",
|
||||
help="sync files after a copy to be more fail safe \
|
||||
(requires Python 3.3 or above, will be ignored with lower versions)")
|
||||
arg_parser.add_argument("-v", "--verbose", action="store_true",
|
||||
help="increase output verbosity")
|
||||
arg_parser.add_argument("filenames", metavar="file", nargs="+",
|
||||
help="file name to work on")
|
||||
options = arg_parser.parse_args()
|
||||
|
||||
# Name of this program
|
||||
progname = os.path.basename(sys.argv[0])
|
||||
|
||||
# Short usage string
|
||||
usage_string = "Usage:\n\t{} <file1> [file2 ...]".format(progname)
|
||||
|
||||
# Error codes
|
||||
ERR_BAD_USAGE = 1
|
||||
|
||||
# List of filenames that were supposed to be worked on but could not because of
|
||||
# an error
|
||||
error_filenames = []
|
||||
|
||||
|
||||
# Check command-line arguments
|
||||
if len(sys.argv) < 2:
|
||||
error(ERR_BAD_USAGE, usage_string)
|
||||
|
||||
|
||||
# Main loop
|
||||
for filename in sys.argv[1:]:
|
||||
for filename in options.filenames:
|
||||
# Exists?
|
||||
if not os.path.exists(filename):
|
||||
warn(quote(filename), "does not exist, ignoring.")
|
||||
|
@ -124,7 +111,8 @@ for filename in sys.argv[1:]:
|
|||
verbose(quote(filename), "has", nlinks, "hard links, proceeding", end="... ")
|
||||
|
||||
# Reserve a temporary file name
|
||||
dirname = os.path.dirname(filename)
|
||||
dirname = os.path.dirname(filename) # filename's directory
|
||||
progname = os.path.basename(__file__) # name of this program
|
||||
try:
|
||||
handle, tmpfilename = tempfile.mkstemp(
|
||||
prefix="{}-".format(filename),
|
||||
|
@ -150,7 +138,7 @@ for filename in sys.argv[1:]:
|
|||
continue
|
||||
|
||||
# Sync (if we are running Python >= 3.3)
|
||||
if sys.version_info.minor >= 3:
|
||||
if options.sync and sys.version_info.minor >= 3:
|
||||
verbose("Syncing", end="... ")
|
||||
os.sync()
|
||||
|
||||
|
|
Loading…
Reference in New Issue