This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move hal zipfile downloading to an importable module
- Loading branch information
Showing
3 changed files
with
87 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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,63 @@ | ||
# | ||
# Utilities for compiling things that link against HAL or need the HAL | ||
# headers | ||
# | ||
# Note: this file must not depend on anything else in the library, so we | ||
# can import it from setup.py. Also used by the wpilib tests | ||
# | ||
|
||
|
||
hal_version = '2017.1.1' | ||
|
||
hal_site = 'http://first.wpi.edu/FRC/roborio/maven/release/edu/wpi/first/wpilib/athena-runtime' | ||
hal_zip = 'athena-runtime-%s.zip' % hal_version | ||
|
||
def _download_halzip(): | ||
''' | ||
Downloads the HAL zipfile to a temporary directory | ||
''' | ||
|
||
import atexit | ||
from urllib.request import urlretrieve, urlcleanup | ||
import sys | ||
|
||
print("Downloading", hal_zip) | ||
|
||
def _reporthook(count, blocksize, totalsize): | ||
percent = int(count*blocksize*100/totalsize) | ||
sys.stdout.write("\r%02d%%" % percent) | ||
sys.stdout.flush() | ||
|
||
filename, _ = urlretrieve("%s/%s/%s" % (hal_site, hal_version, hal_zip), | ||
reporthook=_reporthook) | ||
atexit.register(urlcleanup) | ||
return filename | ||
|
||
def extract_halzip(to=None): | ||
''' | ||
Downloads the HAL zipfile and extracts it to a specified location | ||
:param to: is either a string or a dict of {src: dst} | ||
''' | ||
|
||
import atexit | ||
import shutil | ||
import tempfile | ||
import zipfile | ||
|
||
if to is None: | ||
# generate temporary directory | ||
tod = tempfile.TemporaryDirectory() | ||
to = tod.name | ||
atexit.register(tod.cleanup) | ||
|
||
hal_zipfile = _download_halzip() | ||
with zipfile.ZipFile(hal_zipfile) as z: | ||
if isinstance(to, str): | ||
z.extractall(to) | ||
return to | ||
else: | ||
for src, dst in to.items(): | ||
with z.open(src, 'r') as zfp: | ||
with open(dst, 'wb') as fp: | ||
shutil.copyfileobj(zfp, fp) |
This file contains 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 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