Skip to content

Commit eb57c34

Browse files
committed
Upgrade from "boto" to "boto3"
1 parent ea37887 commit eb57c34

File tree

3 files changed

+54
-37
lines changed

3 files changed

+54
-37
lines changed

seleniumbase/core/s3_manager.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@ def __init__(
2424
)
2525
with pip_find_lock:
2626
try:
27-
from boto.s3.connection import S3Connection
27+
import boto3
2828
except Exception:
29-
shared_utils.pip_install("boto", version="2.49.0")
30-
from boto.s3.connection import S3Connection
31-
self.conn = S3Connection(selenium_access_key, selenium_secret_key)
32-
self.bucket = self.conn.get_bucket(log_bucket)
29+
shared_utils.pip_install("boto3")
30+
import boto3
31+
self.conn = boto3.Session(
32+
aws_access_key_id=selenium_access_key,
33+
aws_secret_access_key=selenium_secret_key,
34+
)
35+
self.bucket = log_bucket
3336
self.bucket_url = bucket_url
3437

3538
def get_key(self, file_name):
36-
"""Create a new Key instance with the given name."""
37-
from boto.s3.key import Key
38-
39-
return Key(bucket=self.bucket, name=file_name)
39+
"""Create a new S3 connection instance with the given name."""
40+
return self.conn.resource("s3").Object(self.bucket, file_name)
4041

4142
def get_bucket(self):
4243
"""Return the bucket being used."""
@@ -53,18 +54,19 @@ def upload_file(self, file_name, file_path):
5354
content_type = "image/jpeg"
5455
elif file_name.endswith(".png"):
5556
content_type = "image/png"
56-
upload_key.set_contents_from_filename(
57-
file_path, headers={"Content-Type": content_type}
57+
upload_key.Bucket().upload_file(
58+
file_path,
59+
file_name,
60+
ExtraArgs={"ACL": "public-read", "ContentType": content_type},
5861
)
59-
upload_key.url = upload_key.generate_url(expires_in=3600).split("?")[0]
60-
try:
61-
upload_key.make_public()
62-
except Exception:
63-
pass
6462

65-
def upload_index_file(self, test_address, timestamp):
63+
def upload_index_file(
64+
self, test_address, timestamp, data_path, save_data_to_logs
65+
):
6666
"""Create an index.html file with links to all the log files
6767
that were just uploaded."""
68+
import os
69+
6870
global already_uploaded_files
6971
already_uploaded_files = list(set(already_uploaded_files))
7072
already_uploaded_files.sort()
@@ -76,15 +78,19 @@ def upload_index_file(self, test_address, timestamp):
7678
"<a href='" + self.bucket_url + ""
7779
"%s'>%s</a>" % (completed_file, completed_file)
7880
)
79-
index.set_contents_from_string(
80-
"<br>".join(index_str), headers={"Content-Type": "text/html"}
81+
index_page = str("<br>".join(index_str))
82+
save_data_to_logs(index_page, "index.html")
83+
file_path = os.path.join(data_path, "index.html")
84+
index.Bucket().upload_file(
85+
file_path,
86+
file_name,
87+
ExtraArgs={"ACL": "public-read", "ContentType": "text/html"},
8188
)
82-
index.make_public()
8389
return "%s%s" % (self.bucket_url, file_name)
8490

8591
def save_uploaded_file_names(self, files):
86-
"""Keep a record of all file names that have been uploaded. Upload log
87-
files related to each test after its execution. Once done, use
88-
already_uploaded_files to create an index file."""
92+
"""Keep a record of all file names that have been uploaded.
93+
Upload log files related to each test after its execution.
94+
Once done, use already_uploaded_files to create an index file."""
8995
global already_uploaded_files
9096
already_uploaded_files.extend(files)

seleniumbase/fixtures/base_case.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15174,16 +15174,16 @@ def tearDown(self):
1517415174
)
1517515175
uploaded_files.append(logfile_name)
1517615176
s3_bucket.save_uploaded_file_names(uploaded_files)
15177-
index_file = s3_bucket.upload_index_file(test_id, guid)
15178-
print("\n\n*** Log files uploaded: ***\n%s\n" % index_file)
15177+
index_file = s3_bucket.upload_index_file(
15178+
test_id, guid, self.data_path, self.save_data_to_logs
15179+
)
15180+
print("\n*** Log files uploaded: ***\n%s\n" % index_file)
1517915181
logging.info(
15180-
"\n\n*** Log files uploaded: ***\n%s\n" % index_file
15182+
"\n*** Log files uploaded: ***\n%s\n" % index_file
1518115183
)
1518215184
if self.with_db_reporting:
1518315185
from seleniumbase.core.testcase_manager import (
1518415186
TestcaseDataPayload,
15185-
)
15186-
from seleniumbase.core.testcase_manager import (
1518715187
TestcaseManager,
1518815188
)
1518915189

seleniumbase/plugins/s3_logging_plugin.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
"""The S3 Plugin for uploading test logs to the S3 bucket specified."""
1+
"""S3 Logging Plugin for SeleniumBase tests that run with pynose / nosetests"""
22
import uuid
3-
import logging
43
import os
5-
from seleniumbase.core.s3_manager import S3LoggingBucket
64
from nose.plugins import Plugin
75

86

@@ -14,25 +12,38 @@ def configure(self, options, conf):
1412
"""Get the options."""
1513
super().configure(options, conf)
1614
self.options = options
15+
self.test_id = None
16+
17+
def save_data_to_logs(self, data, file_name):
18+
from seleniumbase.fixtures import page_utils
19+
20+
test_logpath = os.path.join(self.options.log_path, self.test_id)
21+
file_name = str(file_name)
22+
destination_folder = test_logpath
23+
page_utils._save_data_as(data, destination_folder, file_name)
1724

1825
def afterTest(self, test):
19-
"""After each testcase, upload logs to the S3 bucket."""
26+
"""Upload logs to the S3 bucket after tests complete."""
27+
from seleniumbase.core.s3_manager import S3LoggingBucket
28+
29+
self.test_id = test.test.id()
2030
s3_bucket = S3LoggingBucket()
2131
guid = str(uuid.uuid4().hex)
22-
path = os.path.join(self.options.log_path, test.test.id())
32+
path = os.path.join(self.options.log_path, self.test_id)
2333
uploaded_files = []
2434
for logfile in os.listdir(path):
2535
logfile_name = "%s/%s/%s" % (
2636
guid,
27-
test.test.id(),
37+
self.test_id,
2838
logfile.split(path)[-1],
2939
)
3040
s3_bucket.upload_file(logfile_name, os.path.join(path, logfile))
3141
uploaded_files.append(logfile_name)
3242
s3_bucket.save_uploaded_file_names(uploaded_files)
33-
index_file = s3_bucket.upload_index_file(test.id(), guid)
34-
print("\n\n*** Log files uploaded: ***\n%s\n" % index_file)
35-
logging.error("\n\n*** Log files uploaded: ***\n%s\n" % index_file)
43+
index_file = s3_bucket.upload_index_file(
44+
test.id(), guid, path, self.save_data_to_logs
45+
)
46+
print("\n*** Log files uploaded: ***\n%s\n" % index_file)
3647

3748
# If the SB database plugin is also being used,
3849
# attach a link to the logs index database row.

0 commit comments

Comments
 (0)