ginstall: support reading tar from stdin.
Also add unit tests for the helper method we use to resolve URLs and
paths given on the command line.
Change-Id: I6adf3e9b091571ff93e55e4dc638024ebf3af91c
diff --git a/ginstall/ginstall.py b/ginstall/ginstall.py
index 5db5493..fc354db 100755
--- a/ginstall/ginstall.py
+++ b/ginstall/ginstall.py
@@ -927,7 +927,10 @@
pass
try:
- return open(path)
+ if path == '-':
+ return sys.stdin
+ else:
+ return open(path)
except ValueError:
pass
diff --git a/ginstall/ginstall_test.py b/ginstall/ginstall_test.py
index 8deac64..a4e3c8a 100755
--- a/ginstall/ginstall_test.py
+++ b/ginstall/ginstall_test.py
@@ -21,6 +21,7 @@
import shutil
import StringIO
import struct
+import sys
import tempfile
import unittest
import ginstall
@@ -365,6 +366,21 @@
total = ginstall.GetMemTotal()
self.assertTrue(total < 4*1e9)
+ def testOpenPathOrUrl(self):
+ # URL
+ two_oh_four = ginstall.OpenPathOrUrl('http://www.gstatic.com/generate_204')
+ self.assertEqual(204, two_oh_four.getcode())
+
+ # on-disk file
+ on_disk_file = tempfile.NamedTemporaryFile()
+ testdata = os.urandom(16)
+ on_disk_file.write(testdata)
+ on_disk_file.flush()
+ self.assertEqual(ginstall.OpenPathOrUrl(on_disk_file.name).read(), testdata)
+
+ # stdin (-)
+ self.assertEqual(ginstall.OpenPathOrUrl('-'), sys.stdin)
+
if __name__ == '__main__':
unittest.main()