forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_commands.py
94 lines (79 loc) · 2.62 KB
/
find_commands.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
from __future__ import print_function, division, absolute_import
import re
import os
import sys
import subprocess
from os.path import isdir, isfile, join, expanduser
from conda.utils import memoized
def find_executable(executable, include_others=True):
# backwards compatibility
global dir_paths
if include_others:
if sys.platform == 'win32':
dir_paths = [join(sys.prefix, 'Scripts'),
'C:\\cygwin\\bin']
else:
dir_paths = [join(sys.prefix, 'bin')]
else:
dir_paths = []
dir_paths.extend(os.environ['PATH'].split(os.pathsep))
for dir_path in dir_paths:
if sys.platform == 'win32':
for ext in '.exe', '.bat', '':
path = join(dir_path, executable + ext)
if isfile(path):
return path
else:
path = join(dir_path, executable)
if isfile(expanduser(path)):
return expanduser(path)
return None
@memoized
def find_commands(include_others=True):
if include_others:
if sys.platform == 'win32':
dir_paths = [join(sys.prefix, 'Scripts'),
'C:\\cygwin\\bin']
else:
dir_paths = [join(sys.prefix, 'bin')]
else:
dir_paths = []
if sys.platform == 'win32':
pat = re.compile(r'conda-(\w+)\.(exe|bat)$')
else:
pat = re.compile(r'conda-(\w+)$')
res = set()
for dir_path in dir_paths:
if not isdir(dir_path):
continue
for fn in os.listdir(dir_path):
if not isfile(join(dir_path, fn)):
continue
m = pat.match(fn)
if m:
res.add(m.group(1))
return sorted(res)
def filter_descr(cmd):
args = [find_executable('conda-' + cmd), '--help']
if not args[0]:
print('failed: %s (could not find executable)' % (cmd))
return
try:
output = subprocess.check_output(args)
except (OSError, subprocess.CalledProcessError):
print('failed: %s' % (' '.join(args)))
return
pat = re.compile(r'(\r?\n){2}(.*?)(\r?\n){2}', re.DOTALL)
m = pat.search(output.decode('utf-8'))
descr = ['<could not extract description>'] if m is None else m.group(2).split('\n')
# XXX: using some stuff from textwrap would be better here, as it gets
# longer than 80 characters
print(' %-12s %s' % (cmd, descr[0]))
for d in descr[1:]:
print(' %s' % d)
def help():
print("\nexternal commands:")
for cmd in find_commands():
filter_descr(cmd)
if __name__ == '__main__':
help()