forked from agentultra/openstack-guest-agents-unix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_modules.py
executable file
·162 lines (139 loc) · 4.89 KB
/
install_modules.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
158
159
160
161
162
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (c) 2011 Openstack, LLC.
# All Rights Reserved.
#
# 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.
#
import os
import shutil
import re
import sys
import zipfile
import commands.command_list
# Other modules here that get lazy loaded.. :-/
import bz2
import gzip
import httplib
import zlib
# Make sure we get at least one of these
try:
import anyjson
except Exception:
pass
try:
import json
except Exception:
pass
try:
import simplejson
except Exception:
pass
def install_modules(system_paths, installdir):
c = commands.init(testmode=True)
to_install = set()
def copy_tree(srcdir, destdir):
if not os.path.exists(destdir):
os.mkdir(destdir)
for root, dirs, files in os.walk(srcdir):
for d in dirs:
if not os.path.exists(os.path.join(destdir, d)):
os.mkdir(os.path.join(destdir, d))
d = destdir + root[len(srcdir):]
if not os.path.exists(d):
os.mkdir(d)
for f in files:
# Only install .pyc or .sos, etc
if not f.endswith('.py'):
fname = os.path.join(d, f)
shutil.copy2(os.path.join(root, f), fname)
def _do_install(src, destdir, subdirs_only=False):
print "Installing %s" % src
if os.path.isdir(src):
if not subdirs_only:
subdir = src.rsplit('/', 1)[1]
copy_tree(src, os.path.join(destdir, subdir))
return
for d in os.listdir(src):
if d == "EGG-INFO":
continue
path = os.path.join(src, d)
if os.path.isdir(path):
copy_tree(path, os.path.join(destdir, d))
else:
shutil.copy2(path, destdir)
else:
shutil.copy2(src, destdir)
for modname in sys.modules:
if modname == "__main__":
continue
try:
mod_fn = sys.modules[modname].__file__
except:
continue
mod_fn = os.path.normpath(mod_fn)
base_dir = ''
for p in system_paths:
p_len = len(p)
if mod_fn.startswith(p) and p > len(base_dir):
base_dir = p
# Only install modules that are in the system paths. We install
# our command modules separately.
if base_dir:
# Turn /usr/lib/python2.6/Crypto/Cipher/AES into:
# /usr/lib/python2.6/Crypto
rest_dir = mod_fn[len(base_dir) + 1:]
if '/' in rest_dir:
rest_dir = rest_dir.split('/', 1)[0]
if base_dir.endswith('site-packages'):
idir = installdir + '/site-packages'
else:
idir = installdir
if re.match('.*\.egg', rest_dir):
full_srcdir = os.path.join(base_dir, rest_dir)
if os.path.isdir(full_srcdir):
_do_install(os.path.join(base_dir, rest_dir),
idir, True)
else:
z = zipfile.ZipFile(full_srcdir)
files = z.infolist()
for f in files:
if f.filename == "EGG-INFO" or \
f.filename.startswith("EGG-INFO/"):
continue
z.extract(f, idir)
z.close()
else:
_do_install(os.path.join(base_dir, rest_dir),
idir)
if __name__ == "__main__":
prog_name = sys.argv[0]
if len(sys.argv) != 2:
print "Usage: %s <install_dir>" % prog_name
sys.exit(1)
installdir = sys.argv[1]
sys_paths = sys.path
# Pop off the first directory, which is the directory of this script.
# We do this so we can ignore *our* modules, which are installed
# separately
sys_paths.pop(0)
if not os.path.exists(installdir):
os.makedirs(installdir)
if not os.path.exists(installdir + '/site-packages'):
os.mkdir(installdir + '/site-packages')
if not os.path.isdir(installdir + '/site-packages'):
print "Error: '%s/site-packages' exists and is not a directory" % \
installdir
sys.exit(1)
install_modules(sys_paths, installdir)