Show More
@@ -1,173 +1,179 b'' | |||||
1 | """ File system operations |
|
1 | """ File system operations | |
2 |
|
2 | |||
3 | Contains: Simple variants of normal unix shell commands (icp, imv, irm, |
|
3 | Contains: Simple variants of normal unix shell commands (icp, imv, irm, | |
4 | imkdir, igrep). |
|
4 | imkdir, igrep). | |
5 |
|
5 | |||
6 | Some "otherwise handy" utils ('collect' for gathering files to |
|
6 | Some "otherwise handy" utils ('collect' for gathering files to | |
7 | ~/_ipython/collect, 'inote' for collecting single note lines to |
|
7 | ~/_ipython/collect, 'inote' for collecting single note lines to | |
8 | ~/_ipython/note.txt) |
|
8 | ~/_ipython/note.txt) | |
9 |
|
9 | |||
10 | Mostly of use for bare windows installations where cygwin/equivalent is not |
|
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 |
|
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 |
|
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). |
|
13 | do some useful tricks on their own, though (like use 'mglob' patterns). | |
14 |
|
14 | |||
15 | Not to be confused with ipipe commands (ils etc.) that also start with i. |
|
15 | Not to be confused with ipipe commands (ils etc.) that also start with i. | |
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 | import shutil,os,shlex |
|
21 | import shutil,os,shlex | |
22 | from IPython.external import mglob |
|
22 | from IPython.external import mglob | |
23 | class IpyShellCmdException(Exception): |
|
23 | ||
24 | pass |
|
24 | from IPython.ipapi import UsageError | |
25 |
|
25 | |||
26 | def parse_args(args): |
|
26 | def parse_args(args): | |
27 | """ Given arg string 'CMD files... target', return ([files], target) """ |
|
27 | """ Given arg string 'CMD files... target', return ([files], target) """ | |
28 |
|
28 | |||
29 | tup = args.split(None, 1) |
|
29 | tup = args.split(None, 1) | |
30 | if len(tup) == 1: |
|
30 | if len(tup) == 1: | |
31 |
raise |
|
31 | raise UsageError("Expected arguments for " + tup[0]) | |
32 |
|
32 | |||
33 | tup2 = shlex.split(tup[1]) |
|
33 | tup2 = shlex.split(tup[1]) | |
34 |
|
34 | |||
35 | flist, trg = mglob.expand(tup2[0:-1]), tup2[-1] |
|
35 | flist, trg = mglob.expand(tup2[0:-1]), tup2[-1] | |
36 | if not flist: |
|
36 | if not flist: | |
37 |
raise |
|
37 | raise UsageError("No files found:" + str(tup2[0:-1])) | |
38 | return flist, trg |
|
38 | return flist, trg | |
39 |
|
39 | |||
40 | def icp(ip,arg): |
|
40 | def icp(ip,arg): | |
41 | """ icp files... targetdir |
|
41 | """ icp files... targetdir | |
42 |
|
42 | |||
43 | Copy all files to target, creating dirs for target if necessary |
|
43 | Copy all files to target, creating dirs for target if necessary | |
44 |
|
44 | |||
45 | icp srcdir dstdir |
|
45 | icp srcdir dstdir | |
46 |
|
46 | |||
47 | Copy srcdir to distdir |
|
47 | Copy srcdir to distdir | |
48 |
|
48 | |||
49 | """ |
|
49 | """ | |
50 | import distutils.dir_util |
|
50 | import distutils.dir_util | |
51 |
|
51 | |||
52 | fs, targetdir = parse_args(arg) |
|
52 | fs, targetdir = parse_args(arg) | |
53 | if not os.path.isdir(targetdir): |
|
53 | if not os.path.isdir(targetdir) and len(fs) > 1: | |
54 | distutils.dir_util.mkpath(targetdir,verbose =1) |
|
54 | distutils.dir_util.mkpath(targetdir,verbose =1) | |
55 | for f in fs: |
|
55 | for f in fs: | |
56 | shutil.copy2(f,targetdir) |
|
56 | if os.path.isdir(f): | |
|
57 | shutil.copytree(f, targetdir) | |||
|
58 | else: | |||
|
59 | shutil.copy2(f,targetdir) | |||
57 | return fs |
|
60 | return fs | |
58 | ip.defalias("icp",icp) |
|
61 | ip.defalias("icp",icp) | |
59 |
|
62 | |||
60 | def imv(ip,arg): |
|
63 | def imv(ip,arg): | |
61 | """ imv src tgt |
|
64 | """ imv src tgt | |
62 |
|
65 | |||
63 | Move source to target. |
|
66 | Move source to target. | |
64 | """ |
|
67 | """ | |
65 |
|
68 | |||
66 | fs, target = parse_args(arg) |
|
69 | fs, target = parse_args(arg) | |
67 | if len(fs) > 1: |
|
70 | if len(fs) > 1: | |
68 | assert os.path.isdir(target) |
|
71 | assert os.path.isdir(target) | |
69 | for f in fs: |
|
72 | for f in fs: | |
70 | shutil.move(f, target) |
|
73 | shutil.move(f, target) | |
71 | return fs |
|
74 | return fs | |
72 | ip.defalias("imv",imv) |
|
75 | ip.defalias("imv",imv) | |
73 |
|
76 | |||
74 | def irm(ip,arg): |
|
77 | def irm(ip,arg): | |
75 | """ irm path[s]... |
|
78 | """ irm path[s]... | |
76 |
|
79 | |||
77 | Remove file[s] or dir[s] path. Dirs are deleted recursively. |
|
80 | Remove file[s] or dir[s] path. Dirs are deleted recursively. | |
78 | """ |
|
81 | """ | |
79 | paths = mglob.expand(arg.split(None,1)[1]) |
|
82 | try: | |
|
83 | paths = mglob.expand(arg.split(None,1)[1]) | |||
|
84 | except IndexError: | |||
|
85 | raise UsageError("%irm paths...") | |||
80 | import distutils.dir_util |
|
86 | import distutils.dir_util | |
81 | for p in paths: |
|
87 | for p in paths: | |
82 | print "rm",p |
|
88 | print "rm",p | |
83 | if os.path.isdir(p): |
|
89 | if os.path.isdir(p): | |
84 | distutils.dir_util.remove_tree(p, verbose = 1) |
|
90 | distutils.dir_util.remove_tree(p, verbose = 1) | |
85 | else: |
|
91 | else: | |
86 | os.remove(p) |
|
92 | os.remove(p) | |
87 |
|
93 | |||
88 | ip.defalias("irm",irm) |
|
94 | ip.defalias("irm",irm) | |
89 |
|
95 | |||
90 | def imkdir(ip,arg): |
|
96 | def imkdir(ip,arg): | |
91 | """ imkdir path |
|
97 | """ imkdir path | |
92 |
|
98 | |||
93 | Creates dir path, and all dirs on the road |
|
99 | Creates dir path, and all dirs on the road | |
94 | """ |
|
100 | """ | |
95 | import distutils.dir_util |
|
101 | import distutils.dir_util | |
96 | targetdir = arg.split(None,1)[1] |
|
102 | targetdir = arg.split(None,1)[1] | |
97 | distutils.dir_util.mkpath(targetdir,verbose =1) |
|
103 | distutils.dir_util.mkpath(targetdir,verbose =1) | |
98 |
|
104 | |||
99 | ip.defalias("imkdir",imkdir) |
|
105 | ip.defalias("imkdir",imkdir) | |
100 |
|
106 | |||
101 | def igrep(ip,arg): |
|
107 | def igrep(ip,arg): | |
102 | """ igrep PAT files... |
|
108 | """ igrep PAT files... | |
103 |
|
109 | |||
104 | Very dumb file scan, case-insensitive. |
|
110 | Very dumb file scan, case-insensitive. | |
105 |
|
111 | |||
106 | e.g. |
|
112 | e.g. | |
107 |
|
113 | |||
108 | igrep "test this" rec:*.py |
|
114 | igrep "test this" rec:*.py | |
109 |
|
115 | |||
110 | """ |
|
116 | """ | |
111 | elems = shlex.split(arg) |
|
117 | elems = shlex.split(arg) | |
112 | dummy, pat, fs = elems[0], elems[1], mglob.expand(elems[2:]) |
|
118 | dummy, pat, fs = elems[0], elems[1], mglob.expand(elems[2:]) | |
113 | res = [] |
|
119 | res = [] | |
114 | for f in fs: |
|
120 | for f in fs: | |
115 | found = False |
|
121 | found = False | |
116 | for l in open(f): |
|
122 | for l in open(f): | |
117 | if pat.lower() in l.lower(): |
|
123 | if pat.lower() in l.lower(): | |
118 | if not found: |
|
124 | if not found: | |
119 | print "[[",f,"]]" |
|
125 | print "[[",f,"]]" | |
120 | found = True |
|
126 | found = True | |
121 | res.append(f) |
|
127 | res.append(f) | |
122 | print l.rstrip() |
|
128 | print l.rstrip() | |
123 | return res |
|
129 | return res | |
124 |
|
130 | |||
125 | ip.defalias("igrep",igrep) |
|
131 | ip.defalias("igrep",igrep) | |
126 |
|
132 | |||
127 | def collect(ip,arg): |
|
133 | def collect(ip,arg): | |
128 | """ collect foo/a.txt rec:bar=*.py |
|
134 | """ collect foo/a.txt rec:bar=*.py | |
129 |
|
135 | |||
130 | Copies foo/a.txt to ~/_ipython/collect/foo/a.txt and *.py from bar, |
|
136 | Copies foo/a.txt to ~/_ipython/collect/foo/a.txt and *.py from bar, | |
131 | likewise |
|
137 | likewise | |
132 |
|
138 | |||
133 | Without args, try to open ~/_ipython/collect dir (in win32 at least). |
|
139 | Without args, try to open ~/_ipython/collect dir (in win32 at least). | |
134 | """ |
|
140 | """ | |
135 | from path import path |
|
141 | from path import path | |
136 | basedir = path(ip.options.ipythondir + '/collect') |
|
142 | basedir = path(ip.options.ipythondir + '/collect') | |
137 | try: |
|
143 | try: | |
138 | fs = mglob.expand(arg.split(None,1)[1]) |
|
144 | fs = mglob.expand(arg.split(None,1)[1]) | |
139 | except IndexError: |
|
145 | except IndexError: | |
140 | os.startfile(basedir) |
|
146 | os.startfile(basedir) | |
141 | return |
|
147 | return | |
142 | for f in fs: |
|
148 | for f in fs: | |
143 | f = path(f) |
|
149 | f = path(f) | |
144 | trg = basedir / f.splitdrive()[1].lstrip('/\\') |
|
150 | trg = basedir / f.splitdrive()[1].lstrip('/\\') | |
145 | if f.isdir(): |
|
151 | if f.isdir(): | |
146 | print "mkdir",trg |
|
152 | print "mkdir",trg | |
147 | trg.makedirs() |
|
153 | trg.makedirs() | |
148 | continue |
|
154 | continue | |
149 | dname = trg.dirname() |
|
155 | dname = trg.dirname() | |
150 | if not dname.isdir(): |
|
156 | if not dname.isdir(): | |
151 | dname.makedirs() |
|
157 | dname.makedirs() | |
152 | print f,"=>",trg |
|
158 | print f,"=>",trg | |
153 | shutil.copy2(f,trg) |
|
159 | shutil.copy2(f,trg) | |
154 |
|
160 | |||
155 | ip.defalias("collect",collect) |
|
161 | ip.defalias("collect",collect) | |
156 |
|
162 | |||
157 | def inote(ip,arg): |
|
163 | def inote(ip,arg): | |
158 | """ inote Hello world |
|
164 | """ inote Hello world | |
159 |
|
165 | |||
160 | Adds timestamp and Hello world to ~/_ipython/notes.txt |
|
166 | Adds timestamp and Hello world to ~/_ipython/notes.txt | |
161 |
|
167 | |||
162 | Without args, opens notes.txt for editing. |
|
168 | Without args, opens notes.txt for editing. | |
163 | """ |
|
169 | """ | |
164 | import time |
|
170 | import time | |
165 | fname = ip.options.ipythondir + '/notes.txt' |
|
171 | fname = ip.options.ipythondir + '/notes.txt' | |
166 |
|
172 | |||
167 | try: |
|
173 | try: | |
168 | entry = " === " + time.asctime() + ': ===\n' + arg.split(None,1)[1] + '\n' |
|
174 | entry = " === " + time.asctime() + ': ===\n' + arg.split(None,1)[1] + '\n' | |
169 | f= open(fname, 'a').write(entry) |
|
175 | f= open(fname, 'a').write(entry) | |
170 | except IndexError: |
|
176 | except IndexError: | |
171 | ip.IP.hooks.editor(fname) |
|
177 | ip.IP.hooks.editor(fname) | |
172 |
|
178 | |||
173 | ip.defalias("inote",inote) |
|
179 | ip.defalias("inote",inote) |
General Comments 0
You need to be logged in to leave comments.
Login now