Advertisement
Guest User

Untitled

a guest
Mar 26th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. #!/usr/local/bin/python
  2.  
  3. from urllib import urlopen
  4. from datetime import date
  5. import sys
  6. import os
  7. import zlib
  8. import getopt
  9. import csv
  10. import zipfile
  11.  
  12. #
  13. # Utility functions
  14. #
  15.  
  16. def FindFilesByExt(path, ext):
  17.     ext = ext.lower()
  18.  
  19.     for root, dirs, files in os.walk(path):
  20.         for name in files:
  21.             if name[-len(ext):].lower() == ext:
  22.                 yield os.path.join(root, name)
  23.  
  24. def GetFileCrc32(filename):
  25.     crc = 0
  26.     for line in open(filename, "rb"):
  27.         crc = zlib.crc32(line, crc)
  28.  
  29.     return "%x" % (crc & 0xffffffff)
  30.  
  31. def FormatName(filename):
  32.     if filename[:2] == ".\\":
  33.         filename = filename[2:]
  34.    
  35.     return filename.replace("\\", "/")
  36.  
  37. def GetLastModifiedTime(filename):
  38.     # http://support.microsoft.com/kb/167296
  39.     # How To Convert a UNIX time_t to a Win32 FILETIME or SYSTEMTIME
  40.     EPOCH_AS_FILETIME = 116444736000000000  # January 1, 1970 as MS file time
  41.     HUNDREDS_OF_NANOSECONDS = 10000000
  42.    
  43.     return EPOCH_AS_FILETIME + long(os.path.getmtime(filename)) * HUNDREDS_OF_NANOSECONDS
  44.  
  45. #
  46. # Real code
  47. #
  48.  
  49. class Patch:
  50.     def __init__(self):
  51.         self.name = None
  52.         self.patch_url = None
  53.        
  54.         # Patch file list
  55.         self.file_dict = dict()
  56.         self.file_list = None
  57.        
  58.     def SetName(self, name):
  59.         self.name = name
  60.    
  61.     def SetPatchUrl(self, url):
  62.         self.patch_url = url
  63.    
  64.     def AddFilesFromPatchUrl(self):
  65.         for line in urlopen(self.patch_url):
  66.             line = line.split()
  67.             print line
  68.             line[4] = FormatName(line[4])
  69.             self.file_dict[line[4].lower()] = line
  70.    
  71.     def AddFile(self, filename):
  72.         filename = FormatName(filename)
  73.         mtime = GetLastModifiedTime(filename)
  74.  
  75.         #
  76.         # Format is as following:
  77.         # unpacked_crc unpacked_size low_last_edit high_last_edit path
  78.         #
  79.  
  80.         self.file_dict[filename.lower()] = [GetFileCrc32(filename),
  81.                             "%d" % (os.path.getsize(filename)),
  82.                             "%d" % (mtime >> 32),
  83.                             "%d" % (mtime & 0xffffffff), filename]
  84.                            
  85.         # Sorted list no longer contains all files. Invalidate it.
  86.         self.file_list = None
  87.    
  88.     def GetCrcList(self):
  89.         self.__SortFileList()
  90.        
  91.         output = ""
  92.         for entry in self.file_list:
  93.             output += (" ".join(entry) + "\n")
  94.            
  95.         return output
  96.  
  97.     def __SortFileList(self):
  98.         if not self.file_list:
  99.             self.file_list = [self.file_dict[key] for key in self.file_dict]
  100.             self.file_list.sort(key=lambda entry: entry[4].lower()) # 4 = filename index
  101.  
  102.            
  103. kPatchConfigFieldNames = ["Name", "Url"]
  104.  
  105. def GetPatchInstance(filename, desiredName):
  106.     with open(filename, 'r') as file:
  107.         reader = csv.DictReader(file, fieldnames=kPatchConfigFieldNames, dialect='excel-tab')
  108.         reader.next()
  109.            
  110.         for row in reader:
  111.             if row["Name"] == desiredName:
  112.                 patch = Patch()
  113.                 patch.SetName(row["Name"])
  114.                 patch.SetPatchUrl(row["Url"])
  115.                 return patch
  116.                
  117.     raise RuntimeError("Failed to find %s!" % (desiredName))
  118.     return None
  119.            
  120. def WriteXmlFile(filename, files):
  121.     file = open(filename, "wb+")
  122.    
  123.     file.write('<ScriptFile>')
  124.  
  125.     for f in files:
  126.         file.write('\t<CreateLz Input="%s" Output="%s.lz" />\n' % (f, f))
  127.  
  128.     file.write('</ScriptFile>')
  129.    
  130. def main(argv):
  131.     #
  132.     # Parse command-line arguments
  133.     #
  134.    
  135.     optlist, args = getopt.getopt(argv[1:], 'a:f:p:', ['archive=', 'file=', 'patchcfg='])
  136.  
  137.     archives = list()
  138.     files = list()
  139.     patchConfigName = None
  140.    
  141.     for name, value in optlist:
  142.         if name == "--archive" or name == "-a":
  143.             files.append("pack/" + value + ".eix")
  144.             files.append("pack/" + value + ".epk")
  145.         elif name == "--file" or name == "-f":
  146.             files.append(value)
  147.         elif name == "--patchcfg" or name == "-p":
  148.             patchConfigName = value
  149.            
  150.     #
  151.     # Decide over patch-config to use...
  152.     #
  153.    
  154.     patch = GetPatchInstance("PatchConfig.txt", patchConfigName)
  155.    
  156.     # Add already existing files
  157.     patch.AddFilesFromPatchUrl()
  158.    
  159.     # Process files
  160.     WriteXmlFile("make_patch.xml", files)
  161.    
  162.     os.system("FileArchiver make_patch.xml")
  163.  
  164.     os.unlink("make_patch.xml")
  165.  
  166.     # Create patch ZIP
  167.     zip = zipfile.ZipFile("PATCH_%s_%s.zip" % (patchConfigName, date.today().strftime("%m%d%Y")), "w", zipfile.ZIP_DEFLATED)
  168.    
  169.     for file in files:
  170.         patch.AddFile(file)
  171.         file = file + ".lz"
  172.         zip.write(file)
  173.         os.unlink(file)
  174.        
  175.     zip.writestr("crclist", patch.GetCrcList())
  176.    
  177.     zip.close()
  178.    
  179. main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement