[Python-checkins] distutils2: Really clean up each and every temp dir or file (with help from Alexis)
tarek.ziade
python-checkins at python.org
Sun Aug 8 11:50:45 CEST 2010
tarek.ziade pushed 77bd0cb71ecc to distutils2:
http://hg.python.org/distutils2/rev/77bd0cb71ecc
changeset: 418:77bd0cb71ecc
user: ?ric Araujo <merwok at netwok.org>
date: Sat Jul 31 17:30:38 2010 +0200
summary: Really clean up each and every temp dir or file (with help from Alexis)
files: src/distutils2/tests/support.py, src/distutils2/tests/test_Mixin2to3.py, src/distutils2/tests/test_pypi_dist.py, src/distutils2/tests/test_pypi_simple.py, src/distutils2/tests/test_upload_docs.py, src/distutils2/tests/test_util.py
diff --git a/src/distutils2/tests/support.py b/src/distutils2/tests/support.py
--- a/src/distutils2/tests/support.py
+++ b/src/distutils2/tests/support.py
@@ -93,35 +93,29 @@
class TempdirManager(object):
- """TestCase-compatible mixin to handle temporary directories."""
+ """TestCase-compatible mixin to create temporary directories and files.
+
+ Directories and files created in a test_* method will be removed after it
+ has run.
+ """
def setUp(self):
super(TempdirManager, self).setUp()
- self.tempdirs = []
- self.tempfiles = []
+ self._basetempdir = tempfile.mkdtemp()
def tearDown(self):
super(TempdirManager, self).tearDown()
- while self.tempdirs:
- d = self.tempdirs.pop()
- shutil.rmtree(d, os.name in ('nt', 'cygwin'))
- for file_ in self.tempfiles:
- if os.path.exists(file_):
- os.remove(file_)
+ shutil.rmtree(self._basetempdir, os.name in ('nt', 'cygwin'))
def mktempfile(self):
- """Create a temporary file that will be cleaned up."""
- tempfile_ = tempfile.NamedTemporaryFile()
- self.tempfiles.append(tempfile_.name)
- return tempfile_
+ """Create a read-write temporary file and return it."""
+ fd, fn = tempfile.mkstemp(dir=self._basetempdir)
+ os.close(fd)
+ return open(fn, 'w+')
def mkdtemp(self):
- """Create a temporary directory that will be removed on exit.
-
- Return the path of the directory.
- """
- d = tempfile.mkdtemp()
- self.tempdirs.append(d)
+ """Create a temporary directory and return its path."""
+ d = tempfile.mkdtemp(dir=self._basetempdir)
return d
def write_file(self, path, content='xxx'):
diff --git a/src/distutils2/tests/test_Mixin2to3.py b/src/distutils2/tests/test_Mixin2to3.py
--- a/src/distutils2/tests/test_Mixin2to3.py
+++ b/src/distutils2/tests/test_Mixin2to3.py
@@ -1,6 +1,5 @@
"""Tests for distutils.command.build_py."""
import sys
-import tempfile
import distutils2
from distutils2.tests import support
diff --git a/src/distutils2/tests/test_pypi_dist.py b/src/distutils2/tests/test_pypi_dist.py
--- a/src/distutils2/tests/test_pypi_dist.py
+++ b/src/distutils2/tests/test_pypi_dist.py
@@ -112,24 +112,21 @@
def test_download(self, server):
# Download is possible, and the md5 is checked if given
- add_to_tmpdirs = lambda x: self.tempdirs.append(os.path.dirname(x))
-
url = "%s/simple/foobar/foobar-0.1.tar.gz" % server.full_address
# check md5 if given
- add_to_tmpdirs(dist.download())
dist = Dist("FooBar", "0.1", url=url, url_hashname="md5",
url_hashval="d41d8cd98f00b204e9800998ecf8427e")
+ dist.download(self.mkdtemp())
# a wrong md5 fails
dist2 = Dist("FooBar", "0.1", url=url,
url_hashname="md5", url_hashval="wrongmd5")
- self.assertRaises(HashDoesNotMatch, dist2.download)
- add_to_tmpdirs(dist2.downloaded_location)
+ self.assertRaises(HashDoesNotMatch, dist2.download, self.mkdtemp())
# we can omit the md5 hash
dist3 = Dist("FooBar", "0.1", url=url)
- add_to_tmpdirs(dist3.download())
+ dist3.download(self.mkdtemp())
# and specify a temporary location
# for an already downloaded dist
diff --git a/src/distutils2/tests/test_pypi_simple.py b/src/distutils2/tests/test_pypi_simple.py
--- a/src/distutils2/tests/test_pypi_simple.py
+++ b/src/distutils2/tests/test_pypi_simple.py
@@ -4,7 +4,6 @@
import sys
import os
import shutil
-import tempfile
import urllib2
from distutils2.pypi import simple
diff --git a/src/distutils2/tests/test_upload_docs.py b/src/distutils2/tests/test_upload_docs.py
--- a/src/distutils2/tests/test_upload_docs.py
+++ b/src/distutils2/tests/test_upload_docs.py
@@ -1,13 +1,19 @@
+# -*- encoding: utf8 -*-
"""Tests for distutils.command.upload_docs."""
-# -*- encoding: utf8 -*-
-import httplib, os, os.path, shutil, sys, tempfile, zipfile
-from cStringIO import StringIO
+import os
+import sys
+import httplib
+import shutil
+import zipfile
+try:
+ from cStringIO import StringIO
+except ImportError:
+ from StringIO import StringIO
from distutils2.command import upload_docs as upload_docs_mod
from distutils2.command.upload_docs import (upload_docs, zip_dir,
- encode_multipart)
+ encode_multipart)
from distutils2.core import Distribution
-
from distutils2.errors import DistutilsFileError, DistutilsOptionError
from distutils2.tests import support
@@ -59,7 +65,7 @@
self.cmd = upload_docs(self.dist)
def test_default_uploaddir(self):
- sandbox = tempfile.mkdtemp()
+ sandbox = self.mkdtemp()
previous = os.getcwd()
os.chdir(sandbox)
try:
@@ -72,7 +78,7 @@
def prepare_sample_dir(self, sample_dir=None):
if sample_dir is None:
- sample_dir = tempfile.mkdtemp()
+ sample_dir = self.mkdtemp()
os.mkdir(os.path.join(sample_dir, "docs"))
self.write_file(os.path.join(sample_dir, "docs", "index.html"), "Ce mortel ennui")
self.write_file(os.path.join(sample_dir, "index.html"), "Oh la la")
diff --git a/src/distutils2/tests/test_util.py b/src/distutils2/tests/test_util.py
--- a/src/distutils2/tests/test_util.py
+++ b/src/distutils2/tests/test_util.py
@@ -4,7 +4,6 @@
from copy import copy
from StringIO import StringIO
import subprocess
-import tempfile
import time
from distutils2.tests import captured_stdout
@@ -301,9 +300,9 @@
def test_newer(self):
self.assertRaises(DistutilsFileError, util.newer, 'xxx', 'xxx')
- self.newer_f1 = tempfile.NamedTemporaryFile()
+ self.newer_f1 = self.mktempfile()
time.sleep(1)
- self.newer_f2 = tempfile.NamedTemporaryFile()
+ self.newer_f2 = self.mktempfile()
self.assertTrue(util.newer(self.newer_f2.name, self.newer_f1.name))
def test_find_packages(self):
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list