def get_git_revision(repopath): """Return Git revision for the repository located at repopath Result is a tuple (latest commit hash, branch), with None values on error """ try: git = programs.find_program('git') assert git is not None and osp.isdir(osp.join(repopath, '.git')) # Revision commit = subprocess.Popen([git, 'rev-parse', '--short', 'HEAD'], stdout=subprocess.PIPE, cwd=repopath).communicate() commit = commit[0].strip() if PY3: commit = commit.decode(sys.getdefaultencoding()) # Branch branches = subprocess.Popen([git, 'branch'], stdout=subprocess.PIPE, cwd=repopath).communicate() branches = branches[0] if PY3: branches = branches.decode(sys.getdefaultencoding()) branches = branches.split('\n') active_branch = [b for b in branches if b.startswith('*')] if len(active_branch) != 1: branch = None else: branch = active_branch[0].split(None, 1)[1] return commit, branch except (subprocess.CalledProcessError, AssertionError, AttributeError): return None, None
def create_program_action(parent, text, name, icon=None, nt_name=None): """Create action to run a program""" if is_text_string(icon): icon = get_icon(icon) if os.name == "nt" and nt_name is not None: name = nt_name path = programs.find_program(name) if path is not None: return create_action(parent, text, icon=icon, triggered=lambda: programs.run_program(name))
def create_program_action(parent, text, name, icon=None, nt_name=None): """Create action to run a program""" if is_text_string(icon): icon = get_icon(icon) if os.name == 'nt' and nt_name is not None: name = nt_name path = programs.find_program(name) if path is not None: return create_action(parent, text, icon=icon, triggered=lambda: programs.run_program(name))
def create_program_action(parent, text, icon, name, nt_name=None): """Create action to run a program""" if isinstance(icon, basestring): icon = get_icon(icon) if os.name == 'nt' and nt_name is not None: name = nt_name path = programs.find_program(name) if path is not None: return create_action(parent, text, icon=icon, triggered=lambda: programs.run_program(name))
def run_scm_tool(path, tool): """If path is a valid SCM repository, run the corresponding SCM tool Supported SCM tools: 'commit', 'browse' Return False if the SCM tool is not installed""" infos = get_scm_infos(get_scm_root(path)) for name, args in infos[tool]: if programs.find_program(name): programs.run_program(name, args, cwd=path) return else: raise RuntimeError(_("Please install the %s tool named '%s'") % (infos['name'], name))
def run_vcs_tool(path, action): """If path is a valid VCS repository, run the corresponding VCS tool Supported VCS actions: 'commit', 'browse' Return False if the VCS tool is not installed""" info = get_vcs_info(get_vcs_root(path)) tools = info['actions'][action] for tool, args in tools: if programs.find_program(tool): programs.run_program(tool, args, cwd=path) return else: cmdnames = [name for name, args in tools] raise ActionToolNotFound(info['name'], action, cmdnames)
def get_hg_revision(repopath): """Return Mercurial revision for the repository located at repopath Result is a tuple (global, local, branch), with None values on error For example: >>> get_hg_revision(".") ('eba7273c69df+', '2015+', 'default') """ try: hg = programs.find_program('hg') assert hg is not None and osp.isdir(osp.join(repopath, '.hg')) output, _err = subprocess.Popen([hg, 'id', '-nib', repopath], stdout=subprocess.PIPE).communicate() # output is now: ('eba7273c69df+ 2015+ default\n', None) return tuple(output.decode().strip().split()) except (subprocess.CalledProcessError, AssertionError, AttributeError): # print("Error: Failed to get revision number from Mercurial - %s" % exc) return (None, None, None)
def get_git_revision(repopath): """Return Git revision for the repository located at repopath Result is the latest commit hash, with None on error """ try: git = programs.find_program('git') assert git is not None and osp.isdir(osp.join(repopath, '.git')) commit = subprocess.Popen([git, 'rev-parse', '--short', 'HEAD'], stdout=subprocess.PIPE, cwd=repopath).communicate() if PY3: commit = str(commit[0][:-1]) commit = commit[2:-1] else: commit = commit[0][:-1] return commit except (subprocess.CalledProcessError, AssertionError, AttributeError): return None
def get_hg_revision(repopath): """Return Mercurial revision for the repository located at repopath Result is a tuple (global, local, branch), with None values on error For example: >>> get_hg_revision(".") ('eba7273c69df+', '2015+', 'default') """ try: hg = programs.find_program('hg') assert hg is not None and osp.isdir(osp.join(repopath, '.hg')) output, _err = subprocess.Popen([hg, 'id', '-nib', repopath], stdout=subprocess.PIPE).communicate() # output is now: ('eba7273c69df+ 2015+ default\n', None) # Split 2 times max to allow spaces in branch names. return tuple(output.decode().strip().split(None, 2)) except (subprocess.CalledProcessError, AssertionError, AttributeError): # print("Error: Failed to get revision number from Mercurial - %s" % exc) return (None, None, None)
docs_dest = osp.join(app_python_lib, 'spyderlib', 'doc') shutil.copytree(docs_orig, docs_dest) # Create a minimal library inside Resources to add it to PYTHONPATH instead of # app_python_lib. This must be done when the user changes to an interpreter # that's not the one that comes with the app, to forbid importing modules # inside the app. minimal_lib = osp.join(app_python_lib, 'minimal-lib') os.mkdir(minimal_lib) minlib_pkgs = ['spyderlib', 'spyderplugins'] for p in minlib_pkgs: shutil.copytree(osp.join(app_python_lib, p), osp.join(minimal_lib, p)) # Add necessary Python programs to the app PROGRAMS = ['pylint', 'pep8'] system_progs = [find_program(p) for p in PROGRAMS] progs_dest = [resources + osp.sep + p for p in PROGRAMS] for i in range(len(PROGRAMS)): shutil.copy2(system_progs[i], progs_dest[i]) # Add deps needed for PROGRAMS to the app deps = [] for package in os.listdir(system_python_lib): for d in DEPS: if package.startswith(d): deps.append(package) for i in deps: if osp.isdir(osp.join(system_python_lib, i)): shutil.copytree(osp.join(system_python_lib, i), osp.join(app_python_lib, i))
from spyderlib import dependencies from spyderlib.utils import programs from spyderlib.utils.encoding import to_unicode_from_fs from spyderlib.utils.qthelpers import create_toolbutton from spyderlib.config.base import get_conf_path, get_translation from spyderlib.widgets.onecolumntree import OneColumnTree from spyderlib.widgets.variableexplorer.texteditor import TextEditor from spyderlib.widgets.comboboxes import (PythonModulesComboBox, is_module_or_package) from spyderlib.py3compat import PY3, to_text_string, getcwd, pickle _ = get_translation("p_pylint", dirname="spyderplugins") PYLINT = 'pylint' if PY3: if programs.find_program('pylint3'): PYLINT = 'pylint3' elif programs.find_program('python3-pylint'): PYLINT = 'python3-pylint' PYLINT_PATH = programs.find_program(PYLINT) def get_pylint_version(): """Return pylint version""" global PYLINT_PATH if PYLINT_PATH is None: return process = subprocess.Popen([PYLINT, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=osp.dirname(PYLINT_PATH),
def start(self, wdir=None, args=None, pythonpath=None): filename = to_text_string(self.filecombo.currentText()) if wdir is None: wdir = self._last_wdir if wdir is None: wdir = osp.basename(filename) if args is None: args = self._last_args if args is None: args = [] if pythonpath is None: pythonpath = self._last_pythonpath self._last_wdir = wdir self._last_args = args self._last_pythonpath = pythonpath self.datelabel.setText(_('Profiling, please wait...')) self.process = QProcess(self) self.process.setProcessChannelMode(QProcess.SeparateChannels) self.process.setWorkingDirectory(wdir) self.connect(self.process, SIGNAL("readyReadStandardOutput()"), self.read_output) self.connect(self.process, SIGNAL("readyReadStandardError()"), lambda: self.read_output(error=True)) self.connect(self.process, SIGNAL("finished(int,QProcess::ExitStatus)"), self.finished) self.connect(self.stop_button, SIGNAL("clicked()"), self.process.kill) if pythonpath is not None: env = [to_text_string(_pth) for _pth in self.process.systemEnvironment()] baseshell.add_pathlist_to_PYTHONPATH(env, pythonpath) self.process.setEnvironment(env) self.output = '' self.error_output = '' # remove previous results, since memory_profiler appends to output file # instead of replacing if osp.isfile(self.DATAPATH): os.remove(self.DATAPATH) if os.name == 'nt': # On Windows, one has to replace backslashes by slashes to avoid # confusion with escape characters (otherwise, for example, '\t' # will be interpreted as a tabulation): filename = osp.normpath(filename).replace(os.sep, '/') p_args = ['-m', 'memory_profiler', '-o', '"' + self.DATAPATH + '"', '"' + filename + '"'] if args: p_args.extend(programs.shell_split(args)) executable = programs.find_program('python') executable += ' ' + ' '.join(p_args) executable = executable.replace(os.sep, '/') self.process.start(executable) else: p_args = ['-m', 'memory_profiler', '-o', self.DATAPATH, filename] if args: p_args.extend(programs.shell_split(args)) executable = 'python' self.process.start(executable, p_args) running = self.process.waitForStarted() self.set_running_state(running) if not running: QMessageBox.critical(self, _("Error"), _("Process failed to start"))
# Local imports from spyderlib import dependencies from spyderlib.utils import programs from spyderlib.utils.encoding import to_unicode_from_fs from spyderlib.utils.qthelpers import get_icon, create_toolbutton from spyderlib.baseconfig import get_conf_path, get_translation from spyderlib.widgets.onecolumntree import OneColumnTree from spyderlib.widgets.texteditor import TextEditor from spyderlib.widgets.comboboxes import (PythonModulesComboBox, is_module_or_package) from spyderlib.py3compat import to_text_string, getcwd, pickle _ = get_translation("p_pylint", dirname="spyderplugins") PYLINT_PATH = programs.find_program('pylint') def get_pylint_version(): """Return pylint version""" global PYLINT_PATH if PYLINT_PATH is None: return process = subprocess.Popen(['pylint', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=osp.dirname(PYLINT_PATH), shell=True if os.name == 'nt' else False) lines = to_unicode_from_fs(process.stdout.read()).splitlines() if lines: match = re.match('(pylint|pylint-script.py) ([0-9\.]*)', lines[0]) if match is not None:
# Local imports from spyderlib import dependencies from spyderlib.utils import programs from spyderlib.utils.encoding import to_unicode_from_fs from spyderlib.utils.qthelpers import get_icon, create_toolbutton from spyderlib.baseconfig import get_conf_path, get_translation from spyderlib.widgets.onecolumntree import OneColumnTree from spyderlib.widgets.texteditor import TextEditor from spyderlib.widgets.comboboxes import (PythonModulesComboBox, is_module_or_package) from spyderlib.py3compat import to_text_string, getcwd, pickle _ = get_translation("p_pylint", dirname="spyderplugins") PYLINT_PATH = programs.find_program( '\\Tellurium-Winpython\\python-2.7.9\\Scripts\\pylint') def get_pylint_version(): """Return pylint version""" global PYLINT_PATH if PYLINT_PATH is None: return process = subprocess.Popen(['pylint', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=osp.dirname(PYLINT_PATH), shell=True if os.name == 'nt' else False) lines = to_unicode_from_fs(process.stdout.read()).splitlines() if lines: match = re.match('(pylint|pylint-script.py) ([0-9\.]*)', lines[0])
def is_hg_installed(): """Return True if Mercurial is installed""" return programs.find_program('hg') is not None
from spyderlib.utils.qthelpers import create_toolbutton from spyderlib.widgets.comboboxes import (is_module_or_package, PythonModulesComboBox) from spyderlib.widgets.onecolumntree import OneColumnTree from spyderlib.widgets.variableexplorer.texteditor import TextEditor # This is needed for testing this module as a stand alone script try: _ = get_translation("pylint", "spyplugins.ui.pylint") except KeyError as error: import gettext _ = gettext.gettext PYLINT = 'pylint' if PY3: if programs.find_program('pylint3'): PYLINT = 'pylint3' elif programs.find_program('python3-pylint'): PYLINT = 'python3-pylint' locale_codec = QTextCodec.codecForLocale() PYLINT_PATH = programs.find_program(PYLINT) def get_pylint_version(): """Return pylint version""" if PYLINT_PATH is None: return cwd = osp.dirname(PYLINT_PATH) args = ['--version'] if os.name == 'nt':
# Local imports from spyderlib import dependencies from spyderlib.utils import programs from spyderlib.utils.encoding import to_unicode_from_fs from spyderlib.utils.qthelpers import get_icon, create_toolbutton from spyderlib.baseconfig import get_conf_path, get_translation from spyderlib.widgets.onecolumntree import OneColumnTree from spyderlib.widgets.texteditor import TextEditor from spyderlib.widgets.comboboxes import (PythonModulesComboBox, is_module_or_package) from spyderlib.py3compat import PY3, to_text_string, getcwd, pickle _ = get_translation("p_pylint", dirname="spyderplugins") PYLINT = 'pylint' if PY3 and programs.find_program('pylint3'): PYLINT = 'pylint3' PYLINT_PATH = programs.find_program(PYLINT) def get_pylint_version(): """Return pylint version""" global PYLINT_PATH if PYLINT_PATH is None: return process = subprocess.Popen([PYLINT, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=osp.dirname(PYLINT_PATH), shell=True if os.name == 'nt' else False)
def is_lineprofiler_installed(): """Checks if the program and the library for line_profiler is installed """ return (programs.is_module_installed('line_profiler') and programs.find_program('kernprof') is not None)
def is_lineprofiler_installed(): """Checks if the program and the library for line_profiler is installed """ return (programs.is_module_installed('line_profiler') and programs.find_program('kernprof.py') is not None)
# Local imports from spyderlib import dependencies from spyderlib.utils import programs from spyderlib.utils.encoding import to_unicode_from_fs from spyderlib.utils.qthelpers import get_icon, create_toolbutton from spyderlib.baseconfig import get_conf_path, get_translation from spyderlib.widgets.onecolumntree import OneColumnTree from spyderlib.widgets.texteditor import TextEditor from spyderlib.widgets.comboboxes import (PythonModulesComboBox, is_module_or_package) from spyderlib.py3compat import to_text_string, getcwd, pickle _ = get_translation("p_pylint", dirname="spyderplugins") PYLINT_PATH = programs.find_program('\\Tellurium-Winpython\\python-2.7.9\\Scripts\\pylint') def get_pylint_version(): """Return pylint version""" global PYLINT_PATH if PYLINT_PATH is None: return process = subprocess.Popen(['pylint', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=osp.dirname(PYLINT_PATH), shell=True if os.name == 'nt' else False) lines = to_unicode_from_fs(process.stdout.read()).splitlines() if lines: match = re.match('(pylint|pylint-script.py) ([0-9\.]*)', lines[0]) if match is not None:
# -*- coding: utf-8 -*- """ Created on Sun May 03 20:58:06 2015 @author: Jayit """ import sys import os import os.path as osp import time import re import subprocess from spyderlib.utils import programs PYLINT_PATH = programs.find_program('\\python-2.7.9\\Scripts\\pylint') print(PYLINT_PATH)
from spyderlib import dependencies from spyderlib.utils import programs from spyderlib.utils.encoding import to_unicode_from_fs from spyderlib.utils.qthelpers import create_toolbutton from spyderlib.baseconfig import get_conf_path, get_translation from spyderlib.widgets.onecolumntree import OneColumnTree from spyderlib.widgets.texteditor import TextEditor from spyderlib.widgets.comboboxes import PythonModulesComboBox, is_module_or_package from spyderlib.py3compat import PY3, to_text_string, getcwd, pickle _ = get_translation("p_pylint", dirname="spyderplugins") PYLINT = "pylint" if PY3: if programs.find_program("pylint3"): PYLINT = "pylint3" elif programs.find_program("python3-pylint"): PYLINT = "python3-pylint" PYLINT_PATH = programs.find_program(PYLINT) def get_pylint_version(): """Return pylint version""" global PYLINT_PATH if PYLINT_PATH is None: return process = subprocess.Popen( [PYLINT, "--version"], stdout=subprocess.PIPE,
def start(self, wdir=None, args=None, pythonpath=None): filename = to_text_string(self.filecombo.currentText()) if wdir is None: wdir = self._last_wdir if wdir is None: wdir = osp.basename(filename) if args is None: args = self._last_args if args is None: args = [] if pythonpath is None: pythonpath = self._last_pythonpath self._last_wdir = wdir self._last_args = args self._last_pythonpath = pythonpath self.datelabel.setText(_('Profiling, please wait...')) self.process = QProcess(self) self.process.setProcessChannelMode(QProcess.SeparateChannels) self.process.setWorkingDirectory(wdir) self.connect(self.process, SIGNAL("readyReadStandardOutput()"), self.read_output) self.connect(self.process, SIGNAL("readyReadStandardError()"), lambda: self.read_output(error=True)) self.connect(self.process, SIGNAL("finished(int,QProcess::ExitStatus)"), self.finished) self.connect(self.stop_button, SIGNAL("clicked()"), self.process.kill) if pythonpath is not None: env = [ to_text_string(_pth) for _pth in self.process.systemEnvironment() ] baseshell.add_pathlist_to_PYTHONPATH(env, pythonpath) self.process.setEnvironment(env) self.output = '' self.error_output = '' p_args = ['-lvb', '-o', self.DATAPATH, filename] if args: p_args.extend(programs.shell_split(args)) if os.name == 'nt': # On Windows, one has to replace backslashes by slashes to avoid # confusion with escape characters (otherwise, for example, '\t' # will be interpreted as a tabulation): filename = osp.normpath(filename).replace(os.sep, '/') script_path = programs.find_program('kernprof.py') executable = '{0} {1}'.format(sys.executable, script_path) executable += ' ' + ' '.join(p_args) executable = executable.replace(os.sep, '/') self.process.start(executable) else: executable = 'kernprof.py' self.process.start(executable, p_args) running = self.process.waitForStarted() self.set_running_state(running) if not running: QMessageBox.critical(self, _("Error"), _("Process failed to start"))