##// END OF EJS Templates
ipykit pylaunchers do .ipy now
vivainio -
Show More
@@ -1,54 +1,56 b''
1 import os,sys
1 import os,sys
2
2
3 import ipy_rehashdir,glob
3 import ipy_rehashdir,glob
4 from ipy_rehashdir import selflaunch, PyLauncher
4 from ipy_rehashdir import selflaunch, PyLauncher
5
5
6 def pylaunchers():
6 def pylaunchers():
7 """Create launchers for python scripts in cwd and store them in alias table
7 """Create launchers for python scripts in cwd and store them in alias table
8
8
9 This is useful if you want to invoke .py scripts from ipykit session,
9 This is useful if you want to invoke .py scripts from ipykit session,
10 just adding .py files in PATH does not work without file association.
10 just adding .py files in PATH does not work without file association.
11
11
12 .ipy files will be run like macros.
13
12 """
14 """
13 fs = glob.glob('*.py*')
15 fs = glob.glob('*.?py*')
14 for f in fs:
16 for f in fs:
15 l = PyLauncher(f)
17 l = PyLauncher(f)
16 n = os.path.splitext(f)[0]
18 n = os.path.splitext(f)[0]
17 ip.defalias(n, l)
19 ip.defalias(n, l)
18 ip.magic('store '+n)
20 ip.magic('store '+n)
19
21
20
22
21 def exta_imports():
23 def exta_imports():
22 # add some modules that you'd want to be bundled in the ipykit
24 # add some modules that you'd want to be bundled in the ipykit
23 # library zip file here. Do this if you get ImportErrors from scripts you
25 # library zip file here. Do this if you get ImportErrors from scripts you
24 # try to launch with 'py' or pylaunchers. In theory you could include
26 # try to launch with 'py' or pylaunchers. In theory you could include
25 # the whole stdlib here for full script coverage
27 # the whole stdlib here for full script coverage
26
28
27 # note that this is never run, it's just here for py2exe
29 # note that this is never run, it's just here for py2exe
28 import distutils.dir_util
30 import distutils.dir_util
29
31
30 def main():
32 def main():
31 root = os.environ.get('IPYKITROOT', None)
33 root = os.environ.get('IPYKITROOT', None)
32 if not root:
34 if not root:
33 print "Can't configure ipykit, IPYKITROOT should be set."
35 print "Can't configure ipykit, IPYKITROOT should be set."
34 return
36 return
35
37
36 os.environ["PATH"] = os.environ["PATH"] + ";" + root + "\\bin;"
38 os.environ["PATH"] = os.environ["PATH"] + ";" + root + "\\bin;"
37 ip.to_user_ns("pylaunchers")
39 ip.to_user_ns("pylaunchers")
38
40
39
41
40 def ipython_firstrun(ip):
42 def ipython_firstrun(ip):
41 print "First run of ipykit - configuring"
43 print "First run of ipykit - configuring"
42 ip.defalias('py',selflaunch)
44 ip.defalias('py',selflaunch)
43 ip.defalias('d','ls -F')
45 ip.defalias('d','ls -F')
44 ip.defalias('ls','ls')
46 ip.defalias('ls','ls')
45 ip.magic('store py')
47 ip.magic('store py')
46 ip.magic('store d')
48 ip.magic('store d')
47 ip.magic('store ls')
49 ip.magic('store ls')
48
50
49 def init_ipython(ipy):
51 def init_ipython(ipy):
50 global ip
52 global ip
51 ip = ipy
53 ip = ipy
52 main()
54 main()
53
55
54
56
@@ -1,122 +1,133 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ IPython extension: add %rehashdir magic
2 """ IPython extension: add %rehashdir magic
3
3
4 Usage:
4 Usage:
5
5
6 %rehashdir c:/bin c:/tools
6 %rehashdir c:/bin c:/tools
7 - Add all executables under c:/bin and c:/tools to alias table, in
7 - Add all executables under c:/bin and c:/tools to alias table, in
8 order to make them directly executable from any directory.
8 order to make them directly executable from any directory.
9
9
10 This also serves as an example on how to extend ipython
10 This also serves as an example on how to extend ipython
11 with new magic functions.
11 with new magic functions.
12
12
13 Unlike rest of ipython, this requires Python 2.4 (optional
13 Unlike rest of ipython, this requires Python 2.4 (optional
14 extensions are allowed to do that).
14 extensions are allowed to do that).
15
15
16 """
16 """
17
17
18 import IPython.ipapi
18 import IPython.ipapi
19 ip = IPython.ipapi.get()
19 ip = IPython.ipapi.get()
20
20
21
21
22 import os,re,fnmatch,sys
22 import os,re,fnmatch,sys
23
23
24 def selflaunch(ip,line):
24 def selflaunch(ip,line):
25 """ Launch python script with 'this' interpreter
25 """ Launch python script with 'this' interpreter
26
26
27 e.g. d:\foo\ipython.exe a.py
27 e.g. d:\foo\ipykit.exe a.py
28
28
29 """
29 """
30 cmd = sys.executable + ' ' + line.split(None,1)[1]
30
31 tup = line.split(None,1)
32 if len(tup) == 1:
33 print "Launching nested ipython session"
34 os.system(sys.executable)
35 return
36
37 cmd = sys.executable + ' ' + tup[1]
31 print ">",cmd
38 print ">",cmd
32 os.system(cmd)
39 os.system(cmd)
33
40
34 class PyLauncher:
41 class PyLauncher:
35 """ Invoke selflanucher on the specified script
42 """ Invoke selflanucher on the specified script
36
43
37 This is mostly useful for associating with scripts using::
44 This is mostly useful for associating with scripts using::
38 _ip.defalias('foo',PyLauncher('foo_script.py'))
45 _ip.defalias('foo',PyLauncher('foo_script.py'))
39
46
40 """
47 """
41 def __init__(self,script):
48 def __init__(self,script):
42 self.script = os.path.abspath(script)
49 self.script = os.path.abspath(script)
43 def __call__(self, ip, line):
50 def __call__(self, ip, line):
44 selflaunch("py " + self.script + ' ' + line)
51 if self.script.endswith('.ipy'):
52 ip.runlines(open(self.script).read())
53 else:
54 selflaunch("py " + self.script + ' ' + line)
45 def __repr__(self):
55 def __repr__(self):
46 return 'PyLauncher("%s")' % self.script
56 return 'PyLauncher("%s")' % self.script
57
47 def rehashdir_f(self,arg):
58 def rehashdir_f(self,arg):
48 """ Add executables in all specified dirs to alias table
59 """ Add executables in all specified dirs to alias table
49
60
50 Usage:
61 Usage:
51
62
52 %rehashdir c:/bin;c:/tools
63 %rehashdir c:/bin;c:/tools
53 - Add all executables under c:/bin and c:/tools to alias table, in
64 - Add all executables under c:/bin and c:/tools to alias table, in
54 order to make them directly executable from any directory.
65 order to make them directly executable from any directory.
55
66
56 Without arguments, add all executables in current directory.
67 Without arguments, add all executables in current directory.
57
68
58 """
69 """
59
70
60 # most of the code copied from Magic.magic_rehashx
71 # most of the code copied from Magic.magic_rehashx
61
72
62 def isjunk(fname):
73 def isjunk(fname):
63 junk = ['*~']
74 junk = ['*~']
64 for j in junk:
75 for j in junk:
65 if fnmatch.fnmatch(fname, j):
76 if fnmatch.fnmatch(fname, j):
66 return True
77 return True
67 return False
78 return False
68
79
69 created = []
80 created = []
70 if not arg:
81 if not arg:
71 arg = '.'
82 arg = '.'
72 path = map(os.path.abspath,arg.split(';'))
83 path = map(os.path.abspath,arg.split(';'))
73 alias_table = self.shell.alias_table
84 alias_table = self.shell.alias_table
74
85
75 if os.name == 'posix':
86 if os.name == 'posix':
76 isexec = lambda fname:os.path.isfile(fname) and \
87 isexec = lambda fname:os.path.isfile(fname) and \
77 os.access(fname,os.X_OK)
88 os.access(fname,os.X_OK)
78 else:
89 else:
79
90
80 try:
91 try:
81 winext = os.environ['pathext'].replace(';','|').replace('.','')
92 winext = os.environ['pathext'].replace(';','|').replace('.','')
82 except KeyError:
93 except KeyError:
83 winext = 'exe|com|bat|py'
94 winext = 'exe|com|bat|py'
84 if 'py' not in winext:
95 if 'py' not in winext:
85 winext += '|py'
96 winext += '|py'
86
97
87 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
98 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
88 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
99 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
89 savedir = os.getcwd()
100 savedir = os.getcwd()
90 try:
101 try:
91 # write the whole loop for posix/Windows so we don't have an if in
102 # write the whole loop for posix/Windows so we don't have an if in
92 # the innermost part
103 # the innermost part
93 if os.name == 'posix':
104 if os.name == 'posix':
94 for pdir in path:
105 for pdir in path:
95 os.chdir(pdir)
106 os.chdir(pdir)
96 for ff in os.listdir(pdir):
107 for ff in os.listdir(pdir):
97 if isexec(ff) and not isjunk(ff):
108 if isexec(ff) and not isjunk(ff):
98 # each entry in the alias table must be (N,name),
109 # each entry in the alias table must be (N,name),
99 # where N is the number of positional arguments of the
110 # where N is the number of positional arguments of the
100 # alias.
111 # alias.
101 src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff)
112 src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff)
102 created.append(src)
113 created.append(src)
103 alias_table[src] = (0,tgt)
114 alias_table[src] = (0,tgt)
104 else:
115 else:
105 for pdir in path:
116 for pdir in path:
106 os.chdir(pdir)
117 os.chdir(pdir)
107 for ff in os.listdir(pdir):
118 for ff in os.listdir(pdir):
108 if isexec(ff) and not isjunk(ff):
119 if isexec(ff) and not isjunk(ff):
109 src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff)
120 src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff)
110 src = src.lower()
121 src = src.lower()
111 created.append(src)
122 created.append(src)
112 alias_table[src] = (0,tgt)
123 alias_table[src] = (0,tgt)
113 # Make sure the alias table doesn't contain keywords or builtins
124 # Make sure the alias table doesn't contain keywords or builtins
114 self.shell.alias_table_validate()
125 self.shell.alias_table_validate()
115 # Call again init_auto_alias() so we get 'rm -i' and other
126 # Call again init_auto_alias() so we get 'rm -i' and other
116 # modified aliases since %rehashx will probably clobber them
127 # modified aliases since %rehashx will probably clobber them
117 # self.shell.init_auto_alias()
128 # self.shell.init_auto_alias()
118 finally:
129 finally:
119 os.chdir(savedir)
130 os.chdir(savedir)
120 return created
131 return created
121
132
122 ip.expose_magic("rehashdir",rehashdir_f)
133 ip.expose_magic("rehashdir",rehashdir_f)
General Comments 0
You need to be logged in to leave comments. Login now