Skip to content

Commit

Permalink
Refactor print statements to use logger (#3330)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElusiveEllie authored Mar 14, 2023
1 parent 294286a commit 8936218
Show file tree
Hide file tree
Showing 31 changed files with 305 additions and 189 deletions.
14 changes: 9 additions & 5 deletions bugbug/bug_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

from logging import INFO, basicConfig, getLogger

import dateutil.parser
from dateutil.relativedelta import relativedelta

from bugbug import bugzilla

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


def bool_str(val):
assert val in ["", "0", "1"], f"Unexpected boolean value: '{val}'"
Expand Down Expand Up @@ -536,7 +541,7 @@ def assert_or_log(msg):
if do_assert:
assert False, msg
else:
print(msg)
logger.error(msg)

def parse_flag_change(change):
parts = change.split("(")
Expand Down Expand Up @@ -869,9 +874,8 @@ def get_inconsistencies(bugs):
for bug in bugs:
try:
rollback(bug, do_assert=True)
except Exception as e:
print(bug["id"])
print(e)
except Exception:
logger.exception("Failed to rollback bug %s", bug["id"])
inconsistencies.append(bug)

return inconsistencies
Expand All @@ -888,6 +892,6 @@ def get_inconsistencies(bugs):

for bug in tqdm(bugzilla.get_bugs()):
if args.verbose:
print(bug["id"])
logger.info(bug["id"])

rollback(bug, do_assert=True)
13 changes: 10 additions & 3 deletions bugbug/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import csv
import re
from datetime import datetime
from logging import INFO, basicConfig, getLogger
from typing import Iterable, Iterator, NewType, Optional

import tenacity
Expand All @@ -16,6 +17,9 @@

from bugbug import db, utils

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

BugDict = NewType("BugDict", dict)

BUGS_DB = "data/bugs.json"
Expand Down Expand Up @@ -191,7 +195,7 @@ def download_bugs(bug_ids: Iterable[int], security: bool = False) -> list[BugDic
old_bug_count += 1
new_bug_ids_set.discard(int(bug["id"]))

print(f"Loaded {old_bug_count} bugs.")
logger.info("Loaded %d bugs.", old_bug_count)

new_bug_ids = sorted(list(new_bug_ids_set))

Expand Down Expand Up @@ -417,8 +421,11 @@ def calculate_maintenance_effectiveness_indicator(
"closed": {},
}

print(
f"Calculating maintenance effectiveness indicator for the {team} team from {from_date} to {to_date}"
logger.info(
"Calculating maintenance effectiveness indicator for the %s team from %s to %s",
team,
from_date,
to_date,
)

for severity in MAINTENANCE_EFFECTIVENESS_SEVERITY_WEIGHTS.keys():
Expand Down
6 changes: 5 additions & 1 deletion bugbug/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pickle
from collections import defaultdict
from logging import INFO, basicConfig, getLogger
from typing import Any

import matplotlib
Expand All @@ -29,6 +30,9 @@
from bugbug.nlp import SpacyVectorizer
from bugbug.utils import split_tuple_generator, to_array

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


def classification_report_imbalanced_values(
y_true, y_pred, labels, target_names=None, sample_weight=None, digits=2, alpha=0.1
Expand Down Expand Up @@ -398,7 +402,7 @@ def train(self, importance_cutoff=0.15, limit=None):

self.clf.fit(X_train, self.le.transform(y_train))

print("Model trained")
logger.info("Model trained")

feature_names = self.get_human_readable_feature_names()
if self.calculate_importance and len(feature_names):
Expand Down
19 changes: 11 additions & 8 deletions bugbug/models/annotate_ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

from logging import INFO, basicConfig, getLogger

import xgboost
from imblearn.under_sampling import RandomUnderSampler
from sklearn.compose import ColumnTransformer
Expand All @@ -12,6 +14,9 @@
from bugbug import bugzilla, commit_features, feature_cleanup, labels, repository, utils
from bugbug.model import CommitModel

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


class AnnotateIgnoreModel(CommitModel):
def __init__(self, lemmatization: bool = False) -> None:
Expand Down Expand Up @@ -105,16 +110,14 @@ def get_labels(self):
for node, label in labels.get_labels("annotateignore"):
classes[node] = int(label)

print(
"{} commits that can be ignored".format(
sum(1 for label in classes.values() if label == 1)
)
logger.info(
"%d commits that can be ignored",
sum(1 for label in classes.values() if label == 1),
)

print(
"{} commits that cannot be ignored".format(
sum(1 for label in classes.values() if label == 0)
)
logger.info(
"%d commits that cannot be ignored",
sum(1 for label in classes.values() if label == 0),
)

return classes, [0, 1]
Expand Down
8 changes: 6 additions & 2 deletions bugbug/models/assignee.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# You can obtain one at http://mozilla.org/MPL/2.0/.

from collections import Counter
from logging import INFO, basicConfig, getLogger

import xgboost
from sklearn.compose import ColumnTransformer
Expand All @@ -24,6 +25,9 @@
"[email protected]",
]

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


class AssigneeModel(BugModel):
def __init__(self, lemmatization=False):
Expand Down Expand Up @@ -100,9 +104,9 @@ def get_labels(self):
if count > MINIMUM_ASSIGNMENTS
)

print(f"{len(top_assignees)} assignees")
logger.info("%d assignees", len(top_assignees))
for assignee, count in assignee_counts:
print(f"{assignee}: {count}")
logger.info("%s: %d", assignee, count)

classes = {
bug_id: assignee
Expand Down
18 changes: 10 additions & 8 deletions bugbug/models/backout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# You can obtain one at http://mozilla.org/MPL/2.0/.

from datetime import datetime
from logging import INFO, basicConfig, getLogger

import dateutil.parser
import xgboost
Expand All @@ -16,6 +17,9 @@
from bugbug import bug_features, commit_features, feature_cleanup, repository, utils
from bugbug.model import CommitModel

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


class BackoutModel(CommitModel):
def __init__(self, lemmatization=False, bug_data=False):
Expand Down Expand Up @@ -107,15 +111,13 @@ def get_labels(self):

classes[commit_data["node"]] = 1 if commit_data["backedoutby"] else 0

print(
"{} commits were backed out".format(
sum(1 for label in classes.values() if label == 1)
)
logger.info(
"%d commits were backed out",
sum(1 for label in classes.values() if label == 1),
)
print(
"{} commits were not backed out".format(
sum(1 for label in classes.values() if label == 0)
)
logger.info(
"%d commits were not backed out",
sum(1 for label in classes.values() if label == 0),
)

return classes, [0, 1]
Expand Down
46 changes: 29 additions & 17 deletions bugbug/models/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from collections import Counter
from datetime import datetime, timezone
from logging import INFO, basicConfig, getLogger

import dateutil.parser
import xgboost
Expand All @@ -17,6 +18,9 @@
from bugbug.bugzilla import get_product_component_count
from bugbug.model import BugModel

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


class ComponentModel(BugModel):
PRODUCTS = {
Expand Down Expand Up @@ -164,9 +168,9 @@ def get_labels(self):
component_counts = Counter(classes.values()).most_common()
top_components = set(component for component, count in component_counts)

print(f"{len(top_components)} components")
logger.info("%d components", len(top_components))
for component, count in component_counts:
print(f"{component}: {count}")
logger.info("%s: %d", component, count)

# Assert there is at least one bug for each conflated component.
for conflated_component in self.CONFLATED_COMPONENTS:
Expand Down Expand Up @@ -242,14 +246,18 @@ def check(self):
full_comp = f"{product}::{component}"

if full_comp not in bugs_number.keys():
print(
f"Component {component!r} of product {product!r} doesn't exists, failure"
logger.warning(
"Component %r of product %r doesn't exists, failure",
component,
product,
)
success = False

elif bugs_number[full_comp] <= 0:
print(
f"Component {component!r} of product {product!r} have 0 bugs or less in it, failure"
logger.warning(
"Component %r of product %r have 0 bugs or less in it, failure",
component,
product,
)
success = False

Expand All @@ -265,7 +273,7 @@ def check(self):
]

if not matching_components:
print(f"{conflated_component} doesn't match any component")
logger.warning("%s doesn't match any component", conflated_component)
success = False
continue

Expand All @@ -276,8 +284,9 @@ def check(self):
]

if not matching_components_values:
print(
f"{conflated_component} should match at least one component with more than 0 bugs"
logger.warning(
"%s should match at least one component with more than 0 bugs",
conflated_component,
)
success = False

Expand All @@ -286,13 +295,15 @@ def check(self):

for full_comp in self.CONFLATED_COMPONENTS_MAPPING.values():
if full_comp not in bugs_number:
print(
f"{full_comp} from conflated component mapping doesn't exists, failure"
logger.warning(
"%s from conflated component mapping doesn't exists, failure",
full_comp,
)
success = False
elif bugs_number[full_comp] <= 0:
print(
f"{full_comp} from conflated component mapping have less than 1 bug, failure"
logger.warning(
"%s from conflated component mapping have less than 1 bug, failure",
full_comp,
)
success = False

Expand All @@ -309,7 +320,7 @@ def check(self):
]

if not (matching_components or in_mapping):
print(f"It should be possible to map {conflated_component}")
logger.warning("It should be possible to map %s", conflated_component)
success = False
continue

Expand All @@ -336,15 +347,16 @@ def generate_meaningful_tuples():
if not meaningful_product_components.issubset(
self.meaningful_product_components
):
print("Meaningful product components mismatch")
logger.warning("Meaningful product components mismatch")

new_meaningful_product_components = (
meaningful_product_components.difference(
self.meaningful_product_components
)
)
print(
f"New meaningful product components {new_meaningful_product_components!r}"
logger.info(
"New meaningful product components %r",
new_meaningful_product_components,
)

success = False
Expand Down
8 changes: 6 additions & 2 deletions bugbug/models/defect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# You can obtain one at http://mozilla.org/MPL/2.0/.

import itertools
from logging import INFO, basicConfig, getLogger
from typing import Any

import xgboost
Expand All @@ -15,6 +16,9 @@
from bugbug import bug_features, bugzilla, feature_cleanup, labels, utils
from bugbug.model import BugModel

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


class DefectModel(BugModel):
def __init__(self, lemmatization=False, historical=False):
Expand Down Expand Up @@ -254,8 +258,8 @@ def _has_type_changed(bug):
def get_labels(self) -> tuple[dict[int, Any], list[Any]]:
classes = self.get_bugbug_labels("bug")

print("{} bugs".format(sum(1 for label in classes.values() if label == 1)))
print("{} non-bugs".format(sum(1 for label in classes.values() if label == 0)))
logger.info("%d bugs", (sum(1 for label in classes.values() if label == 1)))
logger.info("%d non-bugs", (sum(1 for label in classes.values() if label == 0)))

return classes, [0, 1]

Expand Down
Loading

0 comments on commit 8936218

Please sign in to comment.