-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathansible-install-lister
executable file
·159 lines (140 loc) · 4.95 KB
/
ansible-install-lister
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
#!/usr/bin/env python2
import os
import stat
import sys
import subprocess
import tempfile
from pprint import pprint
SITE_SCRIPTS = [
'''"import site; print(';'.join(site.getsitepackages()))"''',
'''"import distutils.sysconfig; print(distutils.sysconfig.get_python_lib())"'''
]
ANSIBLE_HOME_SCRIPT = '''import ansible; print(ansible.__file__)'''
def run_command(args):
p = subprocess.Popen(args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
(so, se) = p.communicate()
return (p.returncode, so, se)
class AnsibleInstallLister(object):
def __init__(self):
self.paths = self.get_paths()
pprint(self.paths)
self.python_paths = self.get_python_paths()
pprint(self.python_paths)
self.site_packages_paths = self.get_site_packages_paths()
pprint(self.site_packages_paths)
self.ansible_paths = self.get_ansible_paths()
pprint(self.ansible_paths)
self.ansible_homedirs = self.get_ansible_homedirs()
pprint(self.ansible_homedirs)
self.ansible_moduledirs = self.get_ansible_moduledirs()
#import epdb; epdb.st()
def get_paths(self):
"""List user's environment path(s)"""
paths = []
rawpath = os.environ.get('PATH', '')
for rp in rawpath.split(os.pathsep):
path = os.path.expanduser(rp)
paths.append(path)
return paths
def get_python_paths(self):
ppaths = []
for path in self.paths:
if not os.path.exists(path):
continue
pfiles = os.listdir(path)
for pf in pfiles:
if 'pythonw' in pf:
continue
if pf.endswith('-config'):
continue
if pf.endswith('-build'):
continue
if '-' in pf:
continue
if pf.startswith('python'):
ppaths.append(os.path.join(path, pf))
return ppaths
def get_site_packages_paths(self):
site_paths = []
for ppath in self.python_paths:
for SITE_SCRIPT in SITE_SCRIPTS:
cmd = ppath + ' -c %s' % SITE_SCRIPT
(rc, so, se) = run_command(cmd)
if rc != 0:
continue
xpaths = so.split(';')
for xpath in xpaths:
xpath = xpath.strip()
site_paths.append(xpath)
site_paths = sorted(set(site_paths))
return site_paths
def get_ansible_paths(self):
apaths = []
for path in self.paths:
checkfile = os.path.join(path, 'ansible')
if os.path.exists(checkfile):
checkfile = os.path.realpath(checkfile)
apaths.append(checkfile)
return apaths
def get_ansible_homedirs(self):
home_dirs = []
for ap in self.ansible_paths:
shebang = self.read_file_lines(ap)
if not shebang:
continue
shebang = shebang.strip()
script = None
if 'python' in shebang.lower():
script = shebang + '\n' + ANSIBLE_HOME_SCRIPT
elif 'bash' in shebang:
script = self.get_homebrew_script(ap)
if script:
output = self.run_script(script)
if not output:
continue
output = output.strip()
if not output.endswith('.pyc'):
continue
hd = os.path.dirname(output)
home_dirs.append(hd)
home_dirs = sorted(set(home_dirs))
return home_dirs
def get_homebrew_script(self, binpath):
"""homebrew handler ... make assumptions"""
lines = self.read_file_lines(binpath, lines=2)
lines = lines.split('\n')
brewcmd = lines[1].strip()
cparts = brewcmd.split()
PYPATH = [x for x in cparts if 'PYTHONPATH' in x]
if not PYPATH:
return None
PYPATH = PYPATH[0]
script = lines[0] + '\n'
script += PYPATH
script += ' '
script += '/usr/bin/python -c "' + ANSIBLE_HOME_SCRIPT + '"'
return script
def run_script(self, script):
fo, fn = tempfile.mkstemp()
with open(fn, 'wb') as f:
f.write(script)
os.close(fo)
st = os.stat(fn)
os.chmod(fn, st.st_mode | stat.S_IEXEC)
(rc, so, se) = run_command(fn)
os.remove(fn)
return str(so) + str(se)
def read_file_lines(self, filename, lines=1):
data = ''
with open(filename, 'rb') as f:
for x in xrange(0, lines):
data += f.readline()
return data
def get_ansible_moduledirs(self):
#import epdb; epdb.st()
return None
if __name__ == "__main__":
AnsibleInstallLister()