Skip to content
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

spread, tests: use prebuilt gojq, add simple tool for fetching files #14738

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
29 changes: 28 additions & 1 deletion spread.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,34 @@ prepare: |
exit 1
fi
fi


# native jq replacement, but still with some incompatibilies, see
# https://github.com/itchyny/gojq
# major differences:
# - map keys are sorted by default
# - with --yaml-input, can parse YAML
if os.query is-arm64; then
"$TESTSTOOLS"/simpleget -o /tmp/gojq.tar.gz \
https://github.com/itchyny/gojq/releases/download/v0.12.16/gojq_v0.12.16_linux_arm64.tar.gz
elif os.query is-arm; then
# upstream does not provide prebuilt binaries for armhf
# built with: GOARCH=arm GOARM=7
"$TESTSTOOLS"/simpleget -o /tmp/gojq.tar.gz \
https://storage.googleapis.com/snapd-spread-tests/dependencies/gojq_v0.12.16_linux_armhf.tar.gz
else
# must be amd64
"$TESTSTOOLS"/simpleget -o /tmp/gojq.tar.gz \
https://github.com/itchyny/gojq/releases/download/v0.12.16/gojq_v0.12.16_linux_amd64.tar.gz
fi

(
cd "$PROJECT_PATH/tests/bin"
# only extract gojq, typically under a path:
# -rwxr-xr-x runner/docker 4165784 2024-06-01 16:43 gojq_v0.12.16_linux_amd64/gojq
tar -xvf /tmp/gojq.tar.gz --strip-components=1 --wildcards "*/gojq"
test -x gojq
)

# NOTE: At this stage the source tree is available and no more special
# considerations apply.
"$TESTSLIB"/prepare-restore.sh --prepare-project
Expand Down
9 changes: 0 additions & 9 deletions tests/lib/prepare-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -608,15 +608,6 @@ prepare_project() {
disable_journald_rate_limiting
disable_journald_start_limiting
fi

# native jq replacement, but still with some incompatibilies, see
# https://github.com/itchyny/gojq
# major differences:
# - map keys are sorted by default
# - with --yaml-input, can parse YAML
GOBIN=$PROJECT_PATH/tests/bin \
CGO_ENABLED=0 \
go install github.com/itchyny/gojq/cmd/[email protected]
}

prepare_project_each() {
Expand Down
64 changes: 64 additions & 0 deletions tests/lib/tools/simpleget
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3

# note this script needs to run on Python as old as 3.6
import os.path
import argparse
import logging
import datetime
import tempfile
from urllib import request
from urllib.parse import urlparse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're using urllib, is there a reason why not to use urllib.request.urlretrieve? Then the code would just be:

from urllib import request
request.urlretrieve(opts.URL, opts.output)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use urlretrieve, since it's handling a bit more errors.



def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="simple file getter")
parser.add_argument("-o", "--output", help="output file name")
parser.add_argument("URL", help="download URL")
return parser.parse_args()


def main() -> None:
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.DEBUG
)
opts = parse_arguments()

fromurl = urlparse(opts.URL).path
output = os.path.basename(fromurl)
if opts.output:
output = opts.output

if os.path.exists(output):
raise RuntimeError("output path {} already exists".format(output))

total = 0

def _report(blocks: int, bsize: int, tot: int):
nonlocal total
total = tot
logging.debug("got %d/%d kB", blocks * bsize / 1024.0, total / 1024.0)

# create a temp in the same directory as output
outdir = os.path.dirname(output)

with tempfile.NamedTemporaryFile(mode="wb", dir=outdir, delete=False) as outf:
name = outf.name
outf.close()
try:
now = datetime.datetime.now()
fn, _ = request.urlretrieve(
opts.URL, filename=outf.name, reporthook=_report
)
after = datetime.datetime.now()

os.rename(fn, output)
except:
os.unlink(name)
raise

speed = float(total) / (after - now).total_seconds() / 1024.0
logging.info("wrote %d bytes to %s, speed %.02f kB/s", total, output, speed)


if __name__ == "__main__":
main()
Loading