traffic_relay: bulk port print to modern py3 style
authorJoe Guo <joeg@catalyst.net.nz>
Fri, 13 Apr 2018 03:00:01 +0000 (15:00 +1200)
committerDouglas Bagnall <dbagnall@samba.org>
Fri, 13 Apr 2018 08:36:32 +0000 (10:36 +0200)
Change print to function and avoid the ugly `print >>sys.stderr`.

Signed-off-by: Joe Guo <joeg@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
Autobuild-User(master): Douglas Bagnall <dbagnall@samba.org>
Autobuild-Date(master): Fri Apr 13 10:36:32 CEST 2018 on sn-devel-144

script/traffic_replay

index 7b0c8db64d6e0caa3493cc06362647ab85b7d488..0e97d0a64afeb20f2a1323922f9f81e9840e6433 100755 (executable)
@@ -16,6 +16,7 @@
 # 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 sys
 import os
 import optparse
@@ -28,6 +29,10 @@ from samba.emulate import traffic
 import samba.getopt as options
 
 
+def print_err(*args, **kwargs):
+    print(*args, file=sys.stderr, **kwargs)
+
+
 def main():
 
     desc = ("Generates network traffic 'conversations' based on <summary-file>"
@@ -126,7 +131,7 @@ def main():
         return
 
     if opts.clean_up:
-        print >>sys.stderr, "Removing user and machine accounts"
+        print_err("Removing user and machine accounts")
         lp    = sambaopts.get_loadparm()
         creds = credopts.get_credentials(lp)
         ldb   = traffic.openLdb(host, creds, lp)
@@ -135,17 +140,17 @@ def main():
 
     if summary:
         if not os.path.exists(summary):
-            print >>sys.stderr, "Summary file %s doesn't exist" % summary
+            print_err("Summary file %s doesn't exist" % summary)
             sys.exit(1)
     # the summary-file can be ommitted for --generate-users-only and
     # --cleanup-up, but it should be specified in all other cases
     elif not opts.generate_users_only:
-        print >>sys.stderr, "No summary-file specified to replay traffic from"
+        print_err("No summary-file specified to replay traffic from")
         sys.exit(1)
 
     if not opts.fixed_password:
-        print >>sys.stderr, ("Please use --fixed-password to specify a password"
-                             " for the users created as part of this test")
+        print_err(("Please use --fixed-password to specify a password"
+                             " for the users created as part of this test"))
         sys.exit(1)
 
     lp = sambaopts.get_loadparm()
@@ -157,50 +162,50 @@ def main():
     else:
         domain = lp.get("workgroup")
         if domain == "WORKGROUP":
-            print >>sys.stderr, ("NETBIOS domain does not appear to be "
-                                 "specified, use the --workgroup option")
+            print_err(("NETBIOS domain does not appear to be "
+                       "specified, use the --workgroup option"))
             sys.exit(1)
 
     if not opts.realm and not lp.get('realm'):
-        print >>sys.stderr, "Realm not specified, use the --realm option"
+        print_err("Realm not specified, use the --realm option")
         sys.exit(1)
 
     if opts.generate_users_only and not (opts.number_of_users or
                                          opts.number_of_groups):
-        print >>sys.stderr, ("Please specify the number of users and/or groups "
-                             "to generate.")
+        print_err(("Please specify the number of users and/or groups "
+                   "to generate."))
         sys.exit(1)
 
     if opts.group_memberships and opts.average_groups_per_user:
-        print >>sys.stderr, ("--group-memberships and --average-groups-per-user"
-                             " are incompatible options - use one or the other")
+        print_err(("--group-memberships and --average-groups-per-user"
+                   " are incompatible options - use one or the other"))
         sys.exit(1)
 
     if not opts.number_of_groups and opts.average_groups_per_user:
-        print >>sys.stderr, ("--average-groups-per-user requires "
-                             "--number-of-groups")
+        print_err(("--average-groups-per-user requires "
+                   "--number-of-groups"))
         sys.exit(1)
 
     if not opts.number_of_groups and opts.group_memberships:
-        print >>sys.stderr, "--group-memberships requires --number-of-groups"
+        print_err("--group-memberships requires --number-of-groups")
         sys.exit(1)
 
     if opts.timing_data not in ('-', None):
         try:
             open(opts.timing_data, 'w').close()
         except IOError as e:
-            print >> sys.stderr, ("the supplied timing data destination "
-                                  "(%s) is not writable" % opts.timing_data)
-            print >> sys.stderr, e
+            print_err(("the supplied timing data destination "
+                       "(%s) is not writable" % opts.timing_data))
+            print_err(e)
             sys.exit()
 
     if opts.traffic_summary not in ('-', None):
         try:
             open(opts.traffic_summary, 'w').close()
         except IOError as e:
-            print >> sys.stderr, ("the supplied traffic summary destination "
-                                  "(%s) is not writable" % opts.traffic_summary)
-            print >> sys.stderr, e
+            print_err(("the supplied traffic summary destination "
+                       "(%s) is not writable" % opts.traffic_summary))
+            print_err(e)
             sys.exit()
 
     traffic.DEBUG_LEVEL = opts.debuglevel
@@ -215,8 +220,8 @@ def main():
             conversations, interval, duration, dns_counts = \
                                             traffic.ingest_summaries([summary])
 
-            print >>sys.stderr, ("Using conversations from the traffic summary "
-                                 "file specified")
+            print_err(("Using conversations from the traffic summary "
+                       "file specified"))
 
             # honour the specified duration if it's different to the
             # capture duration
@@ -232,15 +237,15 @@ def main():
             try:
                 model.load(summary)
             except ValueError:
-                print >>sys.stderr, ("Could not parse %s. The summary file "
-                                     "should be the output from either the "
-                                     "traffic_summary.pl or "
-                                     "traffic_learner scripts."
-                                     % summary)
+                print_err(("Could not parse %s. The summary file "
+                           "should be the output from either the "
+                           "traffic_summary.pl or "
+                           "traffic_learner scripts."
+                           % summary))
                 sys.exit()
 
-            print >>sys.stderr, ("Using the specified model file to "
-                                 "generate conversations")
+            print_err(("Using the specified model file to "
+                       "generate conversations"))
 
             conversations = model.generate_conversations(opts.scale_traffic,
                                                          duration,
@@ -252,14 +257,14 @@ def main():
     if opts.debuglevel > 5:
         for c in conversations:
             for p in c.packets:
-                print "    ", p
+                print("    ", p)
 
-        print '=' * 72
+        print('=' * 72)
 
     if opts.number_of_users and opts.number_of_users < len(conversations):
-        print >>sys.stderr, ("--number-of-users (%d) is less than the "
-                             "number of conversations to replay (%d)"
-                             % (opts.number_of_users, len(conversations)))
+        print_err(("--number-of-users (%d) is less than the "
+                   "number of conversations to replay (%d)"
+                   % (opts.number_of_users, len(conversations))))
         sys.exit(1)
 
     number_of_users = max(opts.number_of_users, len(conversations))
@@ -267,23 +272,23 @@ def main():
 
     if not opts.group_memberships and opts.average_groups_per_user:
         opts.group_memberships = opts.average_groups_per_user * number_of_users
-        print >>sys.stderr, ("Using %d group-memberships based on %u average "
-                             "memberships for %d users"
-                             % (opts.group_memberships,
-                                opts.average_groups_per_user, number_of_users))
+        print_err(("Using %d group-memberships based on %u average "
+                   "memberships for %d users"
+                   % (opts.group_memberships,
+                      opts.average_groups_per_user, number_of_users)))
 
     if opts.group_memberships > max_memberships:
-        print >>sys.stderr, ("The group memberships specified (%d) exceeds "
-                             "the total users (%d) * total groups (%d)"
-                             % (opts.group_memberships, number_of_users,
-                                opts.number_of_groups))
+        print_err(("The group memberships specified (%d) exceeds "
+                   "the total users (%d) * total groups (%d)"
+                   % (opts.group_memberships, number_of_users,
+                      opts.number_of_groups)))
         sys.exit(1)
 
     try:
         ldb = traffic.openLdb(host, creds, lp)
     except:
-        print >>sys.stderr, ("\nInitial LDAP connection failed! Did you supply "
-                             "a DNS host name and the correct credentials?")
+        print_err(("\nInitial LDAP connection failed! Did you supply "
+                   "a DNS host name and the correct credentials?"))
         sys.exit(1)
 
     if opts.generate_users_only:
@@ -296,7 +301,7 @@ def main():
         sys.exit()
 
     tempdir = tempfile.mkdtemp(prefix="samba_tg_")
-    print >>sys.stderr, "Using temp dir %s" % tempdir
+    print_err("Using temp dir %s" % tempdir)
 
     traffic.generate_users_and_groups(ldb,
                                       opts.instance_id,
@@ -318,14 +323,14 @@ def main():
         else:
             summary_dest = open(opts.traffic_summary, 'w')
 
-        print >>sys.stderr, "Writing traffic summary"
+        print_err("Writing traffic summary")
         summaries = []
         for c in conversations:
             summaries += c.replay_as_summary_lines()
 
         summaries.sort()
         for (time, line) in summaries:
-            print >>summary_dest, line
+            print(line, file=summary_dest)
 
         exit(0)
 
@@ -351,11 +356,11 @@ def main():
     else:
         timing_dest = open(opts.timing_data, 'w')
 
-    print >>sys.stderr, "Generating statistics"
+    print_err("Generating statistics")
     traffic.generate_stats(statsdir, timing_dest)
 
     if not opts.preserve_tempdir:
-        print >>sys.stderr, "Removing temporary directory"
+        print_err("Removing temporary directory")
         shutil.rmtree(tempdir)