Skip to content

Commit

Permalink
adding caching or remote scripts if the cache=true flag is true.
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Jul 9, 2016
1 parent b2efe53 commit 6c95dea
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
16 changes: 16 additions & 0 deletions docs/cookbook/bestpractices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ setup.py. For everything else, use build.packages.
Is your service a flask application? It should have flask in it's setup.py.

Need nose to run your unit tests? add it via build.packages.install.

-----------------------------
Using cache=True on include()
-----------------------------

There are many cases where you would like
to inherit a script from a remote source, but
would also like to support offline execution of uranium
(once dependencies are installed).

If cache=True is set for build.include, uranium
will cache the script, thus allowing offline execution.


.. code-block:: python
build.include("http://my-remote-base", cache=True)
1 change: 0 additions & 1 deletion docs/cookbook/reuse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ And your consumer script will look like:
.. code-block:: python
# ubuild.py in the project.
from uranium import get_remote_script
build.include("https://internalgit.mycompany.com/shared-python/uranium_base.py")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
]

setup(name='uranium',
version='0.2.44b',
version='0.2.45b',
description='a build system for python',
long_description=open('README.rst').read(),
author='Yusuke Tsutsumi',
Expand Down
10 changes: 10 additions & 0 deletions tests/test_get_remote_script_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ def test_get_remote_script(tmpdir):
script["setup"](build)
httpretty.disable()
httpretty.reset()


def test_get_remote_script_with_cache(tmpdir):
httpretty.enable()
httpretty.register_uri(httpretty.GET, SCRIPT_URL,
body=REMOTE_SCRIPT)
get_remote_script(SCRIPT_URL, cache_dir=tmpdir.strpath)
httpretty.disable()
httpretty.reset()
get_remote_script(SCRIPT_URL, cache_dir=tmpdir.strpath)
10 changes: 8 additions & 2 deletions uranium/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, root, config=None, with_sandbox=True):
self._tasks = Tasks()
self._envvars = EnvironmentVariables()
self._options = None
self._cache_requests = False
self._history = History(
os.path.join(self._root, self.URANIUM_CACHE_DIR, self.HISTORY_NAME)
)
Expand Down Expand Up @@ -108,9 +109,14 @@ def main(build):
self._tasks.add(f)
return f

def include(self, script_path):
def include(self, script_path, cache=False):
""" executes the script at the specified path. """
get_remote_script(script_path, build=self)
if cache and self._cache_requests:
cache_dir = os.path.join(self.URANIUM_CACHE_DIR, "include_cache")
else:
cache_dir = None
get_remote_script(script_path, local_vars={"build": self},
cache_dir=cache_dir)

def run(self, options):
self._warmup()
Expand Down
34 changes: 30 additions & 4 deletions uranium/remote.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
import os
import requests


def get_remote_script(url, **params):
def get_remote_script(url, local_vars=None, cache_dir=None,
refresh_cache=False):
"""
download a remote script, evaluate it, and return a dictionary
containing all of the globals instantiated.
this can be VERY DANGEROUS! downloading and executing
raw code from any remote source can be very insecure.
if a cache directory is provided, the script will
"""
resp = requests.get(url)
body = None
local_vars = local_vars or {}
if cache_dir:
body = _retrieve_script_from_cache(
url, cache_dir,
refresh_cache=refresh_cache
)
else:
body = requests.get(url).text
script_locals = {}
script_locals.update(params)
exec(resp.text, script_locals)
script_locals.update(local_vars)
exec(body, script_locals)
return script_locals


def _retrieve_script_from_cache(url, cache_dir, refresh_cache=False):
path = url.replace("/", "_").replace("\\", "_")
path = os.path.join(cache_dir, path)
if refresh_cache or not os.path.exists(path):
body = requests.get(url).text
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
with open(path, "w") as fh:
fh.write(body)
return body
with open(path) as fh:
return fh.read()

0 comments on commit 6c95dea

Please sign in to comment.