Unzip and Execute Script
Description
The unzip_and_execute
function extracts an outer zip file, reads a install.json
file within the zip, identifies a deploy script and package zip, and executes the deploy script with the package zip file path as an argument.
Function Signature:
def unzip_and_execute(zip_file_path: str, unzip_to_folder_path: str) -> Tuple[Optional[str], Optional[str]]:
Parameters
- zip_file_path (str): Full path to the outer zip file to extract.
- unzip_to_folder_path (str): Path where the contents of the zip file will be extracted.
Returns
- Tuple[Optional[str], Optional[str]]: Returns
None, None
if an error occurs while reading or executing the script, otherwise returns two values: the script filename and the package filename.
Example Usage
unzip_and_execute("/path/to/package_1.2.3.zip", "/path/to/unzip_folder")
Notes
- The function assumes that the outer zip file contains:
install.json
(containinginstallScriptFilename
andzipFilename
),- a deploy script (
deploy.sh
), - and an inner zip file (e.g.,
package.zip
).
- The function extracts the zip file, identifies and executes the deploy script, and then cleans up by removing the script and package zip file after execution.
- The script file is made executable (
chmod 755
).
Error Handling
- FileNotFoundError: Raised if the provided zip file does not exist or any specified file within the extracted folder is missing.
- json.JSONDecodeError: Raised if there is an error reading the
install.json
file. - subprocess.CalledProcessError: Raised if the deploy script fails to execute.
- General Exception: The function catches all exceptions and provides the filename and line number for debugging.