From: Rob Browning Date: Fri, 8 Mar 2013 00:07:30 +0000 (-0600) Subject: Move parse_excludes to helpers and rework it a bit. X-Git-Tag: bup-0.25-rc2~57 X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=bup.git;a=commitdiff_plain;h=3175d4f924749ef7878c5a974febe07bdb20b6c9 Move parse_excludes to helpers and rework it a bit. Don't use f before it's initialized. Narrow the catch to open(), and only catch IOException. Call Options.fatal() when there's an error (as does helpers.parse_date_or_fatal()). Signed-off-by: Rob Browning --- diff --git a/cmd/drecurse-cmd.py b/cmd/drecurse-cmd.py index f804185..218b2ef 100755 --- a/cmd/drecurse-cmd.py +++ b/cmd/drecurse-cmd.py @@ -17,7 +17,7 @@ o = options.Options(optspec) if len(extra) != 1: o.fatal("exactly one filename expected") -excluded_paths = drecurse.parse_excludes(flags) +excluded_paths = parse_excludes(flags, o.fatal) it = drecurse.recursive_dirlist(extra, opt.xdev, excluded_paths=excluded_paths) if opt.profile: diff --git a/cmd/index-cmd.py b/cmd/index-cmd.py index fd37941..5a8913d 100755 --- a/cmd/index-cmd.py +++ b/cmd/index-cmd.py @@ -222,8 +222,7 @@ if opt.clear: log('clear: clearing index.\n') clear_index(indexfile) -excluded_paths = drecurse.parse_excludes(flags) - +excluded_paths = parse_excludes(flags, o.fatal) paths = index.reduce_paths(extra) if opt.update: diff --git a/lib/bup/drecurse.py b/lib/bup/drecurse.py index 866045f..2f4f286 100644 --- a/lib/bup/drecurse.py +++ b/lib/bup/drecurse.py @@ -116,25 +116,3 @@ def recursive_dirlist(paths, xdev, bup_dir=None, excluded_paths=None): except: pass raise - -def parse_excludes(flags): - excluded_paths = [] - - for flag in flags: - (option, parameter) = flag - if option == '--exclude': - excluded_paths.append(realpath(parameter)) - - if option == '--exclude-from': - try: - try: - f = open(realpath(parameter)) - for exclude_path in f.readlines(): - excluded_paths.append(realpath(exclude_path.strip())) - except Error, e: - log("warning: couldn't read %s\n" % parameter) - finally: - f.close() - - return excluded_paths - diff --git a/lib/bup/helpers.py b/lib/bup/helpers.py index d3eea37..ca9358a 100644 --- a/lib/bup/helpers.py +++ b/lib/bup/helpers.py @@ -710,6 +710,24 @@ def parse_date_or_fatal(str, fatal): return date +def parse_excludes(options, fatal): + """Traverse the options and extract all excludes, or call Option.fatal().""" + excluded_paths = [] + + for flag in options: + (option, parameter) = flag + if option == '--exclude': + excluded_paths.append(realpath(parameter)) + elif option == '--exclude-from': + try: + f = open(realpath(parameter)) + except IOError, e: + raise fatal("couldn't read %s" % parameter) + for exclude_path in f.readlines(): + excluded_paths.append(realpath(exclude_path.strip())) + return excluded_paths + + # FIXME: Carefully consider the use of functions (os.path.*, etc.) # that resolve against the current filesystem in the strip/graft # functions for example, but elsewhere as well. I suspect bup's not diff --git a/t/test.sh b/t/test.sh index 797291b..d93671c 100755 --- a/t/test.sh +++ b/t/test.sh @@ -20,6 +20,7 @@ WVSTART "index" D=bupdata.tmp rm -rf $D mkdir $D +WVFAIL bup index --exclude-from $D/cannot-exist $D WVPASSEQ "$(bup index --check -p)" "" WVPASSEQ "$(bup index --check -p $D)" "" WVFAIL [ -e $D.fake ]