Merge pull request #39806 from cachedout/thin_zip

Support the creation of .zip thin file
This commit is contained in:
Pedro Algarvio 2017-03-04 19:58:08 +00:00 committed by GitHub
commit 5de563bb77
2 changed files with 38 additions and 11 deletions

View file

@ -16,7 +16,8 @@ import salt.utils.thin
def generate(extra_mods='', overwrite=False, so_mods='',
python2_bin='python2', python3_bin='python3', absonly=True):
python2_bin='python2', python3_bin='python3', absonly=True,
compress='gzip'):
'''
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
@ -41,7 +42,8 @@ def generate(extra_mods='', overwrite=False, so_mods='',
so_mods,
python2_bin,
python3_bin,
absonly)
absonly,
compress)
def generate_min(extra_mods='', overwrite=False, so_mods='',

View file

@ -154,7 +154,8 @@ def get_tops(extra_mods='', so_mods=''):
def gen_thin(cachedir, extra_mods='', overwrite=False, so_mods='',
python2_bin='python2', python3_bin='python3', absonly=True):
python2_bin='python2', python3_bin='python3', absonly=True,
compress='gzip'):
'''
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
@ -172,7 +173,11 @@ def gen_thin(cachedir, extra_mods='', overwrite=False, so_mods='',
thindir = os.path.join(cachedir, 'thin')
if not os.path.isdir(thindir):
os.makedirs(thindir)
thintar = os.path.join(thindir, 'thin.tgz')
if compress == 'gzip':
thin_ext = 'tgz'
elif compress == 'zip':
thin_ext = 'zip'
thintar = os.path.join(thindir, 'thin.' + thin_ext)
thinver = os.path.join(thindir, 'version')
pythinver = os.path.join(thindir, '.thin-gen-py-version')
salt_call = os.path.join(thindir, 'salt-call')
@ -261,7 +266,10 @@ def gen_thin(cachedir, extra_mods='', overwrite=False, so_mods='',
except ValueError:
pass
tfp = tarfile.open(thintar, 'w:gz', dereference=True)
if compress == 'gzip':
tfp = tarfile.open(thintar, 'w:gz', dereference=True)
elif compress == 'zip':
tfp = zipfile.ZipFile(thintar, 'w')
try: # cwd may not exist if it was removed but salt was run from it
start_dir = os.getcwd()
except OSError:
@ -285,25 +293,42 @@ def gen_thin(cachedir, extra_mods='', overwrite=False, so_mods='',
if not os.path.isdir(top):
# top is a single file module
if os.path.exists(os.path.join(top_dirname, base)):
tfp.add(base, arcname=os.path.join('py{0}'.format(py_ver), base))
if compress == 'gzip':
tfp.add(base, arcname=os.path.join('py{0}'.format(py_ver), base))
elif compress == 'zip':
tfp.write(base, arcname=os.path.join('py{0}'.format(py_ver), 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),
arcname=os.path.join('py{0}'.format(py_ver), root, name))
if compress == 'gzip':
tfp.add(os.path.join(root, name),
arcname=os.path.join('py{0}'.format(py_ver), root, name))
elif compress == 'zip':
try:
# This is a little slow but there's no clear way to detect duplicates
tfp.getinfo(os.path.join('py{0}'.format(py_ver), root, name))
except KeyError:
tfp.write(os.path.join(root, name), arcname=os.path.join('py{0}'.format(py_ver), root, name))
if tempdir is not None:
shutil.rmtree(tempdir)
tempdir = None
os.chdir(thindir)
tfp.add('salt-call')
if compress == 'gzip':
tfp.add('salt-call')
elif compress == 'zip':
tfp.write('salt-call')
with salt.utils.fopen(thinver, 'w+') as fp_:
fp_.write(salt.version.__version__)
with salt.utils.fopen(pythinver, 'w+') as fp_:
fp_.write(str(sys.version_info[0]))
os.chdir(os.path.dirname(thinver))
tfp.add('version')
tfp.add('.thin-gen-py-version')
if compress == 'gzip':
tfp.add('version')
tfp.add('.thin-gen-py-version')
elif compress == 'zip':
tfp.write('version')
tfp.write('.thin-gen-py-version')
if start_dir:
os.chdir(start_dir)
tfp.close()