-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpisi.vim
277 lines (215 loc) · 7.15 KB
/
pisi.vim
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
" File: pisi.vim
" Author: Fatih Arslan
" Version: 0.1
" Description: Lots of useful functions for pisi packagers
" License: GPLv2
"
"
"
""""""""" USAGE
" You can change the keybindings for your personal use
nmap <F2>h :call FixHash()<CR>
nmap <F2>a :call PackageTakeover()<CR>
nmap <F2>c :call AddComment()<CR>
nmap <F2>o :call OpenHomePage()<CR>
nmap <F2>p :call AddPatches()<CR>
nmap <F2>w :call ShowOther()<CR>
nmap <F2>d :call ShowDiff()<CR>
nmap <F2>t :call CreateCommit()<CR>
nmap <F2>s :call SvnCommit()<CR>
"""""""" FUNCTIONS
""""""""""""""""""
function! FixHash()
python << EOF
import os
import vim
import hashlib
import piksemel
buf = vim.current.buffer
correct_syntax = False
for row, line in enumerate(buf[:]):
if "<Archive sha1sum=" in line:
correct_syntax = True
tag = piksemel.parseString(line)
hash = tag.getAttribute("sha1sum")
uri = tag.firstChild().data()
file_name = os.path.basename(uri)
file_path = os.path.join("/var/cache/pisi/archives", file_name)
if not os.path.exists(file_path):
if os.path.exists("tmp.xml"):
os.unlink("tmp.xml")
vim.command("w tmp.xml")
# dont call sudo if invoked as root
if os.geteuid():
vim.command("!sudo pisi -d bi --fetch tmp.xml; rm -f tmp.xml")
else:
vim.command("!pisi -d bi --fetch tmp.xml; rm -f tmp.xml")
new_hash = hashlib.sha1(open(file_path, "r").read()).hexdigest()
tag.setAttribute("sha1sum", new_hash)
buf[row] = " " * 8 + tag.toString()
if not correct_syntax:
print "Error: The Archive attribute 'sha1sum' must come first."
EOF
endfunction
function! PackageTakeover()
python << EOF
import os
import vim
buf = vim.current.buffer
conf_file = os.path.expanduser("~/.packagerinfo")
if os.path.exists(conf_file):
# Read from it
name, email = open(conf_file, "r").read().strip().split(",")
else:
name = vim.eval('input("Please enter your full name as seen in pspec files: ")')
email = vim.eval('input("Please enter your full email as seen in pspec files: ")')
open(conf_file, "w").write("%s,%s" % (name, email))
pkgr_name = " <Name>%s</Name>\n" % name
pkgr_email = " <Email>%s</Email>\n" % email
for row, line in enumerate(buf[:]):
if " <Name>" in line:
buf[row] = pkgr_name
elif " <Email>" in line:
buf[row] = pkgr_email
break
EOF
endfunction
function! AddComment()
python << EOF
import vim
import time
import pisi
import os
conf_file = os.path.expanduser("~/.packagerinfo")
if os.path.exists(conf_file):
# Read from it
name, email = open(conf_file, "r").read().strip().split(",")
else:
name = vim.eval('input("Please enter your full name as seen in pspec files: ")')
email = vim.eval('input("Please enter your full email as seen in pspec files: ")')
open(conf_file, "w").write("%s,%s" % (name, email))
CONSTANTS = pisi.constants.Constants()
pspec = pisi.specfile.SpecFile(CONSTANTS.pspec_file)
release = ' <Update release="%d">' % (int(pspec.history[0].release) + 1)
date = ' <Date>%s</Date>' % time.strftime("%Y-%m-%d")
version = ' <Version>%s</Version>' % pspec.history[0].version
comment = ' <Comment></Comment>'
name = ' <Name>%s</Name>' % name
email = ' <Email>%s</Email>'% email
update= ' </Update>'
buf = vim.current.buffer
win = vim.current.window
for row, line in enumerate(buf[:]):
if "<History>" in line:
#NOTE: do not use double quotes, it can fail
comment_row = row + 5
vim.command("call append(%s, ['%s','%s','%s','%s','%s','%s','%s'])" % (row + 1,
release,
date,
version,
comment,
name,
email,
update))
win.cursor = (comment_row, 21)
def normal(str):
vim.command("normal "+str)
normal("i")
EOF
endfunction
function! OpenHomePage()
python << EOF
import vim
import webbrowser
import piksemel
buf = vim.current.buffer
for row, line in enumerate(buf[:]):
if "<Homepage>" in line:
tag = piksemel.parseString(line)
url = tag.firstChild().data()
try:
xdg_browser = webbrowser.GenericBrowser('xdg-open')
xdg_browser.open(url)
except:
print 'ERROR: could not open url'
EOF
endfunction
function! AddPatches()
python << EOF
import vim
import os
import subprocess
buf = vim.current.buffer
if os.path.exists(".svn"):
cmd = ["svn", "st", "files/"]
else:
cmd = ["git", "status", "-s", "files/"]
output = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0].strip().split("\n")
patches = []
for patch in output:
if patch[0] == "?" or patch[0] == "A":
if patch.endswith("patch") or patch.endswith("diff"):
patch = patch.split("/", 1)[1]
frmt_patch = ' <Patch level="1">%s</Patch>' % patch
patches.append(frmt_patch)
for row, line in enumerate(buf[:]):
if "</Source>" in line:
if "</Patches>" in buf[row - 1]:
for patch in patches:
vim.command("call append(%s, '%s')" % (row - 1, patch))
else:
vim.command("call append(%s, [' <Patches>', ' </Patches>'])" % row)
for patch in patches:
vim.command("call append(%s, '%s')" % (row + 1, patch))
EOF
endfunction
function! ShowDiff()
python << EOF
import vim
cur_filename = vim.eval('@%')
old_filename = ".svn/text-base/%s.svn-base" % cur_filename
vim.command(":vert diffsplit %s" % old_filename)
EOF
endfunction
function! ShowOther()
python << EOF
import vim
cur_filename = vim.eval('@%')
if cur_filename == "pspec.xml":
vim.command(":vs actions.py")
elif cur_filename == "actions.py":
vim.command(":vs pspec.xml")
EOF
endfunction
function! CreateCommit()
python << EOF
import re
import os
import vim
import piksemel
def normal(str):
vim.command("normal "+str)
pspec = piksemel.parse("pspec.xml")
comment_data = pspec.getTag("History").getTag("Update").getTagData("Comment")
file_name = "commit-msg.tmp"
if os.path.exists(file_name):
os.unlink(file_name)
commit_file = open(file_name, "w")
commit_file.write(comment_data)
# \D matches any non-digit chars. we need the bug numbers. so...
bug_comments = [re.sub("\D", "", i) for i in comment_data.split() if "pb#" in i]
if bug_comments:
for i in bug_comments:
commit_file.write("\nBUG:COMMENT:%s" % i)
commit_file.close()
vim.command(":vs commit-msg.tmp")
normal("ggVG=")
EOF
endfunction
function! SvnCommit()
python << EOF
import vim
vim.command(":!svn ci --file commit-msg.tmp")
os.system("rm commit-msg.tmp")
EOF
endfunction