Skip to content

Commit

Permalink
Adding flit backend
Browse files Browse the repository at this point in the history
  • Loading branch information
blast-hardcheese committed Jun 17, 2024
1 parent c69f50b commit f96dafe
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/backends/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
var languageBackends = []api.LanguageBackend{
python.PythonPoetryBackend,
python.PythonPipBackend,
python.PythonFlitBackend,
nodejs.BunBackend,
nodejs.NodejsNPMBackend,
nodejs.NodejsPNPMBackend,
Expand Down
101 changes: 101 additions & 0 deletions internal/backends/python/flit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// flit.go, functions and bindings for flit
package python

import (
"context"
"os/exec"

"github.com/replit/upm/internal/api"
"github.com/replit/upm/internal/nix"
"github.com/replit/upm/internal/pkg"
"github.com/replit/upm/internal/util"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func flitIsAvailable() bool {
_, err := exec.LookPath("flit")
return err == nil
}

func flitGetPackageDir() string {
if path := getCommonPackageDir(); path != "" {
return path
}

if outputB, err := util.GetCmdOutputFallible([]string{
"python",
"-c", "import site; print(site.USER_BASE)",
}); err == nil {
return string(outputB)
}
return ""
}

func flitListSpecfile(mergeAllGroups bool) (pkgs map[api.PkgName]api.PkgSpec) {
cfg, err := readPyproject()
if err != nil {
util.DieIO("%s", err.Error())
}

pkgs = map[api.PkgName]api.PkgSpec{}

if cfg.Project == nil {
return pkgs
}

for _, pkg := range cfg.Project.Dependencies {
if name, spec, found := findPackage(pkg); found {
pkgs[*name] = *spec
}
}

return pkgs
}

// makePythonFlitBackend returns a backend for invoking poetry, given an arg0 for invoking Python
// (either a full path or just a name like "python3") to use when invoking Python.
func makePythonFlitBackend(python string) api.LanguageBackend {
b := api.LanguageBackend{
Name: "python3-flit",
Specfile: "pyproject.toml",
IsAvailable: flitIsAvailable,
Alias: "python-python3-flit",
FilenamePatterns: []string{"*.py"},
Quirks: api.QuirksNotReproducible | api.QuirksCannotAddRemove,
NormalizePackageArgs: normalizePackageArgs,
NormalizePackageName: normalizePackageName,
GetPackageDir: flitGetPackageDir,
SortPackages: pkg.SortPrefixSuffix(normalizePackageName),

Search: searchPypi,
Info: info,
Install: func(ctx context.Context) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "flit install")
defer span.Finish()

util.RunCmd([]string{"flit", "install"})
},
ListSpecfile: flitListSpecfile,
GuessRegexps: pythonGuessRegexps,
Guess: guess,
InstallReplitNixSystemDependencies: func(ctx context.Context, pkgs []api.PkgName) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "python.InstallReplitNixSystemDependencies")
defer span.Finish()
ops := []nix.NixEditorOp{}
for _, pkg := range pkgs {
deps := nix.PythonNixDeps(string(pkg))
ops = append(ops, nix.ReplitNixAddToNixEditorOps(deps)...)
}

for pkg := range flitListSpecfile(true) {
deps := nix.PythonNixDeps(string(pkg))
ops = append(ops, nix.ReplitNixAddToNixEditorOps(deps)...)
}
nix.RunNixEditorOps(ops)
},
}

return b
}
1 change: 1 addition & 0 deletions internal/backends/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,6 @@ func getPython3() string {
}

// PythonPoetryBackend is a UPM backend for Python 3 that uses Poetry.
var PythonFlitBackend = makePythonFlitBackend(getPython3())
var PythonPoetryBackend = makePythonPoetryBackend(getPython3())
var PythonPipBackend = makePythonPipBackend(getPython3())

0 comments on commit f96dafe

Please sign in to comment.