Example #1
0
    def side_effect(*args, **kwargs):
        '''
        This side effect mocks the behaviour of a subprocess.check_output()
        call on a machine with R set up for command-line use.
        '''
        # Get and parse the command passed to os.system()
        command = args[0]
        if re.search('R', command, flags=re.I) and not recognized:
            raise subprocess.CalledProcessError(1, command)

        match = helpers.command_match(command, 'R')

        executable = match.group('executable')
        log = match.group('log')
        append = match.group('append')

        if log is None:
            # If no log path is specified, create one by using the
            # R script's path after replacing .R (if present) with .log.
            source = match.group('source')
            log = '%s.log' % re.sub('\.R', '', source)

        if executable == 'Rscript' and log and append == '2>&1':
            with open(log.replace('>', '').strip(), 'wb') as log_file:
                log_file.write('Test log\n')
            with open('./test_output.txt', 'wb') as target:
                target.write('Test target')
Example #2
0
def python_side_effect(*args, **kwargs):
    command = args[0]
    match   = helpers.command_match(command, 'python')

    if match.group('log'):
        log_path = re.sub('(\s|>)', '', match.group('log'))
        with open(log_path, 'wb') as log_file:
            log_file.write('Test log')
Example #3
0
def python_side_effect(*args, **kwargs):
    '''    Mock subprocess.check_output for testing build_python()'''
    command = args[0]
    match = helpers.command_match(command, 'python')

    if match.group('log'):
        log_path = re.sub('(\s|>)', '', match.group('log'))
        with open(log_path, 'wb') as log_file:
            log_file.write('Test log')
Example #4
0
    def stata_side_effect(*args, **kwargs):
        command = args[0]
        match   = helpers.command_match(command, 'stata')

        if match.group('executable') == recognised_command:
            # Find the Stata script's name
            script_name = match.group('source')
            stata_log   = os.path.basename(script_name).replace('.do', '.log')
            
            # Write a log
            with open(stata_log, 'wb') as logfile:
                logfile.write('Test Stata log.\n')

        else:
            # Raise an error if the executable is not recognised.
            raise subprocess.CalledProcessError(1, command)
Example #5
0
def latex_side_effect(*args, **kwargs):
    '''
    This side effect mocks the behaviour of a subprocess.check_output call.
    The mocked machine has pdflatex set up as a command-line executable
    and can export .tex files to .pdf files only using the "-jobname" option.
    '''
    # Get and parse the command passed to os.system()
    command = args[0]
    match = helpers.command_match(command, 'pdflatex')

    executable = match.group('executable')
    option1 = match.group('option1')
    option2 = match.group('option2')
    source = match.group('source')
    log_redirect = match.group('log_redirect')

    option1_type = re.findall('^(-\w+)', option1)[0]
    interaction = re.findall('\s(\S+)', option1)[0]
    option2_type = re.findall('^(-\w+)', option2)[0]
    target_file = re.findall('\s(\S+)', option2)[0]

    is_pdflatex = bool(re.search('^pdflatex$', executable, flags=re.I))

    # As long as output is redirected, create a log
    if log_redirect:
        log_path = re.sub('>\s*', '', log_redirect)
        with open(log_path, 'wb') as log_file:
            log_file.write('Test log\n')

    # If pdflatex is the executable, the options are correctly specified,
    # and the source exists, produce a .pdf file with the name specified in
    # the -jobname option.

    # Mock a list of the files that pdflatex sees as existing
    # source_exists should be True only if the source script
    # specified in the system command belongs to existing_files.
    existing_files = ['test_script.tex', './input/latex_test_file.tex']
    source_exists  = os.path.abspath(source) in \
                     map(os.path.abspath, existing_files)

    if is_pdflatex and source_exists and option1_type == '-interaction' and option2_type == '-jobname':
        with open('%s.pdf' % target_file, 'wb') as out_file:
            out_file.write('Mock .pdf output')
        with open('%s.log' % target_file, 'wb') as out_file:
            out_file.write('Mock .log output')
Example #6
0
def lyx_side_effect(*args, **kwargs):
    '''
    This side effect mocks the behaviour of a system call.
    The mocked machine has lyx set up as a command-line executable
    and can export .lyx files to .pdf files only using 
    the "-e pdf2" option.
    '''
    # Get and parse the command passed to os.system()
    command = args[0]
    match = helpers.command_match(command, 'lyx')

    executable   = match.group('executable')
    option       = match.group('option')
    source       = match.group('source')
    log_redirect = match.group('log_redirect')

    option_type    = re.findall('^(-\w+)',  option)[0]
    option_setting = re.findall('\s(\w+)$', option)[0]

    is_lyx = bool(re.search('^lyx$', executable, flags = re.I))

    # As long as output is redirected, create a log
    if log_redirect:
        log_path = re.sub('>\s*', '', log_redirect)
        with open(log_path, 'wb') as log_file:
            log_file.write('Test log\n')

    # If LyX is the executable, the options are correctly specified,
    # and the source exists, produce a .pdf file with the same base 
    # name as the source file.

    # Mock a list of the files that LyX sees as existing
    # source_exists should be True only if the source script 
    # specified in the system command belongs to existing_files. 
    existing_files = ['test_script.lyx', './input/lyx_test_file.lyx']
    source_exists  = os.path.abspath(source) in \
                         map(os.path.abspath, existing_files)

    if is_lyx and option_type == '-e' and option_setting == 'pdf2' \
              and source_exists:
        out_path = re.sub('lyx$', 'pdf', source, flags = re.I)
        with open(out_path, 'wb') as out_file:
            out_file.write('Mock .pdf output')
Example #7
0
def r_side_effect(*args, **kwargs):
    '''
    This side effect mocks the behaviour of a system call
    on a machine with R set up for command-line use.
    '''
    # Get and parse the command passed to os.system()
    command = args[0]
    match   = helpers.command_match(command, 'R')

    executable = match.group('executable')
    log        = match.group('log')

    if log is None:
        # If no log path is specified, create one by using the 
        # R script's path after replacing .R (if present) with .Rout.
        source = match.group('source')
        log    = '%s.Rout' % re.sub('\.R', '', source)

    if executable == "R CMD BATCH" and log:
        with open(log.strip(), 'wb') as log_file:
            log_file.write('Test log\n')