-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundlelinker.py
executable file
·334 lines (279 loc) · 9.57 KB
/
bundlelinker.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
"""
bundlelinker
=============
Make ``./bundle`` include symlinks to vim bundles in ``./bundles.all``
according to the bundles listed in ``./Bundlefile``.
Use Cases:
- Isolate which plugin(s) are causing performance issues
- Create plugin subsets
Usage:
.. code:: bash
./bundlelinker.py --help
cd ~/.vim
./bundlelinker.py
./bundlelinker.py -y
# These are equivalent:
./bundlelinker.py -p ~/.vim
./bundlelinker.py -f ~/.vim/Bundlefile --all ~/.vim/bundles.all -b ~/.vim/bundle
"""
import itertools
import json
import logging
import os
import shutil
import sys
import unittest
from pathlib import Path
class Tasks:
SYMLINK = "symlink"
MKDIR = "mkdir"
RM = "remove"
INSTALL = "install"
def generate_bundle_sets(
*, bundle_dir, bundles_all_dir, bundlefile,
):
"""
Keyword Arguments:
bundle_dir (str or Path): directory to create symlinks in
(e.g. ./bundle)
bundles_all_dir (str or Path): where bundles are installed
(e.g. ./bundles.all)
bundlefile (str or Path): ./bundlefile (e.g. Bundlefile)
Returns:
dict: bundles, grouped tasks, and tasks as a list
"""
log = logging.getLogger()
ctx = {}
ctx["bundlefile_entries"] = []
ctx["bundles_all"] = []
ctx["installed"] = []
ctx["unnecessarily_installed"] = None
ctx["symlink_these"] = []
ctx["install_and_symlink_these"] = []
ctx["tasks"] = []
_bundlefile = Path(bundlefile)
if not _bundlefile.exists():
raise Exception(f"bundlefile does not exist: {_bundlefile!r}")
with open(_bundlefile, "r") as f:
ctx["bundlefile_entries"] = f.read().splitlines()
_bundles_all_dir = Path(bundles_all_dir)
if not _bundles_all_dir.exists():
err = f"bundles_all_dir does not exist: {_bundles_all_dir!r}"
log.info(err)
# raise Exception(err)
else:
ctx["bundles_all"] = list(Path(_bundles_all_dir).iterdir())
_bundle_dir = Path(bundle_dir)
if not _bundle_dir.exists():
err = f"bundle_dir does not exist: {_bundle_dir!r}"
log.info(err)
# raise Exception(err)
else:
ctx["installed"] = list(Path(_bundle_dir).iterdir())
installed_names = {f.name: f for f in ctx["installed"]}
bundles_all_names = {f.name: f for f in ctx["bundles_all"]}
for bundle_name in ctx["bundlefile_entries"]:
if bundle_name not in installed_names:
_task = dict(
bundle_dir_path=_bundle_dir / bundle_name,
bundles_all_path=_bundles_all_dir / bundle_name,
)
if bundle_name in bundles_all_names:
task = dict(task=Tasks.SYMLINK, bundle_name=bundle_name)
ctx["symlink_these"].append(task)
else:
task = dict(task=Tasks.INSTALL, bundle_name=bundle_name)
ctx["install_and_symlink_these"].append(task)
task.update(_task)
ctx["tasks"].append(task)
ctx["unnecessarily_installed"] = [
(_bundle_dir / bundlepath.name)
for bundlepath in ctx["installed"]
if bundlepath.name not in ctx["bundlefile_entries"]
]
return ctx
def to_json(obj):
return json.dumps(obj, default=str, indent=2)
def arrange_bundles(
*, bundle_dir, bundles_all_dir, bundlefile, make_changes_to_fs=False
):
"""create symlinks in order to run a subset of vim bundles
Keyword Arguments:
bundle_dir (str): ./bundle
bundles_all_dir (str): ./bundles.all
bundlefile (str): ./Bundlefile
make_changes_to_fs (bool): whether to make changes
Returns:
int: number of changes made or how many changes would be made
"""
_bundles_all_dir = Path(bundles_all_dir)
_bundle_dir = Path(bundle_dir)
_bundlefile = Path(bundlefile)
bundlesets = generate_bundle_sets(
bundle_dir=_bundle_dir,
bundles_all_dir=_bundles_all_dir,
bundlefile=_bundlefile,
)
tasks = []
if not _bundle_dir.exists():
tasks.append(dict(action=Tasks.MKDIR, path=bundle_dir))
# tasks = itertools.chain(tasks, bundlesets["tasks"])
tasks.extend(bundlesets["tasks"])
log = logging.getLogger()
log.debug("# Bundlesets and Tasks")
log.debug(to_json(bundlesets))
log.info("# Tasks")
log.info(to_json(bundlesets["tasks"]))
to_install = []
changes_made = []
if make_changes_to_fs:
for task in tasks:
log.debug(task)
taskname = task.get("task")
if taskname == Tasks.MKDIR:
log.info(f"mkdir {task['path']!r}")
os.mkdir(task["path"])
changes_made.append(task)
elif taskname == Tasks.SYMLINK:
log.info(
f"symlink {task['bundle_dir_path']!r} to {task['bundles_all_path']!r}"
)
os.symlink(
".." / task["bundles_all_path"], task["bundle_dir_path"]
)
changes_made.append(task)
elif taskname == Tasks.RM:
log.info(f"rm {task['path']}")
shutil.rmtree(task["path"])
changes_made.append(task)
elif taskname == Tasks.INSTALL:
to_install.append(taskname)
log.info(
f"{task['bundles_all_path']!r} does not exist.\n"
"Install it manually or with a Bundle in a vimrc."
)
for task in bundlesets["install_and_symlink_these"]:
log.info(
f"{task['bundles_all_path']!r} does not exist.\n"
"Install it manually or with a Bundle in a vimrc."
)
return dict(
tasks=tasks,
changes_made=changes_made,
bundlesets=bundlesets,
to_install=to_install,
)
class Test_bundlelinker(unittest.TestCase):
def setUp(self):
here = Path(__file__).parent
self.args = dict(
bundle_dir=here / "bundle",
bundles_all_dir=here / "bundles.all",
bundlefile=here / "Bundlefile",
)
def test_arrange_bundles(self):
output = arrange_bundles(**self.args)
assert "tasks" in output
assert "changes_made" in output
assert "to_install" in output
assert len(output["changes_made"]) == 0
# def test_arrange_bundles_and_modify(self):
# output = arrange_bundles(make_changes_to_fs=True, **self.args)
def main(argv=None):
"""
bundlelinker main function
Keyword Arguments:
argv (list): commandline arguments (e.g. sys.argv[1:])
Returns:
int:
"""
import logging
import optparse
prs = optparse.OptionParser(
usage="%prog [-v] [-y] [-p <prefix>] [-b <path>] [--all <path>] [-f <Bundlefile>]"
)
prs.add_option("-p", "--prefix", dest="prefix", default="~/.vim")
prs.add_option("--all", dest="bundles_all_dir", help="bundles.all/")
prs.add_option("-b", dest="bundle_dir", help="bundle/")
prs.add_option(
"-f", dest="bundlefile", help="Bundlefile",
)
prs.add_option(
"-y",
"--yes",
"--make-changes",
dest="make_changes_to_fs",
action="store_true",
help="make changes to bundle_dir",
)
prs.add_option(
"--Pu",
"--print-unnecessarily-installed",
dest="print_unnecessarily_installed",
action="store_true",
help="Print things in bundle_dir that aren't in Bundlefile",
)
prs.add_option(
"-v", "--verbose", dest="verbose", action="store_true",
)
prs.add_option(
"-q", "--quiet", dest="quiet", action="store_true",
)
prs.add_option(
"-t", "--test", dest="run_tests", action="store_true",
)
argv = list(argv) if argv else []
(opts, args) = prs.parse_args(args=argv)
loglevel = logging.INFO
if opts.verbose:
loglevel = logging.DEBUG
elif opts.quiet:
loglevel = logging.ERROR
logging.basicConfig(level=loglevel)
log = logging.getLogger("main")
log.debug("argv: %r", argv)
log.debug("opts: %r", opts)
log.debug("args: %r", args)
if opts.run_tests:
sys.argv = [sys.argv[0]] + args
return unittest.main()
EX_OK = 0
if opts.prefix:
prefix = Path(os.path.expanduser(opts.prefix) if opts.prefix else ".")
opts.bundle_dir = (
Path(opts.bundle_dir) if opts.bundle_dir else prefix / "bundle"
)
opts.bundles_all_dir = (
Path(opts.bundles_all_dir)
if opts.bundles_all_dir
else prefix / "bundles.all"
)
opts.bundlefile = (
Path(opts.bundlefile) if opts.bundlefile else prefix / "Bundlefile"
)
log.debug("opts: %r", opts)
output = arrange_bundles(
bundle_dir=opts.bundle_dir,
bundles_all_dir=opts.bundles_all_dir,
bundlefile=opts.bundlefile,
make_changes_to_fs=opts.make_changes_to_fs,
)
if opts.print_unnecessarily_installed:
for pth in output["bundlesets"]["unnecessarily_installed"]:
print(pth)
else:
log.info(f"Done.")
log.info(f"Plan: {len(output['tasks'])} changes.")
log.info(
f"Made: {len(output['changes_made'])} changes. "
"(-y to make changes)"
)
to_install_count = len(output["to_install"])
if to_install_count:
log.info(f"To manually install: {to_install_count}. See logs")
return EX_OK
if __name__ == "__main__":
sys.exit(main(argv=sys.argv[1:]))