Skip to content

Commit

Permalink
Parse data consistently between Python and Go
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Feb 4, 2024
1 parent f8a1773 commit 3dbda79
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 25 deletions.
37 changes: 20 additions & 17 deletions src/golang/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,39 @@ func InstallMicromamba() (string, error) {

type AnacondaPkgAttr struct {
Subdir string `json:"subdir"`
Version string `json:"version"`
BuildNumber int32 `json:"build_number"`
Timestamp uint64 `json:"timestamp"`
SourceUrl string `json:"source_url"`
Md5 string `json:"md5"`
}

type AnacondaPkg struct {
Size uint32 `json:"size"`
Attrs AnacondaPkgAttr `json:"attrs"`
Type string `json:"type"`
Size uint32 `json:"size"`
Attrs AnacondaPkgAttr `json:"attrs"`
Type string `json:"type"`
Version string `json:"version"`
DownloadUrl string `json:"download_url"`
}

type AnacondaPkgAttrs []AnacondaPkgAttr
type AnacondaPkgs []AnacondaPkg

func (a AnacondaPkgAttrs) Len() int { return len(a) }
func (a AnacondaPkgAttrs) Less(i, j int) bool {
func (a AnacondaPkgs) Len() int { return len(a) }
func (a AnacondaPkgs) Less(i, j int) bool {
versioni, _ := version.NewVersion(a[i].Version)
versionj, _ := version.NewVersion(a[j].Version)
if versioni.LessThan(versionj) {
return true
} else if versionj.LessThan(versioni) {
return false
} else {
if a[i].BuildNumber < a[j].BuildNumber {
if a[i].Attrs.BuildNumber < a[j].Attrs.BuildNumber {
return true
} else if a[j].BuildNumber < a[i].BuildNumber {
} else if a[j].Attrs.BuildNumber < a[i].Attrs.BuildNumber {
return false
} else {
return a[i].Timestamp < a[j].Timestamp
return a[i].Attrs.Timestamp < a[j].Attrs.Timestamp
}
}
}
func (a AnacondaPkgAttrs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a AnacondaPkgs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

func InstallCondaStandalone() (string, error) {
// Get the most recent conda-standalone
Expand Down Expand Up @@ -100,18 +99,22 @@ func InstallCondaStandalone() (string, error) {
panic(err.Error())
}

var candidates = make([]AnacondaPkgAttr, 0)
var candidates = make([]AnacondaPkg, 0)
for _, datum := range data {
if datum.Attrs.Subdir == subdir {
candidates = append(candidates, datum.Attrs)
candidates = append(candidates, datum)
}
}
sort.Sort(AnacondaPkgAttrs(candidates))
sort.Sort(AnacondaPkgs(candidates))

if len(candidates) == 0 {
return "", errors.New("No conda-standalone found for " + subdir)
}
chosen := candidates[len(candidates)-1]

downloadUrl := "https:" + chosen.DownloadUrl
installedExe, err := downloadAndUnpackCondaTarBz2(
chosen.SourceUrl, map[string]string{
downloadUrl, map[string]string{
"standalone_conda/conda.exe": targetExeFilename("conda_standalone"),
})

Expand Down
51 changes: 43 additions & 8 deletions src/python/ensureconda/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import uuid
from contextlib import closing
from pathlib import Path
from typing import IO, TYPE_CHECKING, Iterator, Optional
from typing import IO, TYPE_CHECKING, Iterator, NamedTuple, Optional

import filelock
import requests
Expand All @@ -22,6 +22,28 @@
from _typeshed import StrPath


class AnacondaPkgAttr(NamedTuple):
"""Inner for data from Anaconda API
<https://api.anaconda.org/package/anaconda/conda-standalone/files>
<https://api.anaconda.org/package/conda-forge/conda-standalone/files>
"""

subdir: str
build_number: int
timestamp: int


class AnacondaPkg(NamedTuple):
"""Outer model for data from Anaconda API"""

size: int
attrs: AnacondaPkgAttr
type: str
version: str
download_url: str


def request_url_with_retry(url: str) -> requests.Response:
n = 10
for i in range(n):
Expand Down Expand Up @@ -87,20 +109,33 @@ def install_conda_exe() -> Optional[Path]:

candidates = []
for file_info in resp.json():
if file_info["attrs"]["subdir"] == subdir:
info = {**file_info, **file_info["attrs"]}
info_attrs = AnacondaPkgAttr(
subdir=file_info["attrs"]["subdir"],
build_number=file_info["attrs"]["build_number"],
timestamp=file_info["attrs"]["timestamp"],
)
info = AnacondaPkg(
size=file_info["size"],
attrs=info_attrs,
type=file_info["type"],
version=file_info["version"],
download_url=file_info["download_url"],
)
if info.attrs.subdir == subdir:
candidates.append(info)

candidates.sort(
key=lambda attrs: (
Version(attrs["version"]),
attrs["build_number"],
attrs["timestamp"],
key=lambda info: (
Version(info.version),
info.attrs.build_number,
info.attrs.timestamp,
)
)
if len(candidates) == 0:
raise RuntimeError(f"No conda-standalone package found for {subdir}")

chosen = candidates[-1]
url = "https:" + chosen["download_url"]
url = "https:" + chosen.download_url
path_to_written_executable = stream_conda_executable(url)
return path_to_written_executable

Expand Down

0 comments on commit 3dbda79

Please sign in to comment.