Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.8, 0.8.1, and 0.8.2

thgutil: new paths.py file for finding tortoisehg resources

this will require more work, needs to be aware of a __paths__.py file

Changeset f02c20f9a919

Parent 62eac655bb33

by Steve Borho

Changes to 2 files · Browse files at f02c20f9a919 Showing diff from parent 62eac655bb33 Diff from another changeset...

Change 1 of 1 Show Entire File thgutil/​paths.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@@ -0,0 +1,92 @@
+""" +paths.py + Copyright (C) 2009 Steve Borho <steve@borho.org> + +This software may be used and distributed according to the terms +of the GNU General Public License, incorporated herein by reference. +""" + +import os + +def find_root(path): + p = os.path.isdir(path) and path or os.path.dirname(path) + while not os.path.isdir(os.path.join(p, ".hg")): + oldp = p + p = os.path.dirname(p) + if p == oldp: + return None + if not os.access(p, os.R_OK): + return None + return p + +def get_tortoise_icon(icon): + '''Find a tortoise icon, apply to PyGtk window''' + path = os.path.join(get_prog_root(), 'icons', icon) + if os.path.isfile(path): + return path + else: + print 'icon not found', icon + return None + +if os.name == 'nt': + import _winreg + import win32net + USE_OK = 0 # network drive status + + def find_in_path(pgmname): + """ return first executable found in search path """ + ospath = os.environ['PATH'].split(os.pathsep) + pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD') + pathext = pathext.lower().split(os.pathsep) + for path in ospath: + ppath = os.path.join(path, pgmname) + for ext in pathext: + if os.path.exists(ppath + ext): + return ppath + ext + return None + + def get_prog_root(): + try: + return _winreg.QueryValue(_winreg.HKEY_LOCAL_MACHINE, + r"Software\TortoiseHg") + except: + return os.path.dirname(os.path.dirname(__file__)) + + def netdrive_status(drive): + """ + return True if a network drive is accessible (connected, ...), + or None if <drive> is not a network drive + """ + if hasattr(os.path, 'splitunc'): + unc, rest = os.path.splitunc(drive) + if unc: # All UNC paths (\\host\mount) are considered nonlocal + return True + letter = os.path.splitdrive(drive)[0] + _drives, total, _ = win32net.NetUseEnum(None, 1, 0) + for drv in _drives: + if drv['local'] == letter: + info = win32net.NetUseGetInfo(None, letter, 1) + return info['status'] == USE_OK + return False + +else: # Not Windows + + def find_in_path(pgmname): + """ return first executable found in search path """ + ospath = os.environ['PATH'].split(os.pathsep) + for path in ospath: + ppath = os.path.join(path, pgmname) + if os.access(ppath, os.X_OK): + return ppath + return None + + def get_prog_root(): + path = os.path.dirname(os.path.dirname(__file__)) + return os.path.dirname(path) + + def netdrive_status(drive): + """ + return True if a network drive is accessible (connected, ...), + or None if <drive> is not a network drive + """ + return None
Change 1 of 1 Show Entire File thgutil/​thgutil.py Stacked
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
@@ -1,186 +0,0 @@
-""" -thgutil.py - TortoiseHg utility functions - Copyright (C) 2007 TK Soh <teekaysoh@gmail.com> - -This software may be used and distributed according to the terms -of the GNU General Public License, incorporated herein by reference. - -""" - -import os.path - -def find_root(path): - p = os.path.isdir(path) and path or os.path.dirname(path) - while not os.path.isdir(os.path.join(p, ".hg")): - oldp = p - p = os.path.dirname(p) - if p == oldp: - return None - if not os.access(p, os.R_OK): - return None - return p - -if os.name == 'nt': - from win32com.shell import shell, shellcon - import win32con - import win32net - from win32gui import * - from win32api import * - import _winreg - - USE_OK = 0 # network drive status - - def find_path(pgmname, path=None, ext=None): - """ return first executable found in search path """ - ospath = path and path or os.environ['PATH'] - ospath = ospath.split(os.pathsep) - pathext = ext and ext or os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD') - pathext = pathext.lower().split(os.pathsep) - - for path in ospath: - ppath = os.path.join(path, pgmname) - for ext in pathext: - if os.path.exists(ppath + ext): - return ppath + ext - return None - - def get_icon_path(*args): - dir = get_prog_root() - icon = os.path.join(dir, "icons", *args) - if not os.path.isfile(icon): - return None - return icon - - def get_prog_root(): - key = r"Software\TortoiseHg" - cat = _winreg.HKEY_LOCAL_MACHINE - dir = _winreg.QueryValue(cat, key) - if 'THG_ICON_PATH' not in os.environ: - os.environ['THG_ICON_PATH'] = os.path.join(dir, 'icons') - return dir - - def get_tortoise_icon(icon): - '''Find a tortoise icon, apply to PyGtk window''' - # The context menu should set this variable - var = os.environ.get('THG_ICON_PATH', None) - paths = var and [ var ] or [] - try: - # Else try relative paths from hggtk, the repository layout - dir = os.path.dirname(__file__) - paths.append(os.path.join(dir, '..', 'icons')) - # ... or the source installer layout - paths.append(os.path.join(dir, '..', '..', '..', - 'share', 'tortoisehg', 'icons')) - except NameError: # __file__ is not always available - pass - for p in paths: - path = os.path.join(p, 'tortoise', icon) - if os.path.isfile(path): - return path - else: - print 'icon not found', icon - return None - - def netdrive_status(drive): - """ - return True if a network drive is accessible (connected, ...), - or None if <drive> is not a network drive - """ - if hasattr(os.path, 'splitunc'): - unc, rest = os.path.splitunc(drive) - if unc: # All UNC paths (\\host\mount) are considered nonlocal - return True - letter = os.path.splitdrive(drive)[0] - _drives, total, _ = win32net.NetUseEnum(None, 1, 0) - for drv in _drives: - if drv['local'] == letter: - info = win32net.NetUseGetInfo(None, letter, 1) - return info['status'] == USE_OK - return False - - bitmap_cache = {} - def icon_to_bitmap(iconPathName): - """ - create a bitmap based converted from an icon. - - adapted from pywin32's demo program win32gui_menu.py - """ - global bitmap_cache - - cx = GetSystemMetrics(win32con.SM_CXMENUCHECK) - cy = GetSystemMetrics(win32con.SM_CYMENUCHECK) - - # use icon image with size smaller but closer to menu size - if cx >= 16: - ico_x = ico_y = 16 - else: - ico_x = ico_y = 12 - ico_idx = "%d:%d", (cx, cy) - - # see if icon has been cached - try: - return bitmap_cache[iconPathName][ico_idx] - except: - pass - - hicon = LoadImage(0, iconPathName, win32con.IMAGE_ICON, ico_x, ico_y, - win32con.LR_LOADFROMFILE) - - hdcBitmap = CreateCompatibleDC(0) - hdcScreen = GetDC(0) - hbm = CreateCompatibleBitmap(hdcScreen, cx, cy) - hbmOld = SelectObject(hdcBitmap, hbm) - - # Fill the background. - brush = GetSysColorBrush(win32con.COLOR_MENU) - FillRect(hdcBitmap, (0, 0, cx, cy), brush) - - # we try to center the icon image within the bitmap without resizing - # the icon, so that the icon will be display as closely level to the - # menu text as possible. - startx = int((cx-ico_x)/2) - starty = int((cx-ico_y)/2) - DrawIconEx(hdcBitmap, startx, starty, hicon, ico_x, ico_y, 0, 0, - win32con.DI_NORMAL) - - # store bitmap to cache - if iconPathName not in bitmap_cache: - bitmap_cache[iconPathName] = {} - bitmap_cache[iconPathName][ico_idx] = hbm - - # restore settings - SelectObject(hdcBitmap, hbmOld) - DeleteDC(hdcBitmap) - DestroyIcon(hicon) - - return hbm - -else: # Not Windows - - def find_path(pgmname, path=None, ext=None): - """ return first executable found in search path """ - ospath = path and path or os.environ['PATH'] - ospath = ospath.split(os.pathsep) - for path in ospath: - ppath = os.path.join(path, pgmname) - if os.access(ppath, os.X_OK): - return ppath - return None - - def get_icon_path(*args): - return None - - def get_prog_root(): - defpath = os.path.dirname(os.path.dirname(__file__)) - path = os.environ.get('TORTOISEHG_PATH', defpath) - return os.path.isdir(path) and path or os.path.dirname(path) - - def netdrive_status(drive): - """ - return True if a network drive is accessible (connected, ...), - or None if <drive> is not a network drive - """ - return None - - def icon_to_bitmap(iconPathName): - pass