import datetime from pathlib import Path import os from tibi_hardlinks.exceptions import ConfigurationException from tibi_hardlinks.backup_parser import read_backup_properties HEIRACHY = ( "sys_ro.product.model", "sys_ro.serialno", "app_label", "app_version_name", "backup_time", ) def create_backup_directory_name(backup_info, time_format="%Y-%m-%d_%H-%M-%S"): utc_timestamp = backup_info["backup_time"] backup_datetime = datetime.datetime.utcfromtimestamp(utc_timestamp) try: new_time = backup_datetime.strftime(time_format) assert backup_datetime == datetime.datetime.strptime(new_time, time_format) backup_info["backup_time"] = new_time except (ValueError, AssertionError): raise ConfigurationException("customization", "time_format", time_format) backup_directory_name = Path("") for part in HEIRACHY: data = backup_info[part] if len(data) == 0: data = "DEFAULT" backup_directory_name /= data return backup_directory_name def find_backup_data(backup_filepath: Path, backup_info): # find the path of the *.apk, *.properties, and *.tar(.gz) corresponding to backup_filepath backup_root = backup_filepath.parent apk_hash = backup_info["app_apk_md5"] apk_file = filter( lambda apk_file: apk_hash in apk_file.name, backup_root.glob("*.apk*") ) related_files = list(backup_root.glob(backup_filepath.stem + "*")) if len(related_files) > 2: raise Exception for filename in related_files: if ".tar" in filename.name: tar_file = filename property_file = backup_filepath return property_file, apk_file, tar_file class Backup: def __init__(self, property_file: Path): self.property_file = property_file self.backup_info = read_backup_properties(property_file) self.related_files = find_backup_data(self.backup_filepath, self.backup_info) def hardlink_to(self, dest): for filepath in self.related_files: filepath.link_to(dest)