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