home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_635 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  15.6 KB  |  501 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from _msi import *
  5. import os
  6. import string
  7. import re
  8. import sys
  9. AMD64 = 'AMD64' in sys.version
  10. Itanium = 'Itanium' in sys.version
  11. if not AMD64:
  12.     pass
  13. Win64 = Itanium
  14. datasizemask = 255
  15. type_valid = 256
  16. type_localizable = 512
  17. typemask = 3072
  18. type_long = 0
  19. type_short = 1024
  20. type_string = 3072
  21. type_binary = 2048
  22. type_nullable = 4096
  23. type_key = 8192
  24. knownbits = datasizemask | type_valid | type_localizable | typemask | type_nullable | type_key
  25.  
  26. class Table:
  27.     
  28.     def __init__(self, name):
  29.         self.name = name
  30.         self.fields = []
  31.  
  32.     
  33.     def add_field(self, index, name, type):
  34.         self.fields.append((index, name, type))
  35.  
  36.     
  37.     def sql(self):
  38.         fields = []
  39.         keys = []
  40.         self.fields.sort()
  41.         fields = [
  42.             None] * len(self.fields)
  43.         for index, name, type in self.fields:
  44.             index -= 1
  45.             unk = type & ~knownbits
  46.             if unk:
  47.                 print '%s.%s unknown bits %x' % (self.name, name, unk)
  48.             
  49.             size = type & datasizemask
  50.             dtype = type & typemask
  51.             if dtype == type_string:
  52.                 if size:
  53.                     tname = 'CHAR(%d)' % size
  54.                 else:
  55.                     tname = 'CHAR'
  56.             elif dtype == type_short:
  57.                 tname = 'SHORT'
  58.             elif dtype == type_long:
  59.                 tname = 'LONG'
  60.             elif dtype == type_binary:
  61.                 tname = 'OBJECT'
  62.             else:
  63.                 tname = 'unknown'
  64.                 print '%s.%sunknown integer type %d' % (self.name, name, size)
  65.             if type & type_nullable:
  66.                 flags = ''
  67.             else:
  68.                 flags = ' NOT NULL'
  69.             if type & type_localizable:
  70.                 flags += ' LOCALIZABLE'
  71.             
  72.             fields[index] = '`%s` %s%s' % (name, tname, flags)
  73.             if type & type_key:
  74.                 keys.append('`%s`' % name)
  75.                 continue
  76.         
  77.         fields = ', '.join(fields)
  78.         keys = ', '.join(keys)
  79.         return 'CREATE TABLE %s (%s PRIMARY KEY %s)' % (self.name, fields, keys)
  80.  
  81.     
  82.     def create(self, db):
  83.         v = db.OpenView(self.sql())
  84.         v.Execute(None)
  85.         v.Close()
  86.  
  87.  
  88.  
  89. class _Unspecified:
  90.     pass
  91.  
  92.  
  93. def change_sequence(seq, action, seqno = _Unspecified, cond = _Unspecified):
  94.     for i in range(len(seq)):
  95.         if seq[i][0] == action:
  96.             if cond is _Unspecified:
  97.                 cond = seq[i][1]
  98.             
  99.             if seqno is _Unspecified:
  100.                 seqno = seq[i][2]
  101.             
  102.             seq[i] = (action, cond, seqno)
  103.             return None
  104.     
  105.     raise ValueError, 'Action not found in sequence'
  106.  
  107.  
  108. def add_data(db, table, values):
  109.     v = db.OpenView('SELECT * FROM `%s`' % table)
  110.     count = v.GetColumnInfo(MSICOLINFO_NAMES).GetFieldCount()
  111.     r = CreateRecord(count)
  112.     for value in values:
  113.         for i in range(count):
  114.             field = value[i]
  115.             if isinstance(field, (int, long)):
  116.                 r.SetInteger(i + 1, field)
  117.                 continue
  118.             if isinstance(field, basestring):
  119.                 r.SetString(i + 1, field)
  120.                 continue
  121.             if field is None:
  122.                 continue
  123.             if isinstance(field, Binary):
  124.                 r.SetStream(i + 1, field.name)
  125.                 continue
  126.             raise TypeError, 'Unsupported type %s' % field.__class__.__name__
  127.         
  128.         
  129.         try:
  130.             v.Modify(MSIMODIFY_INSERT, r)
  131.         except Exception:
  132.             e = None
  133.             raise MSIError('Could not insert ' + repr(values) + ' into ' + table)
  134.  
  135.         r.ClearData()
  136.     
  137.     v.Close()
  138.  
  139.  
  140. def add_stream(db, name, path):
  141.     v = db.OpenView("INSERT INTO _Streams (Name, Data) VALUES ('%s', ?)" % name)
  142.     r = CreateRecord(1)
  143.     r.SetStream(1, path)
  144.     v.Execute(r)
  145.     v.Close()
  146.  
  147.  
  148. def init_database(name, schema, ProductName, ProductCode, ProductVersion, Manufacturer):
  149.     
  150.     try:
  151.         os.unlink(name)
  152.     except OSError:
  153.         pass
  154.  
  155.     ProductCode = ProductCode.upper()
  156.     db = OpenDatabase(name, MSIDBOPEN_CREATE)
  157.     for t in schema.tables:
  158.         t.create(db)
  159.     
  160.     add_data(db, '_Validation', schema._Validation_records)
  161.     si = db.GetSummaryInformation(20)
  162.     si.SetProperty(PID_TITLE, 'Installation Database')
  163.     si.SetProperty(PID_SUBJECT, ProductName)
  164.     si.SetProperty(PID_AUTHOR, Manufacturer)
  165.     if Itanium:
  166.         si.SetProperty(PID_TEMPLATE, 'Intel64;1033')
  167.     elif AMD64:
  168.         si.SetProperty(PID_TEMPLATE, 'x64;1033')
  169.     else:
  170.         si.SetProperty(PID_TEMPLATE, 'Intel;1033')
  171.     si.SetProperty(PID_REVNUMBER, gen_uuid())
  172.     si.SetProperty(PID_WORDCOUNT, 2)
  173.     si.SetProperty(PID_PAGECOUNT, 200)
  174.     si.SetProperty(PID_APPNAME, 'Python MSI Library')
  175.     si.Persist()
  176.     add_data(db, 'Property', [
  177.         ('ProductName', ProductName),
  178.         ('ProductCode', ProductCode),
  179.         ('ProductVersion', ProductVersion),
  180.         ('Manufacturer', Manufacturer),
  181.         ('ProductLanguage', '1033')])
  182.     db.Commit()
  183.     return db
  184.  
  185.  
  186. def add_tables(db, module):
  187.     for table in module.tables:
  188.         add_data(db, table, getattr(module, table))
  189.     
  190.  
  191.  
  192. def make_id(str):
  193.     str = str.replace(' ', '_')
  194.     str = str.replace('-', '_')
  195.     if str[0] in string.digits:
  196.         str = '_' + str
  197.     
  198.     return str
  199.  
  200.  
  201. def gen_uuid():
  202.     return '{' + UuidCreate().upper() + '}'
  203.  
  204.  
  205. class CAB:
  206.     
  207.     def __init__(self, name):
  208.         self.name = name
  209.         self.files = []
  210.         self.filenames = set()
  211.         self.index = 0
  212.  
  213.     
  214.     def gen_id(self, file):
  215.         logical = _logical = make_id(file)
  216.         pos = 1
  217.         while logical in self.filenames:
  218.             logical = '%s.%d' % (_logical, pos)
  219.             pos += 1
  220.         self.filenames.add(logical)
  221.         return logical
  222.  
  223.     
  224.     def append(self, full, file, logical):
  225.         if os.path.isdir(full):
  226.             return None
  227.         if not logical:
  228.             logical = self.gen_id(file)
  229.         
  230.         self.index += 1
  231.         self.files.append((full, logical))
  232.         return (self.index, logical)
  233.  
  234.     
  235.     def commit(self, db):
  236.         mktemp = mktemp
  237.         import tempfile
  238.         filename = mktemp()
  239.         FCICreate(filename, self.files)
  240.         add_data(db, 'Media', [
  241.             (1, self.index, None, '#' + self.name, None, None)])
  242.         add_stream(db, self.name, filename)
  243.         os.unlink(filename)
  244.         db.Commit()
  245.  
  246.  
  247. _directories = set()
  248.  
  249. class Directory:
  250.     
  251.     def __init__(self, db, cab, basedir, physical, _logical, default, componentflags = None):
  252.         index = 1
  253.         _logical = make_id(_logical)
  254.         logical = _logical
  255.         while logical in _directories:
  256.             logical = '%s%d' % (_logical, index)
  257.             index += 1
  258.         _directories.add(logical)
  259.         self.db = db
  260.         self.cab = cab
  261.         self.basedir = basedir
  262.         self.physical = physical
  263.         self.logical = logical
  264.         self.component = None
  265.         self.short_names = set()
  266.         self.ids = set()
  267.         self.keyfiles = { }
  268.         self.componentflags = componentflags
  269.         if basedir:
  270.             self.absolute = os.path.join(basedir.absolute, physical)
  271.             blogical = basedir.logical
  272.         else:
  273.             self.absolute = physical
  274.             blogical = None
  275.         add_data(db, 'Directory', [
  276.             (logical, blogical, default)])
  277.  
  278.     
  279.     def start_component(self, component = None, feature = None, flags = None, keyfile = None, uuid = None):
  280.         if flags is None:
  281.             flags = self.componentflags
  282.         
  283.         if uuid is None:
  284.             uuid = gen_uuid()
  285.         else:
  286.             uuid = uuid.upper()
  287.         if component is None:
  288.             component = self.logical
  289.         
  290.         self.component = component
  291.         if Win64:
  292.             flags |= 256
  293.         
  294.         if keyfile:
  295.             keyid = self.cab.gen_id(self.absolute, keyfile)
  296.             self.keyfiles[keyfile] = keyid
  297.         else:
  298.             keyid = None
  299.         add_data(self.db, 'Component', [
  300.             (component, uuid, self.logical, flags, None, keyid)])
  301.         if feature is None:
  302.             feature = current_feature
  303.         
  304.         add_data(self.db, 'FeatureComponents', [
  305.             (feature.id, component)])
  306.  
  307.     
  308.     def make_short(self, file):
  309.         parts = file.split('.')
  310.         if len(parts) > 1:
  311.             suffix = parts[-1].upper()
  312.         else:
  313.             suffix = None
  314.         prefix = parts[0].upper()
  315.         if len(prefix) <= 8:
  316.             if not suffix or len(suffix) <= 3:
  317.                 if suffix:
  318.                     file = prefix + '.' + suffix
  319.                 else:
  320.                     file = prefix
  321.             else:
  322.                 prefix = prefix[:6]
  323.                 if suffix:
  324.                     suffix = suffix[:3]
  325.                 
  326.                 pos = 1
  327.                 while suffix:
  328.                     file = '%s~%d.%s' % (prefix, pos, suffix)
  329.                 len(suffix) <= 3
  330.                 file = '%s~%d' % (prefix, pos)
  331.                 if file not in self.short_names:
  332.                     break
  333.                 
  334.                 pos += 1
  335.                 if pos in (10, 100, 1000):
  336.                     prefix = prefix[:-1]
  337.                     continue
  338.             self.short_names.add(file)
  339.             return file
  340.  
  341.     
  342.     def add_file(self, file, src = None, version = None, language = None):
  343.         if not self.component:
  344.             self.start_component(self.logical, current_feature, 0)
  345.         
  346.         if not src:
  347.             src = file
  348.             file = os.path.basename(file)
  349.         
  350.         absolute = os.path.join(self.absolute, src)
  351.         if self.keyfiles.has_key(file):
  352.             logical = self.keyfiles[file]
  353.         else:
  354.             logical = None
  355.         (sequence, logical) = self.cab.append(absolute, file, logical)
  356.         self.ids.add(logical)
  357.         short = self.make_short(file)
  358.         full = '%s|%s' % (short, file)
  359.         filesize = os.stat(absolute).st_size
  360.         attributes = 512
  361.         add_data(self.db, 'File', [
  362.             (logical, self.component, full, filesize, version, language, attributes, sequence)])
  363.         if file.endswith('.py'):
  364.             add_data(self.db, 'RemoveFile', [
  365.                 (logical + 'c', self.component, '%sC|%sc' % (short, file), self.logical, 2),
  366.                 (logical + 'o', self.component, '%sO|%so' % (short, file), self.logical, 2)])
  367.         
  368.         return logical
  369.  
  370.     
  371.     def glob(self, pattern, exclude = None):
  372.         files = glob.glob1(self.absolute, pattern)
  373.         for f in files:
  374.             if exclude and f in exclude:
  375.                 continue
  376.             
  377.             self.add_file(f)
  378.         
  379.         return files
  380.  
  381.     
  382.     def remove_pyc(self):
  383.         add_data(self.db, 'RemoveFile', [
  384.             (self.component + 'c', self.component, '*.pyc', self.logical, 2),
  385.             (self.component + 'o', self.component, '*.pyo', self.logical, 2)])
  386.  
  387.  
  388.  
  389. class Binary:
  390.     
  391.     def __init__(self, fname):
  392.         self.name = fname
  393.  
  394.     
  395.     def __repr__(self):
  396.         return 'msilib.Binary(os.path.join(dirname,"%s"))' % self.name
  397.  
  398.  
  399.  
  400. class Feature:
  401.     
  402.     def __init__(self, db, id, title, desc, display, level = 1, parent = None, directory = None, attributes = 0):
  403.         self.id = id
  404.         if parent:
  405.             parent = parent.id
  406.         
  407.         add_data(db, 'Feature', [
  408.             (id, parent, title, desc, display, level, directory, attributes)])
  409.  
  410.     
  411.     def set_current(self):
  412.         global current_feature
  413.         current_feature = self
  414.  
  415.  
  416.  
  417. class Control:
  418.     
  419.     def __init__(self, dlg, name):
  420.         self.dlg = dlg
  421.         self.name = name
  422.  
  423.     
  424.     def event(self, event, argument, condition = '1', ordering = None):
  425.         add_data(self.dlg.db, 'ControlEvent', [
  426.             (self.dlg.name, self.name, event, argument, condition, ordering)])
  427.  
  428.     
  429.     def mapping(self, event, attribute):
  430.         add_data(self.dlg.db, 'EventMapping', [
  431.             (self.dlg.name, self.name, event, attribute)])
  432.  
  433.     
  434.     def condition(self, action, condition):
  435.         add_data(self.dlg.db, 'ControlCondition', [
  436.             (self.dlg.name, self.name, action, condition)])
  437.  
  438.  
  439.  
  440. class RadioButtonGroup(Control):
  441.     
  442.     def __init__(self, dlg, name, property):
  443.         self.dlg = dlg
  444.         self.name = name
  445.         self.property = property
  446.         self.index = 1
  447.  
  448.     
  449.     def add(self, name, x, y, w, h, text, value = None):
  450.         if value is None:
  451.             value = name
  452.         
  453.         add_data(self.dlg.db, 'RadioButton', [
  454.             (self.property, self.index, value, x, y, w, h, text, None)])
  455.         self.index += 1
  456.  
  457.  
  458.  
  459. class Dialog:
  460.     
  461.     def __init__(self, db, name, x, y, w, h, attr, title, first, default, cancel):
  462.         self.db = db
  463.         self.name = name
  464.         (self.x, self.y, self.w, self.h) = (x, y, w, h)
  465.         add_data(db, 'Dialog', [
  466.             (name, x, y, w, h, attr, title, first, default, cancel)])
  467.  
  468.     
  469.     def control(self, name, type, x, y, w, h, attr, prop, text, next, help):
  470.         add_data(self.db, 'Control', [
  471.             (self.name, name, type, x, y, w, h, attr, prop, text, next, help)])
  472.         return Control(self, name)
  473.  
  474.     
  475.     def text(self, name, x, y, w, h, attr, text):
  476.         return self.control(name, 'Text', x, y, w, h, attr, None, text, None, None)
  477.  
  478.     
  479.     def bitmap(self, name, x, y, w, h, text):
  480.         return self.control(name, 'Bitmap', x, y, w, h, 1, None, text, None, None)
  481.  
  482.     
  483.     def line(self, name, x, y, w, h):
  484.         return self.control(name, 'Line', x, y, w, h, 1, None, None, None, None)
  485.  
  486.     
  487.     def pushbutton(self, name, x, y, w, h, attr, text, next):
  488.         return self.control(name, 'PushButton', x, y, w, h, attr, None, text, next, None)
  489.  
  490.     
  491.     def radiogroup(self, name, x, y, w, h, attr, prop, text, next):
  492.         add_data(self.db, 'Control', [
  493.             (self.name, name, 'RadioButtonGroup', x, y, w, h, attr, prop, text, next, None)])
  494.         return RadioButtonGroup(self, name, prop)
  495.  
  496.     
  497.     def checkbox(self, name, x, y, w, h, attr, prop, text, next):
  498.         return self.control(name, 'CheckBox', x, y, w, h, attr, prop, text, next, None)
  499.  
  500.  
  501.