| 1 | n/a | # Microsoft Installer Library |
|---|
| 2 | n/a | # (C) 2003 Martin v. Loewis |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | import win32com.client.gencache |
|---|
| 5 | n/a | import win32com.client |
|---|
| 6 | n/a | import pythoncom, pywintypes |
|---|
| 7 | n/a | from win32com.client import constants |
|---|
| 8 | n/a | import re, string, os, sets, glob, subprocess, sys, _winreg, struct, _msi |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | try: |
|---|
| 11 | n/a | basestring |
|---|
| 12 | n/a | except NameError: |
|---|
| 13 | n/a | basestring = (str, unicode) |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | # Partially taken from Wine |
|---|
| 16 | n/a | datasizemask= 0x00ff |
|---|
| 17 | n/a | type_valid= 0x0100 |
|---|
| 18 | n/a | type_localizable= 0x0200 |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | typemask= 0x0c00 |
|---|
| 21 | n/a | type_long= 0x0000 |
|---|
| 22 | n/a | type_short= 0x0400 |
|---|
| 23 | n/a | type_string= 0x0c00 |
|---|
| 24 | n/a | type_binary= 0x0800 |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | type_nullable= 0x1000 |
|---|
| 27 | n/a | type_key= 0x2000 |
|---|
| 28 | n/a | # XXX temporary, localizable? |
|---|
| 29 | n/a | knownbits = datasizemask | type_valid | type_localizable | \ |
|---|
| 30 | n/a | typemask | type_nullable | type_key |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | # Summary Info Property IDs |
|---|
| 33 | n/a | PID_CODEPAGE=1 |
|---|
| 34 | n/a | PID_TITLE=2 |
|---|
| 35 | n/a | PID_SUBJECT=3 |
|---|
| 36 | n/a | PID_AUTHOR=4 |
|---|
| 37 | n/a | PID_KEYWORDS=5 |
|---|
| 38 | n/a | PID_COMMENTS=6 |
|---|
| 39 | n/a | PID_TEMPLATE=7 |
|---|
| 40 | n/a | PID_LASTAUTHOR=8 |
|---|
| 41 | n/a | PID_REVNUMBER=9 |
|---|
| 42 | n/a | PID_LASTPRINTED=11 |
|---|
| 43 | n/a | PID_CREATE_DTM=12 |
|---|
| 44 | n/a | PID_LASTSAVE_DTM=13 |
|---|
| 45 | n/a | PID_PAGECOUNT=14 |
|---|
| 46 | n/a | PID_WORDCOUNT=15 |
|---|
| 47 | n/a | PID_CHARCOUNT=16 |
|---|
| 48 | n/a | PID_APPNAME=18 |
|---|
| 49 | n/a | PID_SECURITY=19 |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | def reset(): |
|---|
| 52 | n/a | global _directories |
|---|
| 53 | n/a | _directories = sets.Set() |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | def EnsureMSI(): |
|---|
| 56 | n/a | win32com.client.gencache.EnsureModule('{000C1092-0000-0000-C000-000000000046}', 1033, 1, 0) |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | def EnsureMSM(): |
|---|
| 59 | n/a | try: |
|---|
| 60 | n/a | win32com.client.gencache.EnsureModule('{0ADDA82F-2C26-11D2-AD65-00A0C9AF11A6}', 0, 1, 0) |
|---|
| 61 | n/a | except pywintypes.com_error: |
|---|
| 62 | n/a | win32com.client.gencache.EnsureModule('{0ADDA82F-2C26-11D2-AD65-00A0C9AF11A6}', 0, 2, 0) |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | _Installer=None |
|---|
| 65 | n/a | def MakeInstaller(): |
|---|
| 66 | n/a | global _Installer |
|---|
| 67 | n/a | if _Installer is None: |
|---|
| 68 | n/a | EnsureMSI() |
|---|
| 69 | n/a | _Installer = win32com.client.Dispatch('WindowsInstaller.Installer', |
|---|
| 70 | n/a | resultCLSID='{000C1090-0000-0000-C000-000000000046}') |
|---|
| 71 | n/a | return _Installer |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | _Merge=None |
|---|
| 74 | n/a | def MakeMerge2(): |
|---|
| 75 | n/a | global _Merge |
|---|
| 76 | n/a | if _Merge is None: |
|---|
| 77 | n/a | EnsureMSM() |
|---|
| 78 | n/a | _Merge = win32com.client.Dispatch("Msm.Merge2.1") |
|---|
| 79 | n/a | return _Merge |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | class Table: |
|---|
| 82 | n/a | def __init__(self, name): |
|---|
| 83 | n/a | self.name = name |
|---|
| 84 | n/a | self.fields = [] |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | def add_field(self, index, name, type): |
|---|
| 87 | n/a | self.fields.append((index,name,type)) |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | def sql(self): |
|---|
| 90 | n/a | fields = [] |
|---|
| 91 | n/a | keys = [] |
|---|
| 92 | n/a | self.fields.sort() |
|---|
| 93 | n/a | fields = [None]*len(self.fields) |
|---|
| 94 | n/a | for index, name, type in self.fields: |
|---|
| 95 | n/a | index -= 1 |
|---|
| 96 | n/a | unk = type & ~knownbits |
|---|
| 97 | n/a | if unk: |
|---|
| 98 | n/a | print "%s.%s unknown bits %x" % (self.name, name, unk) |
|---|
| 99 | n/a | size = type & datasizemask |
|---|
| 100 | n/a | dtype = type & typemask |
|---|
| 101 | n/a | if dtype == type_string: |
|---|
| 102 | n/a | if size: |
|---|
| 103 | n/a | tname="CHAR(%d)" % size |
|---|
| 104 | n/a | else: |
|---|
| 105 | n/a | tname="CHAR" |
|---|
| 106 | n/a | elif dtype == type_short: |
|---|
| 107 | n/a | assert size==2 |
|---|
| 108 | n/a | tname = "SHORT" |
|---|
| 109 | n/a | elif dtype == type_long: |
|---|
| 110 | n/a | assert size==4 |
|---|
| 111 | n/a | tname="LONG" |
|---|
| 112 | n/a | elif dtype == type_binary: |
|---|
| 113 | n/a | assert size==0 |
|---|
| 114 | n/a | tname="OBJECT" |
|---|
| 115 | n/a | else: |
|---|
| 116 | n/a | tname="unknown" |
|---|
| 117 | n/a | print "%s.%sunknown integer type %d" % (self.name, name, size) |
|---|
| 118 | n/a | if type & type_nullable: |
|---|
| 119 | n/a | flags = "" |
|---|
| 120 | n/a | else: |
|---|
| 121 | n/a | flags = " NOT NULL" |
|---|
| 122 | n/a | if type & type_localizable: |
|---|
| 123 | n/a | flags += " LOCALIZABLE" |
|---|
| 124 | n/a | fields[index] = "`%s` %s%s" % (name, tname, flags) |
|---|
| 125 | n/a | if type & type_key: |
|---|
| 126 | n/a | keys.append("`%s`" % name) |
|---|
| 127 | n/a | fields = ", ".join(fields) |
|---|
| 128 | n/a | keys = ", ".join(keys) |
|---|
| 129 | n/a | return "CREATE TABLE %s (%s PRIMARY KEY %s)" % (self.name, fields, keys) |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def create(self, db): |
|---|
| 132 | n/a | v = db.OpenView(self.sql()) |
|---|
| 133 | n/a | v.Execute(None) |
|---|
| 134 | n/a | v.Close() |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | class Binary: |
|---|
| 137 | n/a | def __init__(self, fname): |
|---|
| 138 | n/a | self.name = fname |
|---|
| 139 | n/a | def __repr__(self): |
|---|
| 140 | n/a | return 'msilib.Binary(os.path.join(dirname,"%s"))' % self.name |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | def gen_schema(destpath, schemapath): |
|---|
| 143 | n/a | d = MakeInstaller() |
|---|
| 144 | n/a | schema = d.OpenDatabase(schemapath, |
|---|
| 145 | n/a | win32com.client.constants.msiOpenDatabaseModeReadOnly) |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | # XXX ORBER BY |
|---|
| 148 | n/a | v=schema.OpenView("SELECT * FROM _Columns") |
|---|
| 149 | n/a | curtable=None |
|---|
| 150 | n/a | tables = [] |
|---|
| 151 | n/a | v.Execute(None) |
|---|
| 152 | n/a | f = open(destpath, "wt") |
|---|
| 153 | n/a | f.write("from msilib import Table\n") |
|---|
| 154 | n/a | while 1: |
|---|
| 155 | n/a | r=v.Fetch() |
|---|
| 156 | n/a | if not r:break |
|---|
| 157 | n/a | name=r.StringData(1) |
|---|
| 158 | n/a | if curtable != name: |
|---|
| 159 | n/a | f.write("\n%s = Table('%s')\n" % (name,name)) |
|---|
| 160 | n/a | curtable = name |
|---|
| 161 | n/a | tables.append(name) |
|---|
| 162 | n/a | f.write("%s.add_field(%d,'%s',%d)\n" % |
|---|
| 163 | n/a | (name, r.IntegerData(2), r.StringData(3), r.IntegerData(4))) |
|---|
| 164 | n/a | v.Close() |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | f.write("\ntables=[%s]\n\n" % (", ".join(tables))) |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | # Fill the _Validation table |
|---|
| 169 | n/a | f.write("_Validation_records = [\n") |
|---|
| 170 | n/a | v = schema.OpenView("SELECT * FROM _Validation") |
|---|
| 171 | n/a | v.Execute(None) |
|---|
| 172 | n/a | while 1: |
|---|
| 173 | n/a | r = v.Fetch() |
|---|
| 174 | n/a | if not r:break |
|---|
| 175 | n/a | # Table, Column, Nullable |
|---|
| 176 | n/a | f.write("(%s,%s,%s," % |
|---|
| 177 | n/a | (`r.StringData(1)`, `r.StringData(2)`, `r.StringData(3)`)) |
|---|
| 178 | n/a | def put_int(i): |
|---|
| 179 | n/a | if r.IsNull(i):f.write("None, ") |
|---|
| 180 | n/a | else:f.write("%d," % r.IntegerData(i)) |
|---|
| 181 | n/a | def put_str(i): |
|---|
| 182 | n/a | if r.IsNull(i):f.write("None, ") |
|---|
| 183 | n/a | else:f.write("%s," % `r.StringData(i)`) |
|---|
| 184 | n/a | put_int(4) # MinValue |
|---|
| 185 | n/a | put_int(5) # MaxValue |
|---|
| 186 | n/a | put_str(6) # KeyTable |
|---|
| 187 | n/a | put_int(7) # KeyColumn |
|---|
| 188 | n/a | put_str(8) # Category |
|---|
| 189 | n/a | put_str(9) # Set |
|---|
| 190 | n/a | put_str(10)# Description |
|---|
| 191 | n/a | f.write("),\n") |
|---|
| 192 | n/a | f.write("]\n\n") |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | f.close() |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | def gen_sequence(destpath, msipath): |
|---|
| 197 | n/a | dir = os.path.dirname(destpath) |
|---|
| 198 | n/a | d = MakeInstaller() |
|---|
| 199 | n/a | seqmsi = d.OpenDatabase(msipath, |
|---|
| 200 | n/a | win32com.client.constants.msiOpenDatabaseModeReadOnly) |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | v = seqmsi.OpenView("SELECT * FROM _Tables"); |
|---|
| 203 | n/a | v.Execute(None) |
|---|
| 204 | n/a | f = open(destpath, "w") |
|---|
| 205 | n/a | print >>f, "import msilib,os;dirname=os.path.dirname(__file__)" |
|---|
| 206 | n/a | tables = [] |
|---|
| 207 | n/a | while 1: |
|---|
| 208 | n/a | r = v.Fetch() |
|---|
| 209 | n/a | if not r:break |
|---|
| 210 | n/a | table = r.StringData(1) |
|---|
| 211 | n/a | tables.append(table) |
|---|
| 212 | n/a | f.write("%s = [\n" % table) |
|---|
| 213 | n/a | v1 = seqmsi.OpenView("SELECT * FROM `%s`" % table) |
|---|
| 214 | n/a | v1.Execute(None) |
|---|
| 215 | n/a | info = v1.ColumnInfo(constants.msiColumnInfoTypes) |
|---|
| 216 | n/a | while 1: |
|---|
| 217 | n/a | r = v1.Fetch() |
|---|
| 218 | n/a | if not r:break |
|---|
| 219 | n/a | rec = [] |
|---|
| 220 | n/a | for i in range(1,r.FieldCount+1): |
|---|
| 221 | n/a | if r.IsNull(i): |
|---|
| 222 | n/a | rec.append(None) |
|---|
| 223 | n/a | elif info.StringData(i)[0] in "iI": |
|---|
| 224 | n/a | rec.append(r.IntegerData(i)) |
|---|
| 225 | n/a | elif info.StringData(i)[0] in "slSL": |
|---|
| 226 | n/a | rec.append(r.StringData(i)) |
|---|
| 227 | n/a | elif info.StringData(i)[0]=="v": |
|---|
| 228 | n/a | size = r.DataSize(i) |
|---|
| 229 | n/a | bytes = r.ReadStream(i, size, constants.msiReadStreamBytes) |
|---|
| 230 | n/a | bytes = bytes.encode("latin-1") # binary data represented "as-is" |
|---|
| 231 | n/a | if table == "Binary": |
|---|
| 232 | n/a | fname = rec[0]+".bin" |
|---|
| 233 | n/a | open(os.path.join(dir,fname),"wb").write(bytes) |
|---|
| 234 | n/a | rec.append(Binary(fname)) |
|---|
| 235 | n/a | else: |
|---|
| 236 | n/a | rec.append(bytes) |
|---|
| 237 | n/a | else: |
|---|
| 238 | n/a | raise "Unsupported column type", info.StringData(i) |
|---|
| 239 | n/a | f.write(repr(tuple(rec))+",\n") |
|---|
| 240 | n/a | v1.Close() |
|---|
| 241 | n/a | f.write("]\n\n") |
|---|
| 242 | n/a | v.Close() |
|---|
| 243 | n/a | f.write("tables=%s\n" % repr(map(str,tables))) |
|---|
| 244 | n/a | f.close() |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | class _Unspecified:pass |
|---|
| 247 | n/a | def change_sequence(seq, action, seqno=_Unspecified, cond = _Unspecified): |
|---|
| 248 | n/a | "Change the sequence number of an action in a sequence list" |
|---|
| 249 | n/a | for i in range(len(seq)): |
|---|
| 250 | n/a | if seq[i][0] == action: |
|---|
| 251 | n/a | if cond is _Unspecified: |
|---|
| 252 | n/a | cond = seq[i][1] |
|---|
| 253 | n/a | if seqno is _Unspecified: |
|---|
| 254 | n/a | seqno = seq[i][2] |
|---|
| 255 | n/a | seq[i] = (action, cond, seqno) |
|---|
| 256 | n/a | return |
|---|
| 257 | n/a | raise ValueError, "Action not found in sequence" |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | def add_data(db, table, values): |
|---|
| 260 | n/a | d = MakeInstaller() |
|---|
| 261 | n/a | v = db.OpenView("SELECT * FROM `%s`" % table) |
|---|
| 262 | n/a | count = v.ColumnInfo(0).FieldCount |
|---|
| 263 | n/a | r = d.CreateRecord(count) |
|---|
| 264 | n/a | for value in values: |
|---|
| 265 | n/a | assert len(value) == count, value |
|---|
| 266 | n/a | for i in range(count): |
|---|
| 267 | n/a | field = value[i] |
|---|
| 268 | n/a | if isinstance(field, (int, long)): |
|---|
| 269 | n/a | r.SetIntegerData(i+1,field) |
|---|
| 270 | n/a | elif isinstance(field, basestring): |
|---|
| 271 | n/a | r.SetStringData(i+1,field) |
|---|
| 272 | n/a | elif field is None: |
|---|
| 273 | n/a | pass |
|---|
| 274 | n/a | elif isinstance(field, Binary): |
|---|
| 275 | n/a | r.SetStream(i+1, field.name) |
|---|
| 276 | n/a | else: |
|---|
| 277 | n/a | raise TypeError, "Unsupported type %s" % field.__class__.__name__ |
|---|
| 278 | n/a | v.Modify(win32com.client.constants.msiViewModifyInsert, r) |
|---|
| 279 | n/a | r.ClearData() |
|---|
| 280 | n/a | v.Close() |
|---|
| 281 | n/a | |
|---|
| 282 | n/a | def add_stream(db, name, path): |
|---|
| 283 | n/a | d = MakeInstaller() |
|---|
| 284 | n/a | v = db.OpenView("INSERT INTO _Streams (Name, Data) VALUES ('%s', ?)" % name) |
|---|
| 285 | n/a | r = d.CreateRecord(1) |
|---|
| 286 | n/a | r.SetStream(1, path) |
|---|
| 287 | n/a | v.Execute(r) |
|---|
| 288 | n/a | v.Close() |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | def init_database(name, schema, |
|---|
| 291 | n/a | ProductName, ProductCode, ProductVersion, |
|---|
| 292 | n/a | Manufacturer, |
|---|
| 293 | n/a | request_uac = False): |
|---|
| 294 | n/a | try: |
|---|
| 295 | n/a | os.unlink(name) |
|---|
| 296 | n/a | except OSError: |
|---|
| 297 | n/a | pass |
|---|
| 298 | n/a | ProductCode = ProductCode.upper() |
|---|
| 299 | n/a | d = MakeInstaller() |
|---|
| 300 | n/a | # Create the database |
|---|
| 301 | n/a | db = d.OpenDatabase(name, |
|---|
| 302 | n/a | win32com.client.constants.msiOpenDatabaseModeCreate) |
|---|
| 303 | n/a | # Create the tables |
|---|
| 304 | n/a | for t in schema.tables: |
|---|
| 305 | n/a | t.create(db) |
|---|
| 306 | n/a | # Fill the validation table |
|---|
| 307 | n/a | add_data(db, "_Validation", schema._Validation_records) |
|---|
| 308 | n/a | # Initialize the summary information, allowing atmost 20 properties |
|---|
| 309 | n/a | si = db.GetSummaryInformation(20) |
|---|
| 310 | n/a | si.SetProperty(PID_TITLE, "Installation Database") |
|---|
| 311 | n/a | si.SetProperty(PID_SUBJECT, ProductName) |
|---|
| 312 | n/a | si.SetProperty(PID_AUTHOR, Manufacturer) |
|---|
| 313 | n/a | si.SetProperty(PID_TEMPLATE, msi_type) |
|---|
| 314 | n/a | si.SetProperty(PID_REVNUMBER, gen_uuid()) |
|---|
| 315 | n/a | if request_uac: |
|---|
| 316 | n/a | wc = 2 # long file names, compressed, original media |
|---|
| 317 | n/a | else: |
|---|
| 318 | n/a | wc = 2 | 8 # +never invoke UAC |
|---|
| 319 | n/a | si.SetProperty(PID_WORDCOUNT, wc) |
|---|
| 320 | n/a | si.SetProperty(PID_PAGECOUNT, 200) |
|---|
| 321 | n/a | si.SetProperty(PID_APPNAME, "Python MSI Library") |
|---|
| 322 | n/a | # XXX more properties |
|---|
| 323 | n/a | si.Persist() |
|---|
| 324 | n/a | add_data(db, "Property", [ |
|---|
| 325 | n/a | ("ProductName", ProductName), |
|---|
| 326 | n/a | ("ProductCode", ProductCode), |
|---|
| 327 | n/a | ("ProductVersion", ProductVersion), |
|---|
| 328 | n/a | ("Manufacturer", Manufacturer), |
|---|
| 329 | n/a | ("ProductLanguage", "1033")]) |
|---|
| 330 | n/a | db.Commit() |
|---|
| 331 | n/a | return db |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | def add_tables(db, module): |
|---|
| 334 | n/a | for table in module.tables: |
|---|
| 335 | n/a | add_data(db, table, getattr(module, table)) |
|---|
| 336 | n/a | |
|---|
| 337 | n/a | def make_id(str): |
|---|
| 338 | n/a | #str = str.replace(".", "_") # colons are allowed |
|---|
| 339 | n/a | str = str.replace(" ", "_") |
|---|
| 340 | n/a | str = str.replace("-", "_") |
|---|
| 341 | n/a | str = str.replace("+", "_") |
|---|
| 342 | n/a | if str[0] in string.digits: |
|---|
| 343 | n/a | str = "_"+str |
|---|
| 344 | n/a | assert re.match("^[A-Za-z_][A-Za-z0-9_.]*$", str), "FILE"+str |
|---|
| 345 | n/a | return str |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | def gen_uuid(): |
|---|
| 348 | n/a | return str(pythoncom.CreateGuid()) |
|---|
| 349 | n/a | |
|---|
| 350 | n/a | class CAB: |
|---|
| 351 | n/a | def __init__(self, name): |
|---|
| 352 | n/a | self.name = name |
|---|
| 353 | n/a | self.files = [] |
|---|
| 354 | n/a | self.filenames = sets.Set() |
|---|
| 355 | n/a | self.index = 0 |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | def gen_id(self, dir, file): |
|---|
| 358 | n/a | logical = _logical = make_id(file) |
|---|
| 359 | n/a | pos = 1 |
|---|
| 360 | n/a | while logical in self.filenames: |
|---|
| 361 | n/a | logical = "%s.%d" % (_logical, pos) |
|---|
| 362 | n/a | pos += 1 |
|---|
| 363 | n/a | self.filenames.add(logical) |
|---|
| 364 | n/a | return logical |
|---|
| 365 | n/a | |
|---|
| 366 | n/a | def append(self, full, file, logical = None): |
|---|
| 367 | n/a | if os.path.isdir(full): |
|---|
| 368 | n/a | return |
|---|
| 369 | n/a | if not logical: |
|---|
| 370 | n/a | logical = self.gen_id(dir, file) |
|---|
| 371 | n/a | self.index += 1 |
|---|
| 372 | n/a | self.files.append((full, logical)) |
|---|
| 373 | n/a | return self.index, logical |
|---|
| 374 | n/a | |
|---|
| 375 | n/a | def commit(self, db): |
|---|
| 376 | n/a | try: |
|---|
| 377 | n/a | os.unlink(self.name+".cab") |
|---|
| 378 | n/a | except OSError: |
|---|
| 379 | n/a | pass |
|---|
| 380 | n/a | _msi.FCICreate(self.name+".cab", self.files) |
|---|
| 381 | n/a | add_data(db, "Media", |
|---|
| 382 | n/a | [(1, self.index, None, "#"+self.name, None, None)]) |
|---|
| 383 | n/a | add_stream(db, self.name, self.name+".cab") |
|---|
| 384 | n/a | os.unlink(self.name+".cab") |
|---|
| 385 | n/a | db.Commit() |
|---|
| 386 | n/a | |
|---|
| 387 | n/a | _directories = sets.Set() |
|---|
| 388 | n/a | class Directory: |
|---|
| 389 | n/a | def __init__(self, db, cab, basedir, physical, _logical, default, componentflags=None): |
|---|
| 390 | n/a | """Create a new directory in the Directory table. There is a current component |
|---|
| 391 | n/a | at each point in time for the directory, which is either explicitly created |
|---|
| 392 | n/a | through start_component, or implicitly when files are added for the first |
|---|
| 393 | n/a | time. Files are added into the current component, and into the cab file. |
|---|
| 394 | n/a | To create a directory, a base directory object needs to be specified (can be |
|---|
| 395 | n/a | None), the path to the physical directory, and a logical directory name. |
|---|
| 396 | n/a | Default specifies the DefaultDir slot in the directory table. componentflags |
|---|
| 397 | n/a | specifies the default flags that new components get.""" |
|---|
| 398 | n/a | index = 1 |
|---|
| 399 | n/a | _logical = make_id(_logical) |
|---|
| 400 | n/a | logical = _logical |
|---|
| 401 | n/a | while logical in _directories: |
|---|
| 402 | n/a | logical = "%s%d" % (_logical, index) |
|---|
| 403 | n/a | index += 1 |
|---|
| 404 | n/a | _directories.add(logical) |
|---|
| 405 | n/a | self.db = db |
|---|
| 406 | n/a | self.cab = cab |
|---|
| 407 | n/a | self.basedir = basedir |
|---|
| 408 | n/a | self.physical = physical |
|---|
| 409 | n/a | self.logical = logical |
|---|
| 410 | n/a | self.component = None |
|---|
| 411 | n/a | self.short_names = {} |
|---|
| 412 | n/a | self.ids = sets.Set() |
|---|
| 413 | n/a | self.keyfiles = {} |
|---|
| 414 | n/a | self.componentflags = componentflags |
|---|
| 415 | n/a | if basedir: |
|---|
| 416 | n/a | self.absolute = os.path.join(basedir.absolute, physical) |
|---|
| 417 | n/a | blogical = basedir.logical |
|---|
| 418 | n/a | else: |
|---|
| 419 | n/a | self.absolute = physical |
|---|
| 420 | n/a | blogical = None |
|---|
| 421 | n/a | # initially assume that all files in this directory are unpackaged |
|---|
| 422 | n/a | # as files from self.absolute get added, this set is reduced |
|---|
| 423 | n/a | self.unpackaged_files = set() |
|---|
| 424 | n/a | for f in os.listdir(self.absolute): |
|---|
| 425 | n/a | if os.path.isfile(os.path.join(self.absolute, f)): |
|---|
| 426 | n/a | self.unpackaged_files.add(f) |
|---|
| 427 | n/a | add_data(db, "Directory", [(logical, blogical, default)]) |
|---|
| 428 | n/a | |
|---|
| 429 | n/a | def start_component(self, component = None, feature = None, flags = None, keyfile = None, uuid=None): |
|---|
| 430 | n/a | """Add an entry to the Component table, and make this component the current for this |
|---|
| 431 | n/a | directory. If no component name is given, the directory name is used. If no feature |
|---|
| 432 | n/a | is given, the current feature is used. If no flags are given, the directory's default |
|---|
| 433 | n/a | flags are used. If no keyfile is given, the KeyPath is left null in the Component |
|---|
| 434 | n/a | table.""" |
|---|
| 435 | n/a | if flags is None: |
|---|
| 436 | n/a | flags = self.componentflags |
|---|
| 437 | n/a | if uuid is None: |
|---|
| 438 | n/a | uuid = gen_uuid() |
|---|
| 439 | n/a | else: |
|---|
| 440 | n/a | uuid = uuid.upper() |
|---|
| 441 | n/a | if component is None: |
|---|
| 442 | n/a | component = self.logical |
|---|
| 443 | n/a | self.component = component |
|---|
| 444 | n/a | if Win64: |
|---|
| 445 | n/a | flags |= 256 |
|---|
| 446 | n/a | if keyfile: |
|---|
| 447 | n/a | keyid = self.cab.gen_id(self.absolute, keyfile) |
|---|
| 448 | n/a | self.keyfiles[keyfile] = keyid |
|---|
| 449 | n/a | else: |
|---|
| 450 | n/a | keyid = None |
|---|
| 451 | n/a | add_data(self.db, "Component", |
|---|
| 452 | n/a | [(component, uuid, self.logical, flags, None, keyid)]) |
|---|
| 453 | n/a | if feature is None: |
|---|
| 454 | n/a | feature = current_feature |
|---|
| 455 | n/a | add_data(self.db, "FeatureComponents", |
|---|
| 456 | n/a | [(feature.id, component)]) |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | def make_short(self, file): |
|---|
| 459 | n/a | long = file |
|---|
| 460 | n/a | file = re.sub(r'[\?|><:/*"+,;=\[\]]', '_', file) # restrictions on short names |
|---|
| 461 | n/a | parts = file.split(".", 1) |
|---|
| 462 | n/a | if len(parts)>1: |
|---|
| 463 | n/a | suffix = parts[1].upper() |
|---|
| 464 | n/a | else: |
|---|
| 465 | n/a | suffix = '' |
|---|
| 466 | n/a | prefix = parts[0].upper() |
|---|
| 467 | n/a | if len(prefix) <= 8 and '.' not in suffix and len(suffix) <= 3: |
|---|
| 468 | n/a | if suffix: |
|---|
| 469 | n/a | file = prefix+"."+suffix |
|---|
| 470 | n/a | else: |
|---|
| 471 | n/a | file = prefix |
|---|
| 472 | n/a | assert file not in self.short_names, (file, self.short_names[file]) |
|---|
| 473 | n/a | else: |
|---|
| 474 | n/a | prefix = prefix[:6] |
|---|
| 475 | n/a | if suffix: |
|---|
| 476 | n/a | # last three characters of last suffix |
|---|
| 477 | n/a | suffix = suffix.rsplit('.')[-1][:3] |
|---|
| 478 | n/a | pos = 1 |
|---|
| 479 | n/a | while 1: |
|---|
| 480 | n/a | if suffix: |
|---|
| 481 | n/a | file = "%s~%d.%s" % (prefix, pos, suffix) |
|---|
| 482 | n/a | else: |
|---|
| 483 | n/a | file = "%s~%d" % (prefix, pos) |
|---|
| 484 | n/a | if file not in self.short_names: break |
|---|
| 485 | n/a | pos += 1 |
|---|
| 486 | n/a | assert pos < 10000 |
|---|
| 487 | n/a | if pos in (10, 100, 1000): |
|---|
| 488 | n/a | prefix = prefix[:-1] |
|---|
| 489 | n/a | self.short_names[file] = long |
|---|
| 490 | n/a | return file |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | def add_file(self, file, src=None, version=None, language=None): |
|---|
| 493 | n/a | """Add a file to the current component of the directory, starting a new one |
|---|
| 494 | n/a | if there is no current component. By default, the file name in the source |
|---|
| 495 | n/a | and the file table will be identical. If the src file is specified, it is |
|---|
| 496 | n/a | interpreted relative to the current directory. Optionally, a version and a |
|---|
| 497 | n/a | language can be specified for the entry in the File table.""" |
|---|
| 498 | n/a | if not self.component: |
|---|
| 499 | n/a | self.start_component(self.logical, current_feature) |
|---|
| 500 | n/a | if not src: |
|---|
| 501 | n/a | # Allow relative paths for file if src is not specified |
|---|
| 502 | n/a | src = file |
|---|
| 503 | n/a | file = os.path.basename(file) |
|---|
| 504 | n/a | absolute = os.path.join(self.absolute, src) |
|---|
| 505 | n/a | if absolute.startswith(self.absolute): |
|---|
| 506 | n/a | # mark file as packaged |
|---|
| 507 | n/a | relative = absolute[len(self.absolute)+1:] |
|---|
| 508 | n/a | if relative in self.unpackaged_files: |
|---|
| 509 | n/a | self.unpackaged_files.remove(relative) |
|---|
| 510 | n/a | assert not re.search(r'[\?|><:/*]"', file) # restrictions on long names |
|---|
| 511 | n/a | if self.keyfiles.has_key(file): |
|---|
| 512 | n/a | logical = self.keyfiles[file] |
|---|
| 513 | n/a | else: |
|---|
| 514 | n/a | logical = None |
|---|
| 515 | n/a | sequence, logical = self.cab.append(absolute, file, logical) |
|---|
| 516 | n/a | assert logical not in self.ids |
|---|
| 517 | n/a | self.ids.add(logical) |
|---|
| 518 | n/a | short = self.make_short(file) |
|---|
| 519 | n/a | full = "%s|%s" % (short, file) |
|---|
| 520 | n/a | filesize = os.stat(absolute).st_size |
|---|
| 521 | n/a | # constants.msidbFileAttributesVital |
|---|
| 522 | n/a | # Compressed omitted, since it is the database default |
|---|
| 523 | n/a | # could add r/o, system, hidden |
|---|
| 524 | n/a | attributes = 512 |
|---|
| 525 | n/a | add_data(self.db, "File", |
|---|
| 526 | n/a | [(logical, self.component, full, filesize, version, |
|---|
| 527 | n/a | language, attributes, sequence)]) |
|---|
| 528 | n/a | if not version: |
|---|
| 529 | n/a | # Add hash if the file is not versioned |
|---|
| 530 | n/a | filehash = MakeInstaller().FileHash(absolute, 0) |
|---|
| 531 | n/a | add_data(self.db, "MsiFileHash", |
|---|
| 532 | n/a | [(logical, 0, filehash.IntegerData(1), |
|---|
| 533 | n/a | filehash.IntegerData(2), filehash.IntegerData(3), |
|---|
| 534 | n/a | filehash.IntegerData(4))]) |
|---|
| 535 | n/a | # Automatically remove .pyc/.pyo files on uninstall (2) |
|---|
| 536 | n/a | # XXX: adding so many RemoveFile entries makes installer unbelievably |
|---|
| 537 | n/a | # slow. So instead, we have to use wildcard remove entries |
|---|
| 538 | n/a | # if file.endswith(".py"): |
|---|
| 539 | n/a | # add_data(self.db, "RemoveFile", |
|---|
| 540 | n/a | # [(logical+"c", self.component, "%sC|%sc" % (short, file), |
|---|
| 541 | n/a | # self.logical, 2), |
|---|
| 542 | n/a | # (logical+"o", self.component, "%sO|%so" % (short, file), |
|---|
| 543 | n/a | # self.logical, 2)]) |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | def glob(self, pattern, exclude = None): |
|---|
| 546 | n/a | """Add a list of files to the current component as specified in the |
|---|
| 547 | n/a | glob pattern. Individual files can be excluded in the exclude list.""" |
|---|
| 548 | n/a | files = glob.glob1(self.absolute, pattern) |
|---|
| 549 | n/a | for f in files: |
|---|
| 550 | n/a | if exclude and f in exclude: continue |
|---|
| 551 | n/a | self.add_file(f) |
|---|
| 552 | n/a | return files |
|---|
| 553 | n/a | |
|---|
| 554 | n/a | def remove_pyc(self): |
|---|
| 555 | n/a | "Remove .pyc/.pyo files from __pycache__ on uninstall" |
|---|
| 556 | n/a | directory = self.logical + "_pycache" |
|---|
| 557 | n/a | add_data(self.db, "Directory", [(directory, self.logical, "__PYCA~1|__pycache__")]) |
|---|
| 558 | n/a | flags = 256 if Win64 else 0 |
|---|
| 559 | n/a | add_data(self.db, "Component", |
|---|
| 560 | n/a | [(directory, gen_uuid(), directory, flags, None, None)]) |
|---|
| 561 | n/a | add_data(self.db, "FeatureComponents", [(current_feature.id, directory)]) |
|---|
| 562 | n/a | add_data(self.db, "CreateFolder", [(directory, directory)]) |
|---|
| 563 | n/a | add_data(self.db, "RemoveFile", |
|---|
| 564 | n/a | [(self.component, self.component, "*.*", directory, 2), |
|---|
| 565 | n/a | ]) |
|---|
| 566 | n/a | |
|---|
| 567 | n/a | def removefile(self, key, pattern): |
|---|
| 568 | n/a | "Add a RemoveFile entry" |
|---|
| 569 | n/a | add_data(self.db, "RemoveFile", [(self.component+key, self.component, pattern, self.logical, 2)]) |
|---|
| 570 | n/a | |
|---|
| 571 | n/a | |
|---|
| 572 | n/a | class Feature: |
|---|
| 573 | n/a | def __init__(self, db, id, title, desc, display, level = 1, |
|---|
| 574 | n/a | parent=None, directory = None, attributes=0): |
|---|
| 575 | n/a | self.id = id |
|---|
| 576 | n/a | if parent: |
|---|
| 577 | n/a | parent = parent.id |
|---|
| 578 | n/a | add_data(db, "Feature", |
|---|
| 579 | n/a | [(id, parent, title, desc, display, |
|---|
| 580 | n/a | level, directory, attributes)]) |
|---|
| 581 | n/a | def set_current(self): |
|---|
| 582 | n/a | global current_feature |
|---|
| 583 | n/a | current_feature = self |
|---|
| 584 | n/a | |
|---|
| 585 | n/a | class Control: |
|---|
| 586 | n/a | def __init__(self, dlg, name): |
|---|
| 587 | n/a | self.dlg = dlg |
|---|
| 588 | n/a | self.name = name |
|---|
| 589 | n/a | |
|---|
| 590 | n/a | def event(self, ev, arg, cond = "1", order = None): |
|---|
| 591 | n/a | add_data(self.dlg.db, "ControlEvent", |
|---|
| 592 | n/a | [(self.dlg.name, self.name, ev, arg, cond, order)]) |
|---|
| 593 | n/a | |
|---|
| 594 | n/a | def mapping(self, ev, attr): |
|---|
| 595 | n/a | add_data(self.dlg.db, "EventMapping", |
|---|
| 596 | n/a | [(self.dlg.name, self.name, ev, attr)]) |
|---|
| 597 | n/a | |
|---|
| 598 | n/a | def condition(self, action, condition): |
|---|
| 599 | n/a | add_data(self.dlg.db, "ControlCondition", |
|---|
| 600 | n/a | [(self.dlg.name, self.name, action, condition)]) |
|---|
| 601 | n/a | |
|---|
| 602 | n/a | class RadioButtonGroup(Control): |
|---|
| 603 | n/a | def __init__(self, dlg, name, property): |
|---|
| 604 | n/a | self.dlg = dlg |
|---|
| 605 | n/a | self.name = name |
|---|
| 606 | n/a | self.property = property |
|---|
| 607 | n/a | self.index = 1 |
|---|
| 608 | n/a | |
|---|
| 609 | n/a | def add(self, name, x, y, w, h, text, value = None): |
|---|
| 610 | n/a | if value is None: |
|---|
| 611 | n/a | value = name |
|---|
| 612 | n/a | add_data(self.dlg.db, "RadioButton", |
|---|
| 613 | n/a | [(self.property, self.index, value, |
|---|
| 614 | n/a | x, y, w, h, text, None)]) |
|---|
| 615 | n/a | self.index += 1 |
|---|
| 616 | n/a | |
|---|
| 617 | n/a | class Dialog: |
|---|
| 618 | n/a | def __init__(self, db, name, x, y, w, h, attr, title, first, default, cancel): |
|---|
| 619 | n/a | self.db = db |
|---|
| 620 | n/a | self.name = name |
|---|
| 621 | n/a | self.x, self.y, self.w, self.h = x,y,w,h |
|---|
| 622 | n/a | add_data(db, "Dialog", [(name, x,y,w,h,attr,title,first,default,cancel)]) |
|---|
| 623 | n/a | |
|---|
| 624 | n/a | def control(self, name, type, x, y, w, h, attr, prop, text, next, help): |
|---|
| 625 | n/a | add_data(self.db, "Control", |
|---|
| 626 | n/a | [(self.name, name, type, x, y, w, h, attr, prop, text, next, help)]) |
|---|
| 627 | n/a | return Control(self, name) |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | def text(self, name, x, y, w, h, attr, text): |
|---|
| 630 | n/a | return self.control(name, "Text", x, y, w, h, attr, None, |
|---|
| 631 | n/a | text, None, None) |
|---|
| 632 | n/a | |
|---|
| 633 | n/a | def bitmap(self, name, x, y, w, h, text): |
|---|
| 634 | n/a | return self.control(name, "Bitmap", x, y, w, h, 1, None, text, None, None) |
|---|
| 635 | n/a | |
|---|
| 636 | n/a | def line(self, name, x, y, w, h): |
|---|
| 637 | n/a | return self.control(name, "Line", x, y, w, h, 1, None, None, None, None) |
|---|
| 638 | n/a | |
|---|
| 639 | n/a | def pushbutton(self, name, x, y, w, h, attr, text, next): |
|---|
| 640 | n/a | return self.control(name, "PushButton", x, y, w, h, attr, None, text, next, None) |
|---|
| 641 | n/a | |
|---|
| 642 | n/a | def radiogroup(self, name, x, y, w, h, attr, prop, text, next): |
|---|
| 643 | n/a | add_data(self.db, "Control", |
|---|
| 644 | n/a | [(self.name, name, "RadioButtonGroup", |
|---|
| 645 | n/a | x, y, w, h, attr, prop, text, next, None)]) |
|---|
| 646 | n/a | return RadioButtonGroup(self, name, prop) |
|---|
| 647 | n/a | |
|---|
| 648 | n/a | def checkbox(self, name, x, y, w, h, attr, prop, text, next): |
|---|
| 649 | n/a | return self.control(name, "CheckBox", x, y, w, h, attr, prop, text, next, None) |
|---|
| 650 | n/a | |
|---|
| 651 | n/a | def pe_type(path): |
|---|
| 652 | n/a | header = open(path, "rb").read(1000) |
|---|
| 653 | n/a | # offset of PE header is at offset 0x3c |
|---|
| 654 | n/a | pe_offset = struct.unpack("<i", header[0x3c:0x40])[0] |
|---|
| 655 | n/a | assert header[pe_offset:pe_offset+4] == "PE\0\0" |
|---|
| 656 | n/a | machine = struct.unpack("<H", header[pe_offset+4:pe_offset+6])[0] |
|---|
| 657 | n/a | return machine |
|---|
| 658 | n/a | |
|---|
| 659 | n/a | def set_arch_from_file(path): |
|---|
| 660 | n/a | global msi_type, Win64, arch_ext |
|---|
| 661 | n/a | machine = pe_type(path) |
|---|
| 662 | n/a | if machine == 0x14c: |
|---|
| 663 | n/a | # i386 |
|---|
| 664 | n/a | msi_type = "Intel" |
|---|
| 665 | n/a | Win64 = 0 |
|---|
| 666 | n/a | arch_ext = '' |
|---|
| 667 | n/a | elif machine == 0x200: |
|---|
| 668 | n/a | # Itanium |
|---|
| 669 | n/a | msi_type = "Intel64" |
|---|
| 670 | n/a | Win64 = 1 |
|---|
| 671 | n/a | arch_ext = '.ia64' |
|---|
| 672 | n/a | elif machine == 0x8664: |
|---|
| 673 | n/a | # AMD64 |
|---|
| 674 | n/a | msi_type = "x64" |
|---|
| 675 | n/a | Win64 = 1 |
|---|
| 676 | n/a | arch_ext = '.amd64' |
|---|
| 677 | n/a | else: |
|---|
| 678 | n/a | raise ValueError, "Unsupported architecture" |
|---|
| 679 | n/a | msi_type += ";1033" |
|---|