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

feat/python guess inside ifstat #240

Merged
merged 7 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +17 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

sanity check: the if cond: import thing syntax works with all 3.x right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since before 2.7 afaik

pkgs.poetry
pkgs.R
pkgs.ruby
Expand Down
Loading