<s> '''<STR_LIT>''' <EOL> from __future__ import unicode_literals <EOL> import six <EOL> from pyinfra . api import operation <EOL> from . import files <EOL> from . util . files import chmod <EOL> @ operation <EOL> def wait ( state , host , port = None ) : <EOL> '''<STR_LIT>''' <EOL> return [ '''<STR_LIT>''' . format ( port ) ] <EOL> @ operation <EOL> def shell ( state , host , commands , chdir = None ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( commands , six . string_types ) : <EOL> commands = [ commands ] <EOL> if chdir : <EOL> commands = [ <EOL> '<STR_LIT>' . format ( chdir , command ) <EOL> for command in commands <EOL> ] <EOL> return commands <EOL> @ operation <EOL> def script ( state , host , filename , chdir = None ) : <EOL> '''<STR_LIT>''' <EOL> commands = [ ] <EOL> temp_file = state . get_temp_filename ( filename ) <EOL> commands . extend ( files . put ( state , host , filename , temp_file ) ) <EOL> commands . append ( chmod ( temp_file , '<STR_LIT>' ) ) <EOL> if chdir : <EOL> commands . append ( '<STR_LIT>' . format ( chdir , temp_file ) ) <EOL> else : <EOL> commands . append ( temp_file ) <EOL> return commands <EOL> @ operation <EOL> def group ( <EOL> state , host , name , present = True <EOL> ) : <EOL> '''<STR_LIT>''' <EOL> commands = [ ] <EOL> groups = host . fact . groups or [ ] <EOL> is_present = name in groups <EOL> if not present and is_present : <EOL> commands . append ( '<STR_LIT>' . format ( name ) ) <EOL> elif present and not is_present : <EOL> commands . append ( '<STR_LIT>' . format ( name ) ) <EOL> return commands <EOL> @ operation <EOL> def user ( <EOL> state , host , name , <EOL> present = True , home = None , shell = None , group = None , groups = None , <EOL> public_keys = None , ensure_home = True <EOL> ) : <EOL> '''<STR_LIT>''' <EOL> commands = [ ] <EOL> users = host . fact . users or { } <EOL> user = users . get ( name ) <EOL> if groups is None : <EOL> groups = [ ] <EOL> if not present and user : <EOL> commands . append ( '<STR_LIT>' . format ( name ) ) <EOL> return commands <EOL> if present and user is None : <EOL> args = [ ] <EOL> if home : <EOL> args . append ( '<STR_LIT>' . format ( home ) ) <EOL> if shell : <EOL> args . append ( '<STR_LIT>' . format ( shell ) ) <EOL> if group : <EOL> args . append ( '<STR_LIT>' . format ( group ) ) <EOL> if groups : <EOL> args . append ( '<STR_LIT>' . format ( '<STR_LIT:U+002C>' . join ( groups ) ) ) <EOL> commands . append ( '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( args ) , name ) ) <EOL> else : <EOL> args = [ ] <EOL> if home and user [ '<STR_LIT>' ] != home : <EOL> args . append ( '<STR_LIT>' . format ( home ) ) <EOL> if shell and user [ '<STR_LIT>' ] != shell : <EOL> args . append ( '<STR_LIT>' . format ( shell ) ) <EOL> if group and user [ '<STR_LIT>' ] != group : <EOL> args . append ( '<STR_LIT>' . format ( group ) ) <EOL> if set ( user [ '<STR_LIT>' ] ) != set ( groups ) : <EOL> args . append ( '<STR_LIT>' . format ( '<STR_LIT:U+002C>' . join ( groups ) ) ) <EOL> if args : <EOL> commands . append ( '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( args ) , name ) ) <EOL> if home : <EOL> if ensure_home : <EOL> commands . extend ( files . directory ( <EOL> state , host , home , <EOL> user = name , group = name <EOL> ) ) <EOL> if public_keys is not None : <EOL> commands . extend ( files . directory ( <EOL> state , host , <EOL> '<STR_LIT>' . format ( home ) , <EOL> user = name , group = name , <EOL> mode = <NUM_LIT> <EOL> ) ) <EOL> filename = '<STR_LIT>' . format ( home ) <EOL> commands . extend ( files . file ( <EOL> state , host , filename , <EOL> user = name , group = name , <EOL> mode = <NUM_LIT> <EOL> ) ) <EOL> for key in public_keys : <EOL> commands . append ( '<STR_LIT>' . format ( <EOL> filename , key <EOL> ) ) <EOL> return commands </s>
