Skip to content

Instantly share code, notes, and snippets.

@olofk
Last active October 11, 2021 12:36
Show Gist options
  • Select an option

  • Save olofk/0f3911e7fa1028cc4c445bb2039ba06e to your computer and use it in GitHub Desktop.

Select an option

Save olofk/0f3911e7fa1028cc4c445bb2039ba06e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import io
import os
import shutil
import sys
import tarfile
import urllib.request
#TODO: Option to override cache dir
xdg_cache_home = os.environ.get("XDG_CACHE_HOME") or os.path.join(
os.path.expanduser("~"), ".cache"
)
cache_root = os.path.join(xdg_cache_home, "symbiflow")
os.makedirs(cache_root, exist_ok=True)
#TODO: Sanity check inputs
chip = sys.argv[1]
#Get link to package
linkname=f"symbiflow-{chip}_test-latest"
url = f"https://storage.googleapis.com/symbiflow-arch-defs-gha/"
with urllib.request.urlopen(url+linkname) as f:
linkfile = f.read()
#Got link. If local linkfile exists and is equal to the one just downloaded
# we assume to be up to date
cached_link_file = os.path.join(cache_root, linkname)
uptodate = False
if os.path.exists(cached_link_file) and linkfile == open(cached_link_file,'rb').read():
uptodate = True
else:
#Link file is not up to date. Use downloaded as new link file
open(cached_link_file, 'wb').write(linkfile)
#If not cached, download and extract to cache
#TODO: Wipe out old version?
if not uptodate:
with urllib.request.urlopen(linkfile.decode('utf8')) as f2:
xzfile = io.BytesIO(f2.read())
t = tarfile.open(fileobj=xzfile)
t.extractall(cache_root)
#Output path to files
#TODO: Path to individual files, or is dir enough?
print(os.path.join(cache_root, 'share','symbiflow','arch',chip+'_test'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment