From 942e99aec02937c99699305124793cde435d711a Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Mon, 4 Mar 2024 23:24:06 +0000 Subject: [PATCH 1/7] Bumping python version --- replit.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/replit.nix b/replit.nix index 39d2b396..d18a2bf1 100644 --- a/replit.nix +++ b/replit.nix @@ -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 From 74392b13659a9a7bb370e6ebcc82f90b014f9ac4 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Mon, 4 Mar 2024 23:25:31 +0000 Subject: [PATCH 2/7] Logging stderr in case of python failure --- internal/backends/python/grab_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/backends/python/grab_test.go b/internal/backends/python/grab_test.go index cc06b168..30cd2f06 100644 --- a/internal/backends/python/grab_test.go +++ b/internal/backends/python/grab_test.go @@ -1,7 +1,9 @@ package python import ( + "bytes" "context" + "fmt" "os" "os/exec" "path" @@ -144,8 +146,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") From f07a3f55f2b9ac0ad78a766fe03df9d313d62046 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Mon, 4 Mar 2024 23:26:57 +0000 Subject: [PATCH 3/7] Adding a failing test --- internal/backends/python/grab_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/backends/python/grab_test.go b/internal/backends/python/grab_test.go index 30cd2f06..386510d7 100644 --- a/internal/backends/python/grab_test.go +++ b/internal/backends/python/grab_test.go @@ -20,6 +20,7 @@ func TestParseFile(t *testing.T) { "replit": true, "replit.ai": true, "bar": true, + "baz": true, } content := ` @@ -30,6 +31,9 @@ from Flask import Flask import replit import replit.ai import foo #upm package(bar) + +if True: + import baz ` testDir := t.TempDir() From 08aa5d46ef8ff80cb43f4dfe580439a2caa8a702 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 5 Mar 2024 00:10:16 +0000 Subject: [PATCH 4/7] Adding a query for imports within a top-level "if" statement --- internal/backends/python/grab.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/backends/python/grab.go b/internal/backends/python/grab.go index a9670c8e..94fe4e94 100644 --- a/internal/backends/python/grab.go +++ b/internal/backends/python/grab.go @@ -19,7 +19,23 @@ var importsQuery = ` name: (dotted_name) @import)]) (import_from_statement - module_name: (dotted_name) @import)] + module_name: (dotted_name) @import) + + (if_statement + condition: (_) + consequence: (block + [ + (import_statement + name: [(dotted_name) @import + (aliased_import + name: (dotted_name) @import)] + ) + (import_from_statement + module_name: (dotted_name) @import) + ] + ) + ) + ] . From 00ab06a59e89fef5118c2353c5e654b8f5a19208 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 5 Mar 2024 00:35:23 +0000 Subject: [PATCH 5/7] Handling more nested cases --- internal/backends/python/grab.go | 16 +++++++++++++--- internal/backends/python/grab_test.go | 15 ++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/internal/backends/python/grab.go b/internal/backends/python/grab.go index 94fe4e94..e1c0eff7 100644 --- a/internal/backends/python/grab.go +++ b/internal/backends/python/grab.go @@ -22,9 +22,8 @@ var importsQuery = ` module_name: (dotted_name) @import) (if_statement - condition: (_) - consequence: (block - [ + [ + (block [ (import_statement name: [(dotted_name) @import (aliased_import @@ -34,6 +33,17 @@ var importsQuery = ` 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) + ] + ) ) + ] ) ] diff --git a/internal/backends/python/grab_test.go b/internal/backends/python/grab_test.go index 386510d7..7c74893b 100644 --- a/internal/backends/python/grab_test.go +++ b/internal/backends/python/grab_test.go @@ -20,7 +20,10 @@ func TestParseFile(t *testing.T) { "replit": true, "replit.ai": true, "bar": true, - "baz": true, + "cond1": true, + "cond2": true, + "cond3": true, + "cond4": true, } content := ` @@ -32,8 +35,14 @@ import replit import replit.ai import foo #upm package(bar) -if True: - import baz +if False: + import cond1 +elif True: + import cond2 +elif True: + import cond3 +else: + import cond4 ` testDir := t.TempDir() From 6ec61bf03a85bf7bf03ee38629539cf5261d5ddf Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 5 Mar 2024 00:35:57 +0000 Subject: [PATCH 6/7] Reformatting query --- internal/backends/python/grab.go | 63 +++++++++++++++++++------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/internal/backends/python/grab.go b/internal/backends/python/grab.go index e1c0eff7..2962158e 100644 --- a/internal/backends/python/grab.go +++ b/internal/backends/python/grab.go @@ -13,43 +13,54 @@ import ( var importsQuery = ` (module - [(import_statement - name: [(dotted_name) @import - (aliased_import - name: (dotted_name) @import)]) + [ + (import_statement + name: [ + (dotted_name) @import + (aliased_import + name: (dotted_name) @import + ) + ] + ) - (import_from_statement - module_name: (dotted_name) @import) + (import_from_statement + module_name: (dotted_name) @import + ) - (if_statement - [ + (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)] + 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)] + module_name: (dotted_name) @import ) - (import_from_statement - module_name: (dotted_name) @import) - ] - ) ) - ] - ) + ]) + ) + ]) ] . - (comment)? @pragma) + (comment)? @pragma +) ` var pyPathSegmentPatterns = []string{"*.py"} From 4da26001a8a25154e99a74516745e7ffef261d29 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 5 Mar 2024 22:12:55 +0000 Subject: [PATCH 7/7] Formatting --- internal/backends/python/grab.go | 67 +++++++++++--------------------- 1 file changed, 23 insertions(+), 44 deletions(-) diff --git a/internal/backends/python/grab.go b/internal/backends/python/grab.go index 2962158e..610e5dee 100644 --- a/internal/backends/python/grab.go +++ b/internal/backends/python/grab.go @@ -13,54 +13,33 @@ import ( var importsQuery = ` (module - [ - (import_statement - name: [ - (dotted_name) @import - (aliased_import - name: (dotted_name) @import - ) - ] - ) - - (import_from_statement - module_name: (dotted_name) @import - ) - - (if_statement [ - (block [ - (import_statement - name: [ - (dotted_name) @import + [(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 - ) - ]) - ) - ]) - ] + name: (dotted_name) @import)]) + (import_from_statement + 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)]))])] . - (comment)? @pragma -) + (comment)? @pragma) ` var pyPathSegmentPatterns = []string{"*.py"}