1
0
mirror of https://github.com/samba-team/samba.git synced 2025-12-09 00:23:50 +03:00

samba python libs: convert print func to be py2/py3 compatible

Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
This commit is contained in:
Noel Power
2018-03-09 13:53:45 +00:00
committed by Andrew Bartlett
parent aacbe1b65c
commit 9c251740b4
11 changed files with 126 additions and 99 deletions

View File

@@ -16,6 +16,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import print_function
import time
import os
import random
@@ -96,20 +98,21 @@ def debug(level, msg, *args):
"""
if level <= DEBUG_LEVEL:
if not args:
print >> sys.stderr, msg
print(msg, file=sys.stderr)
else:
print >> sys.stderr, msg % tuple(args)
print(msg % tuple(args), file=sys.stderr)
def debug_lineno(*args):
""" Print an unformatted log message to stderr, contaning the line number
"""
tb = traceback.extract_stack(limit=2)
print >> sys.stderr, (" %s:" "\033[01;33m"
"%s " "\033[00m" % (tb[0][2], tb[0][1])),
print((" %s:" "\033[01;33m"
"%s " "\033[00m" % (tb[0][2], tb[0][1])), end=' ',
file=sys.stderr)
for a in args:
print >> sys.stderr, a
print >> sys.stderr
print(a, file=sys.stderr)
print(file=sys.stderr)
sys.stderr.flush()
@@ -120,7 +123,7 @@ def random_colour_print():
def p(*args):
for a in args:
print >>sys.stderr, "%s%s\033[00m" % (prefix, a)
print("%s%s\033[00m" % (prefix, a), file=sys.stderr)
return p
@@ -225,8 +228,9 @@ class Packet(object):
fn = getattr(traffic_packets, fn_name)
except AttributeError as e:
print >>sys.stderr, "Conversation(%s) Missing handler %s" % \
(conversation.conversation_id, fn_name)
print("Conversation(%s) Missing handler %s" % \
(conversation.conversation_id, fn_name),
file=sys.stderr)
return
# Don't display a message for kerberos packets, they're not directly
@@ -273,7 +277,7 @@ class Packet(object):
if fn is traffic_packets.null_packet:
return False
except AttributeError:
print >>sys.stderr, "missing packet %s" % fn_name
print("missing packet %s" % fn_name, file=sys.stderr)
return False
return True
@@ -370,7 +374,8 @@ class ReplayContext(object):
for i in range(5):
p += ',DC'
if p != k and p in dn_map:
print >> sys.stderr, 'dn_map collison %s %s' % (k, p)
print('dn_map collison %s %s' % (k, p),
file=sys.stderr)
continue
dn_map[p] = dn_map[k]
@@ -841,11 +846,12 @@ class Conversation(object):
# the number of concurrent threads, which allows us to make
# larger loads.
if gap > 0.15 and False:
print >>sys.stderr, "sleeping for %f in main process" % (gap - 0.1)
print("sleeping for %f in main process" % (gap - 0.1),
file=sys.stderr)
time.sleep(gap - 0.1)
now = time.time() - start
gap = t - now
print >>sys.stderr, "gap is now %f" % gap
print("gap is now %f" % gap, file=sys.stderr)
self.conversation_id = context.next_conversation_id()
pid = os.fork()
@@ -874,8 +880,8 @@ class Conversation(object):
self.msg("starting %s [miss %.3f pid %d]" % (self, miss, pid))
self.replay(context)
except Exception:
print >>sys.stderr,\
("EXCEPTION in child PID %d, conversation %s" % (pid, self))
print(("EXCEPTION in child PID %d, conversation %s" % (pid, self)),
file=sys.stderr)
traceback.print_exc(sys.stderr)
finally:
sys.stderr.close()
@@ -1004,7 +1010,7 @@ def ingest_summaries(files, dns_mode='count'):
for f in files:
if isinstance(f, str):
f = open(f)
print >>sys.stderr, "Ingesting %s" % (f.name,)
print("Ingesting %s" % (f.name,), file=sys.stderr)
for line in f:
p = Packet(line)
if p.protocol == 'dns' and dns_mode != 'include':
@@ -1020,7 +1026,7 @@ def ingest_summaries(files, dns_mode='count'):
start_time = min(p.timestamp for p in packets)
last_packet = max(p.timestamp for p in packets)
print >>sys.stderr, "gathering packets into conversations"
print("gathering packets into conversations", file=sys.stderr)
conversations = OrderedDict()
for p in packets:
p.timestamp -= start_time
@@ -1241,8 +1247,8 @@ class TrafficModel(object):
conversations.append(c)
client += 1
print >> sys.stderr, ("we have %d conversations at rate %f" %
(len(conversations), rate))
print(("we have %d conversations at rate %f" %
(len(conversations), rate)), file=sys.stderr)
conversations.sort()
return conversations
@@ -1389,8 +1395,8 @@ def replay(conversations,
**kwargs)
if len(accounts) < len(conversations):
print >> sys.stderr, ("we have %d accounts but %d conversations" %
(accounts, conversations))
print(("we have %d accounts but %d conversations" %
(accounts, conversations)), file=sys.stderr)
cstack = zip(sorted(conversations,
key=lambda x: x.start_time, reverse=True),
@@ -1407,7 +1413,8 @@ def replay(conversations,
# to start. Conversations other than the last could still be
# going, but we don't care.
duration = cstack[0][0].packets[-1].timestamp + 1.0
print >>sys.stderr, "We will stop after %.1f seconds" % duration
print("We will stop after %.1f seconds" % duration,
file=sys.stderr)
end = start + duration
@@ -1439,12 +1446,14 @@ def replay(conversations,
elapsed = t - st
fork_time += elapsed
fork_n += 1
print >>sys.stderr, "forked %s in pid %s (in %fs)" % (c, pid,
elapsed)
print("forked %s in pid %s (in %fs)" % (c, pid,
elapsed),
file=sys.stderr)
if fork_n:
print >>sys.stderr, ("forked %d times in %f seconds (avg %f)" %
(fork_n, fork_time, fork_time / fork_n))
print(("forked %d times in %f seconds (avg %f)" %
(fork_n, fork_time, fork_time / fork_n)),
file=sys.stderr)
elif cstack:
debug(2, "no forks in batch ending %f" % batch_end)
@@ -1458,21 +1467,21 @@ def replay(conversations,
break
if pid:
c = children.pop(pid, None)
print >>sys.stderr, ("process %d finished conversation %s;"
" %d to go" %
(pid, c, len(children)))
print(("process %d finished conversation %s;"
" %d to go" %
(pid, c, len(children))), file=sys.stderr)
if time.time() >= end:
print >>sys.stderr, "time to stop"
print("time to stop", file=sys.stderr)
break
except Exception:
print >>sys.stderr, "EXCEPTION in parent"
print("EXCEPTION in parent", file=sys.stderr)
traceback.print_exc()
finally:
for s in (15, 15, 9):
print >>sys.stderr, ("killing %d children with -%d" %
(len(children), s))
print(("killing %d children with -%d" %
(len(children), s)), file=sys.stderr)
for pid in children:
try:
os.kill(pid, s)
@@ -1489,9 +1498,10 @@ def replay(conversations,
raise
if pid != 0:
c = children.pop(pid, None)
print >>sys.stderr, ("kill -%d %d KILLED conversation %s; "
"%d to go" %
(s, pid, c, len(children)))
print(("kill -%d %d KILLED conversation %s; "
"%d to go" %
(s, pid, c, len(children))),
file=sys.stderr)
if time.time() >= end:
break
@@ -1500,7 +1510,8 @@ def replay(conversations,
time.sleep(1)
if children:
print >>sys.stderr, "%d children are missing" % len(children)
print("%d children are missing" % len(children),
file=sys.stderr)
# there may be stragglers that were forked just as ^C was hit
# and don't appear in the list of children. We can get them
@@ -1510,7 +1521,7 @@ def replay(conversations,
try:
os.killpg(0, 2)
except KeyboardInterrupt:
print >>sys.stderr, "ignoring fake ^C"
print("ignoring fake ^C", file=sys.stderr)
def openLdb(host, creds, lp):
@@ -1586,8 +1597,9 @@ def generate_traffic_accounts(ldb, instance_id, number, password):
finds an already existing account or it has generated all the required
accounts.
"""
print >>sys.stderr, ("Generating machine and conversation accounts, "
"as required for %d conversations" % number)
print(("Generating machine and conversation accounts, "
"as required for %d conversations" % number),
file=sys.stderr)
added = 0
for i in range(number, 0, -1):
try:
@@ -1601,7 +1613,8 @@ def generate_traffic_accounts(ldb, instance_id, number, password):
else:
raise
if added > 0:
print >>sys.stderr, "Added %d new machine accounts" % added
print("Added %d new machine accounts" % added,
file=sys.stderr)
added = 0
for i in range(number, 0, -1):
@@ -1617,7 +1630,8 @@ def generate_traffic_accounts(ldb, instance_id, number, password):
raise
if added > 0:
print >>sys.stderr, "Added %d new user accounts" % added
print("Added %d new user accounts" % added,
file=sys.stderr)
def create_machine_account(ldb, instance_id, netbios_name, machinepass):
@@ -1745,29 +1759,31 @@ def generate_users_and_groups(ldb, instance_id, password,
create_ou(ldb, instance_id)
print >>sys.stderr, "Generating dummy user accounts"
print("Generating dummy user accounts", file=sys.stderr)
users_added = generate_users(ldb, instance_id, number_of_users, password)
if number_of_groups > 0:
print >>sys.stderr, "Generating dummy groups"
print("Generating dummy groups", file=sys.stderr)
groups_added = generate_groups(ldb, instance_id, number_of_groups)
if group_memberships > 0:
print >>sys.stderr, "Assigning users to groups"
print("Assigning users to groups", file=sys.stderr)
assignments = assign_groups(number_of_groups,
groups_added,
number_of_users,
users_added,
group_memberships)
print >>sys.stderr, "Adding users to groups"
print("Adding users to groups", file=sys.stderr)
add_users_to_groups(ldb, instance_id, assignments)
if (groups_added > 0 and users_added == 0 and
number_of_groups != groups_added):
print >>sys.stderr, "Warning: the added groups will contain no members"
print("Warning: the added groups will contain no members",
file=sys.stderr)
print >>sys.stderr, ("Added %d users, %d groups and %d group memberships" %
(users_added, groups_added, len(assignments)))
print(("Added %d users, %d groups and %d group memberships" %
(users_added, groups_added, len(assignments))),
file=sys.stderr)
def assign_groups(number_of_groups,
@@ -1911,7 +1927,7 @@ def generate_stats(statsdir, timing_file):
tw(line)
except (ValueError, IndexError):
# not a valid line print and ignore
print >>sys.stderr, line
print(line, file=sys.stderr)
pass
duration = last - first
if successful == 0: