Show More
@@ -0,0 +1,173 b'' | |||||
|
1 | """ File system operations | |||
|
2 | ||||
|
3 | Contains: Simple variants of normal unix shell commands (icp, imv, irm, | |||
|
4 | imkdir, igrep). | |||
|
5 | ||||
|
6 | Some "otherwise handy" utils ('collect' for gathering files to | |||
|
7 | ~/_ipython/collect, 'inote' for collecting single note lines to | |||
|
8 | ~/_ipython/note.txt) | |||
|
9 | ||||
|
10 | Mostly of use for bare windows installations where cygwin/equivalent is not | |||
|
11 | installed and you would otherwise need to deal with dos versions of the | |||
|
12 | commands (that e.g. don't understand / as path separator). These can | |||
|
13 | do some useful tricks on their own, though (like use 'mglob' patterns). | |||
|
14 | ||||
|
15 | Not to be confused with ipipe commands (ils etc.) that also start with i. | |||
|
16 | """ | |||
|
17 | ||||
|
18 | import IPython.ipapi | |||
|
19 | ip = IPython.ipapi.get() | |||
|
20 | ||||
|
21 | import shutil,os,shlex | |||
|
22 | from IPython.external import mglob | |||
|
23 | class IpyShellCmdException(Exception): | |||
|
24 | pass | |||
|
25 | ||||
|
26 | def parse_args(args): | |||
|
27 | """ Given arg string 'CMD files... target', return ([files], target) """ | |||
|
28 | ||||
|
29 | tup = args.split(None, 1) | |||
|
30 | if len(tup) == 1: | |||
|
31 | raise IpyShellCmdException("Expected arguments for " + tup[0]) | |||
|
32 | ||||
|
33 | tup2 = shlex.split(tup[1]) | |||
|
34 | ||||
|
35 | flist, trg = mglob.expand(tup2[0:-1]), tup2[-1] | |||
|
36 | if not flist: | |||
|
37 | raise IpyShellCmdException("No files found:" + str(tup2[0:-1])) | |||
|
38 | return flist, trg | |||
|
39 | ||||
|
40 | def icp(arg): | |||
|
41 | """ icp files... targetdir | |||
|
42 | ||||
|
43 | Copy all files to target, creating dirs for target if necessary | |||
|
44 | ||||
|
45 | icp srcdir dstdir | |||
|
46 | ||||
|
47 | Copy srcdir to distdir | |||
|
48 | ||||
|
49 | """ | |||
|
50 | import distutils.dir_util | |||
|
51 | ||||
|
52 | fs, targetdir = parse_args(arg) | |||
|
53 | if not os.path.isdir(targetdir): | |||
|
54 | distutils.dir_util.mkpath(targetdir,verbose =1) | |||
|
55 | for f in fs: | |||
|
56 | shutil.copy2(f,targetdir) | |||
|
57 | return fs | |||
|
58 | ip.defalias("icp",icp) | |||
|
59 | ||||
|
60 | def imv(arg): | |||
|
61 | """ imv src tgt | |||
|
62 | ||||
|
63 | Move source to target. | |||
|
64 | """ | |||
|
65 | ||||
|
66 | fs, target = parse_args(arg) | |||
|
67 | if len(fs) > 1: | |||
|
68 | assert os.path.isdir(target) | |||
|
69 | for f in fs: | |||
|
70 | shutil.move(f, target) | |||
|
71 | return fs | |||
|
72 | ip.defalias("imv",imv) | |||
|
73 | ||||
|
74 | def irm(arg): | |||
|
75 | """ irm path[s]... | |||
|
76 | ||||
|
77 | Remove file[s] or dir[s] path. Dirs are deleted recursively. | |||
|
78 | """ | |||
|
79 | paths = mglob.expand(arg.split(None,1)[1]) | |||
|
80 | import distutils.dir_util | |||
|
81 | for p in paths: | |||
|
82 | print "rm",p | |||
|
83 | if os.path.isdir(p): | |||
|
84 | distutils.dir_util.remove_tree(p, verbose = 1) | |||
|
85 | else: | |||
|
86 | os.remove(p) | |||
|
87 | ||||
|
88 | ip.defalias("irm",irm) | |||
|
89 | ||||
|
90 | def imkdir(arg): | |||
|
91 | """ imkdir path | |||
|
92 | ||||
|
93 | Creates dir path, and all dirs on the road | |||
|
94 | """ | |||
|
95 | import distutils.dir_util | |||
|
96 | targetdir = arg.split(None,1)[1] | |||
|
97 | distutils.dir_util.mkpath(targetdir,verbose =1) | |||
|
98 | ||||
|
99 | ip.defalias("imkdir",imkdir) | |||
|
100 | ||||
|
101 | def igrep(arg): | |||
|
102 | """ igrep PAT files... | |||
|
103 | ||||
|
104 | Very dumb file scan, case-insensitive. | |||
|
105 | ||||
|
106 | e.g. | |||
|
107 | ||||
|
108 | igrep "test this" rec:*.py | |||
|
109 | ||||
|
110 | """ | |||
|
111 | elems = shlex.split(arg) | |||
|
112 | dummy, pat, fs = elems[0], elems[1], mglob.expand(elems[2:]) | |||
|
113 | res = [] | |||
|
114 | for f in fs: | |||
|
115 | found = False | |||
|
116 | for l in open(f): | |||
|
117 | if pat.lower() in l.lower(): | |||
|
118 | if not found: | |||
|
119 | print "[[",f,"]]" | |||
|
120 | found = True | |||
|
121 | res.append(f) | |||
|
122 | print l.rstrip() | |||
|
123 | return res | |||
|
124 | ||||
|
125 | ip.defalias("igrep",igrep) | |||
|
126 | ||||
|
127 | def collect(arg): | |||
|
128 | """ collect foo/a.txt rec:bar=*.py | |||
|
129 | ||||
|
130 | Copies foo/a.txt to ~/_ipython/collect/foo/a.txt and *.py from bar, | |||
|
131 | likewise | |||
|
132 | ||||
|
133 | Without args, try to open ~/_ipython/collect dir (in win32 at least). | |||
|
134 | """ | |||
|
135 | from path import path | |||
|
136 | basedir = path(ip.options.ipythondir + '/collect') | |||
|
137 | try: | |||
|
138 | fs = mglob.expand(arg.split(None,1)[1]) | |||
|
139 | except IndexError: | |||
|
140 | os.startfile(basedir) | |||
|
141 | return | |||
|
142 | for f in fs: | |||
|
143 | f = path(f) | |||
|
144 | trg = basedir / f.splitdrive()[1].lstrip('/\\') | |||
|
145 | if f.isdir(): | |||
|
146 | f.makedirs() | |||
|
147 | continue | |||
|
148 | dname = trg.dirname() | |||
|
149 | if not dname.isdir(): | |||
|
150 | dname.makedirs() | |||
|
151 | print f,"=>",trg | |||
|
152 | shutil.copy2(f,trg) | |||
|
153 | ||||
|
154 | ip.defalias("collect",collect) | |||
|
155 | ||||
|
156 | def inote(arg): | |||
|
157 | """ inote Hello world | |||
|
158 | ||||
|
159 | Adds timestamp and Hello world to ~/_ipython/notes.txt | |||
|
160 | ||||
|
161 | Without args, opens notes.txt for editing. | |||
|
162 | """ | |||
|
163 | import time | |||
|
164 | fname = ip.options.ipythondir + '/notes.txt' | |||
|
165 | ||||
|
166 | try: | |||
|
167 | entry = time.asctime() + ':\n' + arg.split(None,1)[1] + '\n' | |||
|
168 | f= open(fname, 'a').write(entry) | |||
|
169 | except IndexError: | |||
|
170 | ip.IP.hooks.editor(fname) | |||
|
171 | ||||
|
172 | ip.defalias("inote",inote) | |||
|
173 |
@@ -6,7 +6,7 b' Requires Python 2.3 or newer.' | |||||
6 |
|
6 | |||
7 | This file contains all the classes and helper functions specific to IPython. |
|
7 | This file contains all the classes and helper functions specific to IPython. | |
8 |
|
8 | |||
9 |
$Id: iplib.py 2 |
|
9 | $Id: iplib.py 2713 2007-09-05 18:34:29Z vivainio $ | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | #***************************************************************************** |
|
12 | #***************************************************************************** | |
@@ -2172,7 +2172,8 b' want to merge them back into the new files.""" % locals()' | |||||
2172 | tgt = self.alias_table[line_info.iFun] |
|
2172 | tgt = self.alias_table[line_info.iFun] | |
2173 | # print "=>",tgt #dbg |
|
2173 | # print "=>",tgt #dbg | |
2174 | if callable(tgt): |
|
2174 | if callable(tgt): | |
2175 |
line_out = "_sh." |
|
2175 | line_out = "_sh.%s(%s)" % (line_info.iFun, | |
|
2176 | make_quoted_expr(self.var_expand(line_info.line, depth=2))) | |||
2176 | else: |
|
2177 | else: | |
2177 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) |
|
2178 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) | |
2178 |
|
2179 |
@@ -1,3 +1,16 b'' | |||||
|
1 | 2007-09-05 Ville Vainio <vivainio@gmail.com> | |||
|
2 | ||||
|
3 | * external/mglob.py: expand('dirname') => ['dirname'], instead | |||
|
4 | of ['dirname/foo','dirname/bar', ...]. | |||
|
5 | ||||
|
6 | * Extensions/ipy_fsops.py: added, has usefull shell utils for plain | |||
|
7 | win32 installations: icp, imv, imkdir, igrep, collect (collect | |||
|
8 | is useful for others as well). | |||
|
9 | ||||
|
10 | * iplib.py: on callable aliases (as opposed to old style aliases), | |||
|
11 | do var_expand() immediately, and use make_quoted_expr instead | |||
|
12 | of hardcoded r""" | |||
|
13 | ||||
1 | 2007-09-04 Ville Vainio <vivainio@gmail.com> |
|
14 | 2007-09-04 Ville Vainio <vivainio@gmail.com> | |
2 |
|
15 | |||
3 | * ipy_profile_zope.py: add zope profile, by Stefan Eletzhofer. |
|
16 | * ipy_profile_zope.py: add zope profile, by Stefan Eletzhofer. |
General Comments 0
You need to be logged in to leave comments.
Login now