+ def insert_file(self, new_file, dest_file, rel_path, mode=0o777):
+ self.add_file(rel_path, mode)
+
+ try:
+ # Copy the contents of the file to the correct path
+ dest_dir_path = os.path.dirname(dest_file)
+ if not os.path.isdir(dest_dir_path):
+ os.makedirs(dest_dir_path)
+
+ with open(dest_file, 'wb') as dst_hdl:
+ with open(new_file, 'rb') as src_hdl:
+ shutil.copyfileobj(src_hdl, dst_hdl, 10 * 1024 * 1024)
+
+ # Set the file mode to original
+ os.chmod(dest_file, self._package_file_mode_map[rel_path])
+ except Exception as e:
+ # Clear the file when an exception happens
+ if os.path.isfile(dest_file):
+ os.remove(dest_file)
+
+ raise PackageAppendError(str(e))
+
+ def delete_file(self, dest_file, rel_path):
+ self.remove_file(rel_path)
+
+ try:
+ os.remove(dest_file)
+ except Exception as e:
+ raise PackageAppendError(str(e))
+