~hudson-openstack/nova/bexar

« back to all changes in this revision

Viewing changes to nova/image/local.py

  • Committer: Tarmac
  • Author(s): John Dewey
  • Date: 2011-01-27 17:19:10 UTC
  • mfrom: (611.1.4 lp705131)
  • Revision ID: tarmac-20110127171910-svnyxx6c0rpk6oav
Make unit tests clean up their mess in /tmp after themselves.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import cPickle as pickle
19
19
import os.path
20
20
import random
 
21
import tempfile
21
22
 
22
23
from nova import exception
23
24
from nova.image import service
26
27
class LocalImageService(service.BaseImageService):
27
28
 
28
29
    """Image service storing images to local disk.
 
30
    It assumes that image_ids are integers.
29
31
 
30
 
    It assumes that image_ids are integers."""
 
32
    """
31
33
 
32
34
    def __init__(self):
33
 
        self._path = "/tmp/nova/images"
34
 
        try:
35
 
            os.makedirs(self._path)
36
 
        except OSError:  # Exists
37
 
            pass
 
35
        self._path = tempfile.mkdtemp()
38
36
 
39
37
    def _path_to(self, image_id):
40
38
        return os.path.join(self._path, str(image_id))
56
54
            raise exception.NotFound
57
55
 
58
56
    def create(self, context, data):
59
 
        """
60
 
        Store the image data and return the new image id.
61
 
        """
 
57
        """Store the image data and return the new image id."""
62
58
        id = random.randint(0, 2 ** 31 - 1)
63
59
        data['id'] = id
64
60
        self.update(context, id, data)
72
68
            raise exception.NotFound
73
69
 
74
70
    def delete(self, context, image_id):
75
 
        """
76
 
        Delete the given image.  Raises OSError if the image does not exist.
 
71
        """Delete the given image.
 
72
        Raises OSError if the image does not exist.
 
73
 
77
74
        """
78
75
        try:
79
76
            os.unlink(self._path_to(image_id))
81
78
            raise exception.NotFound
82
79
 
83
80
    def delete_all(self):
84
 
        """
85
 
        Clears out all images in local directory
86
 
        """
 
81
        """Clears out all images in local directory."""
87
82
        for id in self._ids():
88
83
            os.unlink(self._path_to(id))
 
84
 
 
85
    def delete_imagedir(self):
 
86
        """Deletes the local directory.
 
87
        Raises OSError if directory is not empty.
 
88
 
 
89
        """
 
90
        os.rmdir(self._path)