-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.py
executable file
·239 lines (208 loc) · 8.72 KB
/
install.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- coding: utf-8 -*-
import os
import platform
import re
import typing
import json
from pathlib import Path
from urllib import request
import urllib
import sys
import subprocess
import time
import datetime
import argparse
'''
nightly-nvim-build-download.ps1
# Set-ExecutionPolicy RemoteSigned
Invoke-WebRequest -uri https://github.com/neovim/neovim/releases/download/nightly/nvim-win64.zip -OutFile "$HOME/nvim-win64-$(Get-Date -Format 'yyyy-MM-dd').zip"
'''
def _progress(block_num, block_size, total_size):
'''回调函数
@block_num: 已经下载的数据块
@block_size: 数据块的大小
@total_size: 远程文件的大小
'''
sys.stdout.write('\r>> Downloading %.1f%%' % (
float(block_num * block_size) / float(total_size) * 100.0))
sys.stdout.flush()
def download(url, dest_dir: Path):
try:
# create the object, assign it to a variable
# proxy = urllib.request.ProxyHandler({'https': 'socks5://127.0.0.1:1080'})
# # construct a new opener using your proxy settings
# opener = urllib.request.build_opener(proxy)
# # install the openen on the module-level
# urllib.request.install_opener(opener)
# make a request
request.urlretrieve(url , dest_dir, _progress)
except Exception as e:
print(f'\tError retrieving the URL:{dest_dir}, {e}')
def download_vim(replace=False):
start_tim = datetime.datetime.now()
print('Start time:', start_tim.strftime('%Y-%m-%d %H:%M:%S'))
tm = time.strftime("%Y-%m-%d", time.localtime())
# path是下载文件保存的路径
osName = platform.system()
path: Path = Path.home()
dest = Path.home()
head = r'https://github.com/neovim/neovim/releases/download/nightly'
if osName == 'Windows':
path = path.joinpath(f'nvim-win64-{tm}.zip')
url = rf'{head}/nvim-win64.zip'
elif osName == 'Linux':
path = path.joinpath(f'nvim-{tm}.appimage')
url = rf'{head}/nvim.appimage'
elif osName == 'Darwin':
path = path.joinpath(f'nvim-macos-{tm}.tar.gz')
url = rf'{head}/nvim-macos.tar.gz'
print(path)
if not path.exists():
download(url, path)
if replace and osName == 'Darwin':
old: Path = Path.home().joinpath('nvim-osx64')
last_modified = time.strftime('%Y-%m-%d', time.gmtime(os.path.getmtime(os.path.join(old, 'bin', 'nvim'))))
backup: Path = Path.home().joinpath(f'nvim-osx64-{last_modified}')
if old.exists():
os.system(f'mv {old} {backup}')
cmd = f'tar xzvf {path} -C {dest}'
print(f'Execute cmd: {cmd}')
os.system(cmd)
end_tim = datetime.datetime.now()
print('End time:', end_tim.strftime('%Y-%m-%d %H:%M:%S'))
total_tim = end_tim - start_tim # timedelta
print('Total time:', str(total_tim))
submodule_list = ['YouCompleteMe',
'YouCompleteMe/third_party/ycmd',
'YouCompleteMe/third_party/ycmd/third_party/OmniSharpServer',
'python-mode',
]
# TODO: apply patch
def install_ycm_pymode():
base_dir = Path('../.cache/dein/repos/local').resolve()
os.system(
f'git clone https://gitee.com/vpr/YouCompleteMe {base_dir}/YouCompleteMe')
os.system(
f'git clone https://gitee.com/vpr/python-mode {base_dir}/python-mode')
for sub in submodule_list:
path, url = [], []
fn = base_dir.joinpath(sub, '.gitmodules')
print(f'>>>>>>>>>>>>>>>>>>>>{sub}<<<<<<<<<<<<<<<<<<<<')
with open(fn, 'r') as f:
for line in f:
# line = line.strip()
matchObj = re.match(r'\s*(.*) = (.*)', line, re.M | re.I)
if matchObj:
# print(f"matchObj.group(1) : {matchObj.group(1)}, group(2): {matchObj.group(2)}")
if matchObj.group(1) == 'path':
path.append(matchObj.group(2))
elif matchObj.group(1) == 'url':
tmp = f"https://gitee.com/vpr/{matchObj.group(2).split('/')[-1]}"
url.append(tmp)
for p, u in zip(path, url):
at = subprocess.Popen(
[f'git -C {base_dir}/{sub} submodule status {p}'], stdout=subprocess.PIPE, shell=True).communicate()
at = bytes.decode(at[0])
matchObj = re.match(r'\-(.*) (.*).*', at, re.M | re.I)
print(f"matchObj.group(1) : {matchObj.group(1)}")
at = matchObj.group(1)
cmd = f'git clone {u} {base_dir}/{sub}/{p}'
print(cmd)
os.system(cmd)
os.system(f'git -C {base_dir}/{sub}/{p} checkout {at}')
# print(p, u)
# print(path, url)
for sub in submodule_list:
print(sub)
os.system(f'git -C {base_dir}/{sub} status')
os.system(f'python {base_dir}/YouCompleteMe/install.py --clang-completer')
# 优先使用nvim
_alias_vim_cmd = """tee -a ~/.zshrc <<< '
if type nvim > /dev/null 2>&1; then
alias vim="nvim"
fi
'"""
def vim_deps_install():
"""Install Vim and dependence."""
ostype = platform.system()
# TODO: 细化系统发行版
# https://github.com/k-takata/the_silver_searcher-win32/releases
# sudo apt-get install silversearcher-ag
if ostype == 'Linux':
# TODO: 使用nvim.appimage
os.system('sudo pacman --noconfirm -S the_silver_searcher')
os.system('sudo pacman --noconfirm -S ctags')
os.system('sudo pacman --noconfirm -S clang')
os.system('sudo pacman --noconfirm -S cmake')
os.system('sudo pacman --noconfirm -S python-pip')
os.system('sudo pacman --noconfirm -S neovim')
os.system('sudo pacman --noconfirm -S xclip')
os.system('pip install --user pynvim')
os.system('pip install --user yapf')
# 清空垃圾箱 rm -rf /home/zgp/.local/share/Trash/
os.system('pip install --user send2Trash')
os.system('ln -s ~/.vim ~/.config/nvim')
os.system(_alias_vim_cmd)
elif ostype == 'Darwin':
# TODO: install python3
os.system('brew install python3')
# https://mirror.tuna.tsinghua.edu.cn/help/homebrew/
os.system('brew install wget')
os.system('brew install the_silver_searcher')
os.system('brew install clang-format')
# https://github.com/universal-ctags/homebrew-universal-ctags
# TODO: 下载太慢
# os.system('brew install --HEAD universal-ctags/universal-ctags/universal-ctags')
os.system('brew install ctags')
# 提示 /Users/zgp/Library/Python/3.7/bin is not on PATH
os.system('pip3 install --upgrade --user autopep8')
os.system('pip3 install --user pynvim')
os.system('pip3 install --user send2Trash')
# remove soft link to vim
# rm /usr/bin/vi
# ln -s ~/nvim-osx64/bin/nvim /usr/bin/vi
# NOTE brew install vim -> /usr/local/bin/vim
# TODO 清除默认安装的vim, 待修改
# rm /usr/bin/vim /usr/bin/ex /usr/bin/rvim /usr/bin/rview /usr/bin/view /usr/bin/vimdiff /usr/bin/vimtutor
# rm -rf /usr/share/vim/
# Pre-built archives
# curl -LO https://github.com/neovim/neovim/releases/download/nightly/nvim-macos.tar.gz
# tar xzf nvim-macos.tar.gz
# ./nvim-osx64/bin/nvim
# brew install --HEAD neovim
elif ostype == 'Windows':
# https://git-scm.com/downloads
pass
# TODO: 未完成
def update_gitee_mirror_repo():
cmd = "curl -X GET --header 'Content-Type: application/json;charset=UTF-8' 'https://gitee.com/api/v5/users/zgpio/repos?type=all&sort=full_name&page=1&per_page=100'" # 可以直接在命令行中执行的命令
r = os.popen(cmd)
info = r.readlines()[0] # 读取命令行的输出到一个list
info: str
# info = info.strip('\r\n')
# for line in info: # 按行遍历
# line = line.strip('\r\n')
# print(line)
print(info)
conf = json.loads(info)
for k in conf:
if k['description'] == 'Mirror':
print(k['full_name'])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Install Scripts')
parser.add_argument('--uri', type=str, nargs='+', help='specific update uri')
parser.add_argument('-d', '--download-vim', action='store_true', default=False,
help='download nvim from release.')
parser.add_argument('-r', action='store_true', default=False,
help='Replace nvim.')
parser.add_argument('--deps', action='store_true', default=False)
args = parser.parse_args()
# TODO: 导出
# install_ycm_pymode()
# NOTE: Cannot use abbr
if args.download_vim and args.r:
download_vim(replace=True)
elif args.download_vim:
download_vim()
elif args.deps:
vim_deps_install()