9
9
from __future__ import print_function
10
10
11
11
import sys
12
- from sys import stdout , platform
12
+ from sys import stdout , stderr , platform
13
13
from os .path import (join , dirname , realpath , exists , isdir , basename ,
14
14
expanduser )
15
15
from os import listdir , unlink , makedirs , environ , chdir , getcwd , walk , uname
42
42
import argparse
43
43
from appdirs import user_data_dir
44
44
import sh
45
- if sys .stdout .isatty ():
46
- from colorama import Style , Fore
45
+
46
+ from colorama import Style as Colo_Style , Fore as Colo_Fore
47
+ from collections import defaultdict
48
+ class colorama_shim (object ):
49
+ def __init__ (self ):
50
+ self ._dict = defaultdict (str )
51
+ def __getattr__ (self , key ):
52
+ return self ._dict [key ]
53
+ Null_Style = Null_Fore = colorama_shim ()
54
+
55
+ if stdout .isatty ():
56
+ Out_Style = Colo_Style
57
+ Out_Fore = Colo_Fore
58
+ else :
59
+ Out_Style = Null_Style
60
+ Out_Fore = Null_Fore
61
+
62
+ if stderr .isatty ():
63
+ Err_Style = Colo_Style
64
+ Err_Fore = Colo_Fore
47
65
else :
48
- from collections import defaultdict
49
- class colorama_shim (object ):
50
- def __init__ (self ):
51
- self ._dict = defaultdict (str )
52
- def __getattr__ (self , key ):
53
- return self ._dict [key ]
54
- Style = Fore = colorama_shim ()
66
+ Err_Style = Null_Style
67
+ Err_Fore = Null_Fore
55
68
56
69
user_dir = dirname (realpath (os .path .curdir ))
57
70
toolchain_dir = dirname (__file__ )
@@ -64,13 +77,13 @@ class LevelDifferentiatingFormatter(logging.Formatter):
64
77
def format (self , record ):
65
78
if record .levelno > 20 :
66
79
record .msg = '{}{}[WARNING]{}{}: ' .format (
67
- Style .BRIGHT , Fore .RED , Fore .RESET , Style .RESET_ALL ) + record .msg
80
+ Err_Style .BRIGHT , Err_Fore .RED , Err_Fore .RESET , Err_Style .RESET_ALL ) + record .msg
68
81
elif record .levelno > 10 :
69
82
record .msg = '{}[INFO]{}: ' .format (
70
- Style .BRIGHT , Style .RESET_ALL ) + record .msg
83
+ Err_Style .BRIGHT , Err_Style .RESET_ALL ) + record .msg
71
84
else :
72
85
record .msg = '{}{}[DEBUG]{}{}: ' .format (
73
- Style .BRIGHT , Fore .LIGHTBLACK_EX , Fore .RESET , Style .RESET_ALL ) + record .msg
86
+ Err_Style .BRIGHT , Err_Fore .LIGHTBLACK_EX , Err_Fore .RESET , Err_Style .RESET_ALL ) + record .msg
74
87
return super (LevelDifferentiatingFormatter , self ).format (record )
75
88
76
89
logger = logging .getLogger ('p4a' )
@@ -79,7 +92,7 @@ def format(self, record):
79
92
# handler and reset the level
80
93
logger .setLevel (logging .INFO )
81
94
logger .touched = True
82
- ch = logging .StreamHandler (stdout ) if sys . stdout . isatty () else logging . NullHandler ( )
95
+ ch = logging .StreamHandler (stderr )
83
96
formatter = LevelDifferentiatingFormatter ('%(message)s' )
84
97
ch .setFormatter (formatter )
85
98
logger .addHandler (ch )
@@ -90,20 +103,20 @@ def format(self, record):
90
103
91
104
IS_PY3 = sys .version_info [0 ] >= 3
92
105
93
- info ('' .join ([Style .BRIGHT , Fore .RED ,
106
+ info ('' .join ([Err_Style .BRIGHT , Err_Fore .RED ,
94
107
'This python-for-android revamp is an experimental alpha release!' ,
95
- Style .RESET_ALL ]))
96
- info ('' .join ([Fore .RED ,
108
+ Err_Style .RESET_ALL ]))
109
+ info ('' .join ([Err_Fore .RED ,
97
110
('It should work (mostly), but you may experience '
98
111
'missing features or bugs.' ),
99
- Style .RESET_ALL ]))
112
+ Err_Style .RESET_ALL ]))
100
113
101
114
def info_main (* args ):
102
- logger .info ('' .join ([Style .BRIGHT , Fore .GREEN ] + list (args ) +
103
- [Style .RESET_ALL , Fore .RESET ]))
115
+ logger .info ('' .join ([Err_Style .BRIGHT , Err_Fore .GREEN ] + list (args ) +
116
+ [Err_Style .RESET_ALL , Err_Fore .RESET ]))
104
117
105
118
def info_notify (s ):
106
- info ('{}{}{}{}' .format (Style .BRIGHT , Fore .LIGHTBLUE_EX , s , Style .RESET_ALL ))
119
+ info ('{}{}{}{}' .format (Err_Style .BRIGHT , Err_Fore .LIGHTBLUE_EX , s , Err_Style .RESET_ALL ))
107
120
108
121
def pretty_log_dists (dists , log_func = info ):
109
122
infos = []
@@ -112,7 +125,7 @@ def pretty_log_dists(dists, log_func=info):
112
125
'includes recipes ({Fore.GREEN}{recipes}'
113
126
'{Style.RESET_ALL})' .format (
114
127
name = dist .name , recipes = ', ' .join (dist .recipes ),
115
- Fore = Fore , Style = Style ))
128
+ Fore = Err_Fore , Style = Err_Style ))
116
129
117
130
for line in infos :
118
131
log_func ('\t ' + line )
@@ -135,15 +148,15 @@ def shprint(command, *args, **kwargs):
135
148
short_string = string
136
149
if len (string ) > 100 :
137
150
short_string = string [:100 ] + '... (and {} more)' .format (len (string ) - 100 )
138
- logger .info (short_string + Style .RESET_ALL )
151
+ logger .info (short_string + Err_Style .RESET_ALL )
139
152
else :
140
- logger .debug (string + Style .RESET_ALL )
153
+ logger .debug (string + Err_Style .RESET_ALL )
141
154
142
155
output = command (* args , ** kwargs )
143
156
need_closing_newline = False
144
157
for line in output :
145
158
if logger .level > logging .DEBUG :
146
- string = '' .join ([Style .RESET_ALL , '\r ' , ' ' * 11 , 'working ... ' ,
159
+ string = '' .join ([Err_Style .RESET_ALL , '\r ' , ' ' * 11 , 'working ... ' ,
147
160
line [:100 ].replace ('\n ' , '' ).rstrip (), ' ...' ])
148
161
if len (string ) < 20 :
149
162
continue
@@ -225,12 +238,12 @@ def is_exe(fpath):
225
238
@contextlib .contextmanager
226
239
def current_directory (new_dir ):
227
240
cur_dir = getcwd ()
228
- logger .info ('' .join ((Fore .CYAN , '-> directory context ' , new_dir ,
229
- Fore .RESET )))
241
+ logger .info ('' .join ((Err_Fore .CYAN , '-> directory context ' , new_dir ,
242
+ Err_Fore .RESET )))
230
243
chdir (new_dir )
231
244
yield
232
- logger .info ('' .join ((Fore .CYAN , '<- directory context ' , cur_dir ,
233
- Fore .RESET )))
245
+ logger .info ('' .join ((Err_Fore .CYAN , '<- directory context ' , cur_dir ,
246
+ Err_Fore .RESET )))
234
247
chdir (cur_dir )
235
248
236
249
@@ -2606,13 +2619,13 @@ def recipes(self, args):
2606
2619
print ('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} '
2607
2620
'{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}'
2608
2621
'{version:<8}{Style.RESET_ALL}' .format (
2609
- recipe = recipe , Fore = Fore , Style = Style ,
2622
+ recipe = recipe , Fore = Out_Fore , Style = Out_Style ,
2610
2623
version = version ))
2611
2624
print (' {Fore.GREEN}depends: {recipe.depends}'
2612
- '{Fore.RESET}' .format (recipe = recipe , Fore = Fore ))
2625
+ '{Fore.RESET}' .format (recipe = recipe , Fore = Out_Fore ))
2613
2626
if recipe .conflicts :
2614
2627
print (' {Fore.RED}conflicts: {recipe.conflicts}'
2615
- '{Fore.RESET}' .format (recipe = recipe , Fore = Fore ))
2628
+ '{Fore.RESET}' .format (recipe = recipe , Fore = Out_Fore ))
2616
2629
else :
2617
2630
print ("{recipe.name:<12} {recipe.version:<8}" .format (
2618
2631
recipe = recipe ))
@@ -2624,9 +2637,9 @@ def bootstraps(self, args):
2624
2637
for bs in Bootstrap .list_bootstraps ():
2625
2638
bs = Bootstrap .get_bootstrap (bs , self .ctx )
2626
2639
print ('{Fore.BLUE}{Style.BRIGHT}{bs.name}{Style.RESET_ALL}' .format (
2627
- bs = bs , Fore = Fore , Style = Style ))
2640
+ bs = bs , Fore = Out_Fore , Style = Out_Style ))
2628
2641
print (' {Fore.GREEN}depends: {bs.recipe_depends}{Fore.RESET}' .format (
2629
- bs = bs , Fore = Fore ))
2642
+ bs = bs , Fore = Out_Fore ))
2630
2643
2631
2644
def clean_all (self , args ):
2632
2645
'''Delete all build components; the package cache, package builds,
@@ -2852,11 +2865,11 @@ def distributions(self, args):
2852
2865
2853
2866
if dists :
2854
2867
print ('{Style.BRIGHT}Distributions currently installed are:'
2855
- '{Style.RESET_ALL}' .format (Style = Style , Fore = Fore ))
2868
+ '{Style.RESET_ALL}' .format (Style = Out_Style , Fore = Out_Fore ))
2856
2869
pretty_log_dists (dists , print )
2857
2870
else :
2858
2871
print ('{Style.BRIGHT}There are no dists currently built.'
2859
- '{Style.RESET_ALL}' .format (Style = Style ))
2872
+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
2860
2873
2861
2874
def delete_dist (self , args ):
2862
2875
dist = self ._dist
@@ -2915,24 +2928,24 @@ def logcat(self, args):
2915
2928
def build_status (self , args ):
2916
2929
2917
2930
print ('{Style.BRIGHT}Bootstraps whose core components are probably already built:'
2918
- '{Style.RESET_ALL}' .format (Style = Style ))
2931
+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
2919
2932
for filen in os .listdir (join (self .ctx .build_dir , 'bootstrap_builds' )):
2920
2933
print (' {Fore.GREEN}{Style.BRIGHT}{filen}{Style.RESET_ALL}' .format (
2921
- filen = filen , Fore = Fore , Style = Style ))
2934
+ filen = filen , Fore = Out_Fore , Style = Out_Style ))
2922
2935
2923
2936
print ('{Style.BRIGHT}Recipes that are probably already built:'
2924
- '{Style.RESET_ALL}' .format (Style = Style ))
2937
+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
2925
2938
if exists (join (self .ctx .build_dir , 'other_builds' )):
2926
2939
for filen in sorted (os .listdir (join (self .ctx .build_dir , 'other_builds' ))):
2927
2940
name = filen .split ('-' )[0 ]
2928
2941
dependencies = filen .split ('-' )[1 :]
2929
2942
recipe_str = (' {Style.BRIGHT}{Fore.GREEN}{name}'
2930
2943
'{Style.RESET_ALL}' .format (
2931
- Style = Style , name = name , Fore = Fore ))
2944
+ Style = Out_Style , name = name , Fore = Out_Fore ))
2932
2945
if dependencies :
2933
2946
recipe_str += (' ({Fore.BLUE}with ' + ', ' .join (dependencies ) +
2934
- '{Fore.RESET})' ).format (Fore = Fore )
2935
- recipe_str += '{Style.RESET_ALL}' .format (Style = Style )
2947
+ '{Fore.RESET})' ).format (Fore = Out_Fore )
2948
+ recipe_str += '{Style.RESET_ALL}' .format (Style = Out_Style )
2936
2949
print (recipe_str )
2937
2950
2938
2951
0 commit comments