From a817add1fabd91a55090a5f84b056ef5bd62037c Mon Sep 17 00:00:00 2001
From: Davide Gardenal <davide.gardenal@huawei.com>
Date: Thu, 28 Jul 2022 12:25:01 +0200
Subject: [PATCH] upgrade_oniro_utils: create find_latest_backup

Signed-off-by: Davide Gardenal <davide.gardenal@huawei.com>
---
 scripts/upgrade_oniro/upgrade_oniro_utils.py | 26 +++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/scripts/upgrade_oniro/upgrade_oniro_utils.py b/scripts/upgrade_oniro/upgrade_oniro_utils.py
index d3ecb7c9..c25fc111 100644
--- a/scripts/upgrade_oniro/upgrade_oniro_utils.py
+++ b/scripts/upgrade_oniro/upgrade_oniro_utils.py
@@ -11,10 +11,34 @@ def get_manifest_projects():
     projects in Oniro's manifest.
 
     Returns:
-        A new list of dict with all the projects' properties
+        A new list of dict with all the projects' properties.
     """
     import xml.etree.ElementTree as ET
 
     tree = ET.parse("oniro/manifests/default.xml")
     root = tree.getroot()
     return [project.attrib for project in root.findall("project")]
+
+def find_latest_backup():
+    """
+    Find the latest backup of Oniro. This will search only in the
+    current directory.
+
+    Returns:
+        The latest backup name if found, None otherwise.
+    """
+    from pathlib import Path
+    
+    this_dir = Path(".")
+    backup_info_list = []
+    for item in this_dir.iterdir():
+        if "upgrade_oniro_backup" in item.name:
+            
+            backup_info = {
+                "name": item.name,
+                "timestamp":  item.name.split("-")[1].split(".")[:2]
+            }
+            print(backup_info)
+            backup_info_list.append(backup_info)
+    
+    return max(backup_info_list, key=lambda x: int(f'{x["timestamp"][0]}{x["timestamp"][1]}'))["name"]
-- 
GitLab