2020.05.11-Python中调用zip包中的Python脚本

文章目录

  • Abstract
      • define the class body of class DeviceSpecificParams
      • to create a object of class DeviceSpecificParams

Abstract

In the firstly,generalize/epitomize/summarize the content of the call a pyhton script in zip packzge.In the second,analyzing the principle of related function.such as :
WriteFullOTAPackage()
WriteIncrementalOTAPackage()

define the class body of class DeviceSpecificParams

class DeviceSpecificParams(object):
1150   module = None
1151   def __init__(self, **kwargs):
1152     """Keyword arguments to the constructor become attributes of this
1153     object, which is passed to all functions in the device-specific
1154     module."""
1155     for k, v in kwargs.iteritems():
1156       setattr(self, k, v)
1157     self.extras = OPTIONS.extras
1158 
1159     if self.module is None:
1160       path = OPTIONS.device_specific
1161       if not path:
1162         return
1163       try:
1164         if os.path.isdir(path):
1165           info = imp.find_module("releasetools", [path])
1166         else:
1167           d, f = os.path.split(path)
1168           b, x = os.path.splitext(f)
1169           if x == ".py":
1170             f = b
1171           info = imp.find_module(f, [d])
1172         print("loaded device-specific extensions from", path)
1173         self.module = imp.load_module("device_specific", *info)
1174       except ImportError:
1175         print("unable to load device-specific module; assuming none")

to create a object of class DeviceSpecificParams

399   device_specific = common.DeviceSpecificParams(
400       input_zip=input_zip,
401       input_version=OPTIONS.info_dict["recovery_api_version"],
402       output_zip=output_zip,
403       script=script,
404       input_tmp=OPTIONS.input_tmp,
405       metadata=metadata,
406       info_dict=OPTIONS.info_dict)
407 
408   #assert HasRecoveryPatch(input_zip)

The code "imp.find_module(“releasetools”, [path])"is original to the module named imp,aimed to find a module or class. Then a operation of load a module processed in the code “self.module = imp.load_module(“device_specific”, *info)”.

The perform of function through the method that class DeviceSpecificParams object read input target OTA package and load releasetools.py,then load the function to process specific function by the function _DoCall.

1177   def _DoCall(self, function_name, *args, **kwargs):
1178     """Call the named function in the device-specific module, passing
1179     the given args and kwargs.  The first argument to the call will be
1180     the DeviceSpecific object itself.  If there is no module, or the
1181     module does not define the function, return the value of the
1182     'default' kwarg (which itself defaults to None)."""
1183     if self.module is None or not hasattr(self.module, function_name):
1184       return kwargs.get("default", None)
1185     return getattr(self.module, function_name)(*((self,) + args), **kwargs)

函数调用:

1193   def FullOTA_InstallBegin(self):
1194     """Called at the start of full OTA installation."""
1195     return self._DoCall("FullOTA_InstallBegin")

hasattr(self.module, function_name) to estimate/judge the class DeviceSpecificParams object
setattr(object, name, value) 函数对应函数 getattr(),用于设置属性值,该属性不一定是存在的。
getattr(object, name[, default]) 函数用于返回一个对象属性值。

传进来self,也即class DeviceSpecificParams’s instance。

def FullOTA_InstallBegin(info):
714   print "FullOTA_InstallBegin"
715   script = info.script;
716   script_ext = EdifyGeneratorExt(script);
717   output_zip = info.output_zip
718   input_zip = info.input_zip
719   PartitionUpdater.SetupEnv(script, script_ext, OPTIONS)
720   radio_dir = os.path.join(OPTIONS.input_tmp, "RADIO")
721   script_ext.AddNvmergeToZipExt(input_zip, output_zip)
722 
723   if OPTIONS.modem_update:
724     script_ext.UnpackPackageFile("META-INF/com/google/android/nvmerge", os.path.join(OPTIONS.tmp_path, "nvmerge"))
725     script_ext.UnpackPackageFile("META-INF/com/google/android/nvmerge.cfg", os.path.join(OPTIONS.tmp_path, "nvmerge.cfg"))
726 
727   partitions = []
728   configs = GetAllUpdaterConfigs(input_zip)
729 
730   if OPTIONS.partition_change:
731     script_ext.AddRepartsToZipExt(input_zip, output_zip)
732     script_ext.AddE2fsToZipExt(input_zip, output_zip)
733     ParseXml(input_zip, OPTIONS.target_partition_list)
734   for config in configs:
735     if ((OPTIONS.uboot_update and config.update_type == "B") or (OPTIONS.modem_update and config.update_type == "M") ):
736       partition = PartitionFullUpdater(config.mount_point, config.file_name, radio_dir,
737                                      verbatim=config.verbatim,
738                                      mount_point2=config.mount_point2,
739                                      mount_point3=config.mount_point3,
740                                      nv_merge=config.nv_merge,
741                                      spl_merge=config.spl_merge)
742       partition.AddToOutputZip(output_zip)
743       if partition.nv_merge and partition.input.bin:
744         print"partition.mount_point2=%s"%partition.mount_point2
745         print"GetPartitionDev( partition.mount_point2)=%s"%(GetPartitionDev( partition.mount_point2))
746         OPTIONS.backup_list.append( GetPartitionDev( partition.mount_point2))
747         OPTIONS.backup_list.append( GetPartitionDev( partition.mount_point3))
748       partitions.append(partition)
749 
750   if OPTIONS.partition_change:
751     AdjustPartition(info, input_zip, output_zip)
752 
753   for partition in partitions:
754     partition.Update()
755 
756   if OPTIONS.wipe_product_info:
757     partion_productinfo = PartitionUpdater("/productinfo")
758     partion_productinfo.FormatPartition("format productinfo ....")
759 
760   #if OPTIONS.modem_update:
761     #script.DeleteFiles([os.path.join(OPTIONS.tmp_path, "nvmerge"), os.path.join(OPTIONS.tmp_path, "nvmerge.cfg")])
762   #script.DeleteFiles([os.path.join(OPTIONS.tmp_path, "repart"), os.path.join(OPTIONS.sdcard_path, "partition.cfg")])

打印结果

Help on built-in function find_module in module imp:

find_module(...)
    find_module(name, [path]) -> (file, filename, (suffix, mode, type))
    Search for a module.  If path is omitted or None, search for a
    built-in, frozen or special module and continue search in sys.path.
    The module name cannot contain '.'; to search for a submodule of a
    package, pass the submodule name and the package's __path__.

[Ethan] help:None
loaded device-specific extensions from /tmp/targetfiles-G8MEMA/META/releasetools.py
[Ethan] *args=(), **kwargs={}
FullOTA_Assertions
using prebuilt recovery.img from IMAGES...
[Ethan] *args=(), **kwargs={}
FullOTA_InstallBegin
info is <common.DeviceSpecificParams object at 0x7f8c9ec5b750>

你可能感兴趣的:(Android,Python)