Use real tmp files

This commit is contained in:
topjohnwu 2017-11-15 05:25:19 +08:00
parent 68eb0bdec9
commit bb33d9e600

View File

@ -41,6 +41,7 @@ import errno
import shutil import shutil
import lzma import lzma
import base64 import base64
import tempfile
def mv(source, target): def mv(source, target):
print('mv: {} -> {}'.format(source, target)) print('mv: {} -> {}'.format(source, target))
@ -234,7 +235,9 @@ def gen_update_binary():
def zip_main(args): def zip_main(args):
header('* Packing Flashable Zip') header('* Packing Flashable Zip')
with zipfile.ZipFile('tmp_unsigned.zip', 'w', compression=zipfile.ZIP_DEFLATED, allowZip64=False) as zipf: unsigned = tempfile.mkstemp()[1]
with zipfile.ZipFile(unsigned, 'w', compression=zipfile.ZIP_DEFLATED, allowZip64=False) as zipf:
# META-INF # META-INF
# update-binary # update-binary
target = os.path.join('META-INF', 'com', 'google', 'android', 'update-binary') target = os.path.join('META-INF', 'com', 'google', 'android', 'update-binary')
@ -284,12 +287,14 @@ def zip_main(args):
# End of zipping # End of zipping
output = os.path.join('out', 'Magisk-v{}.zip'.format(args.versionString)) output = os.path.join('out', 'Magisk-v{}.zip'.format(args.versionString))
sign_adjust_zip('tmp_unsigned.zip', output) sign_adjust_zip(unsigned, output)
def zip_uninstaller(args): def zip_uninstaller(args):
header('* Packing Uninstaller Zip') header('* Packing Uninstaller Zip')
with zipfile.ZipFile('tmp_unsigned.zip', 'w', compression=zipfile.ZIP_DEFLATED, allowZip64=False) as zipf: unsigned = tempfile.mkstemp()[1]
with zipfile.ZipFile(unsigned, 'w', compression=zipfile.ZIP_DEFLATED, allowZip64=False) as zipf:
# META-INF # META-INF
# update-binary # update-binary
target = os.path.join('META-INF', 'com', 'google', 'android', 'update-binary') target = os.path.join('META-INF', 'com', 'google', 'android', 'update-binary')
@ -329,7 +334,7 @@ def zip_uninstaller(args):
# End of zipping # End of zipping
output = os.path.join('out', 'Magisk-uninstaller-{}.zip'.format(datetime.datetime.now().strftime('%Y%m%d'))) output = os.path.join('out', 'Magisk-uninstaller-{}.zip'.format(datetime.datetime.now().strftime('%Y%m%d')))
sign_adjust_zip('tmp_unsigned.zip', output) sign_adjust_zip(unsigned, output)
def sign_adjust_zip(unsigned, output): def sign_adjust_zip(unsigned, output):
signer_name = 'zipsigner-1.0.jar' signer_name = 'zipsigner-1.0.jar'
@ -354,27 +359,31 @@ def sign_adjust_zip(unsigned, output):
publicKey = os.path.join('ziptools', 'public.certificate.x509.pem') publicKey = os.path.join('ziptools', 'public.certificate.x509.pem')
privateKey = os.path.join('ziptools', 'private.key.pk8') privateKey = os.path.join('ziptools', 'private.key.pk8')
signed = tempfile.mkstemp()[1]
# Unsigned->signed # Unsigned->signed
proc = subprocess.run(['java', '-jar', jarsigner, proc = subprocess.run(['java', '-jar', jarsigner,
publicKey, privateKey, unsigned, 'tmp_signed.zip']) publicKey, privateKey, unsigned, signed])
if proc.returncode != 0: if proc.returncode != 0:
error('First sign flashable zip failed!') error('First sign flashable zip failed!')
adjusted = tempfile.mkstemp()[1]
# Adjust zip # Adjust zip
proc = subprocess.run([os.path.join('ziptools', 'zipadjust'), 'tmp_signed.zip', 'tmp_adjusted.zip']) proc = subprocess.run([os.path.join('ziptools', 'zipadjust'), signed, adjusted])
if proc.returncode != 0: if proc.returncode != 0:
error('Adjust flashable zip failed!') error('Adjust flashable zip failed!')
# Adjusted -> output # Adjusted -> output
proc = subprocess.run(['java', '-jar', jarsigner, proc = subprocess.run(['java', '-jar', jarsigner,
"-m", publicKey, privateKey, 'tmp_adjusted.zip', output]) "-m", publicKey, privateKey, adjusted, output])
if proc.returncode != 0: if proc.returncode != 0:
error('Second sign flashable zip failed!') error('Second sign flashable zip failed!')
# Cleanup # Cleanup
rm(unsigned) rm(unsigned)
rm('tmp_signed.zip') rm(signed)
rm('tmp_adjusted.zip') rm(adjusted)
def cleanup(args): def cleanup(args):
if len(args.target) == 0: if len(args.target) == 0: