Revision 121f3bc0 image_creator/disk.py

b/image_creator/disk.py
1
# -*- coding: utf-8 -*-
2
#
1 3
# Copyright 2012 GRNET S.A. All rights reserved.
2 4
#
3 5
# Redistribution and use in source and binary forms, with or
......
31 33
# interpreted as representing official policies, either expressed
32 34
# or implied, of GRNET S.A.
33 35

  
36
"""Module hosting the Disk class."""
37

  
34 38
from image_creator.util import get_command
35 39
from image_creator.util import try_fail_repeat
36 40
from image_creator.util import free_space
......
50 54
blockdev = get_command('blockdev')
51 55

  
52 56

  
53
TMP_CANDIDATES = ['/var/tmp', os.path.expanduser('~'), '/mnt']
57
def get_tmp_dir(default=None):
58
    """Check tmp directory candidates and return the one with the most
59
    available space.
60
    """
61
    if default is not None:
62
        return default
63

  
64
    TMP_CANDIDATES = ['/var/tmp', os.path.expanduser('~'), '/mnt']
65

  
66
    space = map(free_space, TMP_CANDIDATES)
67

  
68
    max_idx = 0
69
    max_val = space[0]
70
    for i, val in zip(range(len(space)), space):
71
        if val > max_val:
72
            max_val = val
73
            max_idx = i
74

  
75
    # Return the candidate path with more available space
76
    return TMP_CANDIDATES[max_idx]
54 77

  
55 78

  
56 79
class Disk(object):
......
71 94
        self.out = output
72 95
        self.meta = {}
73 96
        self.tmp = tempfile.mkdtemp(prefix='.snf_image_creator.',
74
                                    dir=self._get_tmp_dir(tmp))
97
                                    dir=get_tmp_dir(tmp))
75 98

  
76 99
        self._add_cleanup(shutil.rmtree, self.tmp)
77 100

  
78
    def _get_tmp_dir(self, default=None):
79
        """Check tmp directory candidates and return the one with the most
80
        available space.
81
        """
82
        if default is not None:
83
            return default
84

  
85
        space = map(free_space, TMP_CANDIDATES)
86

  
87
        max_idx = 0
88
        max_val = space[0]
89
        for i, val in zip(range(len(space)), space):
90
            if val > max_val:
91
                max_val = val
92
                max_idx = i
93

  
94
        # Return the candidate path with more available space
95
        return TMP_CANDIDATES[max_idx]
96

  
97 101
    def _add_cleanup(self, job, *args):
98 102
        """Add a new job in the cleanup list"""
99 103
        self._cleanup_jobs.append((job, args))

Also available in: Unified diff