You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.2 KiB

from pathlib import Path
import datetime
from tibi_hardlinks.backup_parser import read_backup_properties
from tibi_hardlinks.exceptions import ConfigurationException
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(property_file: Path, backup_info):
# find the path of the *.apk, *.properties, and *.tar(.gz) corresponding to property_file
backup_root = property_file.parent
try:
apk_hash = backup_info["app_apk_md5"]
apk_file = filter(
lambda apk_file: apk_hash in apk_file.name, backup_root.glob("*.apk*")
)
except KeyError:
apk_file = None
related_files = list(backup_root.glob(property_file.stem + "*"))
if len(related_files) > 2:
raise Exception
tar_file = None
for filename in related_files:
if ".tar" in filename.name:
tar_file = filename
property_file = property_file
return {"property_file": property_file, "apk_file": apk_file, "tar_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.property_file, self.backup_info)
def hardlink_to(self, dest):
for filepath in self.related_files.values():
if filepath:
filepath.link_to(dest)