Skip to content

Commit 7ae5af0

Browse files
authored
[planners] Add Python Poetry detection (#48)
## Summary * Implements Python detection for Poetry projects #35 * Add new --debug flag * Ensure we add stack to docker CMD errors * Fix path being relative (previously `devbox build some/path` did not work) * Adds example poetry project * Use pex to create python executable. * Specify new prod image that has python. RFC: * Add image to state. Currently only implemented for StartStage. Not exported to devbox.json ## How was it tested? `devbox init examples/python-poetry` `devbox shell examples/python-poetry` <img width="741" alt="image" src="https://user-images.githubusercontent.com/544948/187796457-fe292d2e-5870-43f1-9363-f228ab843ecb.png"> `devbox build examples/python-poetry` `docker run -p 8080:8080 devbox` <img width="682" alt="image" src="https://user-images.githubusercontent.com/544948/187795681-6d3cf780-d6ba-4b27-be05-25369a7458e0.png">
1 parent 8470053 commit 7ae5af0

23 files changed

+710
-29
lines changed

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Global gitignore for the entire monorepo. Only add things here that truly
2+
# need to always be ignored regardless of project.
3+
#
4+
# If something is more specific to a particular project, add a gitignore in the
5+
# corresponding subdirectory.
6+
7+
# MacOS filesystem
8+
.DS_Store
9+
10+
# Editors
11+
.idea
12+
.vscode
13+
14+
# NodeJS
15+
node_modules
16+
.yalc
17+
dist
18+
19+
20+
# Python
21+
*.pyc
22+
__pycache__/
23+
*.py[cod]
24+
*$py.class

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Devbox makes it easy to package your application into an OCI-compliant container
140140
Devbox currently detects the following languages:
141141

142142
- Go
143+
- Python (Poetry)
143144

144145
Want more languages? [Ask for a new Language](https://github.com/jetpack-io/devbox/issues) or contribute one via a Pull Request.
145146

boxcli/args.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@
33

44
package boxcli
55

6+
import (
7+
"path/filepath"
8+
9+
"github.com/pkg/errors"
10+
)
11+
612
// Functions that help parse arguments
713

814
// If args empty, defaults to the current directory
915
// Otherwise grabs the path from the first argument
1016
func pathArg(args []string) string {
1117
if len(args) > 0 {
12-
return args[0]
18+
p, err := filepath.Abs(args[0])
19+
if err != nil {
20+
panic(errors.WithStack(err)) // What even triggers this?
21+
}
22+
return p
1323
}
1424
return "."
1525
}

boxcli/midcobra/debug.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved.
2+
// Use of this source code is governed by the license in the LICENSE file.
3+
4+
package midcobra
5+
6+
import (
7+
"log"
8+
"os"
9+
10+
"github.com/spf13/cobra"
11+
"github.com/spf13/pflag"
12+
)
13+
14+
type DebugMiddleware struct {
15+
flag *pflag.Flag
16+
}
17+
18+
var _ Middleware = (*DebugMiddleware)(nil)
19+
20+
func (d *DebugMiddleware) AttachToFlag(flags *pflag.FlagSet, flagName string) {
21+
flags.Bool(
22+
flagName,
23+
false,
24+
"Show full stack traces on errors",
25+
)
26+
d.flag = flags.Lookup(flagName)
27+
d.flag.Hidden = true
28+
}
29+
30+
func (d *DebugMiddleware) preRun(cmd *cobra.Command, args []string) {}
31+
32+
func (d *DebugMiddleware) postRun(cmd *cobra.Command, args []string, runErr error) {
33+
if runErr != nil && d.Debug() {
34+
log.Printf("Error: %+v\n", runErr)
35+
}
36+
}
37+
38+
func (d *DebugMiddleware) Debug() bool {
39+
if d != nil && d.flag.Changed {
40+
return d.flag.Value.String() == "true"
41+
}
42+
return os.Getenv("DEBUG") != ""
43+
}

boxcli/root.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package boxcli
66
import (
77
"context"
88
"errors"
9+
"fmt"
910
"os"
1011
"os/exec"
1112

@@ -14,6 +15,8 @@ import (
1415
"go.jetpack.io/devbox/build"
1516
)
1617

18+
var debugMiddleware *midcobra.DebugMiddleware = &midcobra.DebugMiddleware{}
19+
1720
func RootCmd() *cobra.Command {
1821
command := &cobra.Command{
1922
Use: "devbox",
@@ -41,16 +44,29 @@ func RootCmd() *cobra.Command {
4144
command.AddCommand(RemoveCmd())
4245
command.AddCommand(ShellCmd())
4346
command.AddCommand(VersionCmd())
47+
48+
debugMiddleware.AttachToFlag(command.PersistentFlags(), "debug")
49+
4450
return command
4551
}
4652

4753
func Execute(ctx context.Context, args []string) int {
54+
defer func() {
55+
if r := recover(); r != nil {
56+
if debugMiddleware.Debug() {
57+
fmt.Printf("PANIC (DEBUG MODE ON): %+v\n", r)
58+
} else {
59+
fmt.Printf("Error: %s\n", r)
60+
}
61+
}
62+
}()
4863
exe := midcobra.New(RootCmd())
4964
exe.AddMiddleware(midcobra.Telemetry(&midcobra.TelemetryOpts{
5065
AppName: "devbox",
5166
AppVersion: build.Version,
5267
TelemetryKey: build.TelemetryKey,
5368
}))
69+
exe.AddMiddleware(debugMiddleware)
5470
return exe.Execute(ctx, args)
5571
}
5672

docker/docker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111

1212
"github.com/imdario/mergo"
13+
"github.com/pkg/errors"
1314
"golang.org/x/exp/slices"
1415
)
1516

@@ -59,7 +60,7 @@ func Build(path string, opts ...BuildOptions) error {
5960
return err
6061
}
6162

62-
args := []string{"build", "."}
63+
args := []string{"build", path}
6364
args = ToArgs(args, flags)
6465

6566
dir, fileName := parsePath(path)
@@ -78,7 +79,7 @@ func Build(path string, opts ...BuildOptions) error {
7879
cmd.Stderr = os.Stderr
7980
cmd.Env = append(os.Environ(), "BUILDKIT=1")
8081
cmd.Dir = dir
81-
return cmd.Run()
82+
return errors.WithStack(cmd.Run())
8283
}
8384

8485
func parsePath(path string) (string, string) {

examples/python-poetry/README.rst

Whitespace-only changes.

0 commit comments

Comments
 (0)