#!/usr/bin/python
import sys, json, zlib, os, struct, subprocess
import GetSvnRevision
# this script will compress the given file and write the zip with a header and compressed data
#
# example ZipGameDb.py gameDB.json gamedb.zip
#check the number of arguments
if len(sys.argv)<3:
print 'Invalid number of arguments - 3 required ' + str(len(sys.argv) - 1) + ' received'
exit()
# game db json file name
gameDBJsonFileName = sys.argv[1]
# output zipped game db file name
outputZipFileName = sys.argv[2]
# svn version of the file
svnVersion = GetSvnRevision.get_svn_revision(gameDBJsonFileName)
# get the uncompressed file size
uncompressedSize = os.stat(gameDBJsonFileName).st_size
# open the db file
dbFile = open(gameDBJsonFileName, 'rb')
dbFileContent = dbFile.read()
dbFile.close()
# compress the data
dbFileContentComp = zlib.compress(dbFileContent, 9)
# Write the compressed file data with our own header
header_code = 1196186112 # ('G' << 24) + ('L' << 16) + ('Z' << 8)
# write the header in little endian mode
file_header = struct.pack('<IIII', header_code, uncompressedSize, len(dbFileContentComp), svnVersion)
finalZipFile = open(outputZipFileName, 'wb')
finalZipFile.write(file_header)
finalZipFile.write(dbFileContentComp)
finalZipFile.close()