exit from pyview by ESC
[lfm.git] / lfm / vfs.py
blobbb8581cd58ab6c0eecca6e1da22ec9e0f94193e3
1 # -*- coding: utf-8 -*-
3 """vfs.py
5 This module supplies vfs functionality.
6 """
8 import os, os.path
9 from glob import glob
10 import files
11 import messages
12 import utils
13 import compress
16 ######################################################################
17 ##### module variables
18 app = None
21 ######################################################################
22 ##### VFS
23 # initialize vfs stuff
24 def init(tab, filename, vfstype):
25 """initiliaze vfs stuff"""
27 tempdir = files.mkdtemp()
28 # uncompress
29 st, msg = utils.ProcessFunc('Creating vfs', filename,
30 utils.do_uncompress_dir, filename,
31 tab.path, tempdir, True).run()
32 if st == -1: # error
33 app.display()
34 messages.error('Creating vfs', msg)
35 app.display()
36 return # temppdir deleted by previous call, so we just return
37 elif st == -100: # stopped by user
38 try:
39 files.do_delete(tempdir)
40 except OSError:
41 pass
42 return
43 # update vfs vars
44 vpath = tab.path
45 tab.init(tempdir)
46 tab.vfs = vfstype
47 tab.base = tempdir
48 tab.vbase = os.path.join(vpath, filename) + '#vfs'
49 # refresh the other panel
50 app.regenerate()
53 # copy vfs
54 def copy(tab_org, tab_new):
55 """copy vfs"""
57 tempdir = files.mkdtemp()
58 # copy contents
59 dir_src = tab_org.base
60 for f in glob(os.path.join(dir_src, '*')):
61 f = os.path.basename(f)
62 try:
63 files.do_copy(os.path.join(dir_src, f), os.path.join(tempdir, f))
64 except (IOError, os.error), (errno, strerror):
65 app.display()
66 messages.error('Error regenerating vfs file',
67 '%s (%s)' % (strerror, errno))
68 # init vars
69 tab_new.base = tempdir
70 tab_new.vfs = tab_org.vfs
71 tab_new.vbase = tab_org.vbase
74 # exit from vfs, clean all
75 def exit(tab):
76 """exit from vfs, clean all"""
78 ans = 0
79 rebuild = app.prefs.options['rebuild_vfs']
80 if app.prefs.confirmations['ask_rebuild_vfs']:
81 ans = messages.confirm('Rebuild vfs file', 'Rebuild vfs file', rebuild)
82 app.display()
83 if ans:
84 if tab.vfs == 'pan':
85 return pan_regenerate(tab)
86 else:
87 regenerate_file(tab)
89 files.do_delete(tab.base)
90 app.regenerate()
93 # regenerate vfs file
94 def regenerate_file(tab):
95 """regenerate vfs file: compress new file"""
97 vfs_file = tab.vbase.replace('#vfs', '')
98 # compress file
99 tmpfile = files.mktemp()
100 c = compress.check_compressed_file(vfs_file)
101 cmd = c.build_compressXXX_cmd('*', tmpfile)
102 f = os.path.basename(vfs_file)
103 st, buf = utils.ProcessFunc('Compressing Directory', '\'%s\'' % f,
104 utils.run_shell, cmd, tab.base).run()
105 if st == -1: # error
106 app.display()
107 messages.error('Creating vfs', buf)
108 app.display()
109 try:
110 files.do_delete(tmpfile)
111 except OSError:
112 pass
113 return
114 elif st == -100: # stopped by user
115 try:
116 files.do_delete(tmpfile)
117 except OSError:
118 pass
119 return
120 else:
121 tmpfile += c.exts[0] # zip & rar always adds extension
122 # copy file
123 try:
124 files.do_copy(tmpfile, vfs_file)
125 except (IOError, os.error), (errno, strerror):
126 files.do_delete(tmpfile)
127 return '%s (%s)' % (strerror, errno)
128 files.do_delete(tmpfile)
131 # vfs path join
132 def join(tab):
133 if tab.base == tab.path:
134 return tab.vbase
135 else:
136 return tab.vbase + tab.path.replace(tab.base, '')
139 # initialize panelize vfs stuff
140 def pan_init(tab, fs):
141 """initiliaze panelize vfs stuff"""
143 vfstype = 'pan'
144 tempdir = files.mkdtemp()
145 # copy files
146 for f in fs:
147 f_orig = os.path.join(tab.path, f)
148 f_dest = os.path.join(tempdir, f)
149 d = os.path.join(tempdir, os.path.dirname(f))
150 try:
151 os.makedirs(d)
152 except (IOError, os.error), (errno, strerror):
153 pass
154 try:
155 if os.path.isfile(f_orig):
156 files.do_copy(f_orig, f_dest)
157 elif os.path.isdir(f_orig):
158 os.mkdir(f_dest)
159 except (IOError, os.error), (errno, strerror):
160 messages.error('Can\'t create vfs', '%s (%s)' % (strerror, errno))
161 # update vfs vars
162 vpath = tab.path
163 tab.init(tempdir)
164 tab.vfs = vfstype
165 tab.base = tempdir
166 tab.vbase = vpath + '#vfs'
169 # copy pan vfs
170 def pan_copy(tab_org, tab_new):
171 """copy vfs"""
173 tempdir = files.mkdtemp()
174 # copy contents
175 dir_src = tab_org.base
176 for f in glob(os.path.join(dir_src, '*')):
177 f = os.path.basename(f)
178 try:
179 files.do_copy(os.path.join(dir_src, f), os.path.join(tempdir, f))
180 except (IOError, os.error), (errno, strerror):
181 app.display()
182 messages.error('Error regenerating vfs file',
183 '%s (%s)' % (strerror, errno))
184 # init vars
185 tab_new.base = tempdir
186 tab_new.vfs = tab_org.vfs
187 tab_new.vbase = tab_org.vbase
190 # regenerate vfs pan file
191 def pan_regenerate(tab):
192 """regenerate vfs pan file: copy files"""
194 dir_src = tab.path
195 dir_dest = tab.vbase.replace('#vfs', '')
196 # check if can copy files
197 i, oe = os.popen4('touch ' + dir_dest, 'r')
198 out = oe.read()
199 i.close(), oe.close()
200 if out:
201 return ''.join(out.split(':')[1:])[1:]
202 # copy files
203 for f in glob(os.path.join(dir_src, '*')):
204 f = os.path.basename(f)
205 try:
206 files.do_copy(os.path.join(dir_src, f), os.path.join(dir_dest, f))
207 except (IOError, os.error), (errno, strerror):
208 app.display()
209 messages.error('Error regenerating vfs file',
210 '%s (%s)' % (strerror, errno))
213 ######################################################################