#!/usr/bin/env python3
"""Generates a set of files for Settings.bundle in order to display a list of dependency licenses used in the application.
Liceses will be listed in Settings > Your Application > Third-Party Notices and/or Licenses.
1) Move Licenses.plist and Licenses directory into Settings.bundle.
2) Add entry referencing Licenses in Root.plist.
PreferenceSpecifiers
File
Licenses
Title
Third-Party Notices and/or Licenses
Type
PSChildPaneSpecifier
"""
import os
import pathlib
license_list_template = """
PreferenceSpecifiers
ITEMS_PLACEHOLDER
"""
license_list_item_template = """
File
license_text_file_path_PLACEHOLDER
Title
ITEM_TITLE_PLACEHOLDER
Type
PSChildPaneSpecifier
"""
license_template = """
PreferenceSpecifiers
FooterText
LICENSE_TEXT_PLACEHOLDER
Type
PSGroupSpecifier
"""
licenses_directory_name = "Licenses"
if not os.path.exists(licenses_directory_name):
os.mkdir(licenses_directory_name)
license_list_items = []
for name in os.listdir(path='.'):
directory = name
if not os.path.isdir(directory):
continue
license_text_file_path = os.path.join(directory, "LICENSE")
if not os.path.isfile(license_text_file_path):
continue
license_path_no_extension = licenses_directory_name + "/" + directory
license_list_item = license_list_item_template
license_list_item = license_list_item.replace("license_text_file_path_PLACEHOLDER", license_path_no_extension)
license_list_item = license_list_item.replace("ITEM_TITLE_PLACEHOLDER", directory)
license_list_items.append(license_list_item)
with open(license_text_file_path, 'r') as license_text_file:
license_text = license_text_file.read()
license = license_template.replace("LICENSE_TEXT_PLACEHOLDER", license_text)
license_path = license_path_no_extension + ".plist"
license_file = open(license_path, "w")
license_file.write(license)
license_file.close()
license_list_items_string = "".join(license_list_items)
license_list = license_list_template.replace("ITEMS_PLACEHOLDER", license_list_items_string)
license_list_path = "Licenses.plist"
license_list_file = open(license_list_path, "w")
license_list_file.write(license_list)
license_list_file.close()