Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull request for #26: Allow CL arguments to be passed #46

Merged
merged 5 commits into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This repository contains the following GSLab Python libraries:

Information about each of these packages is available in its internal documentation.

Note: In order to run the unit tests, `mock` (version 2.0.0 or higher) needs to be installed.

Installation
------------
To install this repository's Python libraries, run the `setup.py` script at its root
Expand Down
3 changes: 2 additions & 1 deletion gslab_scons/builders/build_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def build_python(target, source, env):
target_dir = os.path.dirname(target_file)
misc.check_code_extension(source_file, 'python')
log_file = target_dir + '/sconscript.log'
cl_arg = misc.command_line_arg(env)

os.system('python %s > %s' % (source_file, log_file))
os.system('python %s %s > %s' % (source_file, cl_arg, log_file))

end_time = misc.current_time()
log_timestamp(start_time, end_time, log_file)
Expand Down
7 changes: 5 additions & 2 deletions gslab_scons/builders/build_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ def build_r(target, source, env):
target_dir = os.path.dirname(target_file)
misc.check_code_extension(source_file, 'r')
log_file = target_dir + '/sconscript.log'

os.system('R CMD BATCH --no-save %s %s' % (source_file, log_file))

cl_arg = misc.command_line_arg(env)
if cl_arg != '':
cl_arg = "'--args %s'" % cl_arg
os.system("R CMD BATCH --no-save %s %s %s" % (cl_arg, source_file, log_file))

end_time = misc.current_time()
log_timestamp(start_time, end_time, log_file)
Expand Down
11 changes: 6 additions & 5 deletions gslab_scons/builders/build_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def build_stata(target, source, env):
(By default, SCons will try to find each flavour).
'''
start_time = misc.current_time()

cl_arg = misc.command_line_arg(env)

source = misc.make_list_if_string(source)
target = misc.make_list_if_string(target)
source_file = str(source[0])
Expand All @@ -38,15 +39,15 @@ def build_stata(target, source, env):
user_flavor = env['user_flavor']
if user_flavor is not None:
if misc.is_unix():
command = misc.stata_command_unix(user_flavor)
command = misc.stata_command_unix(user_flavor, cl_arg)
elif platform == 'win32':
command = misc.stata_command_win(user_flavor)
command = misc.stata_command_win(user_flavor, cl_arg)
else:
flavors = ['stata-mp', 'stata-se', 'stata']
if misc.is_unix():
for flavor in flavors:
if misc.is_in_path(flavor):
command = misc.stata_command_unix(flavor)
command = misc.stata_command_unix(flavor, cl_arg)
break
elif platform == 'win32':
try:
Expand All @@ -58,7 +59,7 @@ def build_stata(target, source, env):
flavors = [f.replace('.exe', '-64.exe') for f in flavors]
for flavor in flavors:
if misc.is_in_path(flavor):
command = misc.stata_command_win(flavor)
command = misc.stata_command_win(flavor, cl_arg)
break

try:
Expand Down
14 changes: 10 additions & 4 deletions gslab_scons/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ def check_lfs():
raise LFSError('''Either Git LFS is not installed or your Git LFS settings need to be updated.
Please install Git LFS or run 'git lfs install --force' if prompted above.''')

def command_line_arg(env):
try:
cl_arg = env['CL_ARG']
except KeyError:
cl_arg = ''
return cl_arg

def stata_command_unix(flavor):
def stata_command_unix(flavor, cl_arg):
'''
This function returns the appropriate Stata command for a user's
Unix platform.
Expand All @@ -30,16 +36,16 @@ def stata_command_unix(flavor):
'linux' : '-b',
'linux2': '-b'}
option = options[platform]
command = flavor + ' ' + option + ' %s ' # %s will take filename later
command = flavor + ' ' + option + ' %s ' + cl_arg # %s will take filename later
return command


def stata_command_win(flavor):
def stata_command_win(flavor, cl_arg):
'''
This function returns the appropriate Stata command for a user's
Windows platform.
'''
command = flavor + ' /e do' + ' %s ' # %s will take filename later
command = flavor + ' /e do' + ' %s ' + cl_arg # %s will take filename later
return command


