##// END OF EJS Templates
added scandir and fixed how isexec is created
Kory Donati -
Show More
@@ -24,6 +24,7 b' from IPython.testing.skipdoctest import skip_doctest'
24 from IPython.utils.openpy import source_to_unicode
24 from IPython.utils.openpy import source_to_unicode
25 from IPython.utils.process import abbrev_cwd
25 from IPython.utils.process import abbrev_cwd
26 from IPython.utils.terminal import set_term_title
26 from IPython.utils.terminal import set_term_title
27 from os import DirEntry
27
28
28
29
29 @magics_class
30 @magics_class
@@ -31,6 +32,51 b' class OSMagics(Magics):'
31 """Magics to interact with the underlying OS (shell-type functionality).
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 @skip_doctest
80 @skip_doctest
35 @line_magic
81 @line_magic
36 def alias(self, parameter_s=''):
82 def alias(self, parameter_s=''):
@@ -160,19 +206,6 b' class OSMagics(Magics):'
160 os.environ.get('PATH','').split(os.pathsep)]
206 os.environ.get('PATH','').split(os.pathsep)]
161
207
162 syscmdlist = []
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 savedir = os.getcwd()
209 savedir = os.getcwd()
177
210
178 # Now walk the paths looking for executables to alias.
211 # Now walk the paths looking for executables to alias.
@@ -183,42 +216,44 b' class OSMagics(Magics):'
183 for pdir in path:
216 for pdir in path:
184 try:
217 try:
185 os.chdir(pdir)
218 os.chdir(pdir)
186 dirlist = os.listdir(pdir)
187 except OSError:
219 except OSError:
188 continue
220 continue
189 for ff in dirlist:
221 with os.scandir(pdir) as dirlist:
190 if isexec(ff):
222 for ff in dirlist:
191 try:
223 if self.isexec(ff):
192 # Removes dots from the name since ipython
224 fname = ff.name
193 # will assume names with dots to be python.
225 try:
194 if not self.shell.alias_manager.is_alias(ff):
226 # Removes dots from the name since ipython
195 self.shell.alias_manager.define_alias(
227 # will assume names with dots to be python.
196 ff.replace('.',''), ff)
228 if not self.shell.alias_manager.is_alias(fname):
197 except InvalidAliasError:
229 self.shell.alias_manager.define_alias(
198 pass
230 fname.replace('.',''), fname)
199 else:
231 except InvalidAliasError:
200 syscmdlist.append(ff)
232 pass
233 else:
234 syscmdlist.append(fname)
201 else:
235 else:
202 no_alias = Alias.blacklist
236 no_alias = Alias.blacklist
203 for pdir in path:
237 for pdir in path:
204 try:
238 try:
205 os.chdir(pdir)
239 os.chdir(pdir)
206 dirlist = os.listdir(pdir)
207 except OSError:
240 except OSError:
208 continue
241 continue
209 for ff in dirlist:
242 with os.scandir(pdir) as dirlist:
210 base, ext = os.path.splitext(ff)
243 for ff in dirlist:
211 if isexec(ff) and base.lower() not in no_alias:
244 fname = ff.name
212 if ext.lower() == '.exe':
245 base, ext = os.path.splitext(fname)
213 ff = base
246 if self.isexec(ff) and base.lower() not in no_alias:
214 try:
247 if ext.lower() == '.exe':
215 # Removes dots from the name since ipython
248 fname = base
216 # will assume names with dots to be python.
249 try:
217 self.shell.alias_manager.define_alias(
250 # Removes dots from the name since ipython
218 base.lower().replace('.',''), ff)
251 # will assume names with dots to be python.
219 except InvalidAliasError:
252 self.shell.alias_manager.define_alias(
220 pass
253 base.lower().replace('.',''), fname)
221 syscmdlist.append(ff)
254 except InvalidAliasError:
255 pass
256 syscmdlist.append(fname)
222 self.shell.db['syscmdlist'] = syscmdlist
257 self.shell.db['syscmdlist'] = syscmdlist
223 finally:
258 finally:
224 os.chdir(savedir)
259 os.chdir(savedir)
@@ -481,6 +516,7 b' class OSMagics(Magics):'
481
516
482 dh = self.shell.user_ns['_dh']
517 dh = self.shell.user_ns['_dh']
483 if parameter_s:
518 if parameter_s:
519 args = []
484 try:
520 try:
485 args = map(int,parameter_s.split())
521 args = map(int,parameter_s.split())
486 except:
522 except:
@@ -96,27 +96,22 b" ipython locate profile foo # print the path to the directory for profile 'foo'"
96
96
97 def list_profiles_in(path):
97 def list_profiles_in(path):
98 """list profiles in a given root directory"""
98 """list profiles in a given root directory"""
99 files = os.listdir(path)
100 profiles = []
99 profiles = []
101 for f in files:
100 with os.scandir(path) as files:
102 try:
101 for f in files:
103 full_path = os.path.join(path, f)
102 if f.is_dir() and f.name.startswith('profile_'):
104 except UnicodeError:
103 profiles.append(f.name.split('_', 1)[-1])
105 continue
106 if os.path.isdir(full_path) and f.startswith('profile_'):
107 profiles.append(f.split('_',1)[-1])
108 return profiles
104 return profiles
109
105
110
106
111 def list_bundled_profiles():
107 def list_bundled_profiles():
112 """list profiles that are bundled with IPython."""
108 """list profiles that are bundled with IPython."""
113 path = os.path.join(get_ipython_package_dir(), u'core', u'profile')
109 path = os.path.join(get_ipython_package_dir(), u'core', u'profile')
114 files = os.listdir(path)
115 profiles = []
110 profiles = []
116 for profile in files:
111 with os.scandir(path) as files:
117 full_path = os.path.join(path, profile)
112 for profile in files:
118 if os.path.isdir(full_path) and profile != "__pycache__":
113 if profile.is_dir() and profile.name != "__pycache__":
119 profiles.append(profile)
114 profiles.append(profile.name)
120 return profiles
115 return profiles
121
116
122
117
General Comments 0
You need to be logged in to leave comments. Login now