2011-06-19 23:09:59 +04:00
# Samba common functions
#
# Copyright (C) Matthieu Patou <mat@matws.net>
2020-09-11 23:29:46 +03:00
# Copyright (C) Lumir Balhar <lbalhar@redhat.com> 2017
2011-06-19 23:09:59 +04:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2018-03-26 07:07:33 +03:00
2020-09-11 23:29:46 +03:00
def cmp ( x , y ) :
"""
Replacement for built - in function cmp that was removed in Python 3
2018-03-26 07:07:33 +03:00
2020-09-11 23:29:46 +03:00
Compare the two objects x and y and return an integer according to
the outcome . The return value is negative if x < y , zero if x == y
and strictly positive if x > y .
"""
2018-03-26 07:07:33 +03:00
2020-09-11 23:29:46 +03:00
return ( x > y ) - ( x < y )
2018-10-11 02:52:09 +03:00
2011-10-20 06:24:45 +04:00
2011-11-02 19:22:16 +04:00
def confirm ( msg , forced = False , allow_all = False ) :
2011-06-19 23:09:59 +04:00
""" confirm an action with the user
2011-11-02 19:22:16 +04:00
: param msg : A string to print to the user
: param forced : Are the answer forced
2011-06-19 23:09:59 +04:00
"""
if forced :
print ( " %s [YES] " % msg )
return True
2011-07-05 07:02:48 +04:00
mapping = {
' Y ' : True ,
' YES ' : True ,
' ' : False ,
' N ' : False ,
' NO ' : False ,
2018-07-30 09:14:37 +03:00
}
2011-07-05 07:02:48 +04:00
prompt = ' [y/N] '
2011-07-05 06:38:31 +04:00
if allow_all :
2011-07-05 07:02:48 +04:00
mapping [ ' ALL ' ] = ' ALL '
mapping [ ' NONE ' ] = ' NONE '
prompt = ' [y/N/all/none] '
while True :
2020-09-11 23:29:46 +03:00
v = input ( msg + ' %s ' % prompt )
2011-07-05 07:02:48 +04:00
v = v . upper ( )
if v in mapping :
return mapping [ v ]
print ( " Unknown response ' %s ' " % v )
2011-06-19 23:09:59 +04:00
2011-08-05 05:17:06 +04:00
def normalise_int32 ( ivalue ) :
2023-12-14 05:14:27 +03:00
""" normalise a ldap integer to signed 32 bit """
2011-08-05 08:02:39 +04:00
if int ( ivalue ) & 0x80000000 and int ( ivalue ) > 0 :
2011-08-05 05:17:06 +04:00
return str ( int ( ivalue ) - 0x100000000 )
return str ( ivalue )
2011-10-20 06:24:45 +04:00
2024-05-28 10:46:33 +03:00
# Sometimes we have variables whose content can be 'bytes' or
2020-09-11 23:29:46 +03:00
# 'str' and we can't be sure which. Generally this is because the
# code variable can be initialised (or reassigned) a value from different
2024-05-28 10:46:33 +03:00
# api(s) or functions depending on complex conditions or logic.
2020-09-11 23:29:46 +03:00
# If a 'str' object is passed in it is encoded using 'utf8' or if 'bytes'
# is passed in it is returned unchanged.
def get_bytes ( bytesorstring ) :
tmp = bytesorstring
if isinstance ( bytesorstring , str ) :
tmp = bytesorstring . encode ( ' utf8 ' )
elif not isinstance ( bytesorstring , bytes ) :
2023-02-28 06:56:24 +03:00
raise ValueError ( ' Expected bytes or string for %s : %s ' % ( type ( bytesorstring ) , bytesorstring ) )
2020-09-11 23:29:46 +03:00
return tmp
# helper function to get a string from a variable that maybe 'str' or
# 'bytes' if 'bytes' then it is decoded using 'utf8'. If 'str' is passed
# it is returned unchanged
def get_string ( bytesorstring ) :
tmp = bytesorstring
if isinstance ( bytesorstring , bytes ) :
tmp = bytesorstring . decode ( ' utf8 ' )
elif not isinstance ( bytesorstring , str ) :
2023-02-28 06:56:24 +03:00
raise ValueError ( ' Expected bytes or string for %s : %s ' % ( type ( bytesorstring ) , bytesorstring ) )
2020-09-11 23:29:46 +03:00
return tmp