mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Refactor: Simplify build_data()
function
- Rename variables to be more clear about what they do: - reorganise code - simplify a few checks
This commit is contained in:
parent
ccb302462d
commit
265c1dde97
1 changed files with 31 additions and 30 deletions
|
@ -4,49 +4,50 @@ import uuid
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
|
|
||||||
def build_data(dirname, dir_prefix, id_, name):
|
def build_data(base_path, path_prefix, dir_id, dir_name):
|
||||||
data = {
|
data = {
|
||||||
"id": id_,
|
"directory_name": dir_name,
|
||||||
"name": name,
|
"directory_id": dir_id,
|
||||||
"files": [],
|
"files": [],
|
||||||
"dirs": [],
|
"dirs": [],
|
||||||
}
|
}
|
||||||
|
|
||||||
for basename in os.listdir(dirname):
|
if dir_id == "INSTALLFOLDER":
|
||||||
filename = os.path.join(dirname, basename)
|
data["component_id"] = "ApplicationFiles"
|
||||||
if os.path.isfile(filename):
|
else:
|
||||||
data["files"].append(os.path.join(dir_prefix, basename))
|
data["component_id"] = "Component" + dir_id
|
||||||
elif os.path.isdir(filename):
|
data["component_guid"] = str(uuid.uuid4())
|
||||||
if id_ == "INSTALLFOLDER":
|
|
||||||
id_prefix = "Folder"
|
for entry in os.listdir(base_path):
|
||||||
|
entry_path = os.path.join(base_path, entry)
|
||||||
|
if os.path.isfile(entry_path):
|
||||||
|
data["files"].append(os.path.join(path_prefix, entry))
|
||||||
|
elif os.path.isdir(entry_path):
|
||||||
|
if dir_id == "INSTALLFOLDER":
|
||||||
|
next_dir_prefix = "Folder"
|
||||||
else:
|
else:
|
||||||
id_prefix = id_
|
next_dir_prefix = dir_id
|
||||||
|
|
||||||
# Skip lib/PySide6/examples folder due to ilegal file names
|
# Skip lib/PySide6/examples folder due to ilegal file names
|
||||||
if "\\build\\exe.win-amd64-3.12\\lib\\PySide6\\examples" in dirname:
|
if "\\build\\exe.win-amd64-3.12\\lib\\PySide6\\examples" in base_path:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Skip lib/PySide6/qml/QtQuick folder due to ilegal file names
|
# Skip lib/PySide6/qml/QtQuick folder due to ilegal file names
|
||||||
# XXX Since we're not using Qml it should be no problem
|
# XXX Since we're not using Qml it should be no problem
|
||||||
if "\\build\\exe.win-amd64-3.12\\lib\\PySide6\\qml\\QtQuick" in dirname:
|
if "\\build\\exe.win-amd64-3.12\\lib\\PySide6\\qml\\QtQuick" in base_path:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
id_value = f"{id_prefix}{basename.capitalize().replace('-', '_')}"
|
next_dir_id = next_dir_prefix + entry.capitalize().replace("-", "_")
|
||||||
data["dirs"].append(
|
subdata = build_data(
|
||||||
build_data(
|
os.path.join(base_path, entry),
|
||||||
os.path.join(dirname, basename),
|
os.path.join(path_prefix, entry),
|
||||||
os.path.join(dir_prefix, basename),
|
next_dir_id,
|
||||||
id_value,
|
entry,
|
||||||
basename,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(data["files"]) > 0:
|
# Add the subdirectory only if it contains files or subdirectories
|
||||||
if id_ == "INSTALLFOLDER":
|
if subdata["files"] or subdata["dirs"]:
|
||||||
data["component_id"] = "ApplicationFiles"
|
data["dirs"].append(subdata)
|
||||||
else:
|
|
||||||
data["component_id"] = "FolderComponent" + id_[len("Folder") :]
|
|
||||||
data["component_guid"] = str(uuid.uuid4())
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
@ -54,9 +55,9 @@ def build_data(dirname, dir_prefix, id_, name):
|
||||||
def build_dir_xml(root, data):
|
def build_dir_xml(root, data):
|
||||||
attrs = {}
|
attrs = {}
|
||||||
if "id" in data:
|
if "id" in data:
|
||||||
attrs["Id"] = data["id"]
|
attrs["Id"] = data["directory_id"]
|
||||||
if "name" in data:
|
if "name" in data:
|
||||||
attrs["Name"] = data["name"]
|
attrs["Name"] = data["directory_name"]
|
||||||
el = ET.SubElement(root, "Directory", attrs)
|
el = ET.SubElement(root, "Directory", attrs)
|
||||||
for subdata in data["dirs"]:
|
for subdata in data["dirs"]:
|
||||||
build_dir_xml(el, subdata)
|
build_dir_xml(el, subdata)
|
||||||
|
@ -68,7 +69,7 @@ def build_components_xml(root, data):
|
||||||
component_ids.append(data["component_id"])
|
component_ids.append(data["component_id"])
|
||||||
|
|
||||||
if "component_guid" in data:
|
if "component_guid" in data:
|
||||||
dir_ref_el = ET.SubElement(root, "DirectoryRef", Id=data["id"])
|
dir_ref_el = ET.SubElement(root, "DirectoryRef", Id=data["directory_id"])
|
||||||
component_el = ET.SubElement(
|
component_el = ET.SubElement(
|
||||||
dir_ref_el,
|
dir_ref_el,
|
||||||
"Component",
|
"Component",
|
||||||
|
|
Loading…
Reference in a new issue