def setup_vim(): autoload = join(HOME, '.vim', 'autoload') mkdir_p(autoload) sh.curl('-fLo', join(autoload, 'plug.vim'), 'https://raw.github.com/junegunn/vim-plug/master/plug.vim') link_home('vimrc') print ' Running PlugInstall' sh.vim('+PlugInstall', '+qall')
def _change_vim_color_scheme(colorscheme, background): vimrc_path = os.path.expanduser('~/.vimrc') sed_cmd = 's/colorscheme {old_scheme}/colorscheme {new_scheme}/g' sed_cmd = sed_cmd.format(old_scheme=colorscheme[0], new_scheme=colorscheme[1]) sed('-i', sed_cmd, vimrc_path) sed_cmd = 's/background={old_bg}/background={new_bg}/g' sed_cmd = sed_cmd.format(old_bg=background[0], new_bg=background[1]) sed('-i', sed_cmd, vimrc_path) change_color = '<Esc>:set background={new_bg}<CR>:colorscheme {new_scheme}<CR>' change_color = change_color.format(new_bg=background[1], new_scheme=colorscheme[1]) try: for server in vim('--serverlist'): vim('--servername', server.strip(), '--remote-send', change_color) except ErrorReturnCode_1: pass
def run_vim(script, input_str=None, vimrc=VIMRC.name, commands=None): # we can't use a real fifo because it will block on opening, because one # side will wait for the other side to open before unblocking fifo = tempfile.NamedTemporaryFile(delete=True) # we do some super ugly stuff and wrap our script in some helpers, namely a # helper to send output from our snake script to our test script = """ python << EOF import pickle from snake import * def send(stuff): with open("{fifo_filename}", "w") as output: pickle.dump(stuff, output) {script} EOF """.format(script=script, fifo_filename=fifo.name) script_file = create_tmp_file(script) input_file = create_tmp_file(input_str or "") # use our custom vimrc, use binary mode (dont add newlines automatically), # and load script_file as our script to run args = [ "-N", "-n", "-i", "NONE", "-u", vimrc, "-S", script_file.name, "-b" ] # sometimes we need to specify our own commands, but most times not if commands is None: commands = [] #commands = ["exec 'silent !echo '. errmsg"] # save and exit commands.append("wqa!") for command in commands: args.extend(["-c", command]) args.append(input_file.name) env = os.environ.copy() env["LOAD_VIMPY"] = "0" p = sh.vim(*args, _tty_in=True, _env=env) #err = clean_output(p.stdout.decode("ascii")) output = p.stdout input_file.seek(0) changed = input_file.read().decode("utf8") sent_data = fifo.read() if sent_data: output = pickle.loads(sent_data) else: output = None return changed, output
def run_vim(script, input_str=None, vimrc=VIMRC.name, commands=None): # we can't use a real fifo because it will block on opening, because one # side will wait for the other side to open before unblocking fifo = tempfile.NamedTemporaryFile(delete=True, mode="w+b") # we do some super ugly stuff and wrap our script in some helpers, namely a # helper to send output from our snake script to our test script = """ {PYTHON_CMD} << EOF import json from snake import * def send(stuff): with open("{fifo_filename}", "w") as output: json.dump(stuff, output) {script} EOF """.format(PYTHON_CMD=PYTHON_CMD, script=script, fifo_filename=fifo.name) script_file = create_tmp_file(script) input_file = create_tmp_file(input_str or "") # use our custom vimrc, use binary mode (dont add newlines automatically), # and load script_file as our script to run args = ["-N", "-n", "-i", "NONE", "-u", vimrc, "-S", script_file.name, "-b"] # sometimes we need to specify our own commands, but most times not if commands is None: commands = [] #commands = ["exec 'silent !echo '. errmsg"] # save and exit commands.append("wqa!") for command in commands: args.extend(["-c", command]) args.append(input_file.name) env = os.environ.copy() env["LOAD_VIMPY"] = "0" p = sh.vim(*args, _tty_in=True, _env=env) err = output = p.stdout input_file.seek(0) changed = input_file.read().decode("utf8") sent_data = fifo.read() if sent_data: output = json.loads(sent_data) else: output = None return changed, output
def _vim_environment(self): sh.mkdir('-p', hpath('.vim/colors'), **SHARG) sh.cp(wpath('vim/molokai.vim'), hpath('.vim/colors/molokai.vim'), **SHARG) sh.cp(wpath('vim/solarized.vim'), hpath('.vim/colors/solarized.vim'), **SHARG) sh.rsync('-rzvaP', wpath('vim/UltiSnips'), hpath('.vim'), **SHARG) vundle_dir = hpath('.vim/bundle/Vundle.vim') if os.path.isdir(vundle_dir): os.chdir(vundle_dir) sh.git('pull', **SHARG) os.chdir(working_dir) else: sh.git('clone', 'https://github.com/VundleVim/Vundle.vim.git', vundle_dir, **SHARG) template = self.temp_env.get_template(wpath('vim/vimrc.template')) with open(hpath('.vimrc'), 'w') as vimrc: vimrc.write(template.render(python_path=self.python_path)) # sh.cp(wpath('vim/vimrc'), hpath('.vimrc'), **SHARG) sh.vim('+PluginInstall', '+qall', **SHARG) ycm_dir = hpath('.vim/bundle/YouCompleteMe') if not os.path.isdir(ycm_dir): sh.git('clone', 'https://github.com/Valloric/YouCompleteMe.git', ycm_dir, **SHARG) if not os.path.isfile( hpath( '.vim/bundle/YouCompleteMe/third_party/ycmd/ycm_core.so')): os.chdir(ycm_dir) sh.git('submodule', 'update', '--init', '--recursive', **SHARG) sh.zsh('install.sh', '--clang-completer', **SHARG) os.chdir(working_dir)
def process(pr_ids: List[str], timestamp: str): home = expanduser("~") script_workspace_dir = join_path(home, "github_us_diff_generator") script_html_out_dir = join_path(home, "github_us_diff_generator", "html") ensure_dir_created(script_workspace_dir) ensure_dir_created(script_html_out_dir) pr_id_to_files = {} for pr_id in pr_ids: pr_dir = join_path(script_workspace_dir, pr_id) ensure_dir_created(pr_dir) pr_id_to_files[pr_id] = {} full_github_patch_url = get_github_patch_url(pr_id) full_github_diff_url = get_github_diff_url(pr_id) print("Downloading Github PR patch file: {}".format( full_github_patch_url)) patch_file = download_file( full_github_patch_url, join_path(pr_dir, f"{pr_id}_{timestamp}.patch")) print( "Downloading Github PR diff file: {}".format(full_github_diff_url)) diff_file = download_file( full_github_diff_url, join_path(pr_dir, f"{pr_id}_{timestamp}.diff")) pr_id_to_files[pr_id] = UpstreamJira(diff_file, patch_file) print("Downloaded files: " + str(pr_id_to_files)) jira_ids = set() jira_id = None for pr_id, jira in pr_id_to_files.items(): # type: str, UpstreamJira # Determine jira ID from patch file jira_id, paths = extract_jira_and_paths_from_patch_file( pr_id, jira.patch) jira_ids.add(jira_id) if len(jira_ids) > 1: raise ValueError( "Expected Github PRs to belong to one single jira. Multiple Jira IDs found: %s" .format(jira_ids)) if not jira_id: raise ValueError("Jira ID not found!") diff_pairs = [(pr_ids[0], i) for i in pr_ids[1:]] print("PR diffs will be created for: {}".format(diff_pairs)) diff_files = [(pr_id_to_files[pr1].diff, pr_id_to_files[pr2].diff) for pr1, pr2 in diff_pairs] for file1, file2 in diff_files: file_1_pr_id = os.path.basename(file1).split("_")[0] file_2_pr_id = os.path.basename(file2).split("_")[0] html_name = f"{file_1_pr_id}-{file_2_pr_id}_{jira_id}.html" html_full_path = join_path(script_html_out_dir, html_name) print("Creating diff of files: {}, {}".format(file1, file2)) # EXAMPLE full command: # vim $HOME/version1.diff $HOME/version2.diff -d -c TOhtml -c 'w! /tmp/html' -c qa! vim = sh.vim(file1, file2, "-d", "-c", "TOhtml", "-c", f"\"w! {html_full_path}\"", "-c", "diffoff!", "-c", "qa!") print("Created diff HTML file: {}".format(html_full_path)) # UNCOMMENT THIS TO PRINT THE COMMAND WAS RUNNING # print(vim.cmd) # print(vim.call_args) return True
def install_plugins(self): vim("+PluginInstall", "+qall") return "Vim has installed all plugins"
from os.path import join, dirname, abspath, exists import tempfile import sys import unittest import sh import json import codecs import re THIS_DIR = dirname(abspath(__file__)) SNAKE_DIR = join(THIS_DIR, "plugin") # as distinguished from the python *vim* is running TEST_IS_PY3 = sys.version_info[0] == 3 version_str = sh.vim(version=True).strip() VIM_IS_PY3 = "+python3" in version_str PYTHON_CMD = "python3" if VIM_IS_PY3 else "python" def create_tmp_file(code, prefix="tmp", delete=True): """ creates a temporary test file that lives on disk, on which we can run python with sh """ py = tempfile.NamedTemporaryFile(prefix=prefix, delete=delete) if TEST_IS_PY3: code = bytes(code, "UTF-8") py.write(code) py.flush() # we don't explicitly close, because close will remove the file, and we
'errors': False, 'hidden': False, 'modified': False, 'readonly': False, 'unlisted': False}, }, 2: {'flags': {'active': False, 'alternate': False, 'current': False, 'errors': False, 'hidden': True, 'modified': False, 'readonly': False, 'unlisted': False}, 'name': 'test1'}, 3: {'flags': {'active': False, 'alternate': True, 'current': False, 'errors': False, 'hidden': True, 'modified': False, 'readonly': False, 'unlisted': False}, 'name': 'test2'} }) if __name__ == "__main__": print(sh.vim(version=True)) unittest.main(verbosity=2)
def process(revision: str, max_change_num: int = None): revision: List[str] = revision.split("/") revision_no = revision[0] patch_set = revision[1] patchset_trial = range(max_change_num, 0, -1) if max_change_num else {patch_set} for patch in patchset_trial: print("Trying to download revision {} of Patchset {} ".format( revision_no, patch)) patch_set = patch if download_gerrit_revision(patch, revision_no): break with ZipFile(PATCH_ZIP, 'r') as zip: gerrit_file = zip.filelist[0].filename zip.extractall() jira = "" os.remove(PATCH_ZIP) print("Extracting information from gerrit file {}".format(gerrit_file)) paths = [] with open(gerrit_file, 'r') as file: content = file.readlines() for line in content: if line.startswith(COMMIT_MESSAGE_PREFIX): jira = COMMIT_JIRA_REGEX.match(line).groups()[0] elif line.startswith(PATH_PREFIX): path = line.lstrip(PATH_PREFIX).split(" ")[0].lstrip("a/ ") if path: paths.append(path) if len(paths) > PATH_LIMIT: paths = paths[:PATH_LIMIT] upstream_commit_candidates = requests.get(GITHUB_COMMIT_API, params={ "path": paths }).json() upstream_commit = "" for commit in upstream_commit_candidates: if commit['commit']['message'].startswith(jira): upstream_commit = commit['sha'] if upstream_commit: print("Found {} upstream commit: {}".format(jira, upstream_commit)) else: return False print("Downloading upstream commit {}".format(upstream_commit)) download_file(GITHUB_COMMIT_URL_TEMPLATE.format(sha=upstream_commit)) html_name = f"{revision_no}-{patch_set}_{jira}" vim = sh.vim("-d", "-c", "TOhtml", "-c", "w! {}.html".format(html_name), "-c", "qa!", gerrit_file, "{}.diff".format(upstream_commit)) os.remove(gerrit_file) os.remove("{}.diff".format(upstream_commit)) print("Created diff HTML {}.html".format(html_name)) # UNCOMMENT THIS TO PRINT THE COMMAND WAS RUNNING # print(vim.cmd) # print(vim.call_args) return True
from os.path import join, dirname, abspath, exists import tempfile import sys import unittest import sh import json import codecs import re THIS_DIR = dirname(abspath(__file__)) SNAKE_DIR = join(THIS_DIR, "plugin") # as distinguised from the python *vim* is running TEST_IS_PY3 = sys.version_info[0] == 3 version_str = sh.vim(version=True).strip() VIM_IS_PY3 = "+python3" in version_str PYTHON_CMD = "python3" if VIM_IS_PY3 else "python" def create_tmp_file(code, prefix="tmp", delete=True): """ creates a temporary test file that lives on disk, on which we can run python with sh """ py = tempfile.NamedTemporaryFile(prefix=prefix, delete=delete) if TEST_IS_PY3: code = bytes(code, "UTF-8") py.write(code) py.flush() # we don't explicitly close, because close will remove the file, and we
'active': False, 'alternate': False, 'current': False, 'errors': False, 'hidden': True, 'modified': False, 'readonly': False, 'unlisted': False }, 'name': 'test1' }, 3: { 'flags': { 'active': False, 'alternate': True, 'current': False, 'errors': False, 'hidden': True, 'modified': False, 'readonly': False, 'unlisted': False }, 'name': 'test2' } }) if __name__ == "__main__": print(sh.vim(version=True)) unittest.main(verbosity=2)