Source

distribute / tests / test_pkg_resources.py

The branch 'default' does not exist.
import sys
import tempfile
import os
import zipfile

import pkg_resources

class EggRemover(unicode):
	def __call__(self):
		if self in sys.path:
			sys.path.remove(self)
		if os.path.exists(self):
			os.remove(self)

class TestZipProvider(object):
	finalizers = []

	@classmethod
	def setup_class(cls):
		"create a zip egg and add it to sys.path"
		egg = tempfile.NamedTemporaryFile(suffix='.egg', delete=False)
		zip_egg = zipfile.ZipFile(egg, 'w')
		zip_info = zipfile.ZipInfo()
		zip_info.filename = 'mod.py'
		zip_info.date_time = 2013, 5, 12, 13, 25, 0
		zip_egg.writestr(zip_info, 'x = 3\n')
		zip_info = zipfile.ZipInfo()
		zip_info.filename = 'data.dat'
		zip_info.date_time = 2013, 5, 12, 13, 25, 0
		zip_egg.writestr(zip_info, 'hello, world!')
		zip_egg.close()
		egg.close()

		sys.path.append(egg.name)
		cls.finalizers.append(EggRemover(egg.name))

	@classmethod
	def teardown_class(cls):
		for finalizer in cls.finalizers:
			finalizer()

	def test_resource_filename_rewrites_on_change(self):
		"""
		If a previous call to get_resource_filename has saved the file, but
		the file has been subsequently mutated with different file of the
		same size and modification time, it should not be overwritten on a
		subsequent call to get_resource_filename.
		"""
		import mod
		manager = pkg_resources.ResourceManager()
		zp = pkg_resources.ZipProvider(mod)
		filename = zp.get_resource_filename(manager, 'data.dat')
		assert os.stat(filename).st_mtime == 1368379500
		f = open(filename, 'wb')
		f.write('hello, world?')
		f.close()
		os.utime(filename, (1368379500, 1368379500))
		filename = zp.get_resource_filename(manager, 'data.dat')
		f = open(filename)
		assert f.read() == 'hello, world!'
		manager.cleanup_resources()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.