<s> import os <EOL> import tempfile <EOL> from oslo_log import log as logging <EOL> from oslo_utils import excutils <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> from nova import utils <EOL> from nova . virt . disk . mount import api as mount_api <EOL> from nova . virt . disk . vfs import api as vfs <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class VFSLocalFS ( vfs . VFS ) : <EOL> """<STR_LIT>""" <EOL> def _canonical_path ( self , path ) : <EOL> canonpath , _err = utils . execute ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> os . path . join ( self . imgdir , path . lstrip ( "<STR_LIT:/>" ) ) , <EOL> run_as_root = True ) <EOL> if not canonpath . startswith ( os . path . realpath ( self . imgdir ) + '<STR_LIT:/>' ) : <EOL> raise exception . Invalid ( _ ( '<STR_LIT>' ) % path ) <EOL> return canonpath <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , image , partition = None , imgdir = None ) : <EOL> """<STR_LIT>""" <EOL> super ( VFSLocalFS , self ) . __init__ ( image , partition ) <EOL> self . imgdir = imgdir <EOL> self . mount = None <EOL> def setup ( self , mount = True ) : <EOL> self . imgdir = tempfile . mkdtemp ( prefix = "<STR_LIT>" ) <EOL> try : <EOL> mnt = mount_api . Mount . instance_for_format ( self . image , <EOL> self . imgdir , <EOL> self . partition ) <EOL> if mount : <EOL> if not mnt . do_mount ( ) : <EOL> raise exception . NovaException ( mnt . error ) <EOL> self . mount = mnt <EOL> except Exception as e : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . debug ( "<STR_LIT>" , { '<STR_LIT>' : e } ) <EOL> self . teardown ( ) <EOL> def teardown ( self ) : <EOL> try : <EOL> if self . mount : <EOL> self . mount . do_teardown ( ) <EOL> except Exception as e : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : self . imgdir , '<STR_LIT>' : e } ) <EOL> try : <EOL> if self . imgdir : <EOL> os . rmdir ( self . imgdir ) <EOL> except Exception as e : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : self . imgdir , '<STR_LIT>' : e } ) <EOL> self . imgdir = None <EOL> self . mount = None <EOL> def make_path ( self , path ) : <EOL> LOG . debug ( "<STR_LIT>" , path ) <EOL> canonpath = self . _canonical_path ( path ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , canonpath , run_as_root = True ) <EOL> def append_file ( self , path , content ) : <EOL> LOG . debug ( "<STR_LIT>" , path ) <EOL> canonpath = self . _canonical_path ( path ) <EOL> args = [ "<STR_LIT>" , canonpath ] <EOL> kwargs = dict ( process_input = content , run_as_root = True ) <EOL> utils . execute ( '<STR_LIT>' , * args , ** kwargs ) <EOL> def replace_file ( self , path , content ) : <EOL> LOG . debug ( "<STR_LIT>" , path ) <EOL> canonpath = self . _canonical_path ( path ) <EOL> args = [ canonpath ] <EOL> kwargs = dict ( process_input = content , run_as_root = True ) <EOL> utils . execute ( '<STR_LIT>' , * args , ** kwargs ) <EOL> def read_file ( self , path ) : <EOL> LOG . debug ( "<STR_LIT>" , path ) <EOL> canonpath = self . _canonical_path ( path ) <EOL> return utils . read_file_as_root ( canonpath ) <EOL> def has_file ( self , path ) : <EOL> LOG . debug ( "<STR_LIT>" , path ) <EOL> canonpath = self . _canonical_path ( path ) <EOL> exists , _err = utils . trycmd ( '<STR_LIT>' , '<STR_LIT>' , <EOL> canonpath , <EOL> run_as_root = True ) <EOL> return exists <EOL> def set_permissions ( self , path , mode ) : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT:path>' : path , '<STR_LIT>' : mode } ) <EOL> canonpath = self . _canonical_path ( path ) <EOL> utils . execute ( '<STR_LIT>' , "<STR_LIT>" % mode , canonpath , run_as_root = True ) <EOL> def set_ownership ( self , path , user , group ) : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT:path>' : path , '<STR_LIT:user>' : user , '<STR_LIT>' : group } ) <EOL> canonpath = self . _canonical_path ( path ) <EOL> owner = None <EOL> cmd = "<STR_LIT>" <EOL> if group is not None and user is not None : <EOL> owner = user + "<STR_LIT::>" + group <EOL> elif user is not None : <EOL> owner = user <EOL> elif group is not None : <EOL> owner = group <EOL> cmd = "<STR_LIT>" <EOL> if owner is not None : <EOL> utils . execute ( cmd , owner , canonpath , run_as_root = True ) <EOL> def get_image_fs ( self ) : <EOL> if self . mount . device or self . mount . get_dev ( ) : <EOL> out , err = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:value>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , self . mount . device , <EOL> run_as_root = True , <EOL> check_exit_code = [ <NUM_LIT:0> , <NUM_LIT:2> ] ) <EOL> return out . strip ( ) <EOL> return "<STR_LIT>" </s>
