01: import os, zipfile, sys
02:
03: if len(sys.argv) < 3:
04: print('Usage: python maketns.py <output_tns_file.tns> <input_lua_file_1.lua> ... <input_lua_file_n.lua>')
05: print('Note that different lua source files are simply concatenated.')
06: sys.exit(0)
07:
08: outputFileName = sys.argv[1]
09: inputFileNames = sys.argv[2:len(sys.argv)]
10:
11: #hex values of a blank tns file
12: blankTnsHexValues = [
13: '2a', '54', '49', '4d', '4c', '50', '30', '35',
14: '30', '30', '14', '00', '00', '00', '0d', '00',
15: '3c', '17', '8d', '3e', '73', '45', '68', '44',
16: '40', '01', '00', '00', '93', '02', '00', '00',
17: '0c', '00', '00', '00', '44', '6f', '63', '75',
18: '6d', '65', '6e', '74', '2e', '78', '6d', '6c',
19: '0f', 'ce', 'd8', 'd2', '81', '06', '86', '5b',
20: '62', '81', 'c2', 'a6', '21', 'a5', 'fd', '08',
21: '23', '0f', '92', 'ae', '38', '45', '2a', '05',
22: '66', 'bb', 'c7', 'df', '98', '87', '55', '13',
23: '61', '47', '75', '24', '66', 'f9', '71', '1f',
24: 'cd', 'e5', 'b3', '41', '5d', '13', '33', '4e',
25: '72', '0e', '08', 'fb', '76', '87', 'e6', '1d',
26: '8f', '93', '18', '32', 'e7', '06', '92', '6a',
27: '58', 'f0', '77', '22', '09', 'ba', 'ca', '25',
28: 'b3', '8e', '7b', '52', '98', 'c5', '54', '9a',
29: '5f', '8c', '34', '9e', '35', 'f5', '6b', '75',
30: '45', 'c7', '7a', '10', '8a', '5f', '6e', '0d',
31: 'e6', '4a', '1e', '90', '92', 'ba', '1c', 'd1',
32: '96', 'b0', 'c1', '19', 'cd', '5c', '0d', '27',
33: '9b', '19', '2f', '58', '6f', '49', '14', '95',
34: '0d', 'f1', '3a', 'c8', '99', '82', '2e', 'a3',
35: 'ad', '36', '9e', 'b7', '13', '28', 'aa', '9b',
36: '89', 'b5', '30', 'fc', '3f', 'd4', 'd2', '45',
37: 'be', 'c4', '7f', '86', '87', '71', '25', '09',
38: 'd1', 'ed', 'b3', 'd3', '92', '69', 'df', 'b5',
39: '94', '72', 'a6', '37', '58', '58', 'c4', 'd4',
40: '29', 'd1', 'ba', 'af', '84', '4a', '2e', 'cf',
41: '2e', '7b', 'fb', 'bd', '91', 'ac', '99', 'ba',
42: 'a8', 'cc', 'c0', '95', '5b', '21', 'e4', '3d',
43: '58', 'fa', '8f', 'c4', '21', 'c7', '66', 'c5',
44: '7b', 'a7', '31', '9f', '70', '2d', '1e', 'ce',
45: '37', '8a', '13', 'e1', 'e8', '1e', '88', '67',
46: '11', '7c', '3e', '7d', '75', '52', 'bd', '9a',
47: '40', 'd7', 'af', '3d', 'a1', '10', '3a', 'eb',
48: '91', 'c8', '8d', '4a', '24', '9b', '38', '85',
49: '8f', '38', 'b5', '3c', 'b5', 'a3', 'fa', '1b',
50: 'be', '4a', 'ca', '93', 'd7', '41', '69', '02',
51: '0d', 'ad', '30', '6d', 'ba', '08', '54', 'fc',
52: '13', 'e8', 'fa', 'eb', 'c0', 'fd', 'e8', 'ad',
53: '51', '1a', '4a', '89', '95', '3a', '27', 'd4',
54: 'f2', 'cc', '40', '46', 'cd', 'c9', '8a', '14',
55: 'd5', '83', 'df', '60', 'dc', 'ce', '06', '5e',
56: '9e', '2e', 'b1', 'd3', '54', '97', 'f7', '7b',
57: 'ae', '6f', '51', '4f', '74', 'dc', '57', '6a',
58: 'd4', '7f', '37', '2a', '49', 'ba', '86', '2a']
59:
60: #xml code which should be placed before and after the lua code
61: 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>'
62: xmlCodeAfter = '</sc:script></wdgt></card></prob>'
63:
64: convertedLua = ''
65:
66: for inputFileName in inputFileNames:
67: inputFile = open(inputFileName, 'rb')
68: #encoding special chars of each line in the lua code
69: for line in inputFile:
70: conv = line.replace('&', '&')
71: conv = conv.replace('"', '"')
72: conv = conv.replace('\'', ''')
73: conv = conv.replace('<', '<')
74: conv = conv.replace('>', '>')
75: convertedLua += conv
76: inputFile.close()
77:
78: xmlCode = xmlCodeBefore + convertedLua + xmlCodeAfter
79:
80: #compressing the generated xml code to temp.zip
81: tempZip = zipfile.ZipFile('./temp.zip', 'w', zipfile.ZIP_DEFLATED)
82: tempZip.writestr('Problem1.xml', xmlCode)
83: tempZip.close()
84:
85: outputFile = open(outputFileName, 'wb')
86:
87: #writing blank tns file
88: for s in blankTnsHexValues:
89: outputFile.write(chr(eval('0x' + s)))
90:
91: #appending created zip file
92: zipFile = open('./temp.zip', 'rb')
93: outputFile.write(zipFile.read())
94: zipFile.close()
95: outputFile.close()
96:
97: #deleting temp.zip
98: os.remove('./temp.zip')