-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsmgr
executable file
·398 lines (340 loc) · 13.4 KB
/
csmgr
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python
#
# Copyright 2018 Rick Chang <chchang915@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import sys, os, subprocess, shutil, time, argparse, re, shlex, itertools, collections
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import SafeConfigParser as ConfigParser
RED = "\x1b[31m"
GREEN = "\x1b[32m"
YELLOW = "\x1b[33m"
NONE = "\x1b[0m"
CONFIG = {
'local_config_path': '.csmgr.config',
'config_path': '~/.csmgr.config',
'project_list': '.csmgr.project',
'include_files': '',
'suffixes': '.c .h .js .cpp .py .scss .css .java',
'out_list': 'cscope.files',
'meta_files': 'cscope.in.out cscope.out cscope.po.out tags',
'exclude_dirs': '.git node_modules',
'exec_cmds': 'cscope -bqk -i $out_list && ctags -L $out_list',
'max_display': 20,
'delim': "-" * 80,
'delim_end': '=' * 80,
}
def Log(s, endPat=None):
print(s, end=endPat)
sys.stdout.flush()
def Loge(s):
Log(RED + s + NONE)
def Logm(s, endPat=None):
Log(GREEN + s + NONE, endPat)
def Logw(s):
Log(YELLOW + s + NONE)
def Logv(s, verbose):
if verbose:
Log(s)
class FakeSection(object):
SECTION = 'fake section'
def __init__(self, f, name):
self.f = f
self.first = True
self.sectionName = "[%s]\n" % name
def readline(self):
if self.first:
self.first = False
return self.sectionName
return self.f.readline()
def __iter__(self):
if self.first:
self.first = False
yield self.sectionName
line = self.f.readline()
while line:
yield line
line = self.f.readline()
class FileListMgr:
def __init__(self, init_list, opts):
self.list_count = collections.Counter(init_list)
self.opts = opts
self.addCount = 0
self.rmCount = 0
self.showCount = 0
def IsExclude(self, path):
for exclude_dir in self.opts.exclude_dirs:
if path.startswith(exclude_dir):
return True
return False
def SearchAndUpdate(self, pathes):
self.showCount = 0
for path in pathes:
if os.path.isfile(path):
self.FilterFile(path, False)
elif os.path.isdir(path):
self.FilterDir(path)
for key, val in self.list_count.items():
if val == 1:
self.Show('Remove %s' % key)
self.rmCount += 1
def GetFileList(self):
cur_list = []
for key, val in self.list_count.items():
if val != 1:
cur_list.append(key)
return sorted(cur_list)
def Show(self, s):
if self.showCount > self.opts.max_display:
return
Log('%s' % '...' if self.showCount == self.opts.max_display else s)
self.showCount += 1
def FilterFile(self, path, suffixCheck=True):
if os.path.islink(path):
return
if suffixCheck and not path.endswith(tuple(self.opts.suffixes)):
return
rel_path = os.path.relpath(path)
list_count = self.list_count
if rel_path in list_count:
if list_count[rel_path]:
list_count[rel_path] = 0
return
self.Show('Add %s' % rel_path)
list_count[rel_path] = 0
self.addCount += 1
def FilterDir(self, folder):
for i, (path, dirs, files) in enumerate(os.walk(folder)):
for f in files:
if self.IsExclude(os.path.relpath(path, folder)):
continue;
full_path = os.path.join(path, f)
self.FilterFile(full_path, f not in self.opts.include_files)
def TaskDecorator(start_msg=None, end_msg='[done]'):
def decorator(func):
def wrapper(*args, **kwargs):
Log(CONFIG['delim_end'])
if start_msg:
Log(start_msg)
Log(CONFIG['delim'])
start_time = time.time()
ret = func(*args, **kwargs)
elapsed_time = time.time() - start_time
Log("%s (%ss)" % (end_msg, round(elapsed_time, 3)))
return ret
return wrapper
return decorator
def ReadConfig(config, fp):
if hasattr(config, 'read_file'):
config.read_file(fp)
else:
config.readfp(fp)
def InitConfig(path, silent=True):
config_file = os.path.expanduser(path)
if not os.path.isfile(config_file):
if not silent:
Loge("Can't find config file '%s'" % config_file)
return False
config = ConfigParser()
ReadConfig(config, FakeSection(open(config_file), FakeSection.SECTION))
for key in CONFIG:
if not config.has_option(FakeSection.SECTION, key):
continue
val = config.get(FakeSection.SECTION, key)
CONFIG[key] = val
return True
def WriteConfig(path, settings):
config_file = os.path.expanduser(path)
with open(config_file, 'a') as f:
pass
config = ConfigParser()
ReadConfig(config, FakeSection(open(config_file), FakeSection.SECTION))
for key, val in settings.items():
config.set(FakeSection.SECTION, key, val)
with open(config_file, 'w') as f:
for key, val in config.items(FakeSection.SECTION):
f.write("%s = %s\n" % (key, val))
def SetConfig(opts):
if opts.config_file and not InitConfig(opts.config_file, False):
sys.exit(1)
config = vars(opts)
for key, val in CONFIG.items():
if key in config and config[key] is not None:
continue
if key in ['include_files', 'suffixes', 'meta_files', 'exclude_dirs']:
val = re.split('\s+', val)
elif key in ['max_display']:
val = int(val)
config[key] = val
config['exec_cmds'] = config['exec_cmds'].split('&&');
config['exclude_dirs'] = list(map(lambda x: x.rstrip('/'), config['exclude_dirs']))
return opts
def DeleteMeta(opts):
if opts.dry_run:
Log('(dryrun) Remove %s' % ' '.join(opts.meta_files))
return 0
Log('Remove %s' % ' '.join(opts.meta_files))
for f in opts.meta_files:
try:
os.remove(f)
except:
pass
return 0
@TaskDecorator()
def Run(opts, cmd):
Log('Run %s ...' % cmd)
Log(opts.delim)
if not cmd:
return
if opts.dry_run:
Log('(dryrun) %s' % cmd)
return
try:
return subprocess.call(tuple(shlex.split(cmd)))
except Exception as e:
Loge("Run cmd '%s' fail. (%s)" % (cmd, e))
return 1
def UpdateTag(opts, regen=False):
if regen:
DeleteMeta(opts)
for cmd in opts.exec_cmds:
cmd = cmd.replace('$out_list', opts.out_list).strip()
Run(opts, cmd)
return 0
def SaveFileList(opts, filename, filelist):
with open(filename, 'w') as f:
for path in filelist:
f.write(path + "\n")
def GetProjectPath(path):
rel_path = os.path.relpath(path)
if rel_path == '.':
return './'
return rel_path if rel_path[0] == '.' else './' + rel_path
def GetProjectList(opts):
pathes = LoadFileList(opts.project_list)
if opts.update:
return pathes
for path in opts.path:
if not os.path.exists(path):
Loge("'" + path + "' not exited.")
sys.exit(1)
valid_pathes = set([GetProjectPath(path) for path in opts.path])
for path1 in pathes:
for path2 in list(valid_pathes):
isRedundent = False
if os.path.isfile(path2):
if path1 == path2:
isRedundent = True
elif path2.startswith(path1):
isRedundent = True
if isRedundent:
Logw('Redundent path %s (under %s)' % (path2, path1))
valid_pathes.remove(path2)
for path in valid_pathes:
Log('%sAdded path \'%s\' to project list' % ('(dryrun) ' if opts.dry_run else '', path))
if not valid_pathes:
Logw('Nothing to update.')
sys.exit(1)
new_list = sorted(itertools.chain(pathes, valid_pathes))
if not opts.dry_run:
SaveFileList(opts, opts.project_list, new_list)
return new_list
@TaskDecorator('Checking file list ...')
def UpdateFileList(opts, project_list):
fileMgr = FileListMgr(LoadFileList(opts.out_list), opts)
fileMgr.SearchAndUpdate(project_list)
if fileMgr.addCount:
Logm('%sAdded %d files to %s' % ('(dryrun) ' if opts.dry_run else '', fileMgr.addCount, opts.out_list))
if fileMgr.rmCount:
Logm('%sRemoved %d files from %s' % ('(dryrun) ' if opts.dry_run else '', fileMgr.rmCount, opts.out_list))
if fileMgr.rmCount == 0 and fileMgr.addCount == 0 and not opts.update:
Log('Nothing to update.')
return False
file_list = fileMgr.GetFileList()
if not file_list:
Logw('Can\'t find any file')
if not opts.dry_run:
SaveFileList(opts, opts.out_list, file_list)
return file_list
def LoadFileList(name):
if not os.path.isfile(name):
return []
with open(name, 'r') as f:
return f.read().splitlines()
class StrAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, re.split('\s+', values))
def ParseArguments(argv):
parser = argparse.ArgumentParser()
parser.add_argument('path', nargs='*', default=None,
help = 'add path to project list (%s) and run commands for tag generation if %s is changed ex. dir/, file.c' % (CONFIG['project_list'], CONFIG['out_list']))
parser.add_argument('-u', '--update', action='store_true', default=False,
help='Update %s if necessary and run commands for tag generation' % CONFIG['out_list'])
parser.add_argument('-f', '--force', action='store_true', default=False,
help='delete meta data and run commands for tag generation')
parser.add_argument('-p', '--project-list', type=str, default=None,
help='assign project list (default: %s)' % CONFIG['project_list'])
parser.add_argument('-s', '--suffixes', action=StrAction, type=str, default=None,
help='assign suffixes filter (default: %s)' % CONFIG['suffixes'])
parser.add_argument('-i', '--include-files', action=StrAction, type=str, default=None,
help='as#clude files (default: %s)' % CONFIG['include_files'])
parser.add_argument('-c', '--config-file', type=str, default=None,
help='assign config file (default: %s, %s)' % (CONFIG['config_path'], CONFIG['local_config_path']))
parser.add_argument('-o', '--out-list', type=str, default=None,
help='assign the name of output list file (default: %s)' % CONFIG['out_list'])
parser.add_argument('-m', '--meta-files', action=StrAction, type=str, default=None,
help='assign meta data files (default: %s)' % CONFIG['meta_files'])
parser.add_argument('-e', '--exclude-dirs', action=StrAction, type=str, default=None,
help='assign exclude dirs (default: %s)' % CONFIG['exclude_dirs'])
parser.add_argument('-d', '--delete-meta', action='store_true', default=False,
help='delete all meta data')
parser.add_argument('--dry-run', action='store_true', default=False,
help='show what would be done')
parser.add_argument('--max-display', type=int,
help='assign how many paths will be shown in the log')
parser.add_argument('-x', '--exec-cmds', type=str, default=None,
help='assign commands for tag generation. $out_list will be replaced by list file name (check -o for details). Use && concat multiple commands (default: %s)' % CONFIG['exec_cmds'])
parser.add_argument('--verbose', action='store_true', default=False,
help='show more logs')
parser.add_argument('-v', '--version', action='version', version='1.1.2')
opts = parser.parse_args(argv[1:])
if not opts.project_list and not opts.force and not opts.delete_meta and not bool(opts.update) ^ bool(opts.path):
parser.print_help()
sys.exit(1)
opts = SetConfig(opts)
Logv(opts, opts.verbose)
return opts, parser
def main(argv):
InitConfig(CONFIG['config_path'])
InitConfig(CONFIG['local_config_path'])
opts, parser = ParseArguments(argv)
if opts.delete_meta:
return DeleteMeta(opts)
if opts.force:
return UpdateTag(opts, True)
if opts.project_list != CONFIG['project_list']:
Log('Switch to project list %s' % opts.project_list)
WriteConfig(CONFIG['local_config_path'], {'project_list': opts.project_list})
DeleteMeta(opts)
else:
Log("project list: %s" % opts.project_list)
if not opts.update and not opts.path:
return 0
if UpdateFileList(opts, GetProjectList(opts)):
return UpdateTag(opts)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))