def write_pem(text, path, overwrite=True, pem_type=None):
 old_umask = os.umask(63)
 text = get_pem_entry(text, pem_type=pem_type)
 _dhparams = ''
 _private_key = ''
 if (pem_type and (pem_type == 'CERTIFICATE') and os.path.isfile(path) and (not overwrite)):
  _filecontents = _text_or_file(path)
  try:
   _dhparams = get_pem_entry(_filecontents, 'DH PARAMETERS')
  except salt.exceptions.SaltInvocationError:
   pass
  try:
   _private_key = get_pem_entry(_filecontents, 'RSA PRIVATE KEY')
  except salt.exceptions.SaltInvocationError:
   pass
 with salt.utils.fopen(path, 'w') as _fp:
  if (pem_type and (pem_type == 'CERTIFICATE') and _private_key):
   _fp.write(_private_key)
  _fp.write(text)
  if (pem_type and (pem_type == 'CERTIFICATE') and _dhparams):
   _fp.write(_dhparams)
 os.umask(old_umask)
 return 'PEM written to {0}'.format(path)

def write_pem ( text , path , overwrite = True , pem_type = None ) : old_umask = os . umask ( 63 ) text = get_pem_entry ( text , pem_type = pem_type ) _dhparams = '' _private_key = '' if ( pem_type and ( pem_type == 'CERTIFICATE' ) and os . path . isfile ( path ) and ( not overwrite ) ) : _filecontents = _text_or_file ( path ) try : _dhparams = get_pem_entry ( _filecontents , 'DH PARAMETERS' ) except salt . exceptions . SaltInvocationError : pass try : _private_key = get_pem_entry ( _filecontents , 'RSA PRIVATE KEY' ) except salt . exceptions . SaltInvocationError : pass with salt . utils . fopen ( path , 'w' ) as _fp : if ( pem_type and ( pem_type == 'CERTIFICATE' ) and _private_key ) : _fp . write ( _private_key ) _fp . write ( text ) if ( pem_type and ( pem_type == 'CERTIFICATE' ) and _dhparams ) : _fp . write ( _dhparams ) os . umask ( old_umask ) return 'PEM written to {0}' . format ( path )
writes out a pem string fixing any formatting or whitespace issues before writing .