5652: Merge branch 'master' into 5652-arvputget-silent-mode
authorLucas Di Pentima <lucas@curoverse.com>
Mon, 17 Jul 2017 17:05:48 +0000 (14:05 -0300)
committerLucas Di Pentima <lucas@curoverse.com>
Mon, 17 Jul 2017 17:05:48 +0000 (14:05 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@curoverse.com>

sdk/python/arvados/commands/put.py
sdk/python/tests/test_arv_get.py
sdk/python/tests/test_arv_put.py

index 68f63b1c261bcfcfde9dd6b42cbe6ca8a67e15ab..b357bc94b8441380fffae5d41a96fa95c6bf5600 100644 (file)
@@ -193,6 +193,11 @@ Display machine-readable progress on stderr (bytes and, if known,
 total data size).
 """)
 
+_group.add_argument('--silent', action='store_true',
+                    help="""
+Do not produce any output unless an error happens.
+""")
+
 _group = run_opts.add_mutually_exclusive_group()
 _group.add_argument('--resume', action='store_true', default=True,
                     help="""
@@ -243,7 +248,7 @@ def parse_arguments(arguments):
     """)
 
     # Turn on --progress by default if stderr is a tty.
-    if (not (args.batch_progress or args.no_progress)
+    if (not (args.batch_progress or args.no_progress or args.silent)
         and os.isatty(sys.stderr.fileno())):
         args.progress = True
 
@@ -964,9 +969,12 @@ def desired_project_uuid(api_client, project_uuid, num_retries):
 def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
     global api_client
 
-    logger = logging.getLogger('arvados.arv_put')
-    logger.setLevel(logging.INFO)
     args = parse_arguments(arguments)
+    logger = logging.getLogger('arvados.arv_put')
+    if args.silent:
+        logger.setLevel(logging.WARNING)
+    else:
+        logger.setLevel(logging.INFO)
     status = 0
     if api_client is None:
         api_client = arvados.api('v1')
@@ -1135,7 +1143,7 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
     # Print the locator (uuid) of the new collection.
     if output is None:
         status = status or 1
-    else:
+    elif not args.silent:
         stdout.write(output)
         if not output.endswith('\n'):
             stdout.write('\n')
index 2af649c65899e8a5ae7c7eb7eaa0f5a07ff285af..eb9a71fac960c3fd8a3e3d83c1d1cfec6cefc3df 100644 (file)
@@ -5,6 +5,7 @@
 from __future__ import absolute_import
 from future.utils import listitems
 import io
+import mock
 import os
 import re
 import shutil
@@ -16,9 +17,11 @@ import arvados.commands.get as arv_get
 from . import run_test_server
 
 from . import arvados_testutil as tutil
+from .arvados_testutil import ArvadosBaseTestCase
 
 class ArvadosGetTestCase(run_test_server.TestCaseWithServers,
-                         tutil.VersionChecker):
+                         tutil.VersionChecker,
+                         ArvadosBaseTestCase):
     MAIN_SERVER = {}
     KEEP_SERVER = {}
 
@@ -100,7 +103,8 @@ class ArvadosGetTestCase(run_test_server.TestCaseWithServers,
             self.assertEqual(m_from_collection, m_from_file)
 
     def test_get_collection_stripped_manifest(self):
-        col_loc, col_pdh, col_manifest = self.write_test_collection(strip_manifest=True)
+        col_loc, col_pdh, col_manifest = self.write_test_collection(
+            strip_manifest=True)
         # Get the collection manifest by UUID
         r = self.run_get(['--strip-manifest', col_loc, self.tempdir])
         self.assertEqual(0, r)
@@ -138,3 +142,35 @@ class ArvadosGetTestCase(run_test_server.TestCaseWithServers,
         with open(os.path.join(self.tempdir, "foo.txt"), "r") as f:
             self.assertEqual("another foo", f.read())
 
+    def test_no_progress_when_stderr_not_a_tty(self):
+        # Create a collection with a big file (>64MB) to force the progress
+        # to be printed
+        c = collection.Collection()
+        with c.open('bigfile.txt', 'wb') as f:
+            for _ in range(65):
+                f.write("x" * 1024 * 1024)
+        c.save_new()
+        tmpdir = self.make_tmpdir()
+        # Simulate a TTY stderr
+        stderr = mock.MagicMock()
+        stderr.isatty.return_value = True
+        stdout = tutil.BytesIO()
+        # Confirm that progress is written to stderr when is a tty
+        r = arv_get.main(['{}/bigfile.txt'.format(c.manifest_locator()),
+                          '{}/bigfile.txt'.format(tmpdir)],
+                         stdout, stderr)
+        self.assertEqual(0, r)
+        self.assertEqual(b'', stdout.getvalue())
+        self.assertTrue(stderr.write.called)
+        # Clean up and reset stderr mock
+        os.remove('{}/bigfile.txt'.format(tmpdir))
+        stderr = mock.MagicMock()
+        stderr.isatty.return_value = False
+        stdout = tutil.BytesIO()
+        # Confirm that progress is not written to stderr when isn't a tty
+        r = arv_get.main(['{}/bigfile.txt'.format(c.manifest_locator()),
+                          '{}/bigfile.txt'.format(tmpdir)],
+                         stdout, stderr)
+        self.assertEqual(0, r)
+        self.assertEqual(b'', stdout.getvalue())
+        self.assertFalse(stderr.write.called)
index b31d2eac0d06297548cd9935c59e86569225a72a..756e37885ec2f996a6f07b1c29570871f8a25f13 100644 (file)
@@ -969,6 +969,32 @@ class ArvPutIntegrationTest(run_test_server.TestCaseWithServers,
                          r'^\./%s.*:file2.txt' % os.path.basename(tmpdir))
         self.assertRegex(c['manifest_text'], r'^.*:file3.txt')
 
+    def test_silent_mode(self):
+        self.authorize_with('active')
+        tmpdir = self.make_tmpdir()
+        with open(os.path.join(tmpdir, 'test.txt'), 'w') as f:
+            f.write('hello world')
+        pipe = subprocess.Popen(
+            [sys.executable, arv_put.__file__] + ['--silent', tmpdir],
+            stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+            stderr=subprocess.PIPE, env=self.ENVIRON)
+        stdout, stderr = pipe.communicate()
+        # No output should occur on normal operations
+        self.assertNotRegex(stderr.decode(), r'.+')
+        self.assertNotRegex(stdout.decode(), r'.+')
+
+    def test_silent_mode_does_not_avoid_error_messages(self):
+        self.authorize_with('active')
+        pipe = subprocess.Popen(
+            [sys.executable, arv_put.__file__] + ['--silent',
+                                                  '/path/not/existant'],
+            stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+            stderr=subprocess.PIPE, env=self.ENVIRON)
+        stdout, stderr = pipe.communicate()
+        # Error message should be displayed when errors happen
+        self.assertRegex(stderr.decode(), r'.*ERROR:.*')
+        self.assertNotRegex(stdout.decode(), r'.+')
+
 
 if __name__ == '__main__':
     unittest.main()