def test_compile(path, ftype, expected_results, output_file_path): results = GnuCobolCompiler().compile(path, ftype) assert results[0] == expected_results[0] assert len(results[1]) >= len(expected_results[1]) if output_file_path: assert os.path.exists(output_file_path) os.remove(output_file_path)
def _on_file_compiled(self, filename, status, messages): """ Displays compilation status in errors table. """ self.ui.dockWidgetLogs.show() self.ui.tabWidgetLogs.setCurrentIndex(LOG_PAGE_COMPILER) if len(messages) == 0 and status == 0: ext = GnuCobolCompiler().extension_for_type( get_file_type(filename)) name = os.path.splitext(os.path.split(filename)[1])[0] output_dir = Settings().output_directory if not os.path.isabs(output_dir): path = os.path.dirname(filename) output_dir = os.path.abspath(os.path.join(path, output_dir)) path = os.path.join(output_dir, name + ext) self.ui.errorsTable.add_message( CheckerMessage('Compilation succeeded (output: %s)' % path, CheckerMessages.INFO, -1, path=filename)) else: for msg in messages: msg = CheckerMessage(*msg) if msg.status == CheckerMessages.ERROR: self._errors += 1 if len(msg.description.splitlines()) > 1: msg.description = 'For full output see compiler tab...' self.ui.errorsTable.add_message(msg)
def get_system_infos(self): try: import qdarkstyle except ImportError: qdarkstyle_version = 'Not installed' else: qdarkstyle_version = qdarkstyle.__version__ system = platform.system() is_linux = system.lower() == 'linux' return '\n'.join([ '- Operating System: %s' % system + ' (' + ' '.join(platform.linux_distribution()) + ')' if is_linux else '', '- OpenCobolIDE: %s' % __version__, '- GnuCOBOL: %s' % GnuCobolCompiler().get_version(), '- Python: %s (%dbits)' % (platform.python_version(), 64 if sys.maxsize > 2**32 else 32), '- Qt: %s' % QtCore.QT_VERSION_STR, '- PyQt: %s' % QtCore.PYQT_VERSION_STR, '- pyqode.core: %s' % pyqode.core.__version__, '- pyqode.cobol: %s' % pyqode.cobol.__version__, '- pyqode.qt: %s' % pyqode.qt.__version__, '- pygments: %s' % pygments.__version__, '- QDarkStyle: %s' % qdarkstyle_version ])
def __init__(self, parent=None): super().__init__(parent) self.setupUi(self) self.checkBoxVerbose.setChecked(Settings().verbose) self.tabWidget.setCurrentIndex(0) self.tbwVersions.setColumnCount(1) self.tbwVersions.setRowCount(len(self.HEADERS)) self.tbwVersions.setVerticalHeaderLabels(self.HEADERS) self.tbwVersions.setHorizontalHeaderLabels(['Version']) self.tbwVersions.verticalHeader().setStretchLastSection(False) self.labelMain.setText(self.labelMain.text() % __version__) self.setMinimumWidth(640) self.setMinimumHeight(480) self.setWindowIcon(QtGui.QIcon.fromTheme( 'help-about', QtGui.QIcon( ':/ide-icons/rc/dialog-information.png'))) try: import qdarkstyle except ImportError: qdarkstyle_version = 'Not installed' else: qdarkstyle_version = qdarkstyle.__version__ versions = [GnuCobolCompiler().get_version(), QtCore.QT_VERSION_STR, QtCore.PYQT_VERSION_STR, pyqode.core.__version__, pyqode.cobol.__version__, pygments.__version__, qdarkstyle_version] for i, version in enumerate(versions): item = QtWidgets.QTableWidgetItem(version) self.tbwVersions.setItem(i, 0, item) with open(logger.get_path(), 'r') as f: self.textEditLog.setText(f.read()) self.checkBoxVerbose.toggled.connect(self._on_verbose_toggled)
def __init__(self, parent=None): super().__init__(parent) self.setupUi(self) self.checkBoxVerbose.setChecked(Settings().verbose) self.tabWidget.setCurrentIndex(0) self.tbwVersions.setColumnCount(1) self.tbwVersions.setRowCount(len(self.HEADERS)) self.tbwVersions.setVerticalHeaderLabels(self.HEADERS) self.tbwVersions.setHorizontalHeaderLabels(['Version']) self.tbwVersions.verticalHeader().setStretchLastSection(False) self.labelMain.setText(self.labelMain.text() % __version__) self.setMinimumWidth(640) self.setMinimumHeight(480) self.setWindowIcon( QtGui.QIcon.fromTheme( 'help-about', QtGui.QIcon(':/ide-icons/rc/dialog-information.png'))) try: import qdarkstyle except ImportError: qdarkstyle_version = 'Not installed' else: qdarkstyle_version = qdarkstyle.__version__ versions = [ GnuCobolCompiler().get_version(), QtCore.QT_VERSION_STR, QtCore.PYQT_VERSION_STR, pyqode.core.__version__, pyqode.cobol.__version__, pygments.__version__, qdarkstyle_version ] for i, version in enumerate(versions): item = QtWidgets.QTableWidgetItem(version) self.tbwVersions.setItem(i, 0, item) with open(logger.get_path(), 'r') as f: self.textEditLog.setText(f.read()) self.checkBoxVerbose.toggled.connect(self._on_verbose_toggled) self.edit_compiler_infos.setFont(QtGui.QFont(Settings().font, 9)) # from pyqode.core._forms.pyqode_core_rc QtGui.QFontDatabase.addApplicationFont( ':/fonts/rc/SourceCodePro-Regular.ttf') QtGui.QFontDatabase.addApplicationFont( ':/fonts/rc/SourceCodePro-Bold.ttf') template = '''cobc --info ============ %(cobc_infos)s cobcrun --runtime-env ===================== %(cobcrun_infos)s ''' gnucobol_infos = template % { 'cobc_infos': GnuCobolCompiler.get_cobc_infos(), 'cobcrun_infos': GnuCobolCompiler.get_cobcrun_infos() } self.edit_compiler_infos.setPlainText(gnucobol_infos)
def test_extensions(): exts = GnuCobolCompiler().extensions if system.windows: assert exts[0] == '.exe' assert exts[1] == '.dll' else: assert exts[0] == '' assert exts[1] == '.so'
def test_make_command_exe(free, std, ftype, expected_opts): compiler = GnuCobolCompiler() settings = Settings() settings.free_format = free settings.cobol_standard = std pgm, options = compiler.make_command(['HelloWorld.cbl'], ftype, 'bin') assert pgm == Settings().compiler_path for o, eo in zip(options, expected_opts): assert o == eo settings.free_format = free settings.cobol_standard = GnuCobolStandard.default settings.free_format = False
def clean_file(path): output_path = GnuCobolCompiler().get_output_filename( [os.path.split(path)[1]], get_file_type(path)) output_dir = Settings().output_directory if not os.path.isabs(output_dir): output_dir = os.path.abspath( os.path.join(os.path.dirname(path), output_dir)) output_path = os.path.join(output_dir, output_path) try: os.remove(output_path) except OSError: _logger().debug('failed to remove output file %r', output_path) else: _logger().debug('file removed: %s', output_path)
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 check_compiler(self, dotted_extension): compiler_works = False msg = 'Invalid extension' if dotted_extension.lower() in Settings().cobc_extensions: compiler_works = GnuCobolCompiler().is_working() msg = 'GnuCOBOL compiler not found or not working' elif dotted_extension.lower() in Settings().dbpre_extensions: compiler_works = DbpreCompiler().is_working() msg = 'dbpre compiler not working, please check your SQL COBOL ' \ 'configuration' elif dotted_extension.lower() in Settings().esqloc_extensions: compiler_works = EsqlOCCompiler().is_working() msg = 'esqlOC compiler not working, please check your SQL COBOL ' \ 'configuration' return compiler_works, msg
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): """ Compiles the file and all its dependencies. """ def is_dbpre_cobol(path): if path.lower().endswith('.scb'): with open(path, 'r') as f: return 'exec sql' in f.read().lower() return False def is_esqloc_cobol(path): if path.lower().endswith('.sqb'): with open(path, 'r') as f: return 'exec sql' in f.read().lower() return False cobc = GnuCobolCompiler() cobc.started.connect(self.command_started.emit) cobc.output_available.connect(self.output_available.emit) dbpre = DbpreCompiler() dbpre.started.connect(self.command_started.emit) dbpre.output_available.connect(self.output_available.emit) esqloc = EsqlOCCompiler() esqloc.started.connect(self.command_started.emit) esqloc.output_available.connect(self.output_available.emit) files = [self.file_path] if Settings().autodetect_submodules: files += cobc.get_dependencies(self.file_path, recursive=True) _logger().info('running compilation thread: %r', files) for f in files: try: if is_dbpre_cobol(f): status, messages = dbpre.compile(f) elif is_esqloc_cobol(f): status, messages = esqloc.compile(f) else: status, messages = cobc.compile(f, get_file_type(f)) except Exception as e: _logger().exception('exception while compiling') self.errored.emit(f, e) else: self.file_compiled.emit(f, status, messages) self.finished.emit()
def get_runtime_env(): try: import qdarkstyle except ImportError: qdarkstyle_version = 'Not installed' else: qdarkstyle_version = qdarkstyle.__version__ versions = { 'GnuCOBOL': GnuCobolCompiler().get_version(include_all=False), 'Qt': QtCore.QT_VERSION_STR, 'PyQt': QtCore.PYQT_VERSION_STR, 'pyqode.core': pyqode.core.__version__, 'pyqode.cobol': pyqode.cobol.__version__, 'pygments': pygments.__version__, 'qdarkstyle': qdarkstyle_version} return versions
def get_system_infos(): from open_cobol_ide import __version__ from open_cobol_ide.compilers import GnuCobolCompiler from pyqode.qt import QtCore import pyqode.core import pyqode.cobol import pygments try: import qdarkstyle except ImportError: qdarkstyle_version = 'Not installed' else: qdarkstyle_version = qdarkstyle.__version__ def get_linux_distro(): try: out = str(subprocess.check_output(['lsb_release', '-i']), locale.getpreferredencoding()) except OSError: distro = platform.linux_distribution()[0] if not distro: distro = 'linux distribution not found' else: distro = out.split(':')[1].strip() return distro system_info = platform.system() if 'linux' in sys.platform.lower(): system_info = get_linux_distro() elif 'darwin' in sys.platform.lower(): system_info = 'Mac OS X %s' % platform.mac_ver()[0] return '\n'.join([ 'Operating System: %s' % system_info, 'OpenCobolIDE: %s' % __version__, 'GnuCOBOL: %s' % GnuCobolCompiler().get_version( include_all=False), 'Python: %s (%dbits)' % (platform.python_version(), 64 if sys.maxsize > 2**32 else 32), 'Qt: %s' % QtCore.QT_VERSION_STR, 'PyQt: %s' % QtCore.PYQT_VERSION_STR, 'pyqode.core: %s' % pyqode.core.__version__, 'pyqode.cobol: %s' % pyqode.cobol.__version__, 'pyqode.qt: %s' % pyqode.qt.__version__, 'pygments: %s' % pygments.__version__, 'QDarkStyle: %s' % qdarkstyle_version ])
def test_is_working(): assert GnuCobolCompiler().is_working()
def enable_linter(self): self.linter_mode.enabled = GnuCobolCompiler().is_working() _logger().debug('linter enabled: %r', self.linter_mode.enabled)
def test_get_version(): prog = re.compile(r'^.*\d.\d.\d$') assert prog.match(GnuCobolCompiler().get_version()) is not None
('test/testfiles/VIRTUAL-PRINTER.cbl', FileType.MODULE), ('test/testfiles/VIRTUAL-PRINTER2.cbl', FileType.MODULE), ]) def test_get_file_type(path, ftype): assert get_file_type(path) == ftype @pytest.mark.parametrize('file_type, expected', [ (FileType.EXECUTABLE, '.exe' if system.windows else ''), (FileType.MODULE, '.dll' if system.windows else '.so'), ]) def test_type_extension(file_type, expected): assert GnuCobolCompiler().extension_for_type(file_type) == expected exe_ext = GnuCobolCompiler().extension_for_type(FileType.EXECUTABLE) dll_ext = GnuCobolCompiler().extension_for_type(FileType.MODULE) @pytest.mark.parametrize( 'free, std, ftype, expected_opts', [(False, GnuCobolStandard.default, FileType.EXECUTABLE, [ '-x', '-o', '%s' % os.path.join('bin', 'HelloWorld' + exe_ext), '-std=default', '-Wall', '-debug' ]), (True, GnuCobolStandard.default, FileType.EXECUTABLE, [ '-x', '-o', '%s' % os.path.join('bin', 'HelloWorld' + exe_ext), '-std=default', '-Wall', '-debug', '-free' ]),
def test_get_dependencies(filename, expected_results): results = GnuCobolCompiler().get_dependencies(filename) assert sorted(results) == sorted(expected_results)
def test_type_extension(file_type, expected): assert GnuCobolCompiler().extension_for_type(file_type) == expected
def test_get_version(): prog = re.compile(r'^.*\d.\d.\d$') assert prog.match(GnuCobolCompiler().get_version(include_all=False)) \ is not None