File: maketns.py - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

import os, zipfile, sys

if len(sys.argv) < 3:
    print('Usage: python maketns.py <output_tns_file.tns> <input_lua_file_1.lua> ... <input_lua_file_n.lua>')
    print('Note that different lua source files are simply concatenated.')
    sys.exit(0)

outputFileName = sys.argv[1]
inputFileNames = sys.argv[2:len(sys.argv)]

#hex values of a blank tns file
blankTnsHexValues = [
    '2a', '54', '49', '4d', '4c', '50', '30', '35',
    '30', '30', '14', '00', '00', '00', '0d', '00',
    '3c', '17', '8d', '3e', '73', '45', '68', '44',
    '40', '01', '00', '00', '93', '02', '00', '00',
    '0c', '00', '00', '00', '44', '6f', '63', '75',
    '6d', '65', '6e', '74', '2e', '78', '6d', '6c',
    '0f', 'ce', 'd8', 'd2', '81', '06', '86', '5b',
    '62', '81', 'c2', 'a6', '21', 'a5', 'fd', '08',
    '23', '0f', '92', 'ae', '38', '45', '2a', '05',
    '66', 'bb', 'c7', 'df', '98', '87', '55', '13',
    '61', '47', '75', '24', '66', 'f9', '71', '1f',
    'cd', 'e5', 'b3', '41', '5d', '13', '33', '4e',
    '72', '0e', '08', 'fb', '76', '87', 'e6', '1d',
    '8f', '93', '18', '32', 'e7', '06', '92', '6a',
    '58', 'f0', '77', '22', '09', 'ba', 'ca', '25',
    'b3', '8e', '7b', '52', '98', 'c5', '54', '9a',
    '5f', '8c', '34', '9e', '35', 'f5', '6b', '75',
    '45', 'c7', '7a', '10', '8a', '5f', '6e', '0d',
    'e6', '4a', '1e', '90', '92', 'ba', '1c', 'd1',
    '96', 'b0', 'c1', '19', 'cd', '5c', '0d', '27',
    '9b', '19', '2f', '58', '6f', '49', '14', '95',
    '0d', 'f1', '3a', 'c8', '99', '82', '2e', 'a3',
    'ad', '36', '9e', 'b7', '13', '28', 'aa', '9b',
    '89', 'b5', '30', 'fc', '3f', 'd4', 'd2', '45',
    'be', 'c4', '7f', '86', '87', '71', '25', '09',
    'd1', 'ed', 'b3', 'd3', '92', '69', 'df', 'b5',
    '94', '72', 'a6', '37', '58', '58', 'c4', 'd4',
    '29', 'd1', 'ba', 'af', '84', '4a', '2e', 'cf',
    '2e', '7b', 'fb', 'bd', '91', 'ac', '99', 'ba',
    'a8', 'cc', 'c0', '95', '5b', '21', 'e4', '3d',
    '58', 'fa', '8f', 'c4', '21', 'c7', '66', 'c5',
    '7b', 'a7', '31', '9f', '70', '2d', '1e', 'ce',
    '37', '8a', '13', 'e1', 'e8', '1e', '88', '67',
    '11', '7c', '3e', '7d', '75', '52', 'bd', '9a',
    '40', 'd7', 'af', '3d', 'a1', '10', '3a', 'eb',
    '91', 'c8', '8d', '4a', '24', '9b', '38', '85',
    '8f', '38', 'b5', '3c', 'b5', 'a3', 'fa', '1b',
    'be', '4a', 'ca', '93', 'd7', '41', '69', '02',
    '0d', 'ad', '30', '6d', 'ba', '08', '54', 'fc',
    '13', 'e8', 'fa', 'eb', 'c0', 'fd', 'e8', 'ad',
    '51', '1a', '4a', '89', '95', '3a', '27', 'd4',
    'f2', 'cc', '40', '46', 'cd', 'c9', '8a', '14',
    'd5', '83', 'df', '60', 'dc', 'ce', '06', '5e',
    '9e', '2e', 'b1', 'd3', '54', '97', 'f7', '7b',
    'ae', '6f', '51', '4f', '74', 'dc', '57', '6a',
    'd4', '7f', '37', '2a', '49', 'ba', '86', '2a']

#xml code which should be placed before and after the lua code
xmlCodeBefore = '<?xml version="1.0" encoding="UTF-8" ?><prob xmlns="urn:TI.Problem" ver="1.0" pbname=""><sym></sym><card clay="0" h1="10000" h2="10000" w1="10000" w2="10000"><isDummyCard>0</isDummyCard><flag>0</flag><wdgt xmlns:sc="urn:TI.ScriptApp" type="TI.ScriptApp" ver="1.0"><sc:mFlags>0</sc:mFlags><sc:value>-1</sc:value><sc:script>'
xmlCodeAfter = '</sc:script></wdgt></card></prob>'

convertedLua = ''

for inputFileName in inputFileNames:
    inputFile = open(inputFileName, 'rb')
    #encoding special chars of each line in the lua code
    for line in inputFile:
        conv = line.replace('&', '&amp;')
        conv = conv.replace('"', '&quot;')
        conv = conv.replace('\'', '&#039;')
        conv = conv.replace('<', '&lt;')
        conv = conv.replace('>', '&gt;')
        convertedLua += conv
    inputFile.close()

xmlCode = xmlCodeBefore + convertedLua + xmlCodeAfter

#compressing the generated xml code to temp.zip
tempZip = zipfile.ZipFile('./temp.zip', 'w', zipfile.ZIP_DEFLATED)
tempZip.writestr('Problem1.xml', xmlCode)
tempZip.close()

outputFile = open(outputFileName, 'wb')

#writing blank tns file
for s in blankTnsHexValues:
    outputFile.write(chr(eval('0x' + s)))

#appending created zip file
zipFile = open('./temp.zip', 'rb')
outputFile.write(zipFile.read())
zipFile.close()
outputFile.close()

#deleting temp.zip
os.remove('./temp.zip')