<commit_before>from __future__ import print_function

import os
import boto
import pytest
from boto.s3.key import Key as S3Key
from boto.exception import NoAuthHandlerFound
from os.path import join, isfile

s3_bucket = "bokeh-travis"
s3 = "https://s3.amazonaws.com/%s" % s3_bucket
build_id = os.environ.get("TRAVIS_BUILD_ID")
# Can we make this not hard coded and read in the report location from pytest?
report_file = "tests/pytest-report.html"


def pytest_sessionfinish(session, exitstatus):
    try_upload = os.environ.get("UPLOAD_PYTEST_HTML", "False") == "True"
    report_ready = isfile(report_file)
    if try_upload and report_ready:
        try:
            conn = boto.connect_s3()
            bucket = conn.get_bucket(s3_bucket)
            upload = True
        except NoAuthHandlerFound:
            print("Upload was requested but could not connect to S3.")
            upload = False

        if upload is True:
            with open(report_file, "r") as f:
                html = f.read()
            filename = join(build_id, "report.html")
            key = S3Key(bucket, filename)
            key.set_metadata("Content-Type", "text/html")
            key.set_contents_from_string(html, policy="public-read")
            print("\n%s Access report at: %s" % ("---", join(s3, filename)))


@pytest.fixture(scope="session")
def capabilities(capabilities):
    capabilities["browserName"] = "firefox"<commit_msg>Improve clarity of session finish function<commit_after>from __future__ import print_function

import os
import boto
import pytest
from boto.s3.key import Key as S3Key
from boto.exception import NoAuthHandlerFound
from os.path import join

s3_bucket = "bokeh-travis"
s3 = "https://s3.amazonaws.com/%s" % s3_bucket
build_id = os.environ.get("TRAVIS_BUILD_ID")
# Can we make this not hard coded and read in the report location from pytest?
report_file = "tests/pytest-report.html"


def pytest_sessionfinish(session, exitstatus):

    if os.environ.get("UPLOAD_PYTEST_HTML", "False") != "True":
        return

    if hasattr(session.config, 'slaveinput'):
        # when slave nodes (xdist) finish, the report won't be ready
        return

    try:
        conn = boto.connect_s3()
        bucket = conn.get_bucket(s3_bucket)

        with open(report_file, "r") as f:
            html = f.read()

        filename = join(build_id, "report.html")
        key = S3Key(bucket, filename)
        key.set_metadata("Content-Type", "text/html")
        key.set_contents_from_string(html, policy="public-read")
        print("\n%s Access report at: %s" % ("---", join(s3, filename)))

    except NoAuthHandlerFound:
        print("Upload was requested but could not connect to S3.")

    except OSError:
        print("Upload was requested but report was not generated.")


@pytest.fixture(scope="session")
def capabilities(capabilities):
    capabilities["browserName"] = "firefox"
{'fraction_non_alphanumeric': 0.10957960027567196, 'fraction_numerical': 0.007580978635423846, 'mean_word_length': 3.189033189033189, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2924766', 'n_tokens_mistral': 902, 'n_tokens_neox': 907, 'n_words': 244}