def write_command_file(self, filename, commands, inputs=None, outputs=None, cwd=None, environ=None, foreach=False, suffix='.cmd', dry=False, accept_additional_args=False): if suffix is not None: filename = path.addsuffix(filename, suffix) result = ['cmd', '/Q', '/c', filename] if foreach: result += ['$in', '$out'] inputs, outputs = ['%1'], ['%2'] commands = self.replace_commands_inout_vars(commands, inputs, outputs) if dry: return result, filename path.makedirs(path.dirname(path.abs(filename))) with open(filename, 'w') as fp: fp.write('REM This file is automatically generated with Craftr. It is \n') fp.write('REM not recommended to modify it manually.\n\n') if cwd is not None: fp.write('cd ' + shell.quote(cwd) + '\n\n') for key, value in environ.items(): fp.write('set ' + shell.quote('{}={}'.format(key, value), for_ninja=True) + '\n') fp.write('\n') for index, command in enumerate(commands): if accept_additional_args and index == len(commands)-1: command.append(shell.safe('%*')) fp.write(shell.join(command) + '\n') fp.write('if %errorlevel% neq 0 exit %errorlevel%\n\n') return result, filename
def write_command_file(self, filename, commands, inputs=None, outputs=None, cwd=None, environ=None, foreach=False, suffix='.sh', dry=False, accept_additional_args=False): if suffix is not None: filename = path.addsuffix(filename, suffix) result = [filename] if foreach: result += ['$in', '$out'] inputs, outputs = ['%1'], ['%2'] commands = self.replace_commands_inout_vars(commands, inputs, outputs) if dry: return result, filename path.makedirs(path.dirname(filename)) with open(filename, 'w') as fp: # TODO: Make sure this also works for shells other than bash. fp.write('#!' + shell.find_program(environ.get('SHELL', 'bash')) + '\n') fp.write('set -e\n') if cwd: fp.write('cd ' + shell.quote(cwd) + '\n') fp.write('\n') for key, value in environ.items(): fp.write('export {}={}\n'.format(key, shell.quote(value))) fp.write('\n') for index, command in enumerate(commands): if accept_additional_args and index == len(commands) - 1: command.append(shell.safe('$*')) fp.write(shell.join(command)) fp.write('\n') os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH) # rwxrw-r-- return result, filename
def write_command_file(self, filename, commands, inputs=None, outputs=None, cwd=None, environ=None, foreach=False, suffix='.cmd', dry=False, accept_additional_args=False): if suffix is not None: filename = path.addsuffix(filename, suffix) result = ['cmd', '/Q', '/c', filename] if foreach: result += ['$in', '$out'] inputs, outputs = ['%1'], ['%2'] commands = self.replace_commands_inout_vars(commands, inputs, outputs) if dry: return result, filename path.makedirs(path.dirname(path.abs(filename))) with open(filename, 'w') as fp: fp.write( 'REM This file is automatically generated with Craftr. It is \n' ) fp.write('REM not recommended to modify it manually.\n\n') if cwd is not None: fp.write('cd ' + shell.quote(cwd) + '\n\n') for key, value in environ.items(): fp.write( 'set ' + shell.quote('{}={}'.format(key, value), for_ninja=True) + '\n') fp.write('\n') for index, command in enumerate(commands): if accept_additional_args and index == len(commands) - 1: command.append(shell.safe('%*')) fp.write(shell.join(command) + '\n') fp.write('if %errorlevel% neq 0 exit %errorlevel%\n\n') return result, filename
def write_command_file(self, filename, commands, inputs=None, outputs=None, cwd=None, environ=None, foreach=False, suffix='.sh', dry=False, accept_additional_args=False): if suffix is not None: filename = path.addsuffix(filename, suffix) result = [filename] if foreach: result += ['$in', '$out'] inputs, outputs = ['%1'], ['%2'] commands = self.replace_commands_inout_vars(commands, inputs, outputs) if dry: return result, filename path.makedirs(path.dirname(filename)) with open(filename, 'w') as fp: # TODO: Make sure this also works for shells other than bash. fp.write('#!' + shell.find_program(environ.get('SHELL', 'bash')) + '\n') fp.write('set -e\n') if cwd: fp.write('cd ' + shell.quote(cwd) + '\n') fp.write('\n') for key, value in environ.items(): fp.write('export {}={}\n'.format(key, shell.quote(value))) fp.write('\n') for index, command in enumerate(commands): if accept_additional_args and index == len(commands)-1: command.append(shell.safe('$*')) fp.write(shell.join(command)) fp.write('\n') os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH) # rwxrw-r-- return result, filename
def prepare_single_command(self, command, cwd): if cwd is not None: command = [shell.safe('('), 'cd', cwd, shell.safe('&&')] + command + [shell.safe(')')] return command
def prepare_single_command(self, command, cwd): if cwd is not None: command = ['cmd', '/c', 'cd', cwd, shell.safe('&&')] + command return command