Show More
@@ -1,87 +1,94 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ Platform specific utility functions, win32 version |
|
3 | 3 | |
|
4 | 4 | Importing this module directly is not portable - rather, import platutils |
|
5 | 5 | to use these functions in platform agnostic fashion. |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | #***************************************************************************** |
|
9 | 9 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #***************************************************************************** |
|
14 | 14 | |
|
15 | 15 | import os |
|
16 | 16 | |
|
17 | 17 | ignore_termtitle = True |
|
18 | 18 | |
|
19 | 19 | try: |
|
20 | 20 | import ctypes |
|
21 | 21 | |
|
22 | 22 | SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW |
|
23 | 23 | SetConsoleTitleW.argtypes = [ctypes.c_wchar_p] |
|
24 | 24 | |
|
25 | 25 | def set_term_title(title): |
|
26 | 26 | """Set terminal title using ctypes to access the Win32 APIs.""" |
|
27 | 27 | SetConsoleTitleW(title) |
|
28 | 28 | |
|
29 | 29 | except ImportError: |
|
30 | 30 | def set_term_title(title): |
|
31 | 31 | """Set terminal title using the 'title' command.""" |
|
32 | 32 | global ignore_termtitle |
|
33 | 33 | |
|
34 | 34 | try: |
|
35 | 35 | # Cannot be on network share when issuing system commands |
|
36 | 36 | curr = os.getcwd() |
|
37 | 37 | os.chdir("C:") |
|
38 | 38 | ret = os.system("title " + title) |
|
39 | 39 | finally: |
|
40 | 40 | os.chdir(curr) |
|
41 | 41 | if ret: |
|
42 | 42 | # non-zero return code signals error, don't try again |
|
43 | 43 | ignore_termtitle = True |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | def find_cmd(cmd): |
|
47 | 47 | """Find the full path to a .bat or .exe using the win32api module.""" |
|
48 | 48 | try: |
|
49 | import win32api | |
|
49 | from win32api import SearchPath | |
|
50 | 50 | except ImportError: |
|
51 | 51 | raise ImportError('you need to have pywin32 installed for this to work') |
|
52 | 52 | else: |
|
53 | try: | |
|
54 | (path, offest) = win32api.SearchPath(os.environ['PATH'],cmd + '.exe') | |
|
55 | except: | |
|
56 | (path, offset) = win32api.SearchPath(os.environ['PATH'],cmd + '.bat') | |
|
57 | return path | |
|
53 | PATH = os.environ['PATH'] | |
|
54 | extensions = ['.exe', '.com', '.bat', '.py'] | |
|
55 | path = None | |
|
56 | for ext in extensions: | |
|
57 | try: | |
|
58 | path = SearchPath(PATH,cmd + ext)[0] | |
|
59 | except: | |
|
60 | pass | |
|
61 | if path is None: | |
|
62 | raise OSError("command %r not found" % cmd) | |
|
63 | else: | |
|
64 | return path | |
|
58 | 65 | |
|
59 | 66 | |
|
60 | 67 | def get_long_path_name(path): |
|
61 | 68 | """Get a long path name (expand ~) on Windows using ctypes. |
|
62 | 69 | |
|
63 | 70 | Examples |
|
64 | 71 | -------- |
|
65 | 72 | |
|
66 | 73 | >>> get_long_path_name('c:\\docume~1') |
|
67 | 74 | u'c:\\\\Documents and Settings' |
|
68 | 75 | |
|
69 | 76 | """ |
|
70 | 77 | try: |
|
71 | 78 | import ctypes |
|
72 | 79 | except ImportError: |
|
73 | 80 | raise ImportError('you need to have ctypes installed for this to work') |
|
74 | 81 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW |
|
75 | 82 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, |
|
76 | 83 | ctypes.c_uint ] |
|
77 | 84 | |
|
78 | 85 | buf = ctypes.create_unicode_buffer(260) |
|
79 | 86 | rv = _GetLongPathName(path, buf, 260) |
|
80 | 87 | if rv == 0 or rv > 260: |
|
81 | 88 | return path |
|
82 | 89 | else: |
|
83 | 90 | return buf.value |
|
84 | 91 | |
|
85 | 92 | |
|
86 | 93 | def term_clear(): |
|
87 | 94 | os.system('cls') |
General Comments 0
You need to be logged in to leave comments.
Login now