Expand Down
3 changes: 3 additions & 0 deletions gslab_scons/tests/input/R_test_script.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
args = commandArgs(trailingOnly = TRUE)
cat(args[1])

NCols=5
NRows=5
x<-matrix(runif(NCols*NRows), ncol=NCols)
Expand Down
15 changes: 8 additions & 7 deletions gslab_scons/tests/input/python_test_script.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#! /usr/bin/env python
import argparse, pdb
import sys

parser = argparse.ArgumentParser()
parser.add_argument('-i', dest='input', nargs='+')
args = parser.parse_args()

if args.input:
message = args.input
try:
arg = sys.argv[1]
except:
arg = ''

if arg != '':
message = arg
else:
message = 'Test Output \n'
output_name = 'output.txt'
Expand Down
4 changes: 3 additions & 1 deletion gslab_scons/tests/input/stata_test_script.do
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ preliminaries
set obs 10
gen var1 = _n

save_data ../output/stata1.dta, key(var1) log(../output/data_file_manifest.log) replace
di `1`

save ../output/stata`1`.dta, replace
save_data ../output/stata2.dta, key(var1) log(../output/data_file_manifest.log) replace
save_data ../output/stata.csv, key(var1) outsheet log(../output/data_file_manifest.log) replace
save_data ../output/stata.txt, key(var1) outsheet log(../output/data_file_manifest.log) replace
14 changes: 9 additions & 5 deletions gslab_scons/tests/log/make.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ test_bad_extension (test_build_lyx.test_build_lyx)
Test that build_lyx() recognises an inappropriate file extension ... ok
test_default (test_build_lyx.test_build_lyx) ... ok
test_bad_extension (test_build_python.testbuild_python) ... ok
test_clarg (test_build_python.testbuild_python) ... ok
test_default (test_build_python.testbuild_python) ... ok
test_bad_extension (test_build_r.testbuild_r)
Test that build_r() recognises an inappropriate file extension ... ok
test_clarg (test_build_r.testbuild_r) ... ok
test_default (test_build_r.testbuild_r) ... ok
test_bad_extension (test_build_stata.testbuild_stata) ... ok
test_bad_user_executable (test_build_stata.testbuild_stata) ... ok
test_clarg (test_build_stata.testbuild_stata) ... ok
test_default (test_build_stata.testbuild_stata) ... ok
test_user_executable_unix (test_build_stata.testbuild_stata) ... skipped 'skipped test_user_executable_unix because on a windows machine'
test_user_executable_unix (test_build_stata.testbuild_stata) ... ok
test_default (test_build_tables.test_build_tables) ... ok
test_default_string_target (test_build_tables.test_build_tables) ... ok
test_target_extension (test_build_tables.test_build_tables)
Expand All @@ -20,11 +23,12 @@ test_start_log (test_log.test_log) ... ok
test_check_code_extension (test_misc.test_misc)
Unit tests for check_code_extension() ... ok
test_check_lfs (test_misc.test_misc) ... ok
test_command_line_arg (test_misc.test_misc) ... ok
test_current_time (test_misc.test_misc)
Test that current_time() prints times in the expected format ... ok
test_is_64_windows (test_misc.test_misc) ... ok
test_is_exe (test_misc.test_misc) ... skipped 'skipped test_is_exe because on a windows machine'
test_is_in_path (test_misc.test_misc) ... skipped 'skipped test_is_in_path because on a windows machine'
test_is_exe (test_misc.test_misc) ... ok
test_is_in_path (test_misc.test_misc) ... ok
test_is_unix (test_misc.test_misc) ... ok
test_make_list_if_string (test_misc.test_misc) ... ok
test_stata_command_unix (test_misc.test_misc) ... ok
Expand All @@ -40,6 +44,6 @@ test_upload_asset_bad_file (test_release_tools.TestReleaseTools) ... ok
test_upload_asset_standard (test_release_tools.TestReleaseTools) ... ok

----------------------------------------------------------------------
Ran 34 tests in 24.886s
Ran 38 tests in 55.880s

OK (skipped=4)
OK (skipped=1)
14 changes: 11 additions & 3 deletions gslab_scons/tests/test_build_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ def setUp(self):
os.mkdir('../build/')

