Merge lp:~ed.so/duplicity/tempfile.tempdir into lp:~duplicity-team/duplicity/0.7-series

Proposed by edso
Status: Merged
Merged at revision: 1147
Proposed branch: lp:~ed.so/duplicity/tempfile.tempdir
Merge into: lp:~duplicity-team/duplicity/0.7-series
Diff against target: 105 lines (+49/-0)
2 files modified
duplicity/tempdir.py (+11/-0)
testing/unit/test_tempdir.py (+38/-0)
To merge this branch: bzr merge lp:~ed.so/duplicity/tempfile.tempdir
Reviewer Review Type Date Requested Status
Kenneth Loafman Pending
Review via email: mp+276336@code.launchpad.net

This proposal supersedes a proposal from 2015-10-18.

Description of the change

make sure packages using python's tempfile create temp files in duplicity's temp dir

Ken, please try again :) toxed and everything

To post a comment you must log in.
Revision history for this message
Kenneth Loafman (kenneth-loafman) wrote : Posted in a previous version of this proposal

edso, this one fails tox tests with 33 errors.

review: Needs Fixing
Revision history for this message
edso (ed.so) wrote : Posted in a previous version of this proposal

On 18.10.2015 19:28, Kenneth Loafman wrote:
> Review: Needs Fixing
>
> edso, this one fails tox tests with 33 errors.
>

hey Ken,

i have no tox set up here. it's a simple one liner so it's probably no syntax issue but the test's are at fault? essentially it makes the duplicity generated temp folder the base folder _all_ file(path)s generated via python tempfile.

will check what's going on. ..thx ede

Revision history for this message
edso (ed.so) wrote : Posted in a previous version of this proposal

Ken, please try again :) toxed and everything

Revision history for this message
Kenneth Loafman (kenneth-loafman) wrote :

Thanks ede. The added tests are a big help.

Revision history for this message
edso (ed.so) wrote :

On 01.11.2015 13:55, Kenneth Loafman wrote:
> Thanks ede. The added tests are a big help.
>

thx, never too old to learn i guess ;).. ede

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'duplicity/tempdir.py'
2--- duplicity/tempdir.py 2015-01-31 23:30:49 +0000
3+++ duplicity/tempdir.py 2015-10-31 20:34:16 +0000
4@@ -38,6 +38,9 @@
5 # instance
6 _defaultLock = threading.Lock()
7 _defaultInstance = None
8+# backup the initial tmp dir path because we will force tempfile
9+# later to use our generated _defaultInstance.dir() as temproot
10+_initialSystemTempRoot = tempfile.gettempdir()
11
12
13 def default():
14@@ -57,6 +60,8 @@
15 try:
16 if _defaultInstance is None or _defaultInstance.dir() is None:
17 _defaultInstance = TemporaryDirectory(temproot=globals.temproot)
18+ # set the temp dir to be the default in tempfile module from now on
19+ tempfile.tempdir = _defaultInstance.dir()
20 return _defaultInstance
21 finally:
22 _defaultLock.release()
23@@ -116,6 +121,12 @@
24 tempbase - The temp root directory, or None to use system
25 default (recommended).
26 """
27+ if temproot is None:
28+ if globals.temproot:
29+ temproot = globals.temproot
30+ else:
31+ global _initialSystemTempRoot
32+ temproot = _initialSystemTempRoot
33 self.__dir = tempfile.mkdtemp("-tempdir", "duplicity-", temproot)
34
35 log.Info(_("Using temporary directory %s") % util.ufn(self.__dir))
36
37=== modified file 'testing/unit/test_tempdir.py'
38--- testing/unit/test_tempdir.py 2015-08-01 12:05:34 +0000
39+++ testing/unit/test_tempdir.py 2015-10-31 20:34:16 +0000
40@@ -20,6 +20,7 @@
41 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42
43 import os
44+import tempfile
45 import unittest
46
47 from duplicity import tempdir
48@@ -30,20 +31,57 @@
49 def test_all(self):
50 td = tempdir.default()
51
52+ # are generated temp files unique?
53 self.assertTrue(td.mktemp() != td.mktemp())
54
55+ # create and remove a temp dir
56 dir = td.mktemp()
57 os.mkdir(dir)
58 os.rmdir(dir)
59
60+ # test mkstemp()
61 fd, fname = td.mkstemp()
62 os.close(fd)
63 os.unlink(fname)
64 td.forget(fname)
65
66+ # test mkstemp_file()
67 fo, fname = td.mkstemp_file()
68 fo.close() # don't forget, leave to cleanup()
69
70+ # cleanup
71+ td.cleanup()
72+
73+ def test_dirname(self):
74+ """
75+ test if we generated a dirname
76+ """
77+ td = tempdir.default()
78+ dirname = td.dir()
79+ self.assertTrue( dirname is not None )
80+
81+ """
82+ test if duplicity's temp files are created in our temp dir
83+ """
84+ f1d, f1_name = tempdir.default().mkstemp()
85+ f1_dirname = os.path.dirname( f1_name )
86+
87+ self.assertTrue( dirname == f1_dirname )
88+
89+ """
90+ test if tempfile creates in our temp dir now as well by default
91+ """
92+ f2 = tempfile.NamedTemporaryFile()
93+ f2_dirname = os.path.dirname( f2.name )
94+
95+ self.assertTrue( dirname == f2_dirname )
96+
97+ # cleanup
98+ os.close(f1d)
99+ os.unlink(f1_name)
100+ td.forget(f1_name)
101+ f2.close()
102+
103 td.cleanup()
104
105 if __name__ == "__main__":

Subscribers

People subscribed via source and target branches