Show More
@@ -1,173 +1,174 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 | class IpyShellCmdException(Exception): | |
24 | pass |
|
24 | pass | |
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 IpyShellCmdException("Expected arguments for " + tup[0]) |
|
31 | raise IpyShellCmdException("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 IpyShellCmdException("No files found:" + str(tup2[0:-1])) |
|
37 | raise IpyShellCmdException("No files found:" + str(tup2[0:-1])) | |
38 | return flist, trg |
|
38 | return flist, trg | |
39 |
|
39 | |||
40 | def icp(arg): |
|
40 | def icp(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): | |
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 | shutil.copy2(f,targetdir) | |
57 | return fs |
|
57 | return fs | |
58 | ip.defalias("icp",icp) |
|
58 | ip.defalias("icp",icp) | |
59 |
|
59 | |||
60 | def imv(arg): |
|
60 | def imv(arg): | |
61 | """ imv src tgt |
|
61 | """ imv src tgt | |
62 |
|
62 | |||
63 | Move source to target. |
|
63 | Move source to target. | |
64 | """ |
|
64 | """ | |
65 |
|
65 | |||
66 | fs, target = parse_args(arg) |
|
66 | fs, target = parse_args(arg) | |
67 | if len(fs) > 1: |
|
67 | if len(fs) > 1: | |
68 | assert os.path.isdir(target) |
|
68 | assert os.path.isdir(target) | |
69 | for f in fs: |
|
69 | for f in fs: | |
70 | shutil.move(f, target) |
|
70 | shutil.move(f, target) | |
71 | return fs |
|
71 | return fs | |
72 | ip.defalias("imv",imv) |
|
72 | ip.defalias("imv",imv) | |
73 |
|
73 | |||
74 | def irm(arg): |
|
74 | def irm(arg): | |
75 | """ irm path[s]... |
|
75 | """ irm path[s]... | |
76 |
|
76 | |||
77 | Remove file[s] or dir[s] path. Dirs are deleted recursively. |
|
77 | Remove file[s] or dir[s] path. Dirs are deleted recursively. | |
78 | """ |
|
78 | """ | |
79 | paths = mglob.expand(arg.split(None,1)[1]) |
|
79 | paths = mglob.expand(arg.split(None,1)[1]) | |
80 | import distutils.dir_util |
|
80 | import distutils.dir_util | |
81 | for p in paths: |
|
81 | for p in paths: | |
82 | print "rm",p |
|
82 | print "rm",p | |
83 | if os.path.isdir(p): |
|
83 | if os.path.isdir(p): | |
84 | distutils.dir_util.remove_tree(p, verbose = 1) |
|
84 | distutils.dir_util.remove_tree(p, verbose = 1) | |
85 | else: |
|
85 | else: | |
86 | os.remove(p) |
|
86 | os.remove(p) | |
87 |
|
87 | |||
88 | ip.defalias("irm",irm) |
|
88 | ip.defalias("irm",irm) | |
89 |
|
89 | |||
90 | def imkdir(arg): |
|
90 | def imkdir(arg): | |
91 | """ imkdir path |
|
91 | """ imkdir path | |
92 |
|
92 | |||
93 | Creates dir path, and all dirs on the road |
|
93 | Creates dir path, and all dirs on the road | |
94 | """ |
|
94 | """ | |
95 | import distutils.dir_util |
|
95 | import distutils.dir_util | |
96 | targetdir = arg.split(None,1)[1] |
|
96 | targetdir = arg.split(None,1)[1] | |
97 | distutils.dir_util.mkpath(targetdir,verbose =1) |
|
97 | distutils.dir_util.mkpath(targetdir,verbose =1) | |
98 |
|
98 | |||
99 | ip.defalias("imkdir",imkdir) |
|
99 | ip.defalias("imkdir",imkdir) | |
100 |
|
100 | |||
101 | def igrep(arg): |
|
101 | def igrep(arg): | |
102 | """ igrep PAT files... |
|
102 | """ igrep PAT files... | |
103 |
|
103 | |||
104 | Very dumb file scan, case-insensitive. |
|
104 | Very dumb file scan, case-insensitive. | |
105 |
|
105 | |||
106 | e.g. |
|
106 | e.g. | |
107 |
|
107 | |||
108 | igrep "test this" rec:*.py |
|
108 | igrep "test this" rec:*.py | |
109 |
|
109 | |||
110 | """ |
|
110 | """ | |
111 | elems = shlex.split(arg) |
|
111 | elems = shlex.split(arg) | |
112 | dummy, pat, fs = elems[0], elems[1], mglob.expand(elems[2:]) |
|
112 | dummy, pat, fs = elems[0], elems[1], mglob.expand(elems[2:]) | |
113 | res = [] |
|
113 | res = [] | |
114 | for f in fs: |
|
114 | for f in fs: | |
115 | found = False |
|
115 | found = False | |
116 | for l in open(f): |
|
116 | for l in open(f): | |
117 | if pat.lower() in l.lower(): |
|
117 | if pat.lower() in l.lower(): | |
118 | if not found: |
|
118 | if not found: | |
119 | print "[[",f,"]]" |
|
119 | print "[[",f,"]]" | |
120 | found = True |
|
120 | found = True | |
121 | res.append(f) |
|
121 | res.append(f) | |
122 | print l.rstrip() |
|
122 | print l.rstrip() | |
123 | return res |
|
123 | return res | |
124 |
|
124 | |||
125 | ip.defalias("igrep",igrep) |
|
125 | ip.defalias("igrep",igrep) | |
126 |
|
126 | |||
127 | def collect(arg): |
|
127 | def collect(arg): | |
128 | """ collect foo/a.txt rec:bar=*.py |
|
128 | """ collect foo/a.txt rec:bar=*.py | |
129 |
|
129 | |||
130 | Copies foo/a.txt to ~/_ipython/collect/foo/a.txt and *.py from bar, |
|
130 | Copies foo/a.txt to ~/_ipython/collect/foo/a.txt and *.py from bar, | |
131 | likewise |
|
131 | likewise | |
132 |
|
132 | |||
133 | Without args, try to open ~/_ipython/collect dir (in win32 at least). |
|
133 | Without args, try to open ~/_ipython/collect dir (in win32 at least). | |
134 | """ |
|
134 | """ | |
135 | from path import path |
|
135 | from path import path | |
136 | basedir = path(ip.options.ipythondir + '/collect') |
|
136 | basedir = path(ip.options.ipythondir + '/collect') | |
137 | try: |
|
137 | try: | |
138 | fs = mglob.expand(arg.split(None,1)[1]) |
|
138 | fs = mglob.expand(arg.split(None,1)[1]) | |
139 | except IndexError: |
|
139 | except IndexError: | |
140 | os.startfile(basedir) |
|
140 | os.startfile(basedir) | |
141 | return |
|
141 | return | |
142 | for f in fs: |
|
142 | for f in fs: | |
143 | f = path(f) |
|
143 | f = path(f) | |
144 | trg = basedir / f.splitdrive()[1].lstrip('/\\') |
|
144 | trg = basedir / f.splitdrive()[1].lstrip('/\\') | |
145 | if f.isdir(): |
|
145 | if f.isdir(): | |
146 |
|
|
146 | print "mkdir",trg | |
|
147 | trg.makedirs() | |||
147 | continue |
|
148 | continue | |
148 | dname = trg.dirname() |
|
149 | dname = trg.dirname() | |
149 | if not dname.isdir(): |
|
150 | if not dname.isdir(): | |
150 | dname.makedirs() |
|
151 | dname.makedirs() | |
151 | print f,"=>",trg |
|
152 | print f,"=>",trg | |
152 | shutil.copy2(f,trg) |
|
153 | shutil.copy2(f,trg) | |
153 |
|
154 | |||
154 | ip.defalias("collect",collect) |
|
155 | ip.defalias("collect",collect) | |
155 |
|
156 | |||
156 | def inote(arg): |
|
157 | def inote(arg): | |
157 | """ inote Hello world |
|
158 | """ inote Hello world | |
158 |
|
159 | |||
159 | Adds timestamp and Hello world to ~/_ipython/notes.txt |
|
160 | Adds timestamp and Hello world to ~/_ipython/notes.txt | |
160 |
|
161 | |||
161 | Without args, opens notes.txt for editing. |
|
162 | Without args, opens notes.txt for editing. | |
162 | """ |
|
163 | """ | |
163 | import time |
|
164 | import time | |
164 | fname = ip.options.ipythondir + '/notes.txt' |
|
165 | fname = ip.options.ipythondir + '/notes.txt' | |
165 |
|
166 | |||
166 | try: |
|
167 | try: | |
167 | entry = time.asctime() + ':\n' + arg.split(None,1)[1] + '\n' |
|
168 | entry = time.asctime() + ':\n' + arg.split(None,1)[1] + '\n' | |
168 | f= open(fname, 'a').write(entry) |
|
169 | f= open(fname, 'a').write(entry) | |
169 | except IndexError: |
|
170 | except IndexError: | |
170 | ip.IP.hooks.editor(fname) |
|
171 | ip.IP.hooks.editor(fname) | |
171 |
|
172 | |||
172 | ip.defalias("inote",inote) |
|
173 | ip.defalias("inote",inote) | |
173 |
|
174 |
General Comments 0
You need to be logged in to leave comments.
Login now