3078: arv-put supports dynamic stderr.
authorBrett Smith <brett@curoverse.com>
Mon, 7 Jul 2014 18:29:39 +0000 (14:29 -0400)
committerBrett Smith <brett@curoverse.com>
Tue, 8 Jul 2014 13:52:18 +0000 (09:52 -0400)
sdk/python/arvados/commands/put.py
sdk/python/tests/test_arv_put.py

index 6da45e2b89e23d446133c9a5ec522e59d6449ff3..b7cb2eae7d13125caa7ff0a86e2340df24cc18f8 100644 (file)
@@ -328,7 +328,7 @@ def progress_writer(progress_func, outfile=sys.stderr):
 def exit_signal_handler(sigcode, frame):
     sys.exit(-sigcode)
 
-def main(arguments=None, output_to=sys.stdout):
+def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
     args = parse_arguments(arguments)
 
     if args.progress:
@@ -346,7 +346,7 @@ def main(arguments=None, output_to=sys.stdout):
     except (IOError, OSError):
         pass  # Couldn't open cache directory/file.  Continue without it.
     except ResumeCacheConflict:
-        output_to.write(
+        stdout.write(
             "arv-put: Another process is already uploading this data.\n")
         sys.exit(1)
 
@@ -364,7 +364,7 @@ def main(arguments=None, output_to=sys.stdout):
                             for sigcode in CAUGHT_SIGNALS}
 
     if writer.bytes_written > 0:  # We're resuming a previous upload.
-        print >>sys.stderr, "\n".join([
+        print >>stderr, "\n".join([
                 "arv-put: Resuming previous upload from last checkpoint.",
                 "         Use the --no-resume option to start over."])
 
@@ -380,7 +380,7 @@ def main(arguments=None, output_to=sys.stdout):
     writer.finish_current_stream()
 
     if args.progress:  # Print newline to split stderr from stdout for humans.
-        print >>sys.stderr
+        print >>stderr
 
     if args.stream:
         output = writer.manifest_text()
@@ -398,9 +398,9 @@ def main(arguments=None, output_to=sys.stdout):
         # Print the locator (uuid) of the new collection.
         output = collection['uuid']
 
-    output_to.write(output)
+    stdout.write(output)
     if not output.endswith('\n'):
-        output_to.write('\n')
+        stdout.write('\n')
 
     for sigcode, orig_handler in orig_signal_handlers.items():
         signal.signal(sigcode, orig_handler)
index 6e7c729be193267d5ff086164488c007084c20e1..6ee83c6f67af6046c2133a6de68d0cea49f4dc5b 100644 (file)
@@ -325,15 +325,24 @@ class ArvadosPutReportTest(ArvadosBaseTestCase):
 
 class ArvadosPutTest(ArvadosKeepLocalStoreTestCase):
     def call_main_on_test_file(self):
-        self.main_output = StringIO()
+        self.main_stdout = StringIO()
+        self.main_stderr = StringIO()
         with self.make_test_file() as testfile:
             path = testfile.name
-            arv_put.main(['--stream', '--no-progress', path], self.main_output)
+            arv_put.main(['--stream', '--no-progress', path],
+                         self.main_stdout, self.main_stderr)
         self.assertTrue(
             os.path.exists(os.path.join(os.environ['KEEP_LOCAL_STORE'],
                                         '098f6bcd4621d373cade4e832627b4f6')),
             "did not find file stream in Keep store")
 
+    def tearDown(self):
+        for outbuf in ['main_stdout', 'main_stderr']:
+            if hasattr(self, outbuf):
+                getattr(self, outbuf).close()
+                delattr(self, outbuf)
+        super(ArvadosPutTest, self).tearDown()
+
     def test_simple_file_put(self):
         self.call_main_on_test_file()