Skip to content

Commit

Permalink
adding a caching mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed May 19, 2016
1 parent ba6ff0b commit 8ed6af8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
23 changes: 23 additions & 0 deletions uranium/scripts/uranium
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,35 @@
# * run uranium
import os

ROOT = os.path.dirname(__file__)
URANIUM_PATH = os.getenv('URANIUM_PATH', None)
URANIUM_GITHUB_ACCOUNT = os.getenv('URANIUM_GITHUB_ACCOUNT', 'toumorokoshi')
URANIUM_GITHUB_BRANCH = os.getenv('URANIUM_GITHUB_BRANCH', 'master')
URANIUM_STANDALONE_URL = "https://raw.githubusercontent.com/{account}/uranium/{branch}/uranium/scripts/uranium_standalone".format(account=URANIUM_GITHUB_ACCOUNT, branch=URANIUM_GITHUB_BRANCH)
CACHE_DIRECTORY = os.path.join(ROOT, ".uranium")
CACHED_URANIUM_STANDALONE = os.path.join(CACHE_DIRECTORY, "uranium_standalone")


def get_cached_standalone():
if not os.path.isfile(CACHED_URANIUM_STANDALONE):
return None
with open(CACHED_URANIUM_STANDALONE) as fh:
return fh.read().decode("UTF-8")


def store_cached_standalone(body):
if not os.path.exists(CACHE_DIRECTORY):
os.makedirs(CACHE_DIRECTORY)
with open(CACHED_URANIUM_STANDALONE, "w+") as fh:
fh.write(body)

cached_standalone = get_cached_standalone()

if URANIUM_PATH is not None:
print("Executing uranium from " + URANIUM_PATH)
execfile(os.path.realpath(URANIUM_PATH))
elif cached_standalone is not None:
exec(cached_standalone)
else:
print("Downloading uranium from " + URANIUM_STANDALONE_URL)
try:
Expand All @@ -22,4 +43,6 @@ else:

print("loading uranium bootstrapper...")
body = urlopen(URANIUM_STANDALONE_URL).read()
print("caching standalone uranium script...")
store_cached_standalone(body)
exec(body)
9 changes: 6 additions & 3 deletions uranium/scripts/uranium_standalone
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,9 @@ def main(argv):

os.chdir(current_dir)

LOGGER.info("setting up uranium...")
_install_uranium(install_dir, uranium_dir=uranium_dir,
version=options.version)

LOGGER.info("done!")

LOGGER.debug("running uranium...")
return _run_uranium(install_dir, remaining_args)

Expand Down Expand Up @@ -199,6 +196,11 @@ sys.path += paths_to_append


def _install_uranium(virtualenv_dir, uranium_dir=None, version=None):
uranium_executable = os.path.join(virtualenv_dir, 'bin', 'uranium')
if os.path.exists(uranium_executable):
return

LOGGER.info("setting up uranium...")
if uranium_dir:
uranium_dir = os.path.expanduser(uranium_dir)

Expand All @@ -221,6 +223,7 @@ def _install_uranium(virtualenv_dir, uranium_dir=None, version=None):
log_file
))
exit(1)
LOGGER.info("done!")


def _run_uranium(virtualenv_dir, args):
Expand Down

0 comments on commit 8ed6af8

Please sign in to comment.