-
Notifications
You must be signed in to change notification settings - Fork 14
ENG-1793: Install decrypted private-ingredient wheels at consume time #3822
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
Open
mitchell-as
wants to merge
3
commits into
mitchell/eng-1643
Choose a base branch
from
mitchell/eng-1793
base: mitchell/eng-1643
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Package wheelinstall installs a pure-Python wheel into a site-packages | ||
| // directory. | ||
| package wheelinstall | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/ActiveState/cli/internal/errs" | ||
| "github.com/ActiveState/cli/internal/fileutils" | ||
| "github.com/ActiveState/cli/internal/unarchiver" | ||
| ) | ||
|
|
||
| // installerName is the contents of the .dist-info/INSTALLER file (PEP 376). | ||
| const installerName = "state-tool" | ||
|
|
||
| // Install extracts the wheel at wheelPath into sitePackagesDir and writes the | ||
| // INSTALLER marker into its .dist-info. Entries that would escape sitePackagesDir, | ||
| // and wheels without a .dist-info, are rejected. | ||
| func Install(wheelPath, sitePackagesDir string) error { | ||
| if err := fileutils.MkdirUnlessExists(sitePackagesDir); err != nil { | ||
| return errs.Wrap(err, "could not create site-packages directory") | ||
| } | ||
|
|
||
| wheel, err := os.Open(wheelPath) | ||
| if err != nil { | ||
| return errs.Wrap(err, "could not open wheel") | ||
| } | ||
| defer wheel.Close() | ||
|
|
||
| // A wheel is a zip; untrusted-source mode confines entries to the destination. | ||
| ua := unarchiver.NewZip(unarchiver.WithUntrustedSource()) | ||
| if err := ua.Unarchive(wheel, sitePackagesDir); err != nil { | ||
| return errs.Wrap(err, "could not extract wheel") | ||
| } | ||
|
|
||
| if err := writeInstaller(sitePackagesDir); err != nil { | ||
| return errs.Wrap(err, "could not record installer") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // writeInstaller writes the INSTALLER marker into the wheel's sole *.dist-info | ||
| // directory, erroring if there is none. | ||
| func writeInstaller(sitePackagesDir string) error { | ||
| entries, err := os.ReadDir(sitePackagesDir) | ||
| if err != nil { | ||
| return errs.Wrap(err, "could not read site-packages directory") | ||
| } | ||
| for _, e := range entries { | ||
| if e.IsDir() && strings.HasSuffix(e.Name(), ".dist-info") { | ||
| marker := filepath.Join(sitePackagesDir, e.Name(), "INSTALLER") | ||
| if err := fileutils.WriteFile(marker, []byte(installerName+"\n")); err != nil { | ||
| return errs.Wrap(err, "could not write INSTALLER") | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
| return errs.New("wheel has no .dist-info directory") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package wheelinstall | ||
|
|
||
| import ( | ||
| "archive/zip" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| // makeWheel writes a zip (wheel) containing the given name->body entries and | ||
| // returns its path. | ||
| func makeWheel(t *testing.T, dir string, entries map[string]string) string { | ||
| t.Helper() | ||
| wheelPath := filepath.Join(dir, "greeting-1.0-py3-none-any.whl") | ||
| f, err := os.Create(wheelPath) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer f.Close() | ||
| zw := zip.NewWriter(f) | ||
| for name, body := range entries { | ||
| w, err := zw.Create(name) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := w.Write([]byte(body)); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| if err := zw.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| return wheelPath | ||
| } | ||
|
|
||
| func TestInstall(t *testing.T) { | ||
| t.Run("extracts the package and records the installer", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| wheel := makeWheel(t, dir, map[string]string{ | ||
| "greeting/__init__.py": "print('hi')\n", | ||
| "greeting-1.0.dist-info/METADATA": "Name: greeting\nVersion: 1.0\n", | ||
| "greeting-1.0.dist-info/RECORD": "", | ||
| }) | ||
|
|
||
| site := filepath.Join(dir, "site-packages") | ||
| if err := Install(wheel, site); err != nil { | ||
| t.Fatalf("Install: %v", err) | ||
| } | ||
|
|
||
| if got, _ := os.ReadFile(filepath.Join(site, "greeting", "__init__.py")); string(got) != "print('hi')\n" { | ||
| t.Errorf("package not extracted: got %q", got) | ||
| } | ||
| installer, err := os.ReadFile(filepath.Join(site, "greeting-1.0.dist-info", "INSTALLER")) | ||
| if err != nil { | ||
| t.Fatalf("INSTALLER not written: %v", err) | ||
| } | ||
| if string(installer) != installerName+"\n" { | ||
| t.Errorf("INSTALLER = %q, want %q", installer, installerName+"\n") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("a wheel without a .dist-info fails closed", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| wheel := makeWheel(t, dir, map[string]string{"greeting/__init__.py": "x\n"}) | ||
| if err := Install(wheel, filepath.Join(dir, "site-packages")); err == nil { | ||
| t.Error("expected an error for a wheel without a .dist-info directory") | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.