<s> """<STR_LIT>""" <EOL> from re import escape <EOL> from fabric . api import settings , warn <EOL> from fabric . contrib . files import append , uncomment <EOL> from fabtools . files import is_file , watch <EOL> from fabtools . system import ( <EOL> UnsupportedFamily , <EOL> distrib_family , distrib_id , <EOL> get_hostname , set_hostname , <EOL> get_sysctl , set_sysctl , <EOL> supported_locales , <EOL> ) <EOL> from fabtools . utils import run_as_root <EOL> class UnsupportedLocales ( Exception ) : <EOL> def __init__ ( self , locales ) : <EOL> self . locales = sorted ( locales ) <EOL> msg = "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( self . locales ) <EOL> super ( UnsupportedLocales , self ) . __init__ ( msg ) <EOL> def sysctl ( key , value , persist = True ) : <EOL> """<STR_LIT>""" <EOL> if get_sysctl ( key ) != value : <EOL> set_sysctl ( key , value ) <EOL> if persist : <EOL> from fabtools . require import file as require_file <EOL> filename = '<STR_LIT>' % key <EOL> with watch ( filename , use_sudo = True ) as config : <EOL> require_file ( filename , <EOL> contents = '<STR_LIT>' % locals ( ) , <EOL> use_sudo = True ) <EOL> if config . changed : <EOL> if distrib_family ( ) == '<STR_LIT>' : <EOL> with settings ( warn_only = True ) : <EOL> run_as_root ( '<STR_LIT>' ) <EOL> def hostname ( name ) : <EOL> """<STR_LIT>""" <EOL> if get_hostname ( ) != name : <EOL> set_hostname ( name ) <EOL> def locales ( names ) : <EOL> """<STR_LIT>""" <EOL> family = distrib_family ( ) <EOL> if family == '<STR_LIT>' : <EOL> command = '<STR_LIT>' <EOL> if distrib_id ( ) == '<STR_LIT>' : <EOL> config_file = '<STR_LIT>' <EOL> if not is_file ( config_file ) : <EOL> run_as_root ( '<STR_LIT>' % config_file ) <EOL> else : <EOL> config_file = '<STR_LIT>' <EOL> _locales_generic ( names , config_file = config_file , command = command ) <EOL> elif family in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> _locales_generic ( names , config_file = '<STR_LIT>' , command = '<STR_LIT>' ) <EOL> elif distrib_family ( ) == '<STR_LIT>' : <EOL> _locales_redhat ( names ) <EOL> else : <EOL> raise UnsupportedFamily ( supported = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def _locales_generic ( names , config_file , command ) : <EOL> supported = supported_locales ( ) <EOL> _check_for_unsupported_locales ( names , supported ) <EOL> with watch ( config_file , use_sudo = True ) as config : <EOL> charset_from_name = dict ( supported ) <EOL> for name in names : <EOL> charset = charset_from_name [ name ] <EOL> locale = "<STR_LIT>" % ( name , charset ) <EOL> uncomment ( config_file , escape ( locale ) , use_sudo = True , shell = True ) <EOL> append ( config_file , locale , use_sudo = True , partial = True , shell = True ) <EOL> if config . changed : <EOL> run_as_root ( command ) <EOL> def _locales_redhat ( names ) : <EOL> supported = supported_locales ( ) <EOL> _check_for_unsupported_locales ( names , supported ) <EOL> def _check_for_unsupported_locales ( names , supported ) : <EOL> missing = set ( names ) - set ( [ name for name , _ in supported ] ) <EOL> if missing : <EOL> raise UnsupportedLocales ( missing ) <EOL> def locale ( name ) : <EOL> """<STR_LIT>""" <EOL> locales ( [ name ] ) <EOL> def default_locale ( name ) : <EOL> """<STR_LIT>""" <EOL> from fabtools . require import file as require_file <EOL> locale ( name ) <EOL> contents = '<STR_LIT>' % name <EOL> if distrib_family ( ) == '<STR_LIT>' : <EOL> config_file = '<STR_LIT>' <EOL> else : <EOL> config_file = '<STR_LIT>' <EOL> require_file ( config_file , contents , use_sudo = True ) </s>
