home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / win32verstamp.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  7.2 KB  |  171 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. from win32api import BeginUpdateResource, UpdateResource, EndUpdateResource
  5. import os
  6. import struct
  7. import glob
  8. import optparse
  9. VS_FFI_SIGNATURE = -17890115
  10. VS_FFI_STRUCVERSION = 65536
  11. VS_FFI_FILEFLAGSMASK = 63
  12. VOS_NT_WINDOWS32 = 262148
  13. null_byte = '\x00'.encode('ascii')
  14.  
  15. def file_flags(debug):
  16.     if debug:
  17.         return 3
  18.  
  19.  
  20. def file_type(is_dll):
  21.     if is_dll:
  22.         return 2
  23.  
  24.  
  25. def VS_FIXEDFILEINFO(maj, min, sub, build, debug = 0, is_dll = 1):
  26.     return struct.pack('lllllllllllll', VS_FFI_SIGNATURE, VS_FFI_STRUCVERSION, maj << 16 | min, sub << 16 | build, maj << 16 | min, sub << 16 | build, VS_FFI_FILEFLAGSMASK, file_flags(debug), VOS_NT_WINDOWS32, file_type(is_dll), 0, 0, 0)
  27.  
  28.  
  29. def nullterm(s):
  30.     return (unicode(s) + u'\x00').encode('unicode-internal')
  31.  
  32.  
  33. def pad32(s, extra = 2):
  34.     l = 4 - (len(s) + extra & 3)
  35.     if l < 4:
  36.         return s + null_byte * l
  37.  
  38.  
  39. def addlen(s):
  40.     return struct.pack('h', len(s) + 2) + s
  41.  
  42.  
  43. def String(key, value):
  44.     key = nullterm(key)
  45.     value = nullterm(value)
  46.     result = struct.pack('hh', len(value) / 2, 1)
  47.     result = result + key
  48.     result = pad32(result) + value
  49.     return addlen(result)
  50.  
  51.  
  52. def StringTable(key, data):
  53.     key = nullterm(key)
  54.     result = struct.pack('hh', 0, 1)
  55.     result = result + key
  56.     for k, v in data.iteritems():
  57.         result = result + String(k, v)
  58.         result = pad32(result)
  59.     
  60.     return addlen(result)
  61.  
  62.  
  63. def StringFileInfo(data):
  64.     result = struct.pack('hh', 0, 1)
  65.     result = result + nullterm('StringFileInfo')
  66.     result = pad32(result) + StringTable('040904E4', data)
  67.     return addlen(result)
  68.  
  69.  
  70. def Var(key, value):
  71.     result = struct.pack('hh', len(value), 0)
  72.     result = result + nullterm(key)
  73.     result = pad32(result) + value
  74.     return addlen(result)
  75.  
  76.  
  77. def VarFileInfo(data):
  78.     result = struct.pack('hh', 0, 1)
  79.     result = result + nullterm('VarFileInfo')
  80.     result = pad32(result)
  81.     for k, v in data.iteritems():
  82.         result = result + Var(k, v)
  83.     
  84.     return addlen(result)
  85.  
  86.  
  87. def VS_VERSION_INFO(maj, min, sub, build, sdata, vdata, debug = 0, is_dll = 1):
  88.     ffi = VS_FIXEDFILEINFO(maj, min, sub, build, debug, is_dll)
  89.     result = struct.pack('hh', len(ffi), 0)
  90.     result = result + nullterm('VS_VERSION_INFO')
  91.     result = pad32(result) + ffi
  92.     result = pad32(result) + StringFileInfo(sdata) + VarFileInfo(vdata)
  93.     return addlen(result)
  94.  
  95.  
  96. def stamp(pathname, options):
  97.     
  98.     try:
  99.         f = open(pathname, 'a+b')
  100.         f.close()
  101.     except IOError:
  102.         why = None
  103.         print 'WARNING: File %s could not be opened - %s' % (pathname, why)
  104.  
  105.     ver = options.version
  106.     
  107.     try:
  108.         bits = [ int(i) for i in ver.split('.') ]
  109.         (vmaj, vmin, vsub, vbuild) = bits
  110.     except (IndexError, TypeError, ValueError):
  111.         raise ValueError('--version must be a.b.c.d (all integers) - got %r' % ver)
  112.  
  113.     ifn = options.internal_name
  114.     if not ifn:
  115.         ifn = os.path.basename(pathname)
  116.     ofn = options.original_filename
  117.     if not ofn:
  118.         ofn = os.path.basename(pathname)
  119.     sdata = {
  120.         'Comments': options.comments,
  121.         'CompanyName': options.company,
  122.         'FileDescription': options.description,
  123.         'FileVersion': ver,
  124.         'InternalName': ifn,
  125.         'LegalCopyright': options.copyright,
  126.         'LegalTrademarks': options.trademarks,
  127.         'OriginalFilename': ofn,
  128.         'ProductName': options.product,
  129.         'ProductVersion': ver }
  130.     vdata = {
  131.         'Translation': struct.pack('hh', 1033, 1252) }
  132.     is_dll = options.dll
  133.     if is_dll is None:
  134.         is_dll = os.path.splitext(pathname)[1].lower() in '.dll .pyd'.split()
  135.     is_debug = options.debug
  136.     if is_debug is None:
  137.         is_debug = os.path.splitext(pathname)[0].lower().endswith('_d')
  138.     for k, v in sdata.items():
  139.         if v is None:
  140.             sdata[k] = ''
  141.             continue
  142.     vs = VS_VERSION_INFO(vmaj, vmin, vsub, vbuild, sdata, vdata, is_debug, is_dll)
  143.     h = BeginUpdateResource(pathname, 0)
  144.     UpdateResource(h, 16, 1, vs)
  145.     EndUpdateResource(h, 0)
  146.     if options.verbose:
  147.         print 'Stamped:', pathname
  148.  
  149. if __name__ == '__main__':
  150.     parser = optparse.OptionParser('%prog [options] filespec ...', description = __doc__)
  151.     parser.add_option('-q', '--quiet', action = 'store_false', dest = 'verbose', default = True, help = "don't print status messages to stdout")
  152.     parser.add_option('', '--version', default = '0.0.0.0', help = 'The version number as m.n.s.b')
  153.     parser.add_option('', '--dll', help = 'Stamp the file as a DLL.  Default is to look at the\n                            file extension for .dll or .pyd.')
  154.     parser.add_option('', '--debug', help = 'Stamp the file as a debug binary.')
  155.     parser.add_option('', '--product', help = 'The product name to embed.')
  156.     parser.add_option('', '--company', help = 'The company name to embed.')
  157.     parser.add_option('', '--trademarks', help = 'The trademark string to embed.')
  158.     parser.add_option('', '--comments', help = 'The comments string to embed.')
  159.     parser.add_option('', '--copyright', help = 'The copyright message string to embed.')
  160.     parser.add_option('', '--description', metavar = 'DESC', help = 'The description to embed.')
  161.     parser.add_option('', '--internal-name', metavar = 'NAME', help = 'The internal filename to embed. If not specified\n                         the base filename is used.')
  162.     parser.add_option('', '--original-filename', help = 'The original filename to embed. If not specified\n                            the base filename is used.')
  163.     (options, args) = parser.parse_args()
  164.     if not args:
  165.         parser.error('You must supply a file to stamp.  Use --help for details.')
  166.     for g in args:
  167.         for f in glob.glob(g):
  168.             stamp(f, options)
  169.         
  170.     
  171.