~duplicity-team/duplicity/0.8-series

« back to all changes in this revision

Viewing changes to duplicity/dup_temp.py

  • Committer: kenneth at loafman
  • Date: 2018-07-23 16:32:30 UTC
  • Revision ID: kenneth@loafman.com-20180723163230-i226wdy5q2zzgfc7
* Fixed unadorned strings to unicode in duplicity/*/*
  - Some fixup due to shifting indenataion not matching PEP8.
  - Substituted for non-ascii char in jottlibbackend.py comment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# along with duplicity; if not, write to the Free Software Foundation,
20
20
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
21
 
22
 
"""Manage temporary files"""
 
22
u"""Manage temporary files"""
23
23
 
24
24
import os
25
25
import sys
35
35
 
36
36
 
37
37
def new_temppath():
38
 
    """
 
38
    u"""
39
39
    Return a new TempPath
40
40
    """
41
41
    filename = tempdir.default().mktemp()
43
43
 
44
44
 
45
45
class TempPath(path.Path):
46
 
    """
 
46
    u"""
47
47
    Path object used as a temporary file
48
48
    """
49
49
    def delete(self):
50
 
        """
 
50
        u"""
51
51
        Forget and delete
52
52
        """
53
53
        path.Path.delete(self)
54
54
        tempdir.default().forget(self.name)
55
55
 
56
56
    def open_with_delete(self, mode):
57
 
        """
 
57
        u"""
58
58
        Returns a fileobj.  When that is closed, delete file
59
59
        """
60
60
        fh = FileobjHooked(path.Path.open(self, mode))
63
63
 
64
64
 
65
65
def get_fileobj_duppath(dirpath, partname, permname, remname, overwrite=False):
66
 
    """
 
66
    u"""
67
67
    Return a file object open for writing, will write to filename
68
68
 
69
69
    Data will be processed and written to a temporary file.  When the
74
74
        td = tempdir.TemporaryDirectory(dirpath.name)
75
75
        tdpname = td.mktemp()
76
76
        tdp = TempDupPath(tdpname, parseresults=file_naming.parse(partname))
77
 
        fh = FileobjHooked(tdp.filtered_open("wb"), tdp=tdp, dirpath=dirpath,
 
77
        fh = FileobjHooked(tdp.filtered_open(u"wb"), tdp=tdp, dirpath=dirpath,
78
78
                           partname=partname, permname=permname, remname=remname)
79
79
    else:
80
80
        dp = path.DupPath(dirpath.name, index=(partname,))
81
 
        mode = "ab"
 
81
        mode = u"ab"
82
82
        if overwrite:
83
 
            mode = "wb"
 
83
            mode = u"wb"
84
84
        fh = FileobjHooked(dp.filtered_open(mode), tdp=None, dirpath=dirpath,
85
85
                           partname=partname, permname=permname, remname=remname)
86
86
 
95
95
 
96
96
 
97
97
def new_tempduppath(parseresults):
98
 
    """
 
98
    u"""
99
99
    Return a new TempDupPath, using settings from parseresults
100
100
    """
101
101
    filename = tempdir.default().mktemp()
103
103
 
104
104
 
105
105
class TempDupPath(path.DupPath):
106
 
    """
 
106
    u"""
107
107
    Like TempPath, but build around DupPath
108
108
    """
109
109
    def delete(self):
110
 
        """
 
110
        u"""
111
111
        Forget and delete
112
112
        """
113
113
        path.DupPath.delete(self)
114
114
        tempdir.default().forget(self.name)
115
115
 
116
116
    def filtered_open_with_delete(self, mode):
117
 
        """
 
117
        u"""
118
118
        Returns a filtered fileobj.  When that is closed, delete file
119
119
        """
120
120
        fh = FileobjHooked(path.DupPath.filtered_open(self, mode))
121
121
        fh.addhook(self.delete)
122
122
        return fh
123
123
 
124
 
    def open_with_delete(self, mode="rb"):
125
 
        """
 
124
    def open_with_delete(self, mode=u"rb"):
 
125
        u"""
126
126
        Returns a fileobj.  When that is closed, delete file
127
127
        """
128
 
        assert mode == "rb"  # Why write a file and then close it immediately?
 
128
        assert mode == u"rb"  # Why write a file and then close it immediately?
129
129
        fh = FileobjHooked(path.DupPath.open(self, mode))
130
130
        fh.addhook(self.delete)
131
131
        return fh
132
132
 
133
133
 
134
134
class FileobjHooked:
135
 
    """
 
135
    u"""
136
136
    Simulate a file, but add hook on close
137
137
    """
138
138
    def __init__(self, fileobj, tdp=None, dirpath=None,
139
139
                 partname=None, permname=None, remname=None):
140
 
        """
 
140
        u"""
141
141
        Initializer.  fileobj is the file object to simulate
142
142
        """
143
143
        self.fileobj = fileobj  # the actual file object
150
150
        self.remname = remname  # remote filename
151
151
 
152
152
    def write(self, buf):
153
 
        """
 
153
        u"""
154
154
        Write fileobj, return result of write()
155
155
        """
156
156
        return self.fileobj.write(buf)
157
157
 
158
158
    def flush(self):
159
 
        """
 
159
        u"""
160
160
        Flush fileobj and force sync.
161
161
        """
162
162
        self.fileobj.flush()
163
163
        os.fsync(self.fileobj.fileno())
164
164
 
165
165
    def to_partial(self):
166
 
        """
 
166
        u"""
167
167
        We have achieved the first checkpoint, make file visible and permanent.
168
168
        """
169
169
        assert not globals.restart
172
172
        del self.hooklist[0]
173
173
 
174
174
    def to_remote(self):
175
 
        """
 
175
        u"""
176
176
        We have written the last checkpoint, now encrypt or compress
177
177
        and send a copy of it to the remote for final storage.
178
178
        """
189
189
        globals.backend.move(tgt)  # @UndefinedVariable
190
190
 
191
191
    def to_final(self):
192
 
        """
 
192
        u"""
193
193
        We are finished, rename to final, gzip if needed.
194
194
        """
195
195
        src = self.dirpath.append(self.partname)
203
203
            os.rename(src.name, tgt.name)
204
204
 
205
205
    def read(self, length=-1):
206
 
        """
 
206
        u"""
207
207
        Read fileobj, return result of read()
208
208
        """
209
209
        return self.fileobj.read(length)
210
210
 
211
211
    def tell(self):
212
 
        """
 
212
        u"""
213
213
        Returns current location of fileobj
214
214
        """
215
215
        return self.fileobj.tell()
216
216
 
217
217
    def seek(self, offset):
218
 
        """
 
218
        u"""
219
219
        Seeks to a location of fileobj
220
220
        """
221
221
        return self.fileobj.seek(offset)
222
222
 
223
223
    def close(self):
224
 
        """
 
224
        u"""
225
225
        Close fileobj, running hooks right afterwards
226
226
        """
227
227
        assert not self.fileobj.close()
229
229
            hook()
230
230
 
231
231
    def addhook(self, hook):
232
 
        """
 
232
        u"""
233
233
        Add hook (function taking no arguments) to run upon closing
234
234
        """
235
235
        self.hooklist.append(hook)
236
236
 
237
237
    def get_name(self):
238
 
        """
 
238
        u"""
239
239
        Return the name of the file
240
240
        """
241
241
        return self.fileobj.name
244
244
 
245
245
 
246
246
class Block:
247
 
    """
 
247
    u"""
248
248
    Data block to return from SrcIter
249
249
    """
250
250
    def __init__(self, data):
252
252
 
253
253
 
254
254
class SrcIter:
255
 
    """
 
255
    u"""
256
256
    Iterate over source and return Block of data.
257
257
    """
258
258
    def __init__(self, src):
259
259
        self.src = src
260
 
        self.fp = src.open("rb")
 
260
        self.fp = src.open(u"rb")
261
261
 
262
262
    def next(self):
263
263
        try:
264
264
            res = Block(self.fp.read(self.get_read_size()))
265
265
        except Exception:
266
 
            log.FatalError(_("Failed to read %s: %s") %
 
266
            log.FatalError(_(u"Failed to read %s: %s") %
267
267
                           (self.src.uc_name, sys.exc_info()),
268
268
                           log.ErrorCode.generic)
269
269
        if not res.data:
275
275
        return 128 * 1024
276
276
 
277
277
    def get_footer(self):
278
 
        return ""
 
278
        return u""