def test_default(self):
env = ''
env = {}
build_python('../build/py.py', './input/python_test_script.py', env)
logfile_data = open('../build/sconscript.log', 'rU').read()
self.assertIn('Log created:', logfile_data)
if os.path.isfile('../build/sconscript.log'):
os.remove('../build/sconscript.log')


def test_clarg(self):
env = {'CL_ARG' : 'COMMANDLINE'}
build_python('../build/py.py', './input/python_test_script.py', env)
logfile_data = open('output.txt', 'rU').read()
self.assertIn('COMMANDLINE', logfile_data)
if os.path.isfile('../build/sconscript.log'):
os.remove('../build/sconscript.log')

def test_bad_extension(self):
env = ''
env = {}
with self.assertRaises(BadExtensionError):
build_python('../build/py.py',
['bad', './input/python_test_script.py'],
Expand Down
12 changes: 10 additions & 2 deletions gslab_scons/tests/test_build_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ def setUp(self):
os.mkdir('../build/')

def test_default(self):
env = ''
env = {}
build_r('../build/r.rds', './input/R_test_script.R', env)
logfile_data = open('../build/sconscript.log', 'rU').read()
self.assertIn('Log created:', logfile_data)
if os.path.isfile('../build/sconscript.log'):
os.remove('../build/sconscript.log')

def test_clarg(self):
env = {'CL_ARG' : 'COMMANDLINE'}
build_r('../build/r.rds', './input/R_test_script.R', env)
logfile_data = open('../build/sconscript.log', 'rU').read()
self.assertIn('COMMANDLINE', logfile_data)
if os.path.isfile('../build/sconscript.log'):
os.remove('../build/sconscript.log')

def test_bad_extension(self):
'''Test that build_r() recognises an inappropriate file extension'''
env = ''
env = {}
with self.assertRaises(BadExtensionError):
build_r('../build/r.rds', ['bad', './input/R_test_script.R'], env)

Expand Down
10 changes: 9 additions & 1 deletion gslab_scons/tests/test_build_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ def test_default(self):
self.assertIn('Log created:', logfile_data)
if os.path.isfile('./build/sconscript.log'):
os.remove('./build/sconscript.log')


def test_clarg(self):
env = {'user_flavor' : None, 'CL_ARG' : 'COMMANDLINE'}
build_stata('./build/stata.dta', './input/stata_test_script.do', env)
logfile_data = open('./build/sconscript.log', 'rU').read()
self.assertIn('COMMANDLINE', logfile_data)
if os.path.isfile('./build/sconscript.log'):
os.remove('./build/sconscript.log')

@unittest.skipIf(sys.platform.startswith("win"),
"skipped test_user_executable_unix because on a windows machine")
def test_user_executable_unix(self):
Expand Down
14 changes: 10 additions & 4 deletions gslab_scons/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,31 @@ def test_check_lfs(self):
@unittest.skipIf(sys.platform.startswith("win"),
"skipped test_stata_command_unix because on a windows machine")
def test_stata_command_unix(self):
output = misc.stata_command_unix('flavor')
cl_arg = 'test'
output = misc.stata_command_unix('flavor', cl_arg)
if sys.platform == 'darwin':
option = '-e'
else:
option = '-b'
self.assertEqual(output, 'flavor %s' % (option) + ' %s ')
self.assertEqual(output, 'flavor %s' % (option) + ' %s ' + cl_arg)

@unittest.skipUnless(sys.platform.startswith("win"),
"skipped test_stata_command_win because on a unix machine")
def test_stata_command_win(self):
output = misc.stata_command_win('flavor')
self.assertEqual(output, 'flavor %s' % ('/e do') + ' %s ')
cl_arg = 'test'
output = misc.stata_command_win('flavor', cl_arg)
self.assertEqual(output, 'flavor %s' % ('/e do') + ' %s ' + cl_arg)

def test_is_unix(self):
self.assertEqual(misc.is_unix(), not sys.platform.startswith("win"))

def test_is_64_windows(self):
pass

def test_command_line_arg(self):
self.assertEqual(misc.command_line_arg({'test' : ''}), '')
self.assertEqual(misc.command_line_arg({'CL_ARG' : 'Test'}), 'Test')

@unittest.skipIf(sys.platform.startswith("win"),
"skipped test_is_in_path because on a windows machine")
def test_is_in_path(self):
Expand Down