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