run-flake8.ps1 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env pwsh
  2. <#
  3. .SYNOPSIS
  4. Runs flake8 to analyze Python source code for errors and best practices.
  5. #>
  6. # Get path.
  7. $scriptDir = Split-Path $PSCommandPath -Parent
  8. $projectDir = (Get-Item $scriptDir).Parent
  9. # Set working directory to root of project.
  10. Push-Location $projectDir
  11. # Get the diff for the current branch.
  12. $ExitCode = 0
  13. $GitDiff = git diff origin/dev
  14. # If there is no diff between dev, then flake8 everything.
  15. if ( $null -eq $GitDiff ) {
  16. flake8 .
  17. if ($LastExitCode -ne 0) { $ExitCode = $LastExitCode }
  18. }
  19. # Else flake8 just the diff.
  20. else {
  21. Write-Output $GitDiff | flake8 --diff
  22. if ($LastExitCode -ne 0) { $ExitCode = $LastExitCode }
  23. # If the project_template changed, then flake8 the testproject too.
  24. $GitDiffTempl = Write-Output $GitDiff | Select-String -Pattern "^diff .*/project_template/.*"
  25. if ( $null -ne $GitDiffTempl ) {
  26. flake8 testproject
  27. if ($LastExitCode -ne 0) { $ExitCode = $LastExitCode }
  28. }
  29. }
  30. # Write output for humans.
  31. if ($ExitCode -eq 0) {
  32. Write-Host -ForegroundColor Green "[✔] Flake8 passed with no errors"
  33. }
  34. else {
  35. # Write the error in a way that shows up as the failure reason in Azure Pipelines.
  36. Write-Host "##vso[task.LogIssue type=error;]Flake8 exited with errors. Please resolve issues above."
  37. }
  38. # Unset working directory and exit with flake8's exit code.
  39. Pop-Location
  40. exit $ExitCode