-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy paththin.py
180 lines (159 loc) · 5.45 KB
/
thin.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# -*- coding: utf-8 -*-
'''
Generate the salt thin tarball from the installed python files
'''
# Import python libs
from __future__ import absolute_import
import os
import shutil
import tarfile
import zipfile
import tempfile
# Import third party libs
import jinja2
import yaml
import salt.ext.six as six
import tornado
# pylint: disable=import-error,no-name-in-module
try:
import certifi
HAS_CERTIFI = True
except ImportError:
HAS_CERTIFI = False
try:
import markupsafe
HAS_MARKUPSAFE = True
except ImportError:
# Older jinja does not need markupsafe
HAS_MARKUPSAFE = False
# pylint: enable=import-error,no-name-in-module
try:
# Older python where the backport from pypi is installed
from backports import ssl_match_hostname
HAS_SSL_MATCH_HOSTNAME = True
except ImportError:
# Other older python we use our bundled copy
try:
from salt.ext import ssl_match_hostname
HAS_SSL_MATCH_HOSTNAME = True
except ImportError:
HAS_SSL_MATCH_HOSTNAME = False
# Import salt libs
import salt
import salt.utils
SALTCALL = '''
from salt.scripts import salt_call
if __name__ == '__main__':
salt_call()
'''
def thin_path(cachedir):
'''
Return the path to the thin tarball
'''
return os.path.join(cachedir, 'thin', 'thin.tgz')
def gen_thin(cachedir, extra_mods='', overwrite=False, so_mods=''):
'''
Generate the salt-thin tarball and print the location of the tarball
Optional additional mods to include (e.g. mako) can be supplied as a comma
delimited string. Permits forcing an overwrite of the output file as well.
CLI Example:
.. code-block:: bash
salt-run thin.generate
salt-run thin.generate mako
salt-run thin.generate mako,wempy 1
salt-run thin.generate overwrite=1
'''
thindir = os.path.join(cachedir, 'thin')
if not os.path.isdir(thindir):
os.makedirs(thindir)
thintar = os.path.join(thindir, 'thin.tgz')
thinver = os.path.join(thindir, 'version')
salt_call = os.path.join(thindir, 'salt-call')
with salt.utils.fopen(salt_call, 'w+') as fp_:
fp_.write(SALTCALL)
if os.path.isfile(thintar):
with salt.utils.fopen(thinver) as fh_:
if overwrite or not os.path.isfile(thinver):
try:
os.remove(thintar)
except OSError:
pass
elif fh_.read() == salt.version.__version__:
return thintar
tops = [
os.path.dirname(salt.__file__),
os.path.dirname(jinja2.__file__),
os.path.dirname(yaml.__file__),
os.path.dirname(tornado.__file__),
]
tops.append(six.__file__.replace('.pyc', '.py'))
if HAS_CERTIFI:
tops.append(os.path.dirname(certifi.__file__))
if HAS_SSL_MATCH_HOSTNAME:
tops.append(os.path.dirname(os.path.dirname(ssl_match_hostname.__file__)))
for mod in [m for m in extra_mods.split(',') if m]:
if mod not in locals() and mod not in globals():
try:
locals()[mod] = __import__(mod)
moddir, modname = os.path.split(locals()[mod].__file__)
base, ext = os.path.splitext(modname)
if base == '__init__':
tops.append(moddir)
else:
tops.append(os.path.join(moddir, base + '.py'))
except ImportError:
# Not entirely sure this is the right thing, but the only
# options seem to be 1) fail, 2) spew errors, or 3) pass.
# Nothing else in here spits errors, and the markupsafe code
# doesn't bail on import failure, so I followed that lead.
# And of course, any other failure still S/T's.
pass
for mod in [m for m in so_mods.split(',') if m]:
try:
locals()[mod] = __import__(mod)
tops.append(locals()[mod].__file__)
except ImportError:
pass # As per comment above
if HAS_MARKUPSAFE:
tops.append(os.path.dirname(markupsafe.__file__))
tfp = tarfile.open(thintar, 'w:gz', dereference=True)
start_dir = os.getcwd()
tempdir = None
for top in tops:
base = os.path.basename(top)
top_dirname = os.path.dirname(top)
if os.path.isdir(top_dirname):
os.chdir(top_dirname)
else:
# This is likely a compressed python .egg
tempdir = tempfile.mkdtemp()
egg = zipfile.ZipFile(top_dirname)
egg.extractall(tempdir)
top = os.path.join(tempdir, base)
os.chdir(tempdir)
if not os.path.isdir(top):
# top is a single file module
tfp.add(base)
continue
for root, dirs, files in os.walk(base, followlinks=True):
for name in files:
if not name.endswith(('.pyc', '.pyo')):
tfp.add(os.path.join(root, name))
if tempdir is not None:
shutil.rmtree(tempdir)
tempdir = None
os.chdir(thindir)
tfp.add('salt-call')
with salt.utils.fopen(thinver, 'w+') as fp_:
fp_.write(salt.version.__version__)
os.chdir(os.path.dirname(thinver))
tfp.add('version')
os.chdir(start_dir)
tfp.close()
return thintar
def thin_sum(cachedir, form='sha1'):
'''
Return the checksum of the current thin tarball
'''
thintar = gen_thin(cachedir)
return salt.utils.get_hash(thintar, form)