assembler.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from lxml.etree import Element, ElementTree, tostring, fromstring
  2. import subprocess
  3. import pathlib
  4. from base64 import b64encode
  5. import log
  6. def addEntry(element, tag, text):
  7. """
  8. Creates a plist-style XML entry
  9. """
  10. entry = Element(tag)
  11. entry.text = text
  12. element.append(entry)
  13. def compileiOSConfig(manifest, font, outputPath):
  14. """
  15. Takes a finished font and uses the manifest metadata to compile
  16. a valid iOS Configuration Profile with the font embedded.
  17. """
  18. m = manifest['metadata']['iOSConfig']
  19. root = Element('plist', {'version': '1.0'}) # hard-coded attrs.
  20. # Initial Dict
  21. # ------------------------------------------------
  22. dict = Element('dict')
  23. addEntry(dict, 'key', 'PayloadDisplayName')
  24. addEntry(dict, 'string', m['PayloadDisplayName'])
  25. addEntry(dict, 'key', 'PayloadIdentifier')
  26. addEntry(dict, 'string', m['PayloadIdentifier'])
  27. if "PayloadDescription" in m:
  28. addEntry(dict, 'key', 'PayloadDescription')
  29. addEntry(dict, 'string', m['PayloadDescription'])
  30. addEntry(dict, 'key', 'PayloadRemovalDisallowed')
  31. dict.append(Element('false')) # hard-coded
  32. addEntry(dict, 'key', 'PayloadType')
  33. addEntry(dict, 'string', 'Configuration') # hard-coded
  34. addEntry(dict, 'key', 'PayloadUUID')
  35. addEntry(dict, 'string', m['PayloadUUID'])
  36. addEntry(dict, 'key', 'PayloadVersion')
  37. addEntry(dict, 'integer', str(m['PayloadVersion']))
  38. addEntry(dict, 'key', 'PayloadContent')
  39. # PayloadContent
  40. # ------------------------------------------------
  41. contentArray = Element('array')
  42. contentDict = Element('dict')
  43. addEntry(contentDict, 'key', 'Name')
  44. addEntry(contentDict, 'string', m['ContentPayloadName'])
  45. addEntry(contentDict, 'key', 'PayloadIdentifier')
  46. addEntry(contentDict, 'string', m['ContentPayloadIdentifier'])
  47. addEntry(contentDict, 'key', 'PayloadType')
  48. addEntry(contentDict, 'string', 'com.apple.font')
  49. addEntry(contentDict, 'key', 'PayloadUUID')
  50. addEntry(contentDict, 'string', m['ContentPayloadUUID'])
  51. addEntry(contentDict, 'key', 'PayloadVersion')
  52. addEntry(contentDict, 'integer', str(m['ContentPayloadVersion']))
  53. addEntry(contentDict, 'key', 'Font')
  54. # Font
  55. # ------------------------------------------------
  56. with open(font, "rb") as read_file:
  57. encodedFont = b64encode(read_file.read())
  58. fontData = Element('data')
  59. fontData.text = encodedFont
  60. contentDict.append(fontData)
  61. # stitching it together
  62. # ------------------------------------------------
  63. contentArray.append(contentDict)
  64. dict.append(contentArray)
  65. root.append(dict)
  66. appleDoctype = """<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">"""
  67. return tostring(root, pretty_print=True, method="xml", xml_declaration=True, encoding="UTF-8", doctype=appleDoctype)