diff --git a/statuspage/statuspage.py b/statuspage/statuspage.py index 255870b..bd4a773 100644 --- a/statuspage/statuspage.py +++ b/statuspage/statuspage.py @@ -18,6 +18,8 @@ except AttributeError: ROOT = os.path.dirname(os.path.realpath(__file__)) +PY3 = sys.version_info >= (3, 0) + COLORED_LABELS = ( ("1192FC", "investigating",), ("FFA500", "degraded performance"), @@ -150,11 +152,7 @@ def run_update(name, token, org): ref=sha, ) - remote_content = base64.b64decode(index.content) - remote_checksum = hashlib.sha1(remote_content.encode()) - local_checksum = hashlib.sha1(content.encode()) - - if remote_checksum.hexdigest() == local_checksum.hexdigest(): + if is_same_content(content, base64.b64decode(index.content)): click.echo("Local status matches remote status, no need to commit.") return False @@ -174,6 +172,15 @@ def run_update(name, token, org): branch="gh-pages", ) +def is_same_content(c1, c2): + def sha1(c): + if PY3: + if isinstance(c, str): + c = bytes(c, "utf-8") + else: + c = c.encode() + return hashlib.sha1(c) + return sha1(c1).hexdigest() == sha1(c2).hexdigest() def run_create(name, token, systems, org): gh = Github(token) diff --git a/statuspage/tests.py b/statuspage/tests.py index 0bd48be..0414289 100644 --- a/statuspage/tests.py +++ b/statuspage/tests.py @@ -7,7 +7,7 @@ from click.testing import CliRunner from statuspage import cli, update, create, iter_systems, get_severity, SYSTEM_LABEL_COLOR from github import UnknownObjectException - +import codecs class CLITestCase(TestCase): @@ -49,7 +49,7 @@ def setUp(self): self.gh().get_user().get_repo().get_issues.return_value = [self.issue, self.issue1] self.template = Mock() self.template.decoded_content = b"some foo" - self.template.content = b"some other foo".encode("base64") + self.template.content = codecs.encode(b"some other foo", "base64") self.gh().get_user().get_repo().get_file_contents.return_value = self.template self.gh().get_organization().get_repo().get_file_contents.return_value = self.template @@ -108,7 +108,7 @@ def test_update(self): def test_dont_update_when_nothing_changes(self): runner = CliRunner() - self.template.content = b"some foo".encode("base64") + self.template.content = codecs.encode(b"some foo", "base64") result = runner.invoke(update, ["--name", "testrepo", "--token", "token"]) self.assertEqual(result.exit_code, 0) self.gh.assert_called_with("token")