From af725c81e30a99cfc205406cd0a9ba2dd24b9541 Mon Sep 17 00:00:00 2001
From: Alexey Shvetsov <alexxy@gentoo.org>
Date: Wed, 8 May 2019 04:45:01 +0300
Subject: [PATCH] Convert py scripts for py2 and py3 compatibility (#13931)

---
 .../atom/create_custom_upload_command_CDC.py  | 14 ++++---
 .../share/scripts/createSpeedLookupTable.py   | 39 ++++++++++---------
 .../scripts/createTemperatureLookupMarlin.py  | 33 +++++++++-------
 buildroot/share/scripts/g29_auto.py           |  4 +-
 4 files changed, 51 insertions(+), 39 deletions(-)

diff --git a/buildroot/share/atom/create_custom_upload_command_CDC.py b/buildroot/share/atom/create_custom_upload_command_CDC.py
index 2fea2d5b536..46df3872992 100644
--- a/buildroot/share/atom/create_custom_upload_command_CDC.py
+++ b/buildroot/share/atom/create_custom_upload_command_CDC.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python
 #
 #  Builds custom upload command
 #    1) Run platformio as a subprocess to find a COM port
@@ -9,6 +10,9 @@
 #  Will continue on if a COM port isn't found so that the compilation can be done.
 #
 
+from __future__ import print_function
+from __future__ import division
+
 import subprocess
 import os
 import sys
@@ -45,7 +49,7 @@ else:
       global description_CDC
 
 
-      print '\nLooking for Serial Port\n'
+      print('\nLooking for Serial Port\n')
 
     # stream output from subprocess and split it into lines
       pio_subprocess = subprocess.Popen(['platformio', 'device', 'list'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
@@ -78,10 +82,10 @@ else:
         com_CDC = com_CDC.replace('\r', '')
 
       if com_CDC == 'COM_PORT_NOT_FOUND':
-          print com_CDC, '\n'
+          print(com_CDC, '\n')
       else:
-          print 'FOUND: ' ,com_CDC
-          print 'DESCRIPTION: ',  description_CDC , '\n'
+          print('FOUND: ' ,com_CDC)
+          print('DESCRIPTION: ',  description_CDC , '\n')
 
   if current_OS == 'Windows':
 
@@ -114,7 +118,7 @@ else:
 
 #      upload_string = 'avrdude -p usb1286 -c avr109 -P ' + com_CDC + ' -U flash:w:' + source_path + ':i'
       upload_string = avrdude_exe_path + ' -p usb1286 -c avr109 -P ' + com_CDC + ' -C ' + avrdude_conf_path  + ' -U flash:w:' + source_path + ':i'
-      print 'upload_string: ', upload_string
+      print('upload_string: ', upload_string)
 
 
 
diff --git a/buildroot/share/scripts/createSpeedLookupTable.py b/buildroot/share/scripts/createSpeedLookupTable.py
index ade28ee3339..55f78d0e06f 100755
--- a/buildroot/share/scripts/createSpeedLookupTable.py
+++ b/buildroot/share/scripts/createSpeedLookupTable.py
@@ -1,5 +1,8 @@
 #!/usr/bin/env python
 
+from __future__ import print_function
+from __future__ import division
+
 """ Generate the stepper delay lookup table for Marlin firmware. """
 
 import argparse
@@ -16,35 +19,35 @@ args = parser.parse_args()
 cpu_freq = args.cpu_freq * 1000000
 timer_freq = cpu_freq / args.divider
 
-print "#ifndef SPEED_LOOKUPTABLE_H"
-print "#define SPEED_LOOKUPTABLE_H"
-print
-print '#include "Marlin.h"'
-print
+print("#ifndef SPEED_LOOKUPTABLE_H")
+print("#define SPEED_LOOKUPTABLE_H")
+print()
+print('#include "Marlin.h"')
+print()
 
-print "const uint16_t speed_lookuptable_fast[256][2] PROGMEM = {"
+print("const uint16_t speed_lookuptable_fast[256][2] PROGMEM = {")
 a = [ timer_freq / ((i*256)+(args.cpu_freq*2)) for i in range(256) ]
 b = [ a[i] - a[i+1] for i in range(255) ]
 b.append(b[-1])
 for i in range(32):
-    print "  ",
+    print("  ", end=' ')
     for j in range(8):
-        print "{%d, %d}," % (a[8*i+j], b[8*i+j]),
-    print
-print "};"
-print
+        print("{%d, %d}," % (a[8*i+j], b[8*i+j]), end=' ')
+    print()
+print("};")
+print()
 
-print "const uint16_t speed_lookuptable_slow[256][2] PROGMEM = {"
+print("const uint16_t speed_lookuptable_slow[256][2] PROGMEM = {")
 a = [ timer_freq / ((i*8)+(args.cpu_freq*2)) for i in range(256) ]
 b = [ a[i] - a[i+1] for i in range(255) ]
 b.append(b[-1])
 for i in range(32):
-    print "  ",
+    print("  ", end=' ')
     for j in range(8):
-        print "{%d, %d}," % (a[8*i+j], b[8*i+j]),
-    print
-print "};"
-print
+        print("{%d, %d}," % (a[8*i+j], b[8*i+j]), end=' ')
+    print()
+print("};")
+print()
 
-print "#endif"
+print("#endif")
 
diff --git a/buildroot/share/scripts/createTemperatureLookupMarlin.py b/buildroot/share/scripts/createTemperatureLookupMarlin.py
index 83147c502bc..15fe217d382 100755
--- a/buildroot/share/scripts/createTemperatureLookupMarlin.py
+++ b/buildroot/share/scripts/createTemperatureLookupMarlin.py
@@ -18,6 +18,9 @@ Options:
   --num-temps=...   the number of temperature points to calculate (default: 36)
 """
 
+from __future__ import print_function
+from __future__ import division
+
 from math import *
 import sys
 import getopt
@@ -47,9 +50,9 @@ class Thermistor:
         a = y1 - (b + l1**2 *c)*l1
 
         if c < 0:
-            print "//////////////////////////////////////////////////////////////////////////////////////"
-            print "// WARNING: negative coefficient 'c'! Something may be wrong with the measurements! //"
-            print "//////////////////////////////////////////////////////////////////////////////////////"
+            print("//////////////////////////////////////////////////////////////////////////////////////")
+            print("// WARNING: negative coefficient 'c'! Something may be wrong with the measurements! //")
+            print("//////////////////////////////////////////////////////////////////////////////////////")
             c = -c
         self.c1 = a                         # Steinhart-Hart coefficients
         self.c2 = b
@@ -97,7 +100,7 @@ def main(argv):
     try:
         opts, args = getopt.getopt(argv, "h", ["help", "rp=", "t1=", "t2=", "t3=", "num-temps="])
     except getopt.GetoptError as err:
-        print  str(err)
+        print(str(err))
         usage()
         sys.exit(2)
 
@@ -129,27 +132,27 @@ def main(argv):
     up_bound = t.temp(1);
     min_temp = int(TMIN if TMIN > low_bound else low_bound)
     max_temp = int(TMAX if TMAX < up_bound else up_bound)
-    temps = range(max_temp, TMIN+step, step);
+    temps = list(range(max_temp, TMIN+step, step));
 
-    print "// Thermistor lookup table for Marlin"
-    print "// ./createTemperatureLookupMarlin.py --rp=%s --t1=%s:%s --t2=%s:%s --t3=%s:%s --num-temps=%s" % (rp, t1, r1, t2, r2, t3, r3, num_temps)
-    print "// Steinhart-Hart Coefficients: a=%.15g, b=%.15g, c=%.15g " % (t.c1, t.c2, t.c3)
-    print "// Theoretical limits of thermistor: %.2f to %.2f degC" % (low_bound, up_bound)
-    print
-    print "const short temptable[][2] PROGMEM = {"
+    print("// Thermistor lookup table for Marlin")
+    print("// ./createTemperatureLookupMarlin.py --rp=%s --t1=%s:%s --t2=%s:%s --t3=%s:%s --num-temps=%s" % (rp, t1, r1, t2, r2, t3, r3, num_temps))
+    print("// Steinhart-Hart Coefficients: a=%.15g, b=%.15g, c=%.15g " % (t.c1, t.c2, t.c3))
+    print("// Theoretical limits of thermistor: %.2f to %.2f degC" % (low_bound, up_bound))
+    print()
+    print("const short temptable[][2] PROGMEM = {")
 
     for temp in temps:
         adc = t.adc(temp)
-        print "    { OV(%7.2f), %4s }%s // v=%.3f\tr=%.3f\tres=%.3f degC/count" % (adc , temp, \
+        print("    { OV(%7.2f), %4s }%s // v=%.3f\tr=%.3f\tres=%.3f degC/count" % (adc , temp, \
                         ',' if temp != temps[-1] else ' ', \
                         t.voltage(adc), \
                         t.resist( adc), \
                         t.resol(  adc) \
-                    )
-    print "};"
+                    ))
+    print("};")
 
 def usage():
-    print __doc__
+    print(__doc__)
 
 if __name__ == "__main__":
     main(sys.argv[1:])
diff --git a/buildroot/share/scripts/g29_auto.py b/buildroot/share/scripts/g29_auto.py
index 884e62b2a2b..608fa0c0f76 100755
--- a/buildroot/share/scripts/g29_auto.py
+++ b/buildroot/share/scripts/g29_auto.py
@@ -1,10 +1,12 @@
-#!/usr/bin/python3
+#!/usr/bin/python
 
 # This file is for preprocessing gcode and the new G29 Autobedleveling from Marlin
 # It will analyse the first 2 Layer and return the maximum size for this part
 # After this it will replace with g29_keyword = ';MarlinG29Script' with the new G29 LRFB
 # the new file will be created in the same folder.
 
+from __future__ import print_function
+
 # your gcode-file/folder
 folder = './'
 my_file = 'test.gcode'