Changeset 1330

Show
Ignore:
Timestamp:
05/18/08 04:42:48 (4 months ago)
Author:
klattimer
Message:

Committed registry.py with registry patching system

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • wine-doors/branches/0.2/src/registry.py

    r1324 r1330  
    11#/usr/bin/env python 
    2 import sys, os 
     2import os, sys, re 
    33 
    4 def MergeRegFiles( source, destination, output ): 
    5     pass 
     4class Registry: 
     5    def __init__ (self): 
     6        pass 
    67 
    7 def CompareRegFiles( source, destination, outpput ): 
    8     pass 
     8    def __load_registry(self, path): 
     9        """ 
     10        Load all registry files into a single dict, we exclude userdefs.reg but 
     11        can easily add it later if need be. 
     12        """ 
     13        hkcu = self.__load_reg_file("HKEY_CURRENT_USER",  
     14                                    os.path.join(path, "user.reg")) 
     15  
     16        hklm = self.__load_reg_file("HKEY_LOCAL_MACHINE",  
     17                                    os.path.join(path, "system.reg")) 
     18        reg = hkcu 
     19        reg.update(hklm) 
    920 
    10 def CompareRegistries( source, destination, output ): 
    11     if not source.endswith("/"): 
    12         source = source + "/" 
    13     if not destination.endswith("/"): 
    14         source = source + "/" 
    15     CompareRegFiles( source + "system.reg", destination + "system.reg" ) 
    16     CompareRegFiles( source + "user.reg", destination + "user.reg" ) 
    17     CompareRegFiles( source + "userdef.reg", destination + "userdef.reg" ) 
     21        return reg 
    1822 
    19 # TODO Move this to a utility executable 
    20 def Usage( name, error=None ): 
    21     if error: 
    22         print "Error: " + error 
    23     print "Wine-Doors registry tools (C) Karl Lattimer & Wine-Doors project 2008" 
    24     print "Usage: " + name + " <command> <arg1> <arg2> <output>" 
    25     print "Commands" 
    26     print "        --merge <patch1> <patch2>      - merge two registry patch files" 
    27     print "        --diff <source> <destination>  - Create a registry patch between two drives" 
    28     print "        --help, --usage                - show this!" 
    29     sys.exit(1) 
     23    def __load_reg_file(self, regbase, filename): 
     24        """ 
     25        Load a registry file of filename, the file is loaded as a dictionary of 
     26        dictionaries representing paths and key/value pairs 
     27        """ 
     28        regdir = re.compile('\[(.*?)\].*') 
     29        regkey = re.compile('(.*?)=(.*)') 
     30        reg = {} 
     31        regpath = None 
     32        regkeys = {} 
     33 
     34        f = open(filename, 'r') 
     35        for line in f.readlines(): 
     36            line = line.rstrip() 
     37            if line.startswith('['): 
     38                if regpath: 
     39                    reg[regpath] = regkeys 
     40                    regkeys = {} 
     41                m = regdir.match(line) 
     42                regpath = m.group(1).replace("\\\\", "\\") 
     43                regpath = "[" + regbase + "\\" + regpath + "]" 
     44            elif line.startswith('"') and regpath: 
     45                m = regkey.match(line) 
     46                regkeys[m.group(1)] = m.group(2) 
     47        reg[regpath] = regkeys 
     48        f.close() 
     49 
     50        return reg 
     51 
     52    def CreateRegistryPatch(self, source, destination, output=None): 
     53        """ 
     54        Create a .reg file to be patched onto the wine registry 
     55        @source: original registry path 
     56        @destination: how source should be after the patch is applied 
     57        """ 
     58        src = self.__load_registry(source) 
     59        dst = self.__load_registry(destination) 
     60 
     61        patch = {} 
     62        keys = {} 
     63        for path in dst: 
     64            for key in dst[path]: 
     65                if path in src and key in src[path]: 
     66                    if dst[path][key] != src[path][key]: 
     67                        keys[key] = dst[path][key] 
     68                else: 
     69                    keys[key] = dst[path][key] 
     70 
     71            if len(keys) > 0: 
     72                patch[path] = keys 
     73                keys = {} 
     74 
     75        if output: 
     76            f = open(output, 'w') 
     77        else: 
     78            f = sys.stdout 
     79 
     80        f.write("REGEDIT 4\n") 
     81        for path in patch: 
     82            f.write("\n" + path + "\n") 
     83            for key in patch[path]: 
     84                f.write(key + "=" + patch[path][key] + "\n") 
     85            f.flush() 
     86 
     87        if output: 
     88            f.close() 
    3089 
    3190if __name__ == "__main__": 
    32     if len(sys.argv) == 1: 
    33         Usage ( sys.argv[0], "No command specified" ) 
     91    if len(sys.argv) < 1: 
     92        print "Unit test requires two paths as arguments, source and destination wine drives\nincluding .reg files" 
     93    reg = Registry() 
     94    reg.CreateRegistryPatch(sys.argv[1], sys.argv[2]) 
    3495 
    35     for arg in range(len(sys.argv)): 
    36         if arg == 0: 
    37             continue 
    38         elif ( ( sys.argv[arg] == '--help' ) or ( sys.argv[arg] == '--usage' ) ): 
    39             Usage( sys.argv[0] ) 
    40         elif ( sys.argv[arg] == '--merge' ): 
    41             if not os.path.isfile ( sys.argv[arg+1] ): 
    42                 Usage (sys.argv[0], "patch does not exist" + sys.argv[arg+1]) 
    43             elif not os.path.isfile ( sys.argv[arg+2] ): 
    44                 Usage (sys.argv[0], "patch does not exist" + sys.argv[arg+2]) 
    45             MergeRegFiles( sys.argv[arg+1], sys.argv[arg+2], sys.argv[arg+3] ) 
    46         elif ( sys.argv[arg] == '--diff' ): 
    47             if not os.path.isdir ( sys.argv[arg+1] ): 
    48                 Usage (sys.argv[0], "Source path does not exist") 
    49             elif not os.path.isdir ( sys.argv[arg+2] ): 
    50                 Usage (sys.argv[0], "Destination path does not exist") 
    51             CompareRegistries( sys.argv[arg+1], sys.argv[arg+2], sys.argv[arg+3] ) 
    52         elif ( sys.argv[arg].startswith("--") ): 
    53             Usage( sys.argv[0], "Invalid command" )