Commit faaf0cd0 authored by Jérome Perrin's avatar Jérome Perrin

Fix apachedex wrapper

See merge request nexedi/slapos.toolbox!86
parents 840e40bb 0a1fd4b6
...@@ -32,7 +32,7 @@ from __future__ import print_function ...@@ -32,7 +32,7 @@ from __future__ import print_function
import os, errno import os, errno
import subprocess import subprocess
import argparse import argparse
import time import shlex
from datetime import date from datetime import date
# run_apachedex.py <apachedex_executable> /srv/etc/output_folder script_name # run_apachedex.py <apachedex_executable> /srv/etc/output_folder script_name
...@@ -61,8 +61,7 @@ def build_command(apachedex_executable, output_file, ...@@ -61,8 +61,7 @@ def build_command(apachedex_executable, output_file,
raise ValueError("log_list: no log files to analyse were provided") raise ValueError("log_list: no log files to analyse were provided")
if config: if config:
config = filter(None, [x.strip() for x in config.split(' ')]) argument_list.extend(shlex.split(config))
argument_list += config
argument_list.append('--error-detail') argument_list.append('--error-detail')
argument_list += log_list argument_list += log_list
...@@ -74,8 +73,8 @@ def main(): ...@@ -74,8 +73,8 @@ def main():
parser.add_argument("apachedex_executable", metavar="APACHEDEX_EXECUTABLE") parser.add_argument("apachedex_executable", metavar="APACHEDEX_EXECUTABLE")
parser.add_argument("output_folder", metavar="OUTPUT_FOLDER") parser.add_argument("output_folder", metavar="OUTPUT_FOLDER")
parser.add_argument("base_url", metavar="BASE_URL") parser.add_argument("base_url", metavar="BASE_URL")
parser.add_argument("--apache-log-list", dest="apache_log_list", nargs='*') parser.add_argument("--apache-log-list", nargs='*')
parser.add_argument("--configuration", dest="configuration") parser.add_argument("--configuration")
args = parser.parse_args() args = parser.parse_args()
config = args.configuration config = args.configuration
...@@ -97,23 +96,15 @@ def main(): ...@@ -97,23 +96,15 @@ def main():
except ValueError as e: except ValueError as e:
print(e) print(e)
return 1 return 1
process_handler = subprocess.Popen(argument_list,
stdout=subprocess.PIPE, try:
stderr=subprocess.PIPE, subprocess.check_output(argument_list, stderr=subprocess.STDOUT)
universal_newlines=True, except subprocess.CalledProcessError as e:
) print("Error running apachedex", e.output)
stdout, stderr = process_handler.communicate()
if process_handler.returncode != 0:
if stderr:
print(stderr)
return 1 return 1
# Check that output_file is a readable file. # Check that output_file is a readable file.
with open(output_file, 'r'): with open(output_file, 'r'):
print(base_url + '/ApacheDex-%s.html' % today) print(base_url + '/ApacheDex-%s.html' % today)
return 0 return 0
if __name__ == "__main__":
sys.exit(main())
...@@ -64,6 +64,19 @@ class TestApachedexCommand(unittest.TestCase): ...@@ -64,6 +64,19 @@ class TestApachedexCommand(unittest.TestCase):
'--base', 'bar', 'foo', '--base', 'bar', 'foo',
'--default', 'foo', '--default', 'foo',
'--error-detail', self.acesslog1, self.acesslog2 ]) '--error-detail', self.acesslog1, self.acesslog2 ])
def test_complexCommandEscape(self):
command = build_command(self.apachedex,
'bar.html',
[self.acesslog1, self.acesslog2],
'--base "foo bar"')
self.assertEqual(command, ['/bin/apachedex',
'--js-embed',
'--out', 'bar.html',
'--base', 'foo bar',
'--error-detail', self.acesslog1, self.acesslog2 ])
def test_raiseErro(self): def test_raiseErro(self):
self.assertRaises(ValueError, build_command, self.apachedex, 'foo.html', []) self.assertRaises(ValueError, build_command, self.apachedex, 'foo.html', [])
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment