##// END OF EJS Templates
Merge pull request #11365 from kd2718/kd2718/os_scandir_prep...
Matthias Bussonnier -
r24688:32eec539 merge
parent child Browse files
Show More
@@ -31,6 +31,51 b' class OSMagics(Magics):'
31 """Magics to interact with the underlying OS (shell-type functionality).
31 """Magics to interact with the underlying OS (shell-type functionality).
32 """
32 """
33
33
34 def __init__(self, shell=None, **kwargs):
35
36 # Now define isexec in a cross platform manner.
37 self.is_posix = False
38 self.execre = None
39 if os.name == 'posix':
40 self.is_posix = True
41 else:
42 try:
43 winext = os.environ['pathext'].replace(';','|').replace('.','')
44 except KeyError:
45 winext = 'exe|com|bat|py'
46
47 self.execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
48
49 # call up the chain
50 super().__init__(shell=shell, **kwargs)
51
52
53 @skip_doctest
54 def _isexec_POSIX(self, file):
55 """
56 Test for executible on a POSIX system
57 """
58 return file.is_file() and os.access(file.path, os.X_OK)
59
60
61 @skip_doctest
62 def _isexec_WIN(self, file):
63 """
64 Test for executible file on non POSIX system
65 """
66 return file.is_file() and self.execre.match(file.name) is not None
67
68 @skip_doctest
69 def isexec(self, file):
70 """
71 Test for executible file on non POSIX system
72 """
73 if self.is_posix:
74 return self._isexec_POSIX(file)
75 else:
76 return self._isexec_WIN(file)
77
78
34 @skip_doctest
79 @skip_doctest
35 @line_magic
80 @line_magic
36 def alias(self, parameter_s=''):
81 def alias(self, parameter_s=''):
@@ -160,65 +205,59 b' class OSMagics(Magics):'
160 os.environ.get('PATH','').split(os.pathsep)]
205 os.environ.get('PATH','').split(os.pathsep)]
161
206
162 syscmdlist = []
207 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()
208 savedir = os.getcwd()
177
209
178 # Now walk the paths looking for executables to alias.
210 # Now walk the paths looking for executables to alias.
179 try:
211 try:
180 # write the whole loop for posix/Windows so we don't have an if in
212 # write the whole loop for posix/Windows so we don't have an if in
181 # the innermost part
213 # the innermost part
182 if os.name == 'posix':
214 if self.is_posix:
183 for pdir in path:
215 for pdir in path:
184 try:
216 try:
185 os.chdir(pdir)
217 os.chdir(pdir)
186 dirlist = os.listdir(pdir)
187 except OSError:
218 except OSError:
188 continue
219 continue
220
221 # for python 3.6+ rewrite to: with os.scandir(pdir) as dirlist:
222 dirlist = os.scandir(path=pdir)
189 for ff in dirlist:
223 for ff in dirlist:
190 if isexec(ff):
224 if self.isexec(ff):
225 fname = ff.name
191 try:
226 try:
192 # Removes dots from the name since ipython
227 # Removes dots from the name since ipython
193 # will assume names with dots to be python.
228 # will assume names with dots to be python.
194 if not self.shell.alias_manager.is_alias(ff):
229 if not self.shell.alias_manager.is_alias(fname):
195 self.shell.alias_manager.define_alias(
230 self.shell.alias_manager.define_alias(
196 ff.replace('.',''), ff)
231 fname.replace('.',''), fname)
197 except InvalidAliasError:
232 except InvalidAliasError:
198 pass
233 pass
199 else:
234 else:
200 syscmdlist.append(ff)
235 syscmdlist.append(fname)
201 else:
236 else:
202 no_alias = Alias.blacklist
237 no_alias = Alias.blacklist
203 for pdir in path:
238 for pdir in path:
204 try:
239 try:
205 os.chdir(pdir)
240 os.chdir(pdir)
206 dirlist = os.listdir(pdir)
207 except OSError:
241 except OSError:
208 continue
242 continue
243
244 # for python 3.6+ rewrite to: with os.scandir(pdir) as dirlist:
245 dirlist = os.scandir(pdir)
209 for ff in dirlist:
246 for ff in dirlist:
210 base, ext = os.path.splitext(ff)
247 fname = ff.name
211 if isexec(ff) and base.lower() not in no_alias:
248 base, ext = os.path.splitext(fname)
249 if self.isexec(ff) and base.lower() not in no_alias:
212 if ext.lower() == '.exe':
250 if ext.lower() == '.exe':
213 ff = base
251 fname = base
214 try:
252 try:
215 # Removes dots from the name since ipython
253 # Removes dots from the name since ipython
216 # will assume names with dots to be python.
254 # will assume names with dots to be python.
217 self.shell.alias_manager.define_alias(
255 self.shell.alias_manager.define_alias(
218 base.lower().replace('.',''), ff)
256 base.lower().replace('.',''), fname)
219 except InvalidAliasError:
257 except InvalidAliasError:
220 pass
258 pass
221 syscmdlist.append(ff)
259 syscmdlist.append(fname)
260
222 self.shell.db['syscmdlist'] = syscmdlist
261 self.shell.db['syscmdlist'] = syscmdlist
223 finally:
262 finally:
224 os.chdir(savedir)
263 os.chdir(savedir)
@@ -96,27 +96,26 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 = []
100
101 # for python 3.6+ rewrite to: with os.scandir(path) as dirlist:
102 files = os.scandir(path)
101 for f in files:
103 for f in files:
102 try:
104 if f.is_dir() and f.name.startswith('profile_'):
103 full_path = os.path.join(path, f)
105 profiles.append(f.name.split('_', 1)[-1])
104 except UnicodeError:
105 continue
106 if os.path.isdir(full_path) and f.startswith('profile_'):
107 profiles.append(f.split('_',1)[-1])
108 return profiles
106 return profiles
109
107
110
108
111 def list_bundled_profiles():
109 def list_bundled_profiles():
112 """list profiles that are bundled with IPython."""
110 """list profiles that are bundled with IPython."""
113 path = os.path.join(get_ipython_package_dir(), u'core', u'profile')
111 path = os.path.join(get_ipython_package_dir(), u'core', u'profile')
114 files = os.listdir(path)
115 profiles = []
112 profiles = []
113
114 # for python 3.6+ rewrite to: with os.scandir(path) as dirlist:
115 files = os.scandir(path)
116 for profile in files:
116 for profile in files:
117 full_path = os.path.join(path, profile)
117 if profile.is_dir() and profile.name != "__pycache__":
118 if os.path.isdir(full_path) and profile != "__pycache__":
118 profiles.append(profile.name)
119 profiles.append(profile)
120 return profiles
119 return profiles
121
120
122
121
General Comments 0
You need to be logged in to leave comments. Login now