Skip to content

Commit

Permalink
feat/python guess inside ifstat (#240)
Browse files Browse the repository at this point in the history
* Bumping python version

* Logging stderr in case of python failure

* Adding a failing test

* Adding a query for imports within a top-level "if" statement

* Handling more nested cases

* Reformatting query

* Formatting
  • Loading branch information
blast-hardcheese authored Mar 8, 2024
1 parent 936de88 commit d65eaaa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
20 changes: 18 additions & 2 deletions internal/backends/python/grab.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,25 @@ var importsQuery = `
name: [(dotted_name) @import
(aliased_import
name: (dotted_name) @import)])
(import_from_statement
module_name: (dotted_name) @import)]
module_name: (dotted_name) @import)
(if_statement
[(block
[(import_statement
name:
[(dotted_name) @import
(aliased_import
name: (dotted_name) @import)])
(import_from_statement
module_name: (dotted_name) @import)])
(_ (block
[(import_statement
name:
[(dotted_name) @import
(aliased_import
name: (dotted_name) @import)])
(import_from_statement
module_name: (dotted_name) @import)]))])]
.
Expand Down
20 changes: 20 additions & 0 deletions internal/backends/python/grab_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package python

import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path"
Expand All @@ -18,6 +20,10 @@ func TestParseFile(t *testing.T) {
"replit": true,
"replit.ai": true,
"bar": true,
"cond1": true,
"cond2": true,
"cond3": true,
"cond4": true,
}

content := `
Expand All @@ -28,6 +34,15 @@ from Flask import Flask
import replit
import replit.ai
import foo #upm package(bar)
if False:
import cond1
elif True:
import cond2
elif True:
import cond3
else:
import cond4
`

testDir := t.TempDir()
Expand Down Expand Up @@ -144,8 +159,13 @@ import flask

// Sanity test, actually run Python in the environment.
cmd := exec.Command("bash", "-c", "poetry lock -n; poetry install; poetry run python -m foo")

var stderr bytes.Buffer
cmd.Stderr = &stderr
stdout, err := cmd.Output()
if err != nil {

fmt.Println("stderr:", stderr.String())
t.Fatal("failed to execute python", err)
}
lines := strings.Split(strings.TrimSpace(string(stdout)), "\n")
Expand Down
4 changes: 2 additions & 2 deletions replit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
pkgs.nodejs-18_x
pkgs.yarn
pkgs.nodePackages.pnpm
pkgs.python310Full
pkgs.python310Packages.pip
pkgs.python311Full
pkgs.python311Packages.pip
pkgs.poetry
pkgs.R
pkgs.ruby
Expand Down

0 comments on commit d65eaaa

Please sign in to comment.