Show More
@@ -24,6 +24,7 b' from IPython.testing.skipdoctest import skip_doctest' | |||
|
24 | 24 | from IPython.utils.openpy import source_to_unicode |
|
25 | 25 | from IPython.utils.process import abbrev_cwd |
|
26 | 26 | from IPython.utils.terminal import set_term_title |
|
27 | from os import DirEntry | |
|
27 | 28 | |
|
28 | 29 | |
|
29 | 30 | @magics_class |
@@ -31,6 +32,51 b' class OSMagics(Magics):' | |||
|
31 | 32 | """Magics to interact with the underlying OS (shell-type functionality). |
|
32 | 33 | """ |
|
33 | 34 | |
|
35 | def __init__(self, shell=None, **kwargs): | |
|
36 | ||
|
37 | # Now define isexec in a cross platform manner. | |
|
38 | self.is_posix: bool = False | |
|
39 | self.execre = None | |
|
40 | if os.name == 'posix': | |
|
41 | self.is_posix = True | |
|
42 | else: | |
|
43 | try: | |
|
44 | winext = os.environ['pathext'].replace(';','|').replace('.','') | |
|
45 | except KeyError: | |
|
46 | winext = 'exe|com|bat|py' | |
|
47 | ||
|
48 | self.execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) | |
|
49 | ||
|
50 | # call up the chain | |
|
51 | super(OSMagics, self).__init__(shell=shell, **kwargs) | |
|
52 | ||
|
53 | ||
|
54 | @skip_doctest | |
|
55 | def _isexec_POSIX(self, f:DirEntry) -> bool: | |
|
56 | """ | |
|
57 | Test for executible on a POSIX system | |
|
58 | """ | |
|
59 | return f.is_file() and os.access(f.path, os.X_OK) | |
|
60 | ||
|
61 | ||
|
62 | @skip_doctest | |
|
63 | def _isexec_WIN(self, f:DirEntry) -> int: | |
|
64 | """ | |
|
65 | Test for executible file on non POSIX system | |
|
66 | """ | |
|
67 | return f.is_file() and self.execre.match(f.name) is not None | |
|
68 | ||
|
69 | @skip_doctest | |
|
70 | def isexec(self, f:DirEntry) -> bool: | |
|
71 | """ | |
|
72 | Test for executible file on non POSIX system | |
|
73 | """ | |
|
74 | if self.is_posix: | |
|
75 | return self._isexec_POSIX(f) | |
|
76 | else: | |
|
77 | return self._isexec_WIN(f) | |
|
78 | ||
|
79 | ||
|
34 | 80 | @skip_doctest |
|
35 | 81 | @line_magic |
|
36 | 82 | def alias(self, parameter_s=''): |
@@ -160,19 +206,6 b' class OSMagics(Magics):' | |||
|
160 | 206 | os.environ.get('PATH','').split(os.pathsep)] |
|
161 | 207 | |
|
162 | 208 | syscmdlist = [] |
|
163 | # Now define isexec in a cross platform manner. | |
|
164 | if os.name == 'posix': | |
|
165 | isexec = lambda fname:os.path.isfile(fname) and \ | |
|
166 | os.access(fname,os.X_OK) | |
|
167 | else: | |
|
168 | try: | |
|
169 | winext = os.environ['pathext'].replace(';','|').replace('.','') | |
|
170 | except KeyError: | |
|
171 | winext = 'exe|com|bat|py' | |
|
172 | if 'py' not in winext: | |
|
173 | winext += '|py' | |
|
174 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) | |
|
175 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) | |
|
176 | 209 | savedir = os.getcwd() |
|
177 | 210 | |
|
178 | 211 | # Now walk the paths looking for executables to alias. |
@@ -183,42 +216,44 b' class OSMagics(Magics):' | |||
|
183 | 216 | for pdir in path: |
|
184 | 217 | try: |
|
185 | 218 | os.chdir(pdir) |
|
186 | dirlist = os.listdir(pdir) | |
|
187 | 219 | except OSError: |
|
188 | 220 | continue |
|
221 | with os.scandir(pdir) as dirlist: | |
|
189 | 222 | for ff in dirlist: |
|
190 | if isexec(ff): | |
|
223 | if self.isexec(ff): | |
|
224 | fname = ff.name | |
|
191 | 225 | try: |
|
192 | 226 | # Removes dots from the name since ipython |
|
193 | 227 | # will assume names with dots to be python. |
|
194 |
if not self.shell.alias_manager.is_alias(f |
|
|
228 | if not self.shell.alias_manager.is_alias(fname): | |
|
195 | 229 | self.shell.alias_manager.define_alias( |
|
196 |
|
|
|
230 | fname.replace('.',''), fname) | |
|
197 | 231 | except InvalidAliasError: |
|
198 | 232 | pass |
|
199 | 233 | else: |
|
200 |
syscmdlist.append(f |
|
|
234 | syscmdlist.append(fname) | |
|
201 | 235 | else: |
|
202 | 236 | no_alias = Alias.blacklist |
|
203 | 237 | for pdir in path: |
|
204 | 238 | try: |
|
205 | 239 | os.chdir(pdir) |
|
206 | dirlist = os.listdir(pdir) | |
|
207 | 240 | except OSError: |
|
208 | 241 | continue |
|
242 | with os.scandir(pdir) as dirlist: | |
|
209 | 243 | for ff in dirlist: |
|
210 |
|
|
|
211 | if isexec(ff) and base.lower() not in no_alias: | |
|
244 | fname = ff.name | |
|
245 | base, ext = os.path.splitext(fname) | |
|
246 | if self.isexec(ff) and base.lower() not in no_alias: | |
|
212 | 247 | if ext.lower() == '.exe': |
|
213 |
|
|
|
248 | fname = base | |
|
214 | 249 | try: |
|
215 | 250 | # Removes dots from the name since ipython |
|
216 | 251 | # will assume names with dots to be python. |
|
217 | 252 | self.shell.alias_manager.define_alias( |
|
218 |
base.lower().replace('.',''), f |
|
|
253 | base.lower().replace('.',''), fname) | |
|
219 | 254 | except InvalidAliasError: |
|
220 | 255 | pass |
|
221 |
syscmdlist.append(f |
|
|
256 | syscmdlist.append(fname) | |
|
222 | 257 | self.shell.db['syscmdlist'] = syscmdlist |
|
223 | 258 | finally: |
|
224 | 259 | os.chdir(savedir) |
@@ -481,6 +516,7 b' class OSMagics(Magics):' | |||
|
481 | 516 | |
|
482 | 517 | dh = self.shell.user_ns['_dh'] |
|
483 | 518 | if parameter_s: |
|
519 | args = [] | |
|
484 | 520 | try: |
|
485 | 521 | args = map(int,parameter_s.split()) |
|
486 | 522 | except: |
@@ -96,27 +96,22 b" ipython locate profile foo # print the path to the directory for profile 'foo'" | |||
|
96 | 96 | |
|
97 | 97 | def list_profiles_in(path): |
|
98 | 98 | """list profiles in a given root directory""" |
|
99 | files = os.listdir(path) | |
|
100 | 99 | profiles = [] |
|
100 | with os.scandir(path) as files: | |
|
101 | 101 | for f in files: |
|
102 | try: | |
|
103 | full_path = os.path.join(path, f) | |
|
104 | except UnicodeError: | |
|
105 | continue | |
|
106 | if os.path.isdir(full_path) and f.startswith('profile_'): | |
|
107 | profiles.append(f.split('_',1)[-1]) | |
|
102 | if f.is_dir() and f.name.startswith('profile_'): | |
|
103 | profiles.append(f.name.split('_', 1)[-1]) | |
|
108 | 104 | return profiles |
|
109 | 105 | |
|
110 | 106 | |
|
111 | 107 | def list_bundled_profiles(): |
|
112 | 108 | """list profiles that are bundled with IPython.""" |
|
113 | 109 | path = os.path.join(get_ipython_package_dir(), u'core', u'profile') |
|
114 | files = os.listdir(path) | |
|
115 | 110 | profiles = [] |
|
111 | with os.scandir(path) as files: | |
|
116 | 112 | for profile in files: |
|
117 | full_path = os.path.join(path, profile) | |
|
118 | if os.path.isdir(full_path) and profile != "__pycache__": | |
|
119 | profiles.append(profile) | |
|
113 | if profile.is_dir() and profile.name != "__pycache__": | |
|
114 | profiles.append(profile.name) | |
|
120 | 115 | return profiles |
|
121 | 116 | |
|
122 | 117 |
General Comments 0
You need to be logged in to leave comments.
Login now