Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve solacc script #1912

Merged
merged 11 commits into from
Jun 19, 2024
11 changes: 11 additions & 0 deletions tests/integration/source_code/solacc-malformed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
redkite-pricing/01_Data_Prep_Analysis.py
user-behavior-analytics-for-cloud-services/Anomaly Detection Pipeline/0 - Ingest and Explore Raw User Data.py
user-behavior-analytics-for-cloud-services/Anomaly Detection Pipeline/Full Data Engineering Pipeline/Train Anomaly Model on Historical Data.py
user-behavior-analytics-for-cloud-services/Anomaly Detection Pipeline/Full Data Engineering Pipeline/Run Inference on Active Batch.py
product-er-with-images/01_Prep_Data.py
fsi-mrm-generation/templates/Credit Adjudication - Example.py
lr-recommender/03_Train_Model.py
dist/multi-touch-attribution/04_markov_chains.py
security-analysis-tool/notebooks/Utils/workspace_bootstrap.py
# the following fails to parse due to module dependency azure/devops/v5_1/client_factory.py
edge-ml-for-manufacturing/03_Trigger_AzureDevOps_Job.py
45 changes: 37 additions & 8 deletions tests/integration/source_code/solacc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import sys
from pathlib import Path

Expand Down Expand Up @@ -44,16 +45,27 @@ def clone_all():
run_command(f'git clone {url} {dst}')


def lint_all():
def lint_all(file_to_lint: str | None):
asnare marked this conversation as resolved.
Show resolved Hide resolved
# pylint: disable=too-many-nested-blocks
ws = WorkspaceClient(host='...', token='...')
ctx = LocalCheckoutContext(ws).replace(
linter_context_factory=lambda session_state: LinterContext(MigrationIndex([]), session_state)
)
parseable = 0
missing_imports = 0
all_files = list(dist.glob('**/*.py'))
all_files = list(dist.glob('**/*.py')) if file_to_lint is None else [Path(dist, file_to_lint)]
if file_to_lint is None:
unparsed = Path(Path(__file__).parent, "solacc-unparsed.txt")
if unparsed.exists():
os.remove(unparsed)
asnare marked this conversation as resolved.
Show resolved Hide resolved
skipped: set[str] | None = None
malformed = Path(Path(__file__).parent, "solacc-malformed.txt")
ericvergnaud marked this conversation as resolved.
Show resolved Hide resolved
if file_to_lint is None and malformed.exists():
text = malformed.read_text()
skipped = set(text.split("\n"))
for file in all_files:
if skipped and file.relative_to(dist).as_posix() in skipped:
continue
try:
for located_advice in ctx.local_code_linter.lint_path(file):
if located_advice.advice.code == 'import-not-found':
Expand All @@ -63,17 +75,34 @@ def lint_all():
parseable += 1
except Exception as e: # pylint: disable=broad-except
# here we're most likely catching astroid & sqlglot errors
logger.error(f"Error during parsing of {file}: {e}".replace("\n", " "), exc_info=e)
if file_to_lint:
logger.error(f"Error during parsing of {file}: {e}".replace("\n", " "), exc_info=e)
else:
logger.error(f"Error during parsing of {file}: {e}".replace("\n", " "))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these branches needed? 🤔

I think here we can also use logger.exception() which will automatically include the exception stack trace, etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we only want the stack trace when debugging a single file otherwise it pollutes the output

if file_to_lint is None:
# populate solacc-unparsed.txt
with unparsed.open(mode="a") as f:
f.write(file.relative_to(dist).as_posix())
f.write("\n")
parseable_pct = int(parseable / len(all_files) * 100)
logger.info(f"Parseable: {parseable_pct}% ({parseable}/{len(all_files)}), missing imports: {missing_imports}")
logger.info(
f"Skipped: {len(skipped or [])}, parseable: {parseable_pct}% ({parseable}/{len(all_files)}), missing imports: {missing_imports}"
)
if parseable_pct < 100:
sys.exit(1)


if __name__ == "__main__":
def main(args: list[str]):
ericvergnaud marked this conversation as resolved.
Show resolved Hide resolved
install_logger()
logging.root.setLevel(logging.INFO)
logger.info("Cloning...")
clone_all()
file_to_lint = args[1] if len(args) > 1 else None
if not file_to_lint:
# don't clone if linting just one file, assumption is we're troubleshooting
logger.info("Cloning...")
clone_all()
logger.info("Linting...")
asnare marked this conversation as resolved.
Show resolved Hide resolved
lint_all()
lint_all(file_to_lint)


if __name__ == "__main__":
main(sys.argv)
Loading