My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auto_build.py 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. #!/usr/bin/env python
  2. #######################################
  3. #
  4. # Marlin 3D Printer Firmware
  5. # Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  6. #
  7. # Based on Sprinter and grbl.
  8. # Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. #######################################
  24. #######################################
  25. #
  26. # Revision: 2.0.1
  27. #
  28. # Description: script to automate PlatformIO builds
  29. # CLI: python auto_build.py build_option
  30. # build_option (required)
  31. # build executes -> platformio run -e target_env
  32. # clean executes -> platformio run --target clean -e target_env
  33. # upload executes -> platformio run --target upload -e target_env
  34. # traceback executes -> platformio run --target upload -e target_env
  35. # program executes -> platformio run --target program -e target_env
  36. # test executes -> platformio test upload -e target_env
  37. # remote executes -> platformio remote run --target upload -e target_env
  38. # debug executes -> platformio debug -e target_env
  39. #
  40. # 'traceback' just uses the debug variant of the target environment if one exists
  41. #
  42. #######################################
  43. #######################################
  44. #
  45. # General program flow
  46. #
  47. # 1. Scans Configuration.h for the motherboard name and Marlin version.
  48. # 2. Scans pins.h for the motherboard.
  49. # returns the CPU(s) and platformio environment(s) used by the motherboard
  50. # 3. If further info is needed then a popup gets it from the user.
  51. # 4. The OUTPUT_WINDOW class creates a window to display the output of the PlatformIO program.
  52. # 5. A thread is created by the OUTPUT_WINDOW class in order to execute the RUN_PIO function.
  53. # 6. The RUN_PIO function uses a subprocess to run the CLI version of PlatformIO.
  54. # 7. The "iter(pio_subprocess.stdout.readline, '')" function is used to stream the output of
  55. # PlatformIO back to the RUN_PIO function.
  56. # 8. Each line returned from PlatformIO is formatted to match the color coding seen in the
  57. # PlatformIO GUI.
  58. # 9. If there is a color change within a line then the line is broken at each color change
  59. # and sent separately.
  60. # 10. Each formatted segment (could be a full line or a split line) is put into the queue
  61. # IO_queue as it arrives from the platformio subprocess.
  62. # 11. The OUTPUT_WINDOW class periodically samples IO_queue. If data is available then it
  63. # is written to the window.
  64. # 12. The window stays open until the user closes it.
  65. # 13. The OUTPUT_WINDOW class continues to execute as long as the window is open. This allows
  66. # copying, saving, scrolling of the window. A right click popup is available.
  67. #
  68. #######################################
  69. from __future__ import print_function
  70. from __future__ import division
  71. import sys
  72. import os
  73. pwd = os.getcwd() # make sure we're executing from the correct directory level
  74. pwd = pwd.replace('\\', '/')
  75. if 0 <= pwd.find('buildroot/share/atom'):
  76. pwd = pwd[ : pwd.find('buildroot/share/atom')]
  77. os.chdir(pwd)
  78. print('pwd: ', pwd)
  79. num_args = len(sys.argv)
  80. if num_args > 1:
  81. build_type = str(sys.argv[1])
  82. else:
  83. print('Please specify build type')
  84. exit()
  85. print('build_type: ', build_type)
  86. print('\nWorking\n')
  87. python_ver = sys.version_info[0] # major version - 2 or 3
  88. if python_ver == 2:
  89. print("python version " + str(sys.version_info[0]) + "." + str(sys.version_info[1]) + "." + str(sys.version_info[2]))
  90. else:
  91. print("python version " + str(sys.version_info[0]))
  92. print("This script only runs under python 2")
  93. exit()
  94. import platform
  95. current_OS = platform.system()
  96. #globals
  97. target_env = ''
  98. board_name = ''
  99. from datetime import datetime, date, time
  100. #########
  101. # Python 2 error messages:
  102. # Can't find a usable init.tcl in the following directories ...
  103. # error "invalid command name "tcl_findLibrary""
  104. #
  105. # Fix for the above errors on my Win10 system:
  106. # search all init.tcl files for the line "package require -exact Tcl" that has the highest 8.5.x number
  107. # copy it into the first directory listed in the error messages
  108. # set the environmental variables TCLLIBPATH and TCL_LIBRARY to the directory where you found the init.tcl file
  109. # reboot
  110. #########
  111. ##########################################################################################
  112. #
  113. # popup to get input from user
  114. #
  115. ##########################################################################################
  116. def get_answer(board_name, cpu_label_txt, cpu_a_txt, cpu_b_txt):
  117. if python_ver == 2:
  118. import Tkinter as tk
  119. else:
  120. import tkinter as tk
  121. def CPU_exit_3(): # forward declare functions
  122. CPU_exit_3_()
  123. def CPU_exit_4():
  124. CPU_exit_4_()
  125. def kill_session():
  126. kill_session_()
  127. root_get_answer = tk.Tk()
  128. root_get_answer.chk_state_1 = 1 # declare variables used by TK and enable
  129. chk_state_1 = 0 # set initial state of check boxes
  130. global get_answer_val
  131. get_answer_val = 2 # return get_answer_val, set default to match chk_state_1 default
  132. l1 = tk.Label(text=board_name,
  133. fg = "light green",
  134. bg = "dark green",
  135. font = "Helvetica 12 bold").grid(row=1)
  136. l2 = tk.Label(text=cpu_label_txt,
  137. fg = "light green",
  138. bg = "dark green",
  139. font = "Helvetica 16 bold italic").grid(row=2)
  140. b4 = tk.Checkbutton(text=cpu_a_txt,
  141. fg = "black",
  142. font = "Times 20 bold ",
  143. variable=chk_state_1, onvalue=1, offvalue=0,
  144. command = CPU_exit_3).grid(row=3)
  145. b5 = tk.Checkbutton(text=cpu_b_txt,
  146. fg = "black",
  147. font = "Times 20 bold ",
  148. variable=chk_state_1, onvalue=0, offvalue=1,
  149. command = CPU_exit_4).grid(row=4) # use same variable but inverted so they will track
  150. b6 = tk.Button(text="CONFIRM",
  151. fg = "blue",
  152. font = "Times 20 bold ",
  153. command = root_get_answer.destroy).grid(row=5, pady=4)
  154. b7 = tk.Button(text="CANCEL",
  155. fg = "red",
  156. font = "Times 12 bold ",
  157. command = kill_session).grid(row=6, pady=4)
  158. def CPU_exit_3_():
  159. global get_answer_val
  160. get_answer_val = 1
  161. def CPU_exit_4_():
  162. global get_answer_val
  163. get_answer_val = 2
  164. def kill_session_():
  165. raise SystemExit(0) # kill everything
  166. root_get_answer.mainloop()
  167. # end - get answer
  168. #
  169. # move custom board definitions from project folder to PlatformIO
  170. #
  171. def resolve_path(path):
  172. import os
  173. # turn the selection into a partial path
  174. if 0 <= path.find('"'):
  175. path = path[ path.find('"') : ]
  176. if 0 <= path.find(', line '):
  177. path = path.replace(', line ', ':')
  178. path = path.replace('"', '')
  179. #get line and column numbers
  180. line_num = 1
  181. column_num = 1
  182. line_start = path.find(':', 2) # use 2 here so don't eat Windows full path
  183. column_start = path.find(':', line_start + 1)
  184. if column_start == -1:
  185. column_start = len(path)
  186. column_end = path.find(':', column_start + 1)
  187. if column_end == -1:
  188. column_end = len(path)
  189. if 0 <= line_start:
  190. line_num = path[ line_start + 1 : column_start]
  191. if line_num == '':
  192. line_num = 1
  193. if column_start != column_end:
  194. column_num = path[ column_start + 1 : column_end]
  195. if column_num == '':
  196. column_num = 0
  197. index_end = path.find(',')
  198. if 0 <= index_end:
  199. path = path[ : index_end] # delete comma and anything after
  200. index_end = path.find(':', 2)
  201. if 0 <= index_end:
  202. path = path[ : path.find(':', 2)] # delete the line number and anything after
  203. path = path.replace('\\','/')
  204. if 1 == path.find(':') and current_OS == 'Windows':
  205. return path, line_num, column_num # found a full path - no need for further processing
  206. elif 0 == path.find('/') and (current_OS == 'Linux' or current_OS == 'Darwin'):
  207. return path, line_num, column_num # found a full path - no need for further processing
  208. else:
  209. # resolve as many '../' as we can
  210. while 0 <= path.find('../'):
  211. end = path.find('../') - 1
  212. start = path.find('/')
  213. while 0 <= path.find('/',start) and end > path.find('/',start):
  214. start = path.find('/',start) + 1
  215. path = path[0:start] + path[end + 4: ]
  216. # this is an alternative to the above - it just deletes the '../' section
  217. # start_temp = path.find('../')
  218. # while 0 <= path.find('../',start_temp):
  219. # start = path.find('../',start_temp)
  220. # start_temp = start + 1
  221. # if 0 <= start:
  222. # path = path[start + 2 : ]
  223. start = path.find('/')
  224. if start != 0: # make sure path starts with '/'
  225. while 0 == path.find(' '): # eat any spaces at the beginning
  226. path = path[ 1 : ]
  227. path = '/' + path
  228. if current_OS == 'Windows':
  229. search_path = path.replace('/', '\\') # os.walk uses '\' in Windows
  230. else:
  231. search_path = path
  232. start_path = os.path.abspath('')
  233. # search project directory for the selection
  234. found = False
  235. full_path = ''
  236. for root, directories, filenames in os.walk(start_path):
  237. for filename in filenames:
  238. if 0 <= root.find('.git'): # don't bother looking in this directory
  239. break
  240. full_path = os.path.join(root,filename)
  241. if 0 <= full_path.find(search_path):
  242. found = True
  243. break
  244. if found:
  245. break
  246. return full_path, line_num, column_num
  247. # end - resolve_path
  248. #
  249. # Opens the file in the preferred editor at the line & column number
  250. # If the preferred editor isn't already running then it tries the next.
  251. # If none are open then the system default is used.
  252. #
  253. # Editor order:
  254. # 1. Notepad++ (Windows only)
  255. # 2. Sublime Text
  256. # 3. Atom
  257. # 4. System default (opens at line 1, column 1 only)
  258. #
  259. def open_file(path):
  260. import subprocess
  261. file_path, line_num, column_num = resolve_path(path)
  262. if file_path == '' :
  263. return
  264. if current_OS == 'Windows':
  265. editor_note = subprocess.check_output('wmic process where "name=' + "'notepad++.exe'" + '" get ExecutablePath')
  266. editor_sublime = subprocess.check_output('wmic process where "name=' + "'sublime_text.exe'" + '" get ExecutablePath')
  267. editor_atom = subprocess.check_output('wmic process where "name=' + "'atom.exe'" + '" get ExecutablePath')
  268. if 0 <= editor_note.find('notepad++.exe'):
  269. start = editor_note.find('\n') + 1
  270. end = editor_note.find('\n',start + 5) -4
  271. editor_note = editor_note[ start : end]
  272. command = file_path , ' -n' + str(line_num) , ' -c' + str(column_num)
  273. subprocess.Popen([editor_note, command])
  274. elif 0 <= editor_sublime.find('sublime_text.exe'):
  275. start = editor_sublime.find('\n') + 1
  276. end = editor_sublime.find('\n',start + 5) -4
  277. editor_sublime = editor_sublime[ start : end]
  278. command = file_path + ':' + line_num + ':' + column_num
  279. subprocess.Popen([editor_sublime, command])
  280. elif 0 <= editor_atom.find('atom.exe'):
  281. start = editor_atom.find('\n') + 1
  282. end = editor_atom.find('\n',start + 5) -4
  283. editor_atom = editor_atom[ start : end]
  284. command = file_path + ':' + str(line_num) + ':' + str(column_num)
  285. subprocess.Popen([editor_atom, command])
  286. else:
  287. os.startfile(resolve_path(path)) # open file with default app
  288. elif current_OS == 'Linux':
  289. command = file_path + ':' + str(line_num) + ':' + str(column_num)
  290. index_end = command.find(',')
  291. if 0 <= index_end:
  292. command = command[ : index_end] # sometimes a comma magically appears, don't want it
  293. running_apps = subprocess.Popen('ps ax -o cmd', stdout=subprocess.PIPE, shell=True)
  294. (output, err) = running_apps.communicate()
  295. temp = output.split('\n')
  296. def find_editor_linux(name, search_obj):
  297. for line in search_obj:
  298. if 0 <= line.find(name):
  299. path = line
  300. return True, path
  301. return False , ''
  302. (success_sublime, editor_path_sublime) = find_editor_linux('sublime_text',temp)
  303. (success_atom, editor_path_atom) = find_editor_linux('atom',temp)
  304. if success_sublime:
  305. subprocess.Popen([editor_path_sublime, command])
  306. elif success_atom:
  307. subprocess.Popen([editor_path_atom, command])
  308. else:
  309. os.system('xdg-open ' + file_path )
  310. elif current_OS == 'Darwin': # MAC
  311. command = file_path + ':' + str(line_num) + ':' + str(column_num)
  312. index_end = command.find(',')
  313. if 0 <= index_end:
  314. command = command[ : index_end] # sometimes a comma magically appears, don't want it
  315. running_apps = subprocess.Popen('ps axwww -o command', stdout=subprocess.PIPE, shell=True)
  316. (output, err) = running_apps.communicate()
  317. temp = output.split('\n')
  318. def find_editor_mac(name, search_obj):
  319. for line in search_obj:
  320. if 0 <= line.find(name):
  321. path = line
  322. if 0 <= path.find('-psn'):
  323. path = path[ : path.find('-psn') - 1 ]
  324. return True, path
  325. return False , ''
  326. (success_sublime, editor_path_sublime) = find_editor_mac('Sublime',temp)
  327. (success_atom, editor_path_atom) = find_editor_mac('Atom',temp)
  328. if success_sublime:
  329. subprocess.Popen([editor_path_sublime, command])
  330. elif success_atom:
  331. subprocess.Popen([editor_path_atom, command])
  332. else:
  333. os.system('open ' + file_path )
  334. # end - open_file
  335. # gets the last build environment
  336. def get_build_last():
  337. env_last = ''
  338. DIR_PWD = os.listdir('.')
  339. if '.pio' in DIR_PWD:
  340. date_last = 0.0
  341. DIR__pioenvs = os.listdir('.pio')
  342. for name in DIR__pioenvs:
  343. if 0 <= name.find('.') or 0 <= name.find('-'): # skip files in listing
  344. continue
  345. DIR_temp = os.listdir('.pio/build/' + name)
  346. for names_temp in DIR_temp:
  347. if 0 == names_temp.find('firmware.'):
  348. date_temp = os.path.getmtime('.pio/build/' + name + '/' + names_temp)
  349. if date_temp > date_last:
  350. date_last = date_temp
  351. env_last = name
  352. return env_last
  353. # gets the board being built from the Configuration.h file
  354. # returns: board name, major version of Marlin being used (1 or 2)
  355. def get_board_name():
  356. board_name = ''
  357. # get board name
  358. with open('Marlin/Configuration.h', 'r') as myfile:
  359. Configuration_h = myfile.read()
  360. Configuration_h = Configuration_h.split('\n')
  361. Marlin_ver = 0 # set version to invalid number
  362. for lines in Configuration_h:
  363. if 0 == lines.find('#define CONFIGURATION_H_VERSION 01'):
  364. Marlin_ver = 1
  365. if 0 == lines.find('#define CONFIGURATION_H_VERSION 02'):
  366. Marlin_ver = 2
  367. board = lines.find(' BOARD_') + 1
  368. motherboard = lines.find(' MOTHERBOARD ') + 1
  369. define = lines.find('#define ')
  370. comment = lines.find('//')
  371. if (comment == -1 or comment > board) and \
  372. board > motherboard and \
  373. motherboard > define and \
  374. define >= 0 :
  375. spaces = lines.find(' ', board) # find the end of the board substring
  376. if spaces == -1:
  377. board_name = lines[board : ]
  378. else:
  379. board_name = lines[board : spaces]
  380. break
  381. return board_name, Marlin_ver
  382. # extract first environment name it finds after the start position
  383. # returns: environment name and position to start the next search from
  384. def get_env_from_line(line, start_position):
  385. env = ''
  386. next_position = -1
  387. env_position = line.find('env:', start_position)
  388. if 0 < env_position:
  389. next_position = line.find(' ', env_position + 4)
  390. if 0 < next_position:
  391. env = line[env_position + 4 : next_position]
  392. else:
  393. env = line[env_position + 4 : ] # at the end of the line
  394. return env, next_position
  395. #scans pins.h for board name and returns the environment(s) it finds
  396. def get_starting_env(board_name_full, version):
  397. # get environment starting point
  398. if version == 1:
  399. path = 'Marlin/pins.h'
  400. if version == 2:
  401. path = 'Marlin/src/pins/pins.h'
  402. with open(path, 'r') as myfile:
  403. pins_h = myfile.read()
  404. env_A = ''
  405. env_B = ''
  406. env_C = ''
  407. board_name = board_name_full[ 6 : ] # only use the part after "BOARD_" since we're searching the pins.h file
  408. pins_h = pins_h.split('\n')
  409. environment = ''
  410. board_line = ''
  411. cpu_A = ''
  412. cpu_B = ''
  413. i = 0
  414. list_start_found = False
  415. for lines in pins_h:
  416. i = i + 1 # i is always one ahead of the index into pins_h
  417. if 0 < lines.find("Unknown MOTHERBOARD value set in Configuration.h"):
  418. break # no more
  419. if 0 < lines.find('1280'):
  420. list_start_found = True
  421. if list_start_found == False: # skip lines until find start of CPU list
  422. continue
  423. board = lines.find(board_name)
  424. comment_start = lines.find('// ')
  425. cpu_A_loc = comment_start
  426. cpu_B_loc = 0
  427. if board > 0: # need to look at the next line for environment info
  428. cpu_line = pins_h[i]
  429. comment_start = cpu_line.find('// ')
  430. env_A, next_position = get_env_from_line(cpu_line, comment_start) # get name of environment & start of search for next
  431. env_B, next_position = get_env_from_line(cpu_line, next_position) # get next environment, if it exists
  432. env_C, next_position = get_env_from_line(cpu_line, next_position) # get next environment, if it exists
  433. break
  434. return env_A, env_B, env_C
  435. # scans input string for CPUs that the users may need to select from
  436. # returns: CPU name
  437. def get_CPU_name(environment):
  438. CPU_list = ('1280', '2560','644', '1284', 'LPC1768', 'DUE')
  439. CPU_name = ''
  440. for CPU in CPU_list:
  441. if 0 < environment.find(CPU):
  442. return CPU
  443. # get environment to be used for the build
  444. # returns: environment
  445. def get_env(board_name, ver_Marlin):
  446. def no_environment():
  447. print('ERROR - no environment for this board')
  448. print(board_name)
  449. raise SystemExit(0) # no environment so quit
  450. def invalid_board():
  451. print('ERROR - invalid board')
  452. print(board_name)
  453. raise SystemExit(0) # quit if unable to find board
  454. CPU_question = ( ('1280', '2560', " 1280 or 2560 CPU? "), ('644', '1284', " 644 or 1284 CPU? ") )
  455. if 0 < board_name.find('MELZI') :
  456. get_answer(' ' + board_name + ' ', " Which flavor of Melzi? ", "Melzi (Optiboot bootloader)", "Melzi ")
  457. if 1 == get_answer_val:
  458. target_env = 'melzi_optiboot'
  459. else:
  460. target_env = 'melzi'
  461. else:
  462. env_A, env_B, env_C = get_starting_env(board_name, ver_Marlin)
  463. if env_A == '':
  464. no_environment()
  465. if env_B == '':
  466. return env_A # only one environment so finished
  467. CPU_A = get_CPU_name(env_A)
  468. CPU_B = get_CPU_name(env_B)
  469. for item in CPU_question:
  470. if CPU_A == item[0]:
  471. get_answer(' ' + board_name + ' ', item[2], item[0], item[1])
  472. if 2 == get_answer_val:
  473. target_env = env_B
  474. else:
  475. target_env = env_A
  476. return target_env
  477. if env_A == 'LPC1768':
  478. if build_type == 'traceback' or (build_type == 'clean' and get_build_last() == 'LPC1768_debug_and_upload'):
  479. target_env = 'LPC1768_debug_and_upload'
  480. else:
  481. target_env = 'LPC1768'
  482. elif env_A == 'DUE':
  483. target_env = 'DUE'
  484. if build_type == 'traceback' or (build_type == 'clean' and get_build_last() == 'DUE_debug'):
  485. target_env = 'DUE_debug'
  486. elif env_B == 'DUE_USB':
  487. get_answer(' ' + board_name + ' ', " DUE: need download port ", "USB (native USB) port", "Programming port ")
  488. if 1 == get_answer_val:
  489. target_env = 'DUE_USB'
  490. else:
  491. target_env = 'DUE'
  492. else:
  493. invalid_board()
  494. if build_type == 'traceback' and target_env != 'LPC1768_debug_and_upload' and target_env != 'DUE_debug' and Marlin_ver == 2:
  495. print("ERROR - this board isn't setup for traceback")
  496. print('board_name: ', board_name)
  497. print('target_env: ', target_env)
  498. raise SystemExit(0)
  499. return target_env
  500. # end - get_env
  501. # puts screen text into queue so that the parent thread can fetch the data from this thread
  502. import Queue
  503. IO_queue = Queue.Queue()
  504. PIO_queue = Queue.Queue()
  505. def write_to_screen_queue(text, format_tag = 'normal'):
  506. double_in = [text, format_tag]
  507. IO_queue.put(double_in, block = False)
  508. #
  509. # send one line to the terminal screen with syntax highlighting
  510. #
  511. # input: unformatted text, flags from previous run
  512. # returns: formatted text ready to go to the terminal, flags from this run
  513. #
  514. # This routine remembers the status from call to call because previous
  515. # lines can affect how the current line is highlighted
  516. #
  517. # 'static' variables - init here and then keep updating them from within print_line
  518. warning = False
  519. warning_FROM = False
  520. error = False
  521. standard = True
  522. prev_line_COM = False
  523. next_line_warning = False
  524. warning_continue = False
  525. line_counter = 0
  526. def line_print(line_input):
  527. global warning
  528. global warning_FROM
  529. global error
  530. global standard
  531. global prev_line_COM
  532. global next_line_warning
  533. global warning_continue
  534. global line_counter
  535. # all '0' elements must precede all '1' elements or they'll be skipped
  536. platformio_highlights = [
  537. ['Environment', 0, 'highlight_blue'],
  538. ['[SKIP]', 1, 'warning'],
  539. ['[IGNORED]', 1, 'warning'],
  540. ['[ERROR]', 1, 'error'],
  541. ['[FAILED]', 1, 'error'],
  542. ['[SUCCESS]', 1, 'highlight_green']
  543. ]
  544. def write_to_screen_with_replace(text, highlights): # search for highlights & split line accordingly
  545. did_something = False
  546. for highlight in highlights:
  547. found = text.find(highlight[0])
  548. if did_something == True:
  549. break
  550. if found >= 0 :
  551. did_something = True
  552. if 0 == highlight[1]:
  553. found_1 = text.find(' ')
  554. found_tab = text.find('\t')
  555. if found_1 < 0 or found_1 > found_tab:
  556. found_1 = found_tab
  557. write_to_screen_queue(text[ : found_1 + 1 ])
  558. for highlight_2 in highlights:
  559. if highlight[0] == highlight_2[0] :
  560. continue
  561. found = text.find(highlight_2[0])
  562. if found >= 0 :
  563. found_space = text.find(' ', found_1 + 1)
  564. found_tab = text.find('\t', found_1 + 1)
  565. if found_space < 0 or found_space > found_tab:
  566. found_space = found_tab
  567. found_right = text.find(']', found + 1)
  568. write_to_screen_queue(text[found_1 + 1 : found_space + 1 ], highlight[2])
  569. write_to_screen_queue(text[found_space + 1 : found + 1 ])
  570. write_to_screen_queue(text[found + 1 : found_right], highlight_2[2])
  571. write_to_screen_queue(text[found_right : ] + '\n')
  572. break
  573. break
  574. if 1 == highlight[1]:
  575. found_right = text.find(']', found + 1)
  576. write_to_screen_queue(text[ : found + 1 ])
  577. write_to_screen_queue(text[found + 1 : found_right ], highlight[2])
  578. write_to_screen_queue(text[found_right : ] + '\n' + '\n')
  579. break
  580. if did_something == False:
  581. r_loc = text.find('\r') + 1
  582. if r_loc > 0 and r_loc < len(text): # need to split this line
  583. text = text.split('\r')
  584. for line in text:
  585. if line != '':
  586. write_to_screen_queue(line + '\n')
  587. else:
  588. write_to_screen_queue(text + '\n')
  589. # end - write_to_screen_with_replace
  590. # scan the line
  591. line_counter = line_counter + 1
  592. max_search = len(line_input)
  593. if max_search > 3 :
  594. max_search = 3
  595. beginning = line_input[:max_search]
  596. # set flags
  597. if 0 < line_input.find(': warning: '): # start of warning block
  598. warning = True
  599. warning_FROM = False
  600. error = False
  601. standard = False
  602. prev_line_COM = False
  603. prev_line_COM = False
  604. warning_continue = True
  605. if 0 < line_input.find('Thank you') or 0 < line_input.find('SUMMARY') :
  606. warning = False #standard line found
  607. warning_FROM = False
  608. error = False
  609. standard = True
  610. prev_line_COM = False
  611. warning_continue = False
  612. elif beginning == 'War' or \
  613. beginning == '#er' or \
  614. beginning == 'In ' or \
  615. (beginning != 'Com' and prev_line_COM == True and not(beginning == 'Arc' or beginning == 'Lin' or beginning == 'Ind') or \
  616. next_line_warning == True):
  617. warning = True #warning found
  618. warning_FROM = False
  619. error = False
  620. standard = False
  621. prev_line_COM = False
  622. elif beginning == 'Com' or \
  623. beginning == 'Ver' or \
  624. beginning == ' [E' or \
  625. beginning == 'Rem' or \
  626. beginning == 'Bui' or \
  627. beginning == 'Ind' or \
  628. beginning == 'PLA':
  629. warning = False #standard line found
  630. warning_FROM = False
  631. error = False
  632. standard = True
  633. prev_line_COM = False
  634. warning_continue = False
  635. elif beginning == '***':
  636. warning = False # error found
  637. warning_FROM = False
  638. error = True
  639. standard = False
  640. prev_line_COM = False
  641. elif 0 < line_input.find(': error:') or \
  642. 0 < line_input.find(': fatal error:'): # start of warning /error block
  643. warning = False # error found
  644. warning_FROM = False
  645. error = True
  646. standard = False
  647. prev_line_COM = False
  648. warning_continue = True
  649. elif beginning == 'fro' and warning == True or \
  650. beginning == '.pi' : # start of warning /error block
  651. warning_FROM = True
  652. prev_line_COM = False
  653. warning_continue = True
  654. elif warning_continue == True:
  655. warning = True
  656. warning_FROM = False # keep the warning status going until find a standard line or an error
  657. error = False
  658. standard = False
  659. prev_line_COM = False
  660. warning_continue = True
  661. else:
  662. warning = False # unknown so assume standard line
  663. warning_FROM = False
  664. error = False
  665. standard = True
  666. prev_line_COM = False
  667. warning_continue = False
  668. if beginning == 'Com':
  669. prev_line_COM = True
  670. # print based on flags
  671. if standard == True:
  672. write_to_screen_with_replace(line_input, platformio_highlights) #print white on black with substitutions
  673. if warning == True:
  674. write_to_screen_queue(line_input + '\n', 'warning')
  675. if error == True:
  676. write_to_screen_queue(line_input + '\n', 'error')
  677. # end - line_print
  678. def run_PIO(dummy):
  679. ##########################################################################
  680. # #
  681. # run Platformio #
  682. # #
  683. ##########################################################################
  684. # build platformio run -e target_env
  685. # clean platformio run --target clean -e target_env
  686. # upload platformio run --target upload -e target_env
  687. # traceback platformio run --target upload -e target_env
  688. # program platformio run --target program -e target_env
  689. # test platformio test upload -e target_env
  690. # remote platformio remote run --target upload -e target_env
  691. # debug platformio debug -e target_env
  692. global build_type
  693. global target_env
  694. global board_name
  695. print('build_type: ', build_type)
  696. import subprocess
  697. import sys
  698. print('starting platformio')
  699. if build_type == 'build':
  700. # platformio run -e target_env
  701. # combine stdout & stderr so all compile messages are included
  702. pio_subprocess = subprocess.Popen(['platformio', 'run', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  703. elif build_type == 'clean':
  704. # platformio run --target clean -e target_env
  705. # combine stdout & stderr so all compile messages are included
  706. pio_subprocess = subprocess.Popen(['platformio', 'run', '--target', 'clean', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  707. elif build_type == 'upload':
  708. # platformio run --target upload -e target_env
  709. # combine stdout & stderr so all compile messages are included
  710. pio_subprocess = subprocess.Popen(['platformio', 'run', '--target', 'upload', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  711. elif build_type == 'traceback':
  712. # platformio run --target upload -e target_env - select the debug environment if there is one
  713. # combine stdout & stderr so all compile messages are included
  714. pio_subprocess = subprocess.Popen(['platformio', 'run', '--target', 'upload', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  715. elif build_type == 'program':
  716. # platformio run --target program -e target_env
  717. # combine stdout & stderr so all compile messages are included
  718. pio_subprocess = subprocess.Popen(['platformio', 'run', '--target', 'program', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  719. elif build_type == 'test':
  720. #platformio test upload -e target_env
  721. # combine stdout & stderr so all compile messages are included
  722. pio_subprocess = subprocess.Popen(['platformio', 'test', 'upload', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  723. elif build_type == 'remote':
  724. # platformio remote run --target upload -e target_env
  725. # combine stdout & stderr so all compile messages are included
  726. pio_subprocess = subprocess.Popen(['platformio', 'remote', 'run', '--target', 'program', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  727. elif build_type == 'debug':
  728. # platformio debug -e target_env
  729. # combine stdout & stderr so all compile messages are included
  730. pio_subprocess = subprocess.Popen(['platformio', 'debug', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  731. else:
  732. print('ERROR - unknown build type: ', build_type)
  733. raise SystemExit(0) # kill everything
  734. # stream output from subprocess and split it into lines
  735. for line in iter(pio_subprocess.stdout.readline, ''):
  736. line_print(line.replace('\n', ''))
  737. # append info used to run PlatformIO
  738. write_to_screen_queue('\nBoard name: ' + board_name + '\n') # put build info at the bottom of the screen
  739. write_to_screen_queue('Build type: ' + build_type + '\n')
  740. write_to_screen_queue('Environment used: ' + target_env + '\n')
  741. write_to_screen_queue(str(datetime.now()) + '\n')
  742. # end - run_PIO
  743. ########################################################################
  744. import time
  745. import threading
  746. import Tkinter as tk
  747. import ttk
  748. import Queue
  749. import subprocess
  750. import sys
  751. que = Queue.Queue()
  752. #IO_queue = Queue.Queue()
  753. from Tkinter import Tk, Frame, Text, Scrollbar, Menu
  754. from tkMessageBox import askokcancel
  755. import tkFileDialog
  756. from tkMessageBox import askokcancel
  757. import tkFileDialog
  758. class output_window(Text):
  759. # based on Super Text
  760. global continue_updates
  761. continue_updates = True
  762. global search_position
  763. search_position = '' # start with invalid search position
  764. global error_found
  765. error_found = False # are there any errors?
  766. def __init__(self):
  767. self.root = tk.Tk()
  768. self.frame = tk.Frame(self.root)
  769. self.frame.pack(fill='both', expand=True)
  770. # text widget
  771. #self.text = tk.Text(self.frame, borderwidth=3, relief="sunken")
  772. Text.__init__(self, self.frame, borderwidth=3, relief="sunken")
  773. self.config(tabs=(400,)) # configure Text widget tab stops
  774. self.config(background = 'black', foreground = 'white', font= ("consolas", 12), wrap = 'word', undo = 'True')
  775. #self.config(background = 'black', foreground = 'white', font= ("consolas", 12), wrap = 'none', undo = 'True')
  776. self.config(height = 24, width = 100)
  777. self.config(insertbackground = 'pale green') # keyboard insertion point
  778. self.pack(side='left', fill='both', expand=True)
  779. self.tag_config('normal', foreground = 'white')
  780. self.tag_config('warning', foreground = 'yellow' )
  781. self.tag_config('error', foreground = 'red')
  782. self.tag_config('highlight_green', foreground = 'green')
  783. self.tag_config('highlight_blue', foreground = 'cyan')
  784. self.tag_config('error_highlight_inactive', background = 'dim gray')
  785. self.tag_config('error_highlight_active', background = 'light grey')
  786. self.bind_class("Text","<Control-a>", self.select_all) # required in windows, works in others
  787. self.bind_all("<Control-Shift-E>", self.scroll_errors)
  788. self.bind_class("<Control-Shift-R>", self.rebuild)
  789. # scrollbar
  790. scrb = tk.Scrollbar(self.frame, orient='vertical', command=self.yview)
  791. self.config(yscrollcommand=scrb.set)
  792. scrb.pack(side='right', fill='y')
  793. #self.scrb_Y = tk.Scrollbar(self.frame, orient='vertical', command=self.yview)
  794. #self.scrb_Y.config(yscrollcommand=self.scrb_Y.set)
  795. #self.scrb_Y.pack(side='right', fill='y')
  796. #self.scrb_X = tk.Scrollbar(self.frame, orient='horizontal', command=self.xview)
  797. #self.scrb_X.config(xscrollcommand=self.scrb_X.set)
  798. #self.scrb_X.pack(side='bottom', fill='x')
  799. #scrb_X = tk.Scrollbar(self, orient=tk.HORIZONTAL, command=self.xview) # tk.HORIZONTAL now have a horizsontal scroll bar BUT... shrinks it to a postage stamp and hides far right behind the vertical scroll bar
  800. #self.config(xscrollcommand=scrb_X.set)
  801. #scrb_X.pack(side='bottom', fill='x')
  802. #scrb= tk.Scrollbar(self, orient='vertical', command=self.yview)
  803. #self.config(yscrollcommand=scrb.set)
  804. #scrb.pack(side='right', fill='y')
  805. #self.config(height = 240, width = 1000) # didn't get the size baCK TO NORMAL
  806. #self.pack(side='left', fill='both', expand=True) # didn't get the size baCK TO NORMAL
  807. # pop-up menu
  808. self.popup = tk.Menu(self, tearoff=0)
  809. self.popup.add_command(label='Copy', command=self._copy)
  810. self.popup.add_command(label='Paste', command=self._paste)
  811. self.popup.add_separator()
  812. self.popup.add_command(label='Cut', command=self._cut)
  813. self.popup.add_separator()
  814. self.popup.add_command(label='Select All', command=self._select_all)
  815. self.popup.add_command(label='Clear All', command=self._clear_all)
  816. self.popup.add_separator()
  817. self.popup.add_command(label='Save As', command=self._file_save_as)
  818. self.popup.add_separator()
  819. #self.popup.add_command(label='Repeat Build(CTL-shift-r)', command=self._rebuild)
  820. self.popup.add_command(label='Repeat Build', command=self._rebuild)
  821. self.popup.add_separator()
  822. self.popup.add_command(label='Scroll Errors (CTL-shift-e)', command=self._scroll_errors)
  823. self.popup.add_separator()
  824. self.popup.add_command(label='Open File at Cursor', command=self._open_selected_file)
  825. if current_OS == 'Darwin': # MAC
  826. self.bind('<Button-2>', self._show_popup) # macOS only
  827. else:
  828. self.bind('<Button-3>', self._show_popup) # Windows & Linux
  829. # threading & subprocess section
  830. def start_thread(self, ):
  831. global continue_updates
  832. # create then start a secondary thread to run an arbitrary function
  833. # must have at least one argument
  834. self.secondary_thread = threading.Thread(target = lambda q, arg1: q.put(run_PIO(arg1)), args=(que, ''))
  835. self.secondary_thread.start()
  836. continue_updates = True
  837. # check the Queue in 50ms
  838. self.root.after(50, self.check_thread)
  839. self.root.after(50, self.update)
  840. def check_thread(self): # wait for user to kill the window
  841. global continue_updates
  842. if continue_updates == True:
  843. self.root.after(10, self.check_thread)
  844. def update(self):
  845. global continue_updates
  846. if continue_updates == True:
  847. self.root.after(10, self.update)#method is called every 50ms
  848. temp_text = ['0','0']
  849. if IO_queue.empty():
  850. if not(self.secondary_thread.is_alive()):
  851. continue_updates = False # queue is exhausted and thread is dead so no need for further updates
  852. else:
  853. try:
  854. temp_text = IO_queue.get(block = False)
  855. except Queue.Empty:
  856. continue_updates = False # queue is exhausted so no need for further updates
  857. else:
  858. self.insert('end', temp_text[0], temp_text[1])
  859. self.see("end") # make the last line visible (scroll text off the top)
  860. # text editing section
  861. def _scroll_errors(self):
  862. global search_position
  863. global error_found
  864. if search_position == '': # first time so highlight all errors
  865. countVar = tk.IntVar()
  866. search_position = '1.0'
  867. search_count = 0
  868. while search_position != '' and search_count < 100:
  869. search_position = self.search("error", search_position, stopindex="end", count=countVar, nocase=1)
  870. search_count = search_count + 1
  871. if search_position != '':
  872. error_found = True
  873. end_pos = '{}+{}c'.format(search_position, 5)
  874. self.tag_add("error_highlight_inactive", search_position, end_pos)
  875. search_position = '{}+{}c'.format(search_position, 1) # point to the next character for new search
  876. else:
  877. break
  878. if error_found:
  879. if search_position == '':
  880. search_position = self.search("error", '1.0', stopindex="end", nocase=1) # new search
  881. else: # remove active highlight
  882. end_pos = '{}+{}c'.format(search_position, 5)
  883. start_pos = '{}+{}c'.format(search_position, -1)
  884. self.tag_remove("error_highlight_active", start_pos, end_pos)
  885. search_position = self.search("error", search_position, stopindex="end", nocase=1) # finds first occurrence AGAIN on the first time through
  886. if search_position == "": # wrap around
  887. search_position = self.search("error", '1.0', stopindex="end", nocase=1)
  888. end_pos = '{}+{}c'.format(search_position, 5)
  889. self.tag_add("error_highlight_active", search_position, end_pos) # add active highlight
  890. self.see(search_position)
  891. search_position = '{}+{}c'.format(search_position, 1) # point to the next character for new search
  892. def scroll_errors(self, event):
  893. self._scroll_errors()
  894. def _rebuild(self):
  895. #global board_name
  896. #global Marlin_ver
  897. #global target_env
  898. #board_name, Marlin_ver = get_board_name()
  899. #target_env = get_env(board_name, Marlin_ver)
  900. self.start_thread()
  901. def rebuild(self, event):
  902. print("event happened")
  903. self._rebuild()
  904. def _open_selected_file(self):
  905. current_line = self.index('insert')
  906. line_start = current_line[ : current_line.find('.')] + '.0'
  907. line_end = current_line[ : current_line.find('.')] + '.200'
  908. self.mark_set("path_start", line_start)
  909. self.mark_set("path_end", line_end)
  910. path = self.get("path_start", "path_end")
  911. from_loc = path.find('from ')
  912. colon_loc = path.find(': ')
  913. if 0 <= from_loc and ((colon_loc == -1) or (from_loc < colon_loc)) :
  914. path = path [ from_loc + 5 : ]
  915. if 0 <= colon_loc:
  916. path = path [ : colon_loc ]
  917. if 0 <= path.find('\\') or 0 <= path.find('/'): # make sure it really contains a path
  918. open_file(path)
  919. def _file_save_as(self):
  920. self.filename = tkFileDialog.asksaveasfilename(defaultextension = '.txt')
  921. f = open(self.filename, 'w')
  922. f.write(self.get('1.0', 'end'))
  923. f.close()
  924. def copy(self, event):
  925. try:
  926. selection = self.get(*self.tag_ranges('sel'))
  927. self.clipboard_clear()
  928. self.clipboard_append(selection)
  929. except TypeError:
  930. pass
  931. def cut(self, event):
  932. try:
  933. selection = self.get(*self.tag_ranges('sel'))
  934. self.clipboard_clear()
  935. self.clipboard_append(selection)
  936. self.delete(*self.tag_ranges('sel'))
  937. except TypeError:
  938. pass
  939. def _show_popup(self, event):
  940. '''right-click popup menu'''
  941. if self.root.focus_get() != self:
  942. self.root.focus_set()
  943. try:
  944. self.popup.tk_popup(event.x_root, event.y_root, 0)
  945. finally:
  946. self.popup.grab_release()
  947. def _cut(self):
  948. try:
  949. selection = self.get(*self.tag_ranges('sel'))
  950. self.clipboard_clear()
  951. self.clipboard_append(selection)
  952. self.delete(*self.tag_ranges('sel'))
  953. except TypeError:
  954. pass
  955. def cut(self, event):
  956. self._cut()
  957. def _copy(self):
  958. try:
  959. selection = self.get(*self.tag_ranges('sel'))
  960. self.clipboard_clear()
  961. self.clipboard_append(selection)
  962. except TypeError:
  963. pass
  964. def copy(self, event):
  965. self._copy()
  966. def _paste(self):
  967. self.insert('insert', self.selection_get(selection='CLIPBOARD'))
  968. def _select_all(self):
  969. self.tag_add('sel', '1.0', 'end')
  970. def select_all(self, event):
  971. self.tag_add('sel', '1.0', 'end')
  972. def _clear_all(self):
  973. #'''erases all text'''
  974. #
  975. #isok = askokcancel('Clear All', 'Erase all text?', frame=self,
  976. # default='ok')
  977. #if isok:
  978. # self.delete('1.0', 'end')
  979. self.delete('1.0', 'end')
  980. # end - output_window
  981. def main():
  982. ##########################################################################
  983. # #
  984. # main program #
  985. # #
  986. ##########################################################################
  987. global build_type
  988. global target_env
  989. global board_name
  990. board_name, Marlin_ver = get_board_name()
  991. target_env = get_env(board_name, Marlin_ver)
  992. os.environ["BUILD_TYPE"] = build_type # let sub processes know what is happening
  993. os.environ["TARGET_ENV"] = target_env
  994. os.environ["BOARD_NAME"] = board_name
  995. auto_build = output_window()
  996. auto_build.start_thread() # executes the "run_PIO" function
  997. auto_build.root.mainloop()
  998. if __name__ == '__main__':
  999. main()