def _run(self): """ Runs the current editor program. :return: """ # compilation has finished, we can run the program that corresponds # to the current editor file editor = self.app.edit.current_editor file_type = editor.file_type self.ui.tabWidgetLogs.setCurrentIndex(LOG_PAGE_RUN) self.ui.dockWidgetLogs.show() self.ui.consoleOutput.clear() output_dir = Settings().output_directory if not os.path.isabs(output_dir): output_dir = os.path.join(os.path.dirname(editor.file.path), output_dir) if Settings().working_dir: wd = Settings().working_dir else: wd = output_dir filename = os.path.splitext(editor.file.name)[0] + \ GnuCobolCompiler().extension_for_type(file_type) program = os.path.join(output_dir, filename) if not os.path.exists(program): _logger().warning('cannot run %s, file does not exists', program) return _logger().info('running program: %r (working dir=%r)', program, wd) if Settings().external_terminal: self._run_in_external_terminal(program, wd, file_type) self.enable_run(True) self.enable_compile(True) else: self.ui.consoleOutput.setFocus(True) for item in self.run_buttons + [self.ui.actionRun]: item.setEnabled(False) path = GnuCobolCompiler.setup_process_environment().value('PATH') env = Settings().run_environemnt if 'PATH' not in list(env.keys()): env['PATH'] = path if file_type == FileType.MODULE: cobcrun = system.which('cobcrun') self.ui.consoleOutput.start_process( cobcrun, [os.path.splitext(editor.file.name)[0]], working_dir=wd, env=env) else: self.ui.consoleOutput.start_process(program, working_dir=wd, env=env, print_command=True)
def lint(request_data): """ Performs linting of a COBOL document. This method will perform on the pyqode backend. :param request_data: work request data (dict) :return: status, messages """ print('running open_cobol_ide.linter.lint') code = request_data['code'] path = request_data['path'] extension = os.path.splitext(path)[1] print(('valid compiler extensions: %r' % settings.Settings().cobc_extensions)) messages = [] if extension.lower() in settings.Settings().cobc_extensions: # code might not have been saved yet, run cobc on a tmp file # we use a time stamp to avoid overwriting the file another cobc # instance might be compiling. file_name = os.path.split(path)[1] file_name, ext = os.path.splitext(file_name) tmp_name = '%s.%s%s' % (file_name, str(int(time.time())), ext) tmp_pth = os.path.join(tempfile.gettempdir(), tmp_name) print(('writing code to temporary file: %r' % tmp_pth)) with open(tmp_pth, 'w') as f: f.write(code) compiler = GnuCobolCompiler() pgm, args = make_linter_command(tmp_name, path) print(('linter command: %s %s' % (pgm, ' '.join(args)))) process = QtCore.QProcess() process.setProcessEnvironment( GnuCobolCompiler.setup_process_environment()) process.setWorkingDirectory(os.path.dirname(tmp_pth)) process.setProcessChannelMode(QtCore.QProcess.MergedChannels) print('running compiler process') print(('working directory: %s' % process.workingDirectory())) process.start(pgm, args) print('waiting for compilation to finish...') process.waitForFinished() output = process.readAllStandardOutput().data().decode( locale.getpreferredencoding()) print(('linter raw output: %s' % output)) messages = compiler.parse_output(output, process.workingDirectory()) print(('linter parsed output: %r' % messages)) print('removing temporary file...') os.remove(tmp_pth) return messages
def lint(request_data): """ Performs linting of a COBOL document. This method will perform on the pyqode backend. :param request_data: work request data (dict) :return: status, messages """ print('running open_cobol_ide.linter.lint') code = request_data['code'] path = request_data['path'] extension = os.path.splitext(path)[1] print('valid compiler extensions: %r' % settings.Settings().cobc_extensions) messages = [] if extension.lower() in settings.Settings().cobc_extensions: # code might not have been saved yet, run cobc on a tmp file # we use a time stamp to avoid overwriting the file another cobc # instance might be compiling. file_name = os.path.split(path)[1] file_name, ext = os.path.splitext(file_name) tmp_name = '%s.%s%s' % (file_name, str(int(time.time())), ext) tmp_pth = os.path.join(tempfile.gettempdir(), tmp_name) print('writing code to temporary file: %r' % tmp_pth) with open(tmp_pth, 'w') as f: f.write(code) compiler = GnuCobolCompiler() pgm, args = make_linter_command(tmp_name, path) print('linter command: %s %s' % (pgm, ' '.join(args))) process = QtCore.QProcess() process.setProcessEnvironment( GnuCobolCompiler.setup_process_environment()) process.setWorkingDirectory(os.path.dirname(tmp_pth)) process.setProcessChannelMode(QtCore.QProcess.MergedChannels) print('running compiler process') print('working directory: %s' % process.workingDirectory()) process.start(pgm, args) print('waiting for compilation to finish...') process.waitForFinished() output = process.readAllStandardOutput().data().decode( locale.getpreferredencoding()) print('linter raw output: %s' % output) messages = compiler.parse_output(output, process.workingDirectory()) print('linter parsed output: %r' % messages) print('removing temporary file...') os.remove(tmp_pth) return messages
def _run(self): """ Runs the current editor program. :return: """ # compilation has finished, we can run the program that corresponds # to the current editor file editor = self.app.edit.current_editor file_type = editor.file_type self.ui.tabWidgetLogs.setCurrentIndex(LOG_PAGE_RUN) self.ui.dockWidgetLogs.show() self.ui.consoleOutput.clear() output_dir = Settings().output_directory if not os.path.isabs(output_dir): output_dir = os.path.join( os.path.dirname(editor.file.path), output_dir) if Settings().working_dir: wd = Settings().working_dir else: wd = output_dir filename = os.path.splitext(editor.file.name)[0] + \ GnuCobolCompiler().extension_for_type(file_type) program = os.path.join(output_dir, filename) if not os.path.exists(program): _logger().warning('cannot run %s, file does not exists', program) return _logger().info('running program: %r (working dir=%r)', program, wd) if Settings().external_terminal: self._run_in_external_terminal(program, wd, file_type) self.enable_run(True) self.enable_compile(True) else: self.ui.consoleOutput.setFocus(True) for item in self.run_buttons + [self.ui.actionRun]: item.setEnabled(False) path = GnuCobolCompiler.setup_process_environment().value('PATH') env = Settings().run_environemnt if 'PATH' not in env.keys(): env['PATH'] = path if file_type == FileType.MODULE: cobcrun = system.which('cobcrun') self.ui.consoleOutput.start_process( cobcrun, [os.path.splitext(editor.file.name)[0]], working_dir=wd, env=env) else: self.ui.consoleOutput.start_process(program, working_dir=wd, env=env, print_command=True)
def _run_in_external_terminal(self, program, wd, file_type): """ Runs a program in an external terminal. :param program: program to run :param wd: working directory """ self.ui.consoleOutput.append( "Launched in external terminal") pyqode_console = system.which('pyqode-console') env = os.environ.copy() for k, v in Settings().run_environemnt.items(): env[k] = v env['PATH'] = GnuCobolCompiler.setup_process_environment().value( 'PATH') if file_type == FileType.MODULE: program = QtCore.QFileInfo(program).baseName() if system.windows: cmd = [pyqode_console, program] if file_type == FileType.MODULE: cmd.insert(1, system.which('cobcrun')) subprocess.Popen(cmd, cwd=wd, creationflags=subprocess.CREATE_NEW_CONSOLE, env=env) elif system.darwin: cmd = ['open', program] if file_type == FileType.MODULE: cmd.insert(1, system.which('cobcrun')) subprocess.Popen(cmd, cwd=wd, env=env) else: if file_type == FileType.EXECUTABLE: cmd = ['"%s %s"' % (pyqode_console, program)] else: cmd = ['"%s %s %s"' % (pyqode_console, system.which('cobcrun'), program)] cmd = system.shell_split( Settings().external_terminal_command.strip()) + cmd subprocess.Popen(' '.join(cmd), cwd=wd, shell=True, env=env) _logger().info('running program in external terminal: %s', ' '.join(cmd))
def _run_in_external_terminal(self, program, wd, file_type): """ Runs a program in an external terminal. :param program: program to run :param wd: working directory """ pyqode_console = system.which('pyqode-console') cobc_run_insert_pos = 1 if pyqode_console is None: from pyqode.core.tools import console pyqode_console = [sys.executable, console.__file__] cobc_run_insert_pos = 2 else: pyqode_console = [pyqode_console] env = os.environ.copy() for k, v in list(Settings().run_environemnt.items()): env[k] = v if 'PATH' not in list(Settings().run_environemnt.keys()): env['PATH'] = GnuCobolCompiler.setup_process_environment().value( 'PATH') if file_type == FileType.MODULE: program = QtCore.QFileInfo(program).baseName() if system.windows: cmd = pyqode_console + [program] if file_type == FileType.MODULE: cmd.insert(cobc_run_insert_pos, system.which('cobcrun')) try: _logger().debug( "running program in external terminal: %r, %r, %r" % (cmd, wd, env)) subprocess.Popen(cmd, cwd=wd, creationflags=subprocess.CREATE_NEW_CONSOLE, env=env) except (OSError, subprocess.CalledProcessError): msg = "Failed to launch program in external terminal (cmd=%s) " % ' '.join( cmd) _logger().exception(msg) self.ui.consoleOutput.appendPlainText(msg) return elif system.darwin: if file_type == FileType.MODULE: create_script(PATH_RUN_MODULE_SCRIPT, RUN_MODULE_SCRIPT, (wd, system.which('cobcrun'), program)) cmd = [ 'open', '-n', '-a', 'Terminal', '--args', PATH_RUN_MODULE_SCRIPT ] else: create_script(PATH_RUN_PROGRAM_SCRIPT, RUN_PROGRAM_SCRIPT, (wd, program)) cmd = [ 'open', '-n', '-a', 'Terminal', '--args', PATH_RUN_PROGRAM_SCRIPT ] try: subprocess.Popen(cmd, cwd=wd, env=env) except (OSError, subprocess.CalledProcessError): msg = "Failed to launch program in external terminal (cmd=%s) " % ' '.join( cmd) _logger().exception(msg) self.ui.consoleOutput.appendPlainText(msg) return else: if file_type == FileType.EXECUTABLE: cmd = ['"%s %s"' % (' '.join(pyqode_console), program)] else: cmd = [ '"%s %s %s"' % (' '.join(pyqode_console), system.which('cobcrun'), program) ] cmd = system.shell_split( Settings().external_terminal_command.strip()) + cmd try: subprocess.Popen(' '.join(cmd), cwd=wd, shell=True, env=env) except (OSError, subprocess.CalledProcessError): msg = "Failed to launch program in external terminal (cmd=%s) " % ' '.join( cmd) _logger().exception(msg) self.ui.consoleOutput.appendPlainText(msg) return _logger().info('running program in external terminal: %s', ' '.join(cmd)) self.ui.consoleOutput.appendPlainText( "Launched in external terminal: %s" % ' '.join(cmd))
def _run_in_external_terminal(self, program, wd, file_type): """ Runs a program in an external terminal. :param program: program to run :param wd: working directory """ pyqode_console = system.which('pyqode-console') cobc_run_insert_pos = 1 if pyqode_console is None: from pyqode.core.tools import console pyqode_console = [sys.executable, console.__file__] cobc_run_insert_pos = 2 else: pyqode_console = [pyqode_console] env = os.environ.copy() for k, v in Settings().run_environemnt.items(): env[k] = v if 'PATH' not in Settings().run_environemnt.keys(): env['PATH'] = GnuCobolCompiler.setup_process_environment().value( 'PATH') if file_type == FileType.MODULE: program = QtCore.QFileInfo(program).baseName() if system.windows: cmd = pyqode_console + [program] if file_type == FileType.MODULE: cmd.insert(cobc_run_insert_pos, system.which('cobcrun')) try: _logger().debug("running program in external terminal: %r, %r, %r" % (cmd, wd, env)) subprocess.Popen(cmd, cwd=wd, creationflags=subprocess.CREATE_NEW_CONSOLE, env=env) except (OSError, subprocess.CalledProcessError): msg = "Failed to launch program in external terminal (cmd=%s) " % ' '.join(cmd) _logger().exception(msg) self.ui.consoleOutput.appendPlainText(msg) return elif system.darwin: if file_type == FileType.MODULE: create_script(PATH_RUN_MODULE_SCRIPT, RUN_MODULE_SCRIPT, (wd, system.which('cobcrun'), program)) cmd = ['open', '-n', '-a', 'Terminal', '--args', PATH_RUN_MODULE_SCRIPT] else: create_script(PATH_RUN_PROGRAM_SCRIPT, RUN_PROGRAM_SCRIPT, (wd, program)) cmd = ['open', '-n', '-a', 'Terminal', '--args', PATH_RUN_PROGRAM_SCRIPT] try: subprocess.Popen(cmd, cwd=wd, env=env) except (OSError, subprocess.CalledProcessError): msg = "Failed to launch program in external terminal (cmd=%s) " % ' '.join(cmd) _logger().exception(msg) self.ui.consoleOutput.appendPlainText(msg) return else: if file_type == FileType.EXECUTABLE: cmd = ['"%s %s"' % (' '.join(pyqode_console), program)] else: cmd = ['"%s %s %s"' % (' '.join(pyqode_console), system.which('cobcrun'), program)] cmd = system.shell_split(Settings().external_terminal_command.strip()) + cmd try: subprocess.Popen(' '.join(cmd), cwd=wd, shell=True, env=env) except (OSError, subprocess.CalledProcessError): msg = "Failed to launch program in external terminal (cmd=%s) " % ' '.join(cmd) _logger().exception(msg) self.ui.consoleOutput.appendPlainText(msg) return _logger().info('running program in external terminal: %s', ' '.join(cmd)) self.ui.consoleOutput.appendPlainText("Launched in external terminal: %s" % ' '.join(cmd))