From 4e5941fa572f701dd98015760e46023afa8d2a1e Mon Sep 17 00:00:00 2001 From: Giuliano Zaro <3684609+GMagician@users.noreply.github.com> Date: Sun, 22 Sep 2019 22:38:07 +0200 Subject: [PATCH] AutoBuild compatibility with Python3 (#15331) --- buildroot/share/atom/auto_build.py | 98 +++++++++++++++--------------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/buildroot/share/atom/auto_build.py b/buildroot/share/atom/auto_build.py index c8a23dcbe..358667661 100644 --- a/buildroot/share/atom/auto_build.py +++ b/buildroot/share/atom/auto_build.py @@ -95,12 +95,7 @@ print('\nWorking\n') python_ver = sys.version_info[0] # major version - 2 or 3 -if python_ver == 2: - print("python version " + str(sys.version_info[0]) + "." + str(sys.version_info[1]) + "." + str(sys.version_info[2])) -else: - print("python version " + str(sys.version_info[0])) - print("This script only runs under python 2") - exit() +print("python version " + str(sys.version_info[0]) + "." + str(sys.version_info[1]) + "." + str(sys.version_info[2])) import platform current_OS = platform.system() @@ -135,9 +130,9 @@ def get_answer(board_name, cpu_label_txt, cpu_a_txt, cpu_b_txt): if python_ver == 2: - import Tkinter as tk + import Tkinter as tk else: - import tkinter as tk + import tkinter as tk def CPU_exit_3(): # forward declare functions @@ -615,9 +610,12 @@ def get_env(board_name, ver_Marlin): # end - get_env # puts screen text into queue so that the parent thread can fetch the data from this thread -import Queue -IO_queue = Queue.Queue() -PIO_queue = Queue.Queue() +if python_ver == 2: + import Queue as queue +else: + import queue as queue +IO_queue = queue.Queue() +#PIO_queue = queue.Queue() not used! def write_to_screen_queue(text, format_tag = 'normal'): double_in = [text, format_tag] IO_queue.put(double_in, block = False) @@ -940,9 +938,13 @@ def run_PIO(dummy): raise SystemExit(0) # kill everything # stream output from subprocess and split it into lines - for line in iter(pio_subprocess.stdout.readline, ''): - line_print(line.replace('\n', '')) - + if python_ver == 2: + for line in iter(pio_subprocess.stdout.readline, ''): + line_print(line.replace('\n', '')) + else: + for line in iter(pio_subprocess.stdout.readline, b''): + line = line.decode('utf-8') + line_print(line.replace('\n', '')) # append info used to run PlatformIO write_to_screen_queue('\nBoard name: ' + board_name + '\n') # put build info at the bottom of the screen @@ -958,21 +960,22 @@ def run_PIO(dummy): import time import threading -import Tkinter as tk -import ttk -import Queue +if python_ver == 2: + import Tkinter as tk + import Queue as queue + import ttk + from Tkinter import Tk, Frame, Text, Scrollbar, Menu + #from tkMessageBox import askokcancel this is not used: removed + import tkFileDialog as fileDialog +else: + import tkinter as tk + import queue as queue + from tkinter import ttk, Tk, Frame, Text, Scrollbar, Menu + from tkinter import filedialog import subprocess import sys -que = Queue.Queue() -#IO_queue = Queue.Queue() - -from Tkinter import Tk, Frame, Text, Scrollbar, Menu -from tkMessageBox import askokcancel - -import tkFileDialog -from tkMessageBox import askokcancel -import tkFileDialog - +que = queue.Queue() +#IO_queue = queue.Queue() class output_window(Text): # based on Super Text @@ -1177,7 +1180,7 @@ class output_window(Text): def _file_save_as(self): - self.filename = tkFileDialog.asksaveasfilename(defaultextension = '.txt') + self.filename = fileDialog.asksaveasfilename(defaultextension = '.txt') f = open(self.filename, 'w') f.write(self.get('1.0', 'end')) f.close() @@ -1267,33 +1270,28 @@ class output_window(Text): def main(): - ########################################################################## - # # - # main program # - # # - ########################################################################## + ########################################################################## + # # + # main program # + # # + ########################################################################## - global build_type - global target_env - global board_name + global build_type + global target_env + global board_name - board_name, Marlin_ver = get_board_name() + board_name, Marlin_ver = get_board_name() - target_env = get_env(board_name, Marlin_ver) + target_env = get_env(board_name, Marlin_ver) - os.environ["BUILD_TYPE"] = build_type # let sub processes know what is happening - os.environ["TARGET_ENV"] = target_env - os.environ["BOARD_NAME"] = board_name - - # Re-use the VSCode terminal, if possible - if os.environ.get('PLATFORMIO_CALLER', '') == 'vscode': - sys_PIO() - else: - auto_build = output_window() - auto_build.start_thread() # executes the "run_PIO" function - - auto_build.root.mainloop() + # Re-use the VSCode terminal, if possible + if os.environ.get('PLATFORMIO_CALLER', '') == 'vscode': + sys_PIO() + else: + auto_build = output_window() + auto_build.start_thread() # executes the "run_PIO" function + auto_build.root.mainloop()