<s> import glob <EOL> import os <EOL> import shutil <EOL> import tempfile <EOL> from subprocess import Popen , STDOUT , PIPE , check_call , CalledProcessError <EOL> from kokki . base import Fail <EOL> from kokki . providers . package import PackageProvider <EOL> class DebianAptProvider ( PackageProvider ) : <EOL> def get_current_status ( self ) : <EOL> self . current_version = None <EOL> self . candidate_version = None <EOL> proc = Popen ( "<STR_LIT>" % self . resource . package_name , shell = True , stdout = PIPE ) <EOL> out = proc . communicate ( ) [ <NUM_LIT:0> ] <EOL> for line in out . split ( "<STR_LIT:\n>" ) : <EOL> line = line . strip ( ) . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if len ( line ) != <NUM_LIT:2> : <EOL> continue <EOL> ver = line [ <NUM_LIT:1> ] . strip ( ) <EOL> if line [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> self . current_version = None if ver == '<STR_LIT>' else ver <EOL> self . log . debug ( "<STR_LIT>" % ( self . resource . package_name , self . current_version ) ) <EOL> elif line [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> self . candidate_version = ver <EOL> if self . candidate_version == "<STR_LIT>" : <EOL> raise Fail ( "<STR_LIT>" % self . resource . package_name ) <EOL> def install_package ( self , name , version ) : <EOL> if self . resource . build_vars : <EOL> self . _install_package_source ( name , version ) <EOL> else : <EOL> self . _install_package_default ( name , version ) <EOL> def _install_package_default ( self , name , version ) : <EOL> return <NUM_LIT:0> == check_call ( "<STR_LIT>" % ( name , version ) , <EOL> shell = True , stdout = PIPE , stderr = STDOUT ) <EOL> def _install_package_source ( self , name , version ) : <EOL> build_vars = "<STR_LIT:U+0020>" . join ( self . resource . build_vars ) <EOL> run_check_call = lambda s , ** kw : check_call ( s , shell = True , stdout = PIPE , stderr = STDOUT , ** kw ) <EOL> pkgdir = tempfile . mkdtemp ( suffix = name ) <EOL> try : <EOL> run_check_call ( "<STR_LIT>" ) <EOL> run_check_call ( "<STR_LIT>" % ( name , version ) ) <EOL> run_check_call ( "<STR_LIT>" % ( name , version ) , cwd = pkgdir ) <EOL> try : <EOL> builddir = [ p for p in glob . iglob ( "<STR_LIT>" % ( pkgdir , name ) ) if os . path . isdir ( p ) ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise Fail ( "<STR_LIT>" % name ) <EOL> run_check_call ( "<STR_LIT>" % build_vars , cwd = builddir ) <EOL> try : <EOL> run_check_call ( "<STR_LIT>" , cwd = pkgdir ) <EOL> except CalledProcessError : <EOL> pass <EOL> finally : <EOL> shutil . rmtree ( pkgdir ) <EOL> return True <EOL> def remove_package ( self , name ) : <EOL> return <NUM_LIT:0> == check_call ( "<STR_LIT>" % name , <EOL> shell = True , stdout = PIPE , stderr = STDOUT ) <EOL> def purge_package ( self , name ) : <EOL> return <NUM_LIT:0> == check_call ( "<STR_LIT>" % name , <EOL> shell = True , stdout = PIPE , stderr = STDOUT ) <EOL> def upgrade_package ( self , name , version ) : <EOL> return self . install_package ( name , version ) </s>
