forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request getredash#139 from erans/master
Added support for running scripts as queries
- Loading branch information
Showing
4 changed files
with
105 additions
and
1 deletion.
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
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,48 @@ | ||
import json | ||
import logging | ||
import sys | ||
import os | ||
import subprocess | ||
|
||
# We use subprocess.check_output because we are lazy. | ||
# If someone will really want to run this on Python < 2.7 they can easily update the code to run | ||
# Popen, check the retcodes and other things and read the standard output to a variable. | ||
if not "check_output" in subprocess.__dict__: | ||
print "ERROR: This runner uses subprocess.check_output function which exists in Python 2.7" | ||
|
||
def script(connection_string): | ||
|
||
def query_runner(query): | ||
try: | ||
json_data = None | ||
error = None | ||
|
||
# Poor man's protection against running scripts from output the scripts directory | ||
if connection_string.find("../") > -1: | ||
return None, "Scripts can only be run from the configured scripts directory" | ||
|
||
query = query.strip() | ||
|
||
script = os.path.join(connection_string, query) | ||
if not os.path.exists(script): | ||
return None, "Script '%s' not found in script directory" % query | ||
|
||
output = subprocess.check_output(script, shell=False) | ||
if output != None: | ||
output = output.strip() | ||
if output != "": | ||
return output, None | ||
|
||
error = "Error reading output" | ||
except subprocess.CalledProcessError as e: | ||
return None, str(e) | ||
except KeyboardInterrupt: | ||
error = "Query cancelled by user." | ||
json_data = None | ||
except Exception as e: | ||
raise sys.exc_info()[1], None, sys.exc_info()[2] | ||
|
||
return json_data, error | ||
|
||
query_runner.annotate_query = False | ||
return query_runner |
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,45 @@ | ||
import json | ||
import logging | ||
import sys | ||
import os | ||
import urllib2 | ||
|
||
def url(connection_string): | ||
|
||
def query_runner(query): | ||
base_url = connection_string | ||
|
||
try: | ||
json_data = None | ||
error = None | ||
|
||
query = query.strip() | ||
|
||
if base_url is not None and base_url != "": | ||
if query.find("://") > -1: | ||
return None, "Accepting only relative URLs to '%s'" % base_url | ||
|
||
if base_url is None: | ||
base_url = "" | ||
|
||
url = base_url + query | ||
|
||
json_data = urllib2.urlopen(url).read().strip() | ||
|
||
if not json_data: | ||
error = "Error reading data from '%s'" % url | ||
|
||
return json_data, error | ||
|
||
except urllib2.URLError as e: | ||
return None, str(e) | ||
except KeyboardInterrupt: | ||
error = "Query cancelled by user." | ||
json_data = None | ||
except Exception as e: | ||
raise sys.exc_info()[1], None, sys.exc_info()[2] | ||
|
||
return json_data, error | ||
|
||
query_runner.annotate_query = False | ||
return query_runner |
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