Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.5, 0.6, and 0.7

tortoise: revise registry handling for TortoiseOverlays.dll

implementation adapted from TortoiseBzr

Changeset 959fb18266a7

Parent 160e2fef05e9

by TK Soh

Changes to 3 files · Browse files at 959fb18266a7 Showing diff from parent 160e2fef05e9 Diff from another changeset...

 
129
130
131
132
133
134
135
136
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
139
140
 
129
130
131
 
 
 
 
 
 
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
@@ -129,12 +129,24 @@
  ]     registry_keys = [ - (_winreg.HKEY_CLASSES_ROOT, r"*\shellex\ContextMenuHandlers\TortoiseHg"), - (_winreg.HKEY_CLASSES_ROOT, r"Directory\Background\shellex\ContextMenuHandlers\TortoiseHg"), - (_winreg.HKEY_CLASSES_ROOT, r"Directory\shellex\ContextMenuHandlers\TortoiseHg"), - (_winreg.HKEY_CLASSES_ROOT, r"Folder\shellex\ContextMenuHandlers\TortoiseHg"), - (_winreg.HKEY_CLASSES_ROOT, r"Directory\shellex\DragDropHandlers\TortoiseHg"), - (_winreg.HKEY_CLASSES_ROOT, r"Folder\shellex\DragDropHandlers\TortoiseHg"), + (_winreg.HKEY_CLASSES_ROOT, + r"*\shellex\ContextMenuHandlers\TortoiseHg", + [(None, _reg_clsid_)]), + (_winreg.HKEY_CLASSES_ROOT, + r"Directory\Background\shellex\ContextMenuHandlers\TortoiseHg", + [(None, _reg_clsid_)]), + (_winreg.HKEY_CLASSES_ROOT, + r"Directory\shellex\ContextMenuHandlers\TortoiseHg", + [(None, _reg_clsid_)]), + (_winreg.HKEY_CLASSES_ROOT, + r"Folder\shellex\ContextMenuHandlers\TortoiseHg", + [(None, _reg_clsid_)]), + (_winreg.HKEY_CLASSES_ROOT, + r"Directory\shellex\DragDropHandlers\TortoiseHg", + [(None, _reg_clsid_)]), + (_winreg.HKEY_CLASSES_ROOT, + r"Folder\shellex\DragDropHandlers\TortoiseHg", + [(None, _reg_clsid_)]),   ]     def __init__(self):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
 
 
 
 
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
 
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
 # Published under the GNU GPL, v2 or later.  # Copyright (C) 2007 Henry Ludemann <misc@hl.id.au>  # Copyright (C) 2007 TK Soh <teekaysoh@gmail.com>    import os  import win32api  import win32con  from win32com.shell import shell, shellcon  import _winreg  from mercurial import hg, cmdutil, util  from mercurial import repo as _repo  import thgutil  import sys    # FIXME: quick workaround traceback caused by missing "closed"  # attribute in win32trace.  from mercurial import ui  def write_err(self, *args):   for a in args:   sys.stderr.write(str(a))  ui.ui.write_err = write_err    # file/directory status  UNCHANGED = "unchanged"  ADDED = "added"  MODIFIED = "modified"  UNKNOWN = "unknown"  NOT_IN_REPO = "n/a"    # file status cache  CACHE_TIMEOUT = 5000  overlay_cache = {}  cache_tick_count = 0  cache_root = None  cache_pdir = None    # some misc constants  S_OK = 0  S_FALSE = 1    def add_dirs(list):   dirs = set()   for f in list:   dir = os.path.dirname(f)   if dir in dirs:   continue   while dir:   dirs.add(dir)   dir = os.path.dirname(dir)   list.extend(dirs)    class IconOverlayExtension(object):   """   Class to implement icon overlays for source controlled files.     Displays a different icon based on version control status.     NOTE: The system allocates only 15 slots in _total_ for all   icon overlays; we (will) use 6, tortoisecvs uses 7... not a good   recipe for a happy system.   """     counter = 0     _com_interfaces_ = [shell.IID_IShellIconOverlayIdentifier]   _public_methods_ = [   "GetOverlayInfo", "GetPriority", "IsMemberOf"   ]   _reg_threading_ = 'Apartment'     def GetOverlayInfo(self):   return ("", 0, 0)     def GetPriority(self):   return 0     def _get_installed_overlays():   key = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE,   "Software\\Microsoft\\Windows\\" +   "CurrentVersion\\Explorer\\" +   "ShellIconOverlayIdentifiers",   0,   win32con.KEY_READ)   keys = win32api.RegEnumKeyEx(key)   handlercount = len(keys)   print "number of overlay handlers installed = %d" % handlercount   for i, k in enumerate(keys):   print i, k   win32api.RegCloseKey(key)   return handlercount     def _get_state(self, upath):   """   Get the state of a given path in source control.   """   global overlay_cache, cache_tick_count   global cache_root, cache_pdir     #print "called: _get_state(%s)" % path   tc = win32api.GetTickCount()     try:   # handle some Asian charsets   path = upath.encode('mbcs')   except:   path = upath     # check if path is cached   pdir = os.path.dirname(path)   if cache_pdir == pdir and overlay_cache:   if tc - cache_tick_count < CACHE_TIMEOUT:   try:   status = overlay_cache[path]   except:   status = UNKNOWN   print "%s: %s (cached)" % (path, status)   return status   else:   print "Timed out!!"   overlay_cache.clear()     # path is a drive   if path.endswith(":\\"):   overlay_cache[path] = UNKNOWN   return NOT_IN_REPO     # open repo   if cache_pdir == pdir:   root = cache_root   else:   print "find new root"   cache_pdir = pdir   cache_root = root = thgutil.find_root(pdir)   print "_get_state: root = ", root   if root is None:   print "_get_state: not in repo"   overlay_cache = {None : None}   cache_tick_count = win32api.GetTickCount()   return NOT_IN_REPO     try:   tc1 = win32api.GetTickCount()   repo = hg.repository(ui.ui(), path=root)   print "hg.repository() took %d ticks" % (win32api.GetTickCount() - tc1)     # check if to display overlay icons in this repo   global_opts = ui.ui().configlist('tortoisehg', 'overlayicons', [])   repo_opts = repo.ui.configlist('tortoisehg', 'overlayicons', [])     print "%s: global overlayicons = " % path, global_opts   print "%s: repo overlayicons = " % path, repo_opts   is_netdrive = thgutil.netdrive_status(path) is not None   if (is_netdrive and 'localdisks' in global_opts) \   or 'False' in repo_opts:   print "%s: overlayicons disabled" % path   overlay_cache = {None : None}   cache_tick_count = win32api.GetTickCount()   return NOT_IN_REPO   except _repo.RepoError:   # We aren't in a working tree   print "%s: not in repo" % dir   overlay_cache[path] = UNKNOWN   return NOT_IN_REPO     # get file status   tc1 = win32api.GetTickCount()     modified, added, removed, deleted = [], [], [], []   unknown, ignored, clean = [], [], []   files = []   try:   files, matchfn, anypats = cmdutil.matchpats(repo, [pdir])   modified, added, removed, deleted, unknown, ignored, clean = \   repo.status(files=files, list_ignored=True,   list_clean=True, list_unknown=True)     # add directory status to list   for grp in (clean,modified,added,removed,deleted,ignored,unknown):   add_dirs(grp)   except util.Abort, inst:   print "abort: %s" % inst   print "treat as unknown : %s" % path   return UNKNOWN     print "status() took %d ticks" % (win32api.GetTickCount() - tc1)     # cached file info   tc = win32api.GetTickCount()   overlay_cache = {}   for grp, st in (   (ignored, UNKNOWN),   (unknown, UNKNOWN),   (clean, UNCHANGED),   (added, ADDED),   (removed, MODIFIED),   (deleted, MODIFIED),   (modified, MODIFIED)):   for f in grp:   fpath = os.path.join(repo.root, os.path.normpath(f))   overlay_cache[fpath] = st     if path in overlay_cache:   status = overlay_cache[path]   else:   status = overlay_cache[path] = UNKNOWN   print "%s: %s" % (path, status)   cache_tick_count = win32api.GetTickCount()   return status     def IsMemberOf(self, path, attrib):   try:   tc = win32api.GetTickCount()   if self._get_state(path) == self.state:   return S_OK   return S_FALSE   finally:   print "IsMemberOf: _get_state() took %d ticks" % \   (win32api.GetTickCount() - tc)    def make_icon_overlay(name, icon_type, state, clsid):   """   Make an icon overlay COM class.     Used to create different COM server classes for highlighting the   files with different source controlled states (eg: unchanged,   modified, ...).   """   classname = "%sOverlay" % name   prog_id = "Mercurial.ShellExtension.%s" % classname   desc = "Mercurial icon overlay shell extension for %s files" % name.lower()   reg = [ - (_winreg.HKEY_LOCAL_MACHINE, r"Software\TortoiseOverlays\%s" % icon_type) ] + (_winreg.HKEY_LOCAL_MACHINE, + r"Software\TortoiseOverlays\%s" % icon_type, + [("TortoiseHg", clsid)]) + ]   cls = type(   classname,   (IconOverlayExtension, ),   dict(_reg_clsid_=clsid, _reg_progid_=prog_id, _reg_desc_=desc, registry_keys=reg, stringKey="HG", state=state))     _overlay_classes.append(cls)   # We need to register the class as global, as pythoncom will   # create an instance of it.   globals()[classname] = cls    _overlay_classes = []  make_icon_overlay("Changed", "Modified", MODIFIED, "{4D0F33E1-654C-4A1B-9BE8-E47A98752BAB}")  make_icon_overlay("Unchanged", "Normal", UNCHANGED, "{4D0F33E2-654C-4A1B-9BE8-E47A98752BAB}")  make_icon_overlay("Added", "Added", ADDED, "{4D0F33E3-654C-4A1B-9BE8-E47A98752BAB}")    def get_overlay_classes():   """   Get a list of all registerable icon overlay classes   """   return _overlay_classes
Change 1 of 5 Show Entire File tortoisehg.py Stacked
 
26
27
28
 
 
 
 
 
 
 
 
 
 
29
30
31
 
39
40
41
 
42
43
44
 
71
72
73
74
75
 
 
 
 
 
76
77
78
 
85
86
87
88
89
90
91
92
93
94
 
 
 
 
 
 
 
 
95
96
97
 
104
105
106
 
 
107
108
109
 
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
49
50
51
52
53
54
55
 
82
83
84
 
 
85
86
87
88
89
90
91
92
 
99
100
101
 
 
 
 
 
 
 
102
103
104
105
106
107
108
109
110
111
112
 
119
120
121
122
123
124
125
126
@@ -26,6 +26,16 @@
 bin_path = os.path.dirname(os.path.join(os.getcwd(), sys.argv[0]))  print "bin path = ", bin_path   +def check_tortoise_overlays(): + # TortoiseOverlays must be installed, and we must be able to write there. + try: + hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, + r"Software\TortoiseOverlays", 0, + _winreg.KEY_ALL_ACCESS) + except WindowsError: + print "TortoiseOverlays is not installed." + sys.exit(1) +  # TortoiseHg registry setup  def register_tortoise_path(unregister=False):   key = r"Software\TortoiseHg" @@ -39,6 +49,7 @@
   # for COM registration via py2exe  def DllRegisterServer(): + check_tortoise_overlays()   RegisterServer(ContextMenuExtension)   RegisterServer(ChangedOverlay)   RegisterServer(AddedOverlay) @@ -71,8 +82,11 @@
  pass     # Add the appropriate shell extension registry keys - for category, keyname in cls.registry_keys: - _winreg.SetValue(category, keyname, _winreg.REG_SZ, cls._reg_clsid_) + for category, keyname, values in cls.registry_keys: + hkey = _winreg.CreateKey(category, keyname) + for (name, val) in values: + # todo: handle ints? + _winreg.SetValueEx(hkey, name, 0, _winreg.REG_SZ, val)     # register the extension on Approved list   try: @@ -85,13 +99,14 @@
  print cls._reg_desc_, "registration complete."    def UnregisterServer(cls): - for category, keyname in cls.registry_keys: - try: - _winreg.DeleteKey(category, keyname) - except WindowsError, details: - import errno - if details.errno != errno.ENOENT: - raise + for category, keyname, values in cls.registry_keys: + hkey = _winreg.OpenKey(category, keyname, 0, _winreg.KEY_ALL_ACCESS) + for (name, val) in values: + # todo: handle ints? + try: + _winreg.DeleteValue(hkey, name) + except WindowsError, exc: + print "Failed to remove registry key %s: %s" % (keyname, exc)     # unregister the extension from Approved list   try: @@ -104,6 +119,8 @@
  print cls._reg_desc_, "unregistration complete."    if __name__=='__main__': + check_tortoise_overlays() +   from win32com.server import register   register.UseCommandLine(ContextMenuExtension,   finalize_register = lambda: RegisterServer(ContextMenuExtension),