-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackageinfo.py
157 lines (125 loc) · 4.83 KB
/
packageinfo.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
#!/usr/bin/env python
# coding: utf-8
import os
def shift_path(top, dirpath, filename):
def _shift_path(dir, file):
if dir == top:
return dir, file
else:
head, tail = os.path.split(dir)
return _shift_path(head, os.path.join(tail, file))
assert os.path.isabs(top)
assert os.path.isabs(dirpath)
return _shift_path(dirpath, filename)
def split_all(relpath):
def _split_all(relpath):
(head, tail) = os.path.split(relpath)
if head == '':
return [tail]
else:
return split_all(head) + [tail]
assert not os.path.isabs(relpath)
return _split_all(relpath)
class PackageInfo:
class ContainAny:
""" Always returns True for any filepath. """
def __call__(self, f):
return True
class ContainSuffix:
""" Returns True if filepath ends with given suffix. """
def __init__(self, suffix):
self.suffix = suffix
def __call__(self, f):
return f.endswith(self.suffix)
class ContainSpecialDirs:
""" Returns True if filepath is under the special directory. """
def __call__(self, f):
dirs = split_all(f)
return dirs and dirs[0] != 'after' and PackageInfo.SPECIAL_DIR_RULES.has_key(dirs[0])
SPECIAL_DIR_RULES = {
'after': ContainSpecialDirs(),
'autoload': ContainSuffix('.vim'),
'colors': ContainSuffix('.vim'),
'compiler': ContainSuffix('.vim'),
# 'dict': ContainAny(),
'doc': ContainAny(),
'ftdetect': ContainSuffix('.vim'),
'ftplugin': ContainSuffix('.vim'),
'indent': ContainSuffix('.vim'),
'keymap': ContainAny(),
'lang': ContainAny(),
'macros': ContainAny(),
'plugin': ContainSuffix('.vim'),
'spell': ContainAny(),
'syntax': ContainSuffix('.vim'),
# 'tools': ContainAny(),
# 'tutor': ContainAny(),
}
SPECIAL_DIRS = SPECIAL_DIR_RULES.keys()
def __init__(self, dir):
self.dir = dir
def files(self):
""" Utility method to get all files under self.dir. """
for dirpath, dirnames, filenames in os.walk(self.dir):
for f in filenames:
yield os.path.join(dirpath, f)
def dirs(self):
""" Utility method to get all directories under self.dir. """
for dirpath, dirnames, filenames in os.walk(self.dir):
for d in dirnames:
yield os.path.join(dirpath, d)
def special_dirs(self):
""" Returns all PackageInfo.SPECIAL_DIRS in self.dir. """
return (d
for d in PackageInfo.SPECIAL_DIRS
if os.path.isdir(os.path.join(self.dir, d)))
def is_special_dir(self, dir):
""" Returns boolean value if dir is in PackageInfo.SPECIAL_DIR_RULES. """
return PackageInfo.SPECIAL_DIR_RULES.has_key(dir)
def is_necessary(self, relpath):
""" Returns boolean value if relpath is necessary file. """
assert not os.path.isabs(relpath)
if not os.path.isfile(os.path.join(self.dir, relpath)):
return False
dirs = split_all(relpath)
if len(dirs) <= 1:
return False
d, f = dirs[0], apply(os.path.join, dirs[1:])
if PackageInfo.SPECIAL_DIR_RULES.has_key(d):
return PackageInfo.SPECIAL_DIR_RULES[d](f)
else:
return False
def necessary_files(self):
""" Returns all necessary files. """
for dirpath, _, filenames in os.walk(self.dir):
for f in filenames:
_, f = shift_path(self.dir, dirpath, f)
if self.is_necessary(f):
yield f
def unnecessary_files(self):
""" Returns all unnecessary files. """
for dirpath, _, filenames in os.walk(self.dir):
for f in filenames:
_, f = shift_path(self.dir, dirpath, f)
if not self.is_necessary(f):
yield f
def main():
dir = os.path.join(os.environ['HOME'], ".vim")
if __debug__:
print "\n\n\n---------------------- .special_dirs() ----------------------"
for d in PackageInfo(dir).special_dirs():
print d
print "\n\n\n---------------------- .necessary_files() ----------------------"
for f in PackageInfo(dir).necessary_files():
print f
print "\n\n\n---------------------- .unnecessary_files() ----------------------"
for f in PackageInfo(dir).unnecessary_files():
print f
print "\n\n\n---------------------- .dirs() ----------------------"
for d in PackageInfo(dir).dirs():
print d
print "\n\n\n---------------------- .files() ----------------------"
for f in PackageInfo(dir).files():
print f
if __name__ == '__main__':
main()