Browse Source

Merge pull request #2382 from ThomasWaldmann/backports2

1.0-maint backports
enkore 8 years ago
parent
commit
bb7d4bac13
3 changed files with 17 additions and 5 deletions
  1. 8 3
      borg/archiver.py
  2. 2 2
      borg/helpers.py
  3. 7 0
      borg/testsuite/archiver.py

+ 8 - 3
borg/archiver.py

@@ -6,6 +6,7 @@ import argparse
 import faulthandler
 import functools
 import inspect
+import itertools
 import os
 import re
 import shlex
@@ -2060,6 +2061,9 @@ class Archiver:
         if cmd is not None and result.func == self.do_serve:
             forced_result = result
             argv = shlex.split(cmd)
+            # Drop environment variables (do *not* interpret them) before trying to parse
+            # the borg command line.
+            argv = list(itertools.dropwhile(lambda arg: '=' in arg, argv))
             result = self.parse_args(argv[1:])
             if result.func != forced_result.func:
                 # someone is trying to execute a different borg subcommand, don't do that!
@@ -2075,10 +2079,11 @@ class Archiver:
             args = self.preprocess_args(args)
         parser = self.build_parser(args)
         args = parser.parse_args(args or ['-h'])
-        if args.func == self.do_create:
+        # This works around http://bugs.python.org/issue9351
+        func = getattr(args, 'func', None) or getattr(args, 'fallback_func')
+        if func == self.do_create and not args.paths:
             # need at least 1 path but args.paths may also be populated from patterns
-            if not args.paths:
-                parser.error('Need at least one PATH argument.')
+            parser.error('Need at least one PATH argument.')
         return args
 
     def run(self, args):

+ 2 - 2
borg/helpers.py

@@ -216,9 +216,9 @@ def prune_within(archives, within):
         hours = int(within[:-1]) * multiplier[within[-1]]
     except (KeyError, ValueError):
         # I don't like how this displays the original exception too:
-        raise argparse.ArgumentTypeError('Unable to parse --within option: "%s"' % within)
+        raise argparse.ArgumentTypeError('Unable to parse --keep-within option: "%s"' % within)
     if hours <= 0:
-        raise argparse.ArgumentTypeError('Number specified using --within option must be positive')
+        raise argparse.ArgumentTypeError('Number specified using --keep-within option must be positive')
     target = datetime.now(timezone.utc) - timedelta(seconds=hours * 3600)
     return [a for a in archives if a.ts > target]
 

+ 7 - 0
borg/testsuite/archiver.py

@@ -1800,6 +1800,13 @@ def test_get_args():
                              'borg init /')
     assert args.func == archiver.do_serve
 
+    # Check that environment variables in the forced command don't cause issues. If the command
+    # were not forced, environment variables would be interpreted by the shell, but this does not
+    # happen for forced commands - we get the verbatim command line and need to deal with env vars.
+    args = archiver.get_args(['borg', 'serve', ],
+                             'BORG_HOSTNAME_IS_UNIQUE=yes borg serve --info')
+    assert args.func == archiver.do_serve
+
 
 class TestBuildFilter:
     def test_basic(self):