1
1
import argparse
2
+ import copy
3
+ import errno
2
4
import glob
5
+ import os
3
6
import pathlib
4
7
import platform
5
- import errno
6
- import os
7
- import yaml
8
- import sys
9
8
import re
10
- import copy
11
9
import subprocess
10
+ import sys
11
+ import traceback
12
+ import yaml
13
+ import tempfile
12
14
import pyparsing as pp
13
15
import termcolor as tc
14
16
import enum as en
22
24
class defaults :
23
25
24
26
if getattr (sys , 'frozen' , False ):
25
- src = os .getcwd () # If Frozen - $Q2K = [cwd]
27
+ frozen = True
28
+ src = '' #os.getcwd() # If Frozen - $Q2K = [cwd]
26
29
else : # If Live, use bundle_dir
27
30
frozen = False
28
31
src = os .path .dirname (os .path .abspath (__file__ )) # else $Q2K is its own install dir, seperate from working directory
29
32
30
33
version = q2kversion
31
- # Directories
34
+ # Directories
35
+ libs = os .path .join (src , 'lib' ) # Local Libs Default is $Q2K/libs/
36
+ cache = os .path .join (src , '.cache' ,'cache_kb.yaml' ) # Cache Default is $Q2K/.cache/cache_kb.yaml
37
+
38
+ if frozen :
39
+ qmk = os .path .join (src , 'qmk_firmware' ) # QMK Directory - To be provided by user Default is $Q2K/qmk_firmware/
40
+ keyp = os .path .join (src , 'q2k_out' ,'keyplus' ) # Output. This is a relative directory (default) Default is $Q2K/q2k_out/keyplus/ (Frozen)
41
+ kbf = os .path .join (src , 'q2k_out' ,'kbfirmware' )
42
+ else :
43
+ qmk = os .path .join (os .getcwd (), 'qmk_firmware' ) # QMK Directory - To be provided by user Default is $Q2K/qmk_firmware/
44
+ keyp = os .path .join (os .getcwd (),'q2k_out' ,'keyplus' ) # Output. This is a relative directory. Default is [cwd]/q2k_out/keyplus/ (Live)
45
+ kbf = os .path .join (os .getcwd (),'q2k_out' ,'kbfirmware' )
46
+
47
+ if platform .system () == 'Linux' : # AVR-GCC Compiler.
48
+ avr_gcc = 'avr-gcc' # avr-gcc for linux Default is avr-gcc (Linux)
49
+ elif platform .system () == 'Windows' :
50
+ avr_gcc = os .path .join (src , 'avr-gcc' , 'bin' , 'avr-gcc.exe' ) # avr-gcc.exe for Windows Default is $Q2K/avr-gcc/bin/avr-gcc.exe (Windows)
32
51
33
- libs = os .path .join (src , 'lib' ) # Local Libs - We want these to be in a single, FIXED directory. [Avoid using relative directories] Default is $Q2K/libs
34
- cache = os .path .join (src , '.cache' ,'cache_kb.yaml' ) # Cache - We want this to be in a single, FIXED directory. [Avoid using relative directories] Default is $Q2K/.cache/cache_kb.yaml
35
- qmk = os .path .join (src , 'qmk' ) # QMK Directory - To be provided by user Default is [cwd]/qmk
36
- keyp = os .path .join (os .getcwd (),'q2k_out' ,'keyplus' ) # Output. This can either be fixed in one location (defined by user) or be a relative directory (default) Default is [cwd]/q2k_out/keyplus
37
- kbf = os .path .join (os .getcwd (),'q2k_out' ,'kbfirmware' )
38
52
# Lists
39
53
qmk_nonstd_dir = ['handwired' , 'converter' ,
40
54
'clueboard' , 'lfkeyboards' ] # Currently only lfkeyboards causes any issues, however it is good to be verbose here
@@ -44,6 +58,11 @@ class defaults:
44
58
# Misc
45
59
invalid_kc = 'trns' # What to set invalid KC codes to
46
60
61
+ if platform .system () == 'Linux' :
62
+ print_lines = '─────────────────────────────────────────────────────────────────'
63
+ elif platform .system () == 'Windows' :
64
+ print_lines = '──────────────────────────────────────────────────────────────'
65
+
47
66
# ===========================================================================================
48
67
# Console Output
49
68
# ===========================================================================================
@@ -279,12 +298,7 @@ def __preproc(self, kblibs, arg_list, DEBUG=False):
279
298
# Setting up -I and custom define options
280
299
qdir = os .path .join (self .__dirs ['QMK dir' ], 'keyboards' )
281
300
kb = self .__kb .name
282
- if platform .system () == 'Linux' :
283
- cc = ['avr-gcc' , '-E' ]
284
- elif platform .system () == 'Windows' :
285
- avr_gcc = os .path .join (defaults .src , 'avr-gcc' , 'bin' , 'avr-gcc.exe' )
286
- cc = [avr_gcc , '-E' ]
287
-
301
+ cc = [defaults .avr_gcc , '-E' ]
288
302
kbdefine = 'KEYBOARD_' + '_' .join (kblibs )
289
303
QMK_KEYBOARD_H = 'QMK_KEYBOARD_H=\" ' + kb + '.h\" '
290
304
libs = ['-D' , kbdefine , '-D' , QMK_KEYBOARD_H , '-I' + self .__dirs ['Local libs' ]]
@@ -297,17 +311,34 @@ def __preproc(self, kblibs, arg_list, DEBUG=False):
297
311
if DEBUG : print (' ' .join (argv ))
298
312
299
313
try :
300
- output = subprocess .check_output (argv )
314
+ #app_stdout = sys.stdout
315
+ #app_stderr = sys.stderr
316
+ #sys.stdout = sys.__stdout__
317
+ #sys.stderr = sys.__stderr__
318
+
319
+ #sys.stdout = app_stdout
320
+ #sys.stderr = app_stderr
321
+
322
+ si = subprocess .STARTUPINFO ()
323
+ si .dwFlags |= subprocess .STARTF_USESHOWWINDOW
324
+
325
+ output = subprocess .check_output (argv , stdin = subprocess .PIPE , stderr = subprocess .PIPE , startupinfo = si )
326
+
301
327
return output
302
328
except subprocess .CalledProcessError as e :
303
329
err_num = e .returncode
304
- if err_num == 1 :
305
- print ( err_num )
330
+ if err_num == errno . EPERM :
331
+ self . __console . warning ([ 'Compiler failed to read ' + argv [ - 1 ]] )
306
332
output = e .output
307
333
return output
308
334
else :
309
- self . console . warning ([ 'Potentially catastrophic segfault related compilation error' ] )
335
+ print ( traceback . format_exc (), file = sys . stderr )
310
336
return
337
+ except OSError as e :
338
+ if e .errno == errno .ENOEXEC :
339
+ self .__console .error (['Could not find avr-gcc compiler' , 'Check if avr-gcc is installed in' ,defaults .avr_gcc ])
340
+ else :
341
+ print (traceback .format_exc (), file = sys .stderr )
311
342
312
343
def preproc_header (self , path ):
313
344
@@ -375,7 +406,7 @@ def get_rev_info(self, rev):
375
406
return r
376
407
if r .name == 'n/a' :
377
408
return r
378
- #───────────────────────────────────────────────────────────────────────────────────────────
409
+ # ===========================================================================================
379
410
# KB and revision Information
380
411
# ===========================================================================================
381
412
class rev_info :
@@ -668,7 +699,7 @@ def __find(self):
668
699
try :
669
700
with open (self .__loc , 'r' ) as f :
670
701
self .kbo_list = yaml .load (f )
671
- self .__console .note (['Using cached list from ' + self .__loc , '--cache to reset' ])
702
+ self .__console .note (['Using cached list from ' + self .__loc , '--cache to reset' , defaults . print_lines ])
672
703
except :
673
704
self .__console .warning (['Failed to load from ' + self .__loc , 'Generating new cache_kb.yaml...' ])
674
705
self .__write ()
@@ -753,9 +784,9 @@ def __write(self):
753
784
if self .kbo_list :
754
785
# Dump cache info to text file for faster processing in future
755
786
self .__save_cache ()
756
- self .__console .note (['New cache_kb.yaml successfully generated' , 'Location: ' + self .__loc ])
787
+ self .__console .note (['New cache_kb.yaml successfully generated' , 'Location: ' + self .__loc , defaults . print_lines ])
757
788
else :
758
- self .__console .warning (['No keyboard information found' , 'Check QMK directory location in pref.yaml : ' + self .__qmk ])
789
+ self .__console .warning (['No keyboard information found' , 'Check QMK directory location in pref.yaml : ' + self .__qmk , defaults . print_lines ])
759
790
760
791
761
792
def __find_layout_names (self , kbo ):
@@ -806,15 +837,12 @@ def __find_layout_names(self, kbo):
806
837
807
838
def __save_cache (self ):
808
839
path = os .path .split (self .__loc )[0 ]
809
- print (self .__loc )
810
- print (path )
811
- #======================================================================================================================
812
840
if not os .path .exists (path ):
813
841
try :
814
842
os .makedirs (path )
815
843
except OSError as e :
816
844
if e .errno != errno .EEXIST and os .path .isdir (path ):
817
- raise
845
+ raise
818
846
try :
819
847
with open (self .__loc , 'w' ) as f :
820
848
yaml .dump (self .kbo_list , f )
@@ -824,6 +852,7 @@ def __save_cache(self):
824
852
def _clear_cache (self ):
825
853
if os .path .isfile (self .__loc ):
826
854
os .remove (self .__loc )
855
+ self .kbo_list = []
827
856
828
857
def _keyboard_list (self ,):
829
858
kb_names = []
@@ -913,8 +942,7 @@ def __set_dirs(self):
913
942
pref_yaml = os .path .join (defaults .src , 'pref.yaml' )
914
943
with open (pref_yaml , 'r' ) as f :
915
944
self .dirs = yaml .load (f )
916
-
917
- self .console .note (['─────────────────────────────────────────────────────────────────' , 'Using preferences from ' + pref_yaml , '--reset to reset to defaults' ])
945
+ self .console .note ([defaults .print_lines , 'Using preferences from ' + pref_yaml , '--reset to reset to defaults' ])
918
946
919
947
except FileNotFoundError :
920
948
self .__generate_dirs ()
@@ -937,7 +965,7 @@ def __generate_dirs(self):
937
965
with open (pref_yaml , 'w' ) as f :
938
966
f .write ('# Q2K Folder Locations\n ' )
939
967
yaml .dump (dirs , f , default_flow_style = False )
940
- self .console .note (['─────────────────────────────────────────────────────────────────' , 'New pref.yaml generated @ ' + pref_yaml ])
968
+ self .console .note ([defaults . print_lines , 'New pref.yaml generated @ ' + pref_yaml ])
941
969
942
970
except FileNotFoundError :
943
971
self .console .error (['Failed to generate ' + pref_yaml ])
@@ -1025,9 +1053,9 @@ def set_kb(self, keyboard='', rev='', keymap='', template=''):
1025
1053
self .build_kb = build_kbo
1026
1054
self .build_rev = build_revo
1027
1055
if rev :
1028
- self .console .note (['─────────────────────────────────────────────────────────────────' , 'Building ' + keyboard + os .sep + rev + ':' + keymap + ':' + template , '─────────────────────────────────────────────────────────────────' ])
1056
+ self .console .note ([defaults . print_lines , 'Building ' + keyboard + os .sep + rev + ':' + keymap + ':' + template , defaults . print_lines ])
1029
1057
else :
1030
- self .console .note (['─────────────────────────────────────────────────────────────────' , 'Building ' + keyboard + ':' + keymap + ':' + template , '─────────────────────────────────────────────────────────────────' ])
1058
+ self .console .note ([defaults . print_lines , 'Building ' + keyboard + ':' + keymap + ':' + template , defaults . print_lines ])
1031
1059
else :
1032
1060
print_kb_list = ', ' .join (self .keyboard_list ())
1033
1061
self .console .error (['Invalid Keyboard Name - ' + keyboard , 'Valid Names: ' + print_kb_list ])
@@ -1510,7 +1538,7 @@ def __create_keyplus_yaml(self, DEBUG=False):
1510
1538
else :
1511
1539
path_list = kblibs + [keymap ]
1512
1540
output_path = '_' .join (path_list )
1513
- output_yaml = out_dir + output_path + '.yaml'
1541
+ output_yaml = os . path . join ( out_dir , output_path + '.yaml' )
1514
1542
if not os .path .exists (out_dir ):
1515
1543
try :
1516
1544
os .makedirs (out_dir )
@@ -1548,4 +1576,3 @@ def q2keyplus_gui(keyboard, rev, keymap, template):
1548
1576
# Uncomment this to run as a traditional python script (Commnand Line Interface)
1549
1577
#if __name__ == '__main__':
1550
1578
#q2keyplus()
1551
-
0 commit comments