Commit 1ab0a72d authored by Raphael Kubo da Costa's avatar Raphael Kubo da Costa Committed by Commit Bot

Make upload_to_google_storage.py and tests work with Python 3.

Call decode() and encode() appropriately to manipulate bytes and strings.

Bug: 984182, 1009819
Change-Id: I8a73788ab912d6c6682012308954e4ab4df5fa98
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1859998
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Auto-Submit: Raphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Reviewed-by: 's avatarEdward Lesmes <ehmaldonado@chromium.org>
parent 377d251f
......@@ -71,7 +71,7 @@ class UploadTests(unittest.TestCase):
'%s/%s' % (self.base_url, self.lorem_ipsum_sha1)))])
self.assertTrue(os.path.exists(output_filename))
self.assertEqual(
open(output_filename, 'rb').read(),
open(output_filename, 'rb').read().decode(),
'7871c8e24da15bad8b0be2c36edc9dc77e37727f')
os.remove(output_filename)
self.assertEqual(code, 0)
......@@ -103,9 +103,9 @@ class UploadTests(unittest.TestCase):
def test_upload_single_file_remote_exists(self):
filenames = [self.lorem_ipsum]
output_filename = '%s.sha1' % self.lorem_ipsum
etag_string = 'ETag: 634d7c1ed3545383837428f031840a1e'
self.gsutil.add_expected(0, '', '')
self.gsutil.add_expected(0, etag_string, '')
etag_string = b'ETag: 634d7c1ed3545383837428f031840a1e'
self.gsutil.add_expected(0, b'', b'')
self.gsutil.add_expected(0, etag_string, b'')
code = upload_to_google_storage.upload_to_google_storage(
filenames, self.base_url, self.gsutil, False, False, 1, False, None)
self.assertEqual(
......@@ -116,7 +116,7 @@ class UploadTests(unittest.TestCase):
('ls', '-L', '%s/%s' % (self.base_url, self.lorem_ipsum_sha1)))])
self.assertTrue(os.path.exists(output_filename))
self.assertEqual(
open(output_filename, 'rb').read(),
open(output_filename, 'rb').read().decode(),
'7871c8e24da15bad8b0be2c36edc9dc77e37727f')
os.remove(output_filename)
self.assertEqual(code, 0)
......@@ -150,7 +150,7 @@ class UploadTests(unittest.TestCase):
output_filename = '%s.sha1' % self.lorem_ipsum
fake_hash = '6871c8e24da15bad8b0be2c36edc9dc77e37727f'
with open(output_filename, 'wb') as f:
f.write(fake_hash) # Fake hash.
f.write(fake_hash.encode()) # Fake hash.
code = upload_to_google_storage.upload_to_google_storage(
filenames, self.base_url, self.gsutil, False, False, 1, True, None)
self.assertEqual(
......@@ -162,7 +162,7 @@ class UploadTests(unittest.TestCase):
('check_call',
('cp', filenames[0], '%s/%s' % (self.base_url, fake_hash)))])
self.assertEqual(
open(output_filename, 'rb').read(), fake_hash)
open(output_filename, 'rb').read().decode(), fake_hash)
os.remove(output_filename)
self.assertEqual(code, 0)
......
......@@ -63,13 +63,13 @@ def get_md5_cached(filename):
# See if we can find an existing MD5 sum stored in a file.
if os.path.exists('%s.md5' % filename):
with open('%s.md5' % filename, 'rb') as f:
md5_match = re.search('([a-z0-9]{32})', f.read())
md5_match = re.search('([a-z0-9]{32})', f.read().decode())
if md5_match:
return md5_match.group(1)
else:
md5_hash = get_md5(filename)
with open('%s.md5' % filename, 'wb') as f:
f.write(md5_hash)
f.write(md5_hash.encode())
return md5_hash
......@@ -84,7 +84,7 @@ def _upload_worker(
if gsutil.check_call('ls', file_url)[0] == 0 and not force:
# File exists, check MD5 hash.
_, out, _ = gsutil.check_call_with_retries('ls', '-L', file_url)
etag_match = re.search(r'ETag:\s+([a-z0-9]{32})', out)
etag_match = re.search(r'ETag:\s+([a-z0-9]{32})', out.decode())
if etag_match:
remote_md5 = etag_match.group(1)
# Calculate the MD5 checksum to match it to Google Storage's ETag.
......@@ -176,15 +176,15 @@ def upload_to_google_storage(
'Main> Found hash for %s, sha1 calculation skipped.' % filename)
with open(filename + '.sha1', 'rb') as f:
sha1_file = f.read(1024)
if not re.match('^([a-z0-9]{40})$', sha1_file):
if not re.match('^([a-z0-9]{40})$', sha1_file.decode()):
print('Invalid sha1 hash file %s.sha1' % filename, file=sys.stderr)
return 1
upload_queue.put((filename, sha1_file))
upload_queue.put((filename, sha1_file.decode()))
continue
stdout_queue.put('Main> Calculating hash for %s...' % filename)
sha1_sum = get_sha1(filename)
with open(filename + '.sha1', 'wb') as f:
f.write(sha1_sum)
f.write(sha1_sum.encode())
stdout_queue.put('Main> Done calculating hash for %s.' % filename)
upload_queue.put((filename, sha1_sum))
hashing_duration = time.time() - hashing_start
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment