-
Notifications
You must be signed in to change notification settings - Fork 3
/
zpt
executable file
·146 lines (122 loc) · 4.42 KB
/
zpt
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
#!/usr/bin/env python
import os
import sys
from optparse import OptionParser
class ZenPack:
def __init__(self, name, path):
self.name = name
self.path = path
def getName(self):
return self.name
def getRootPath(self):
return self.path
def getPath(self):
return os.path.join(self.path, self.name.replace('.', os.path.sep))
def getShortcutNames(self):
return [
self.getName(),
'.'.join(self.getName().split('.')[1:]),
self.getName().split('.')[-1],
]
class ZenPackTool:
basePaths = []
packs = {}
def addInstalledPacks(self):
packDir = os.path.join(os.environ['ZENHOME'], 'ZenPacks')
try:
easy_install = open(os.path.join(packDir, 'easy-install.pth'), 'r')
except IOError:
return
for line in easy_install:
line = line.strip()
if line.startswith('/'):
packName = line.split(os.path.sep)[-1]
self.packs[packName] = ZenPack(packName, line)
elif line.startswith('./'):
packName = line.split(os.path.sep)[-1]
packName = packName.split('-')[0]
self.packs[packName] = ZenPack(packName,
os.path.join(packDir, line[2:]))
easy_install.close()
def addSourcePath(self, path):
if path in self.basePaths:
return
try:
for f in os.listdir(path):
if f.startswith('ZenPacks.') and not f.endswith('.egg'):
self.packs[f] = ZenPack(f, os.path.join(path, f))
except OSError:
# Skip missing directories.
pass
def getNames(self):
return [ p.getName() for p in self.packs.values() ]
def getShortcutNames(self):
shortcuts = []
for p in self.packs.values():
shortcuts.extend(p.getShortcutNames())
return shortcuts
def getRootPaths(self):
return [ p.getRootPath() for p in self.packs.values() ]
def findPack(self, match):
if match in self.packs:
return self.packs[match]
for name, pack in self.packs.items():
if match in name:
return self.packs[name]
if __name__ == '__main__':
if 'ZENHOME' not in os.environ:
print >> sys.stderr, "ZENHOME is not set."
sys.exit(1)
parser = OptionParser()
parser.add_option('--source', dest='source',
action='append',
help='Base path to look for ZenPacks in.')
parser.add_option('--names', dest='names',
action='store_true', default=False,
help='List all ZenPack names')
parser.add_option('--shortcuts', dest='shortcuts',
action='store_true', default=False,
help='List all ZenPack names and shortcut names')
parser.add_option('--paths', dest='paths',
action='store_true', default=False,
help='List all ZenPack paths')
parser.add_option('--match', dest='match',
help='Name of the ZenPack to get the path for')
parser.add_option('--workdir', dest='workdir',
action='store_true', default=False,
help='Get the working directory of the ZenPack')
parser.add_option('--rootdir', dest='rootdir',
action='store_true', default=False,
help='Get the root directory of the ZenPack')
parser.add_option('--fullname', dest='fullname',
action='store_true', default=False,
help='Get the full ZenPack name instead of path')
options, args = parser.parse_args()
if not options.names and not options.shortcuts and not options.paths:
if not options.match:
print >> sys.stderr, "You must specify the match."
sys.exit(2)
t = ZenPackTool()
t.addInstalledPacks()
for source in options.source or []:
t.addSourcePath(source)
if options.names:
for name in t.getNames():
print name
elif options.shortcuts:
for name in t.getShortcutNames():
print name
elif options.paths:
for path in t.getRootPaths():
print path
elif options.match:
pack = t.findPack(options.match)
if not pack:
print >> sys.stderr, "ZenPack not found."
sys.exit(3)
if options.fullname:
print pack.getName()
elif options.rootdir:
print pack.getRootPath()
else:
print pack.getPath()