Skip to content

Commit

Permalink
Replace print with logger in snapshot script (#376)
Browse files Browse the repository at this point in the history
* Remove print and use logger

* Add ignored trunk files
  • Loading branch information
ClementWalter authored Sep 27, 2023
1 parent 8d32427 commit 299b2b4
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 28 deletions.
8 changes: 8 additions & 0 deletions .trunk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*out
*logs
*actions
*notifications
*tools
plugins
user_trunk.yaml
user.yaml
2 changes: 2 additions & 0 deletions .trunk/configs/.isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
profile=black
7 changes: 7 additions & 0 deletions .trunk/configs/.shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enable=all
source-path=SCRIPTDIR
disable=SC2154

# If you're having issues with shellcheck following source, disable the errors via:
# disable=SC1090
# disable=SC1091
10 changes: 10 additions & 0 deletions .trunk/configs/.yamllint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rules:
quoted-strings:
required: only-when-needed
extra-allowed: ["{|}"]
empty-values:
forbid-in-block-mappings: true
forbid-in-flow-mappings: true
key-duplicates: {}
octal-values:
forbid-implicit-octal: true
5 changes: 5 additions & 0 deletions .trunk/configs/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generic, formatter-friendly config.
select = ["B", "D3", "D4", "E", "F"]

# Never enforce `E501` (line length violations). This should be handled by formatters.
ignore = ["E501"]
54 changes: 54 additions & 0 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# This file controls the behavior of Trunk: https://docs.trunk.io/cli
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1
cli:
version: 1.16.1
plugins:
sources:
- id: trunk
ref: v1.2.4
uri: https://github.com/trunk-io/plugins
runtimes:
enabled:
- go@1.21.0
- node@18.12.1
- python@3.10.8
lint:
files:
- name: cairo
extensions:
- cairo
definitions:
- name: cairo
files: [cairo]
commands:
- output: rewrite
success_codes: [0]
run: scarb fmt
run_linter_from: workspace
enabled:
# https://github.com/software-mansion/scarb/issues/700
# - cairo@SYSTEM
- actionlint@1.6.26
- bandit@1.7.5
- black@23.9.1
- checkov@2.4.9
- git-diff-check
- isort@5.12.0
- markdownlint@0.37.0
- oxipng@8.0.0
- prettier@3.0.3
- ruff@0.0.291
- shellcheck@0.9.0
- shfmt@3.6.0
- taplo@0.8.1
- trivy@0.45.1
- trufflehog@3.57.0
- yamllint@1.32.0
actions:
disabled:
- trunk-announce
- trunk-fmt-pre-commit
enabled:
- trunk-check-pre-push
- trunk-upgrade-available
45 changes: 17 additions & 28 deletions scripts/compare_snapshot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import os
import re

Expand All @@ -8,6 +9,9 @@
import zipfile
from pathlib import Path

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def get_github_token_from_env(file_path=".env"):
"""Read the .env file and extract the GITHUB_TOKEN value."""
Expand All @@ -22,7 +26,9 @@ def get_github_token_from_env(file_path=".env"):
except FileNotFoundError:
return None
except ValueError:
print(f"Error: Invalid format in {file_path}. Expected 'KEY=VALUE' format.")
logger.error(
f"Error: Invalid format in {file_path}. Expected 'KEY=VALUE' format."
)
return None


Expand Down Expand Up @@ -118,28 +124,6 @@ def compare_snapshots(current, previous):
return improvements, worsened


def print_formatted_output(improvements, worsened, gas_changes):
"""Print results formatted."""
if improvements or worsened:
print("****BETTER****")
for elem in improvements:
print(elem)

print("\n")
print("****WORST****")
for elem in worsened:
print(elem)

gas_statement = (
"performance degradation, gas consumption +"
if gas_changes > 0
else "performance improvement, gas consumption"
)
print(f"Overall gas change: {gas_statement}{format(gas_changes, '.2f')} %")
else:
print("No changes in gas consumption.")


def total_gas_used(current, previous):
"""Return the total gas used in the current and previous snapshot, not taking into account added tests."""
common_keys = set(current.keys()) & set(previous.keys())
Expand All @@ -151,18 +135,23 @@ def total_gas_used(current, previous):


def main():
# Load previous snapshot
previous_snapshot = get_previous_snapshot()
if previous_snapshot is None:
print("Error: Failed to load previous snapshot.")
logger.error("Error: Failed to load previous snapshot.")
return

current_snapshots = get_current_gas_snapshot()
improvements, worsened = compare_snapshots(current_snapshots, previous_snapshot)
cur_gas, prev_gas = total_gas_used(current_snapshots, previous_snapshot)
print_formatted_output(
improvements, worsened, (cur_gas - prev_gas) * 100 / prev_gas
)
logger.info("****BETTER****")
for elem in improvements:
logger.info(elem)

logger.info("****WORST****")
for elem in worsened:
logger.info(elem)

logger.info(f"Overall gas change: {(cur_gas - prev_gas) * 100 / prev_gas:.2%}")
if worsened:
raise ValueError("Gas usage increased")

Expand Down

0 comments on commit 299b2b4

Please sign in to comment.