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')) commit = programs.run_program(git, ['rev-parse', '--short', 'HEAD'], cwd=repopath).communicate() commit = commit[0].strip() if PY3: commit = commit.decode(sys.getdefaultencoding()) # Branch branches = programs.run_program(git, ['branch'], 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 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 external_editor(self, filename, goto=-1): """Edit in an external editor Recommended: SciTE (e.g. to go to line where an error did occur)""" editor_path = CONF.get('internal_console', 'external_editor/path') goto_option = CONF.get('internal_console', 'external_editor/gotoline') try: args = [filename] if goto > 0 and goto_option: args.append('%s%d'.format(goto_option, goto)) programs.run_program(editor_path, args) except OSError: self.write_error("External editor was not found:" " %s\n" % editor_path)
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 create_program_action(parent, text, icon, name, nt_name=None): """Create action to run a program""" if os.name == 'nt': if nt_name is not None: name = nt_name name = programs.get_nt_program_name(name) if isinstance(icon, basestring): icon = get_icon(icon) if programs.is_program_installed(name): 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 os.name == 'nt': if nt_name is None: name += ".exe" else: name = nt_name if isinstance(icon, basestring): icon = get_icon(icon) if programs.is_program_installed(name): return create_action(parent, text, icon=icon, triggered=lambda: programs.run_program(name))
def get_pylint_version(): """Return pylint version""" if PYLINT_PATH is None: return cwd = osp.dirname(PYLINT_PATH) args = ['--version'] if os.name == 'nt': cmd = ' '.join([PYLINT] + args) process = programs.run_shell_command(cmd, cwd=cwd) else: process = programs.run_program(PYLINT, args, cwd=cwd) lines = to_unicode_from_fs(process.stdout.read()).splitlines() if lines: regex = '({0}*|pylint-script.py) ([0-9\.]*)'.format(PYLINT) match = re.match(regex, lines[0]) if match is not None: return match.groups()[1]
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: assert osp.isdir(osp.join(repopath, '.hg')) proc = programs.run_program('hg', ['id', '-nib', repopath]) output, _err = proc.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)
def check(args, source_code, filename=None, options=None): """Check source code with checker defined with *args* (list) Returns an empty list if checker is not installed""" if args is None: return [] if options is not None: args += options if any(['pyflakes' in arg for arg in args]): # Pyflakes requires an ending new line (pep8 don't! -- see Issue 1123) # Note: this code is not used right now as it is faster to invoke # pyflakes in current Python interpreter (see `check_with_pyflakes` # function above) than calling it through a subprocess source_code += '\n' if filename is None: # Creating a temporary file because file does not exist yet # or is not up-to-date tempfd = tempfile.NamedTemporaryFile(suffix=".py", delete=False) tempfd.write(source_code) tempfd.close() args.append(tempfd.name) else: args.append(filename) cmd = args[0] cmdargs = args[1:] proc = programs.run_program(cmd, cmdargs) output = proc.communicate()[0].strip().decode().splitlines() if filename is None: os.unlink(tempfd.name) results = [] coding = encoding.get_coding(source_code) lines = source_code.splitlines() for line in output: lineno = int(re.search(r'(\:[\d]+\:)', line).group()[1:-1]) try: text = to_text_string(lines[lineno-1], coding) except TypeError: text = to_text_string(lines[lineno-1]) if 'analysis:ignore' not in text: message = line[line.find(': ')+2:] results.append((message, lineno)) return results
def check(args, source_code, filename=None, options=None): """Check source code with checker defined with *args* (list) Returns an empty list if checker is not installed""" if args is None: return [] if options is not None: args += options if any(['pyflakes' in arg for arg in args]): # Pyflakes requires an ending new line (pep8 don't! -- see Issue 1123) # Note: this code is not used right now as it is faster to invoke # pyflakes in current Python interpreter (see `check_with_pyflakes` # function above) than calling it through a subprocess source_code += '\n' if filename is None: # Creating a temporary file because file does not exist yet # or is not up-to-date tempfd = tempfile.NamedTemporaryFile(suffix=".py", delete=False) tempfd.write(source_code) tempfd.close() args.append(tempfd.name) else: args.append(filename) cmd = args[0] cmdargs = args[1:] proc = programs.run_program(cmd, cmdargs) output = proc.communicate()[0].strip().decode().splitlines() if filename is None: os.unlink(tempfd.name) results = [] coding = encoding.get_coding(source_code) lines = source_code.splitlines() for line in output: lineno = int(re.search(r'(\:[\d]+\:)', line).group()[1:-1]) try: text = to_text_string(lines[lineno - 1], coding) except TypeError: text = to_text_string(lines[lineno - 1]) if 'analysis:ignore' not in text: message = line[line.find(': ') + 2:] results.append((message, lineno)) return results