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
41
41
import argparse
42
42
from appdirs import user_data_dir
43
43
import sh
44
- if sys .stdout .isatty ():
45
- from colorama import Style , Fore
44
+
45
+ from colorama import Style as Colo_Style , Fore as Colo_Fore
46
+ from collections import defaultdict
47
+ class colorama_shim (object ):
48
+ def __init__ (self ):
49
+ self ._dict = defaultdict (str )
50
+ def __getattr__ (self , key ):
51
+ return self ._dict [key ]
52
+ Null_Style = Null_Fore = colorama_shim ()
53
+
54
+ if stdout .isatty ():
55
+ Out_Style = Colo_Style
56
+ Out_Fore = Colo_Fore
57
+ else :
58
+ Out_Style = Null_Style
59
+ Out_Fore = Null_Fore
60
+
61
+ if stderr .isatty ():
62
+ Err_Style = Colo_Style
63
+ Err_Fore = Colo_Fore
46
64
else :
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
- Style = Fore = colorama_shim ()
65
+ Err_Style = Null_Style
66
+ Err_Fore = Null_Fore
54
67
55
68
user_dir = dirname (realpath (os .path .curdir ))
56
69
toolchain_dir = dirname (__file__ )
@@ -63,13 +76,13 @@ class LevelDifferentiatingFormatter(logging.Formatter):
63
76
def format (self , record ):
64
77
if record .levelno > 20 :
65
78
record .msg = '{}{}[WARNING]{}{}: ' .format (
66
- Style .BRIGHT , Fore .RED , Fore .RESET , Style .RESET_ALL ) + record .msg
79
+ Err_Style .BRIGHT , Err_Fore .RED , Err_Fore .RESET , Err_Style .RESET_ALL ) + record .msg
67
80
elif record .levelno > 10 :
68
81
record .msg = '{}[INFO]{}: ' .format (
69
- Style .BRIGHT , Style .RESET_ALL ) + record .msg
82
+ Err_Style .BRIGHT , Err_Style .RESET_ALL ) + record .msg
70
83
else :
71
84
record .msg = '{}{}[DEBUG]{}{}: ' .format (
72
- Style .BRIGHT , Fore .LIGHTBLACK_EX , Fore .RESET , Style .RESET_ALL ) + record .msg
85
+ Err_Style .BRIGHT , Err_Fore .LIGHTBLACK_EX , Err_Fore .RESET , Err_Style .RESET_ALL ) + record .msg
73
86
return super (LevelDifferentiatingFormatter , self ).format (record )
74
87
75
88
logger = logging .getLogger ('p4a' )
@@ -78,7 +91,7 @@ def format(self, record):
78
91
# handler and reset the level
79
92
logger .setLevel (logging .INFO )
80
93
logger .touched = True
81
- ch = logging .StreamHandler (stdout ) if sys . stdout . isatty () else logging . NullHandler ( )
94
+ ch = logging .StreamHandler (stderr )
82
95
formatter = LevelDifferentiatingFormatter ('%(message)s' )
83
96
ch .setFormatter (formatter )
84
97
logger .addHandler (ch )
@@ -89,20 +102,20 @@ def format(self, record):
89
102
90
103
IS_PY3 = sys .version_info [0 ] >= 3
91
104
92
- info ('' .join ([Style .BRIGHT , Fore .RED ,
105
+ info ('' .join ([Err_Style .BRIGHT , Err_Fore .RED ,
93
106
'This python-for-android revamp is an experimental alpha release!' ,
94
- Style .RESET_ALL ]))
95
- info ('' .join ([Fore .RED ,
107
+ Err_Style .RESET_ALL ]))
108
+ info ('' .join ([Err_Fore .RED ,
96
109
('It should work (mostly), but you may experience '
97
110
'missing features or bugs.' ),
98
- Style .RESET_ALL ]))
111
+ Err_Style .RESET_ALL ]))
99
112
100
113
def info_main (* args ):
101
- logger .info ('' .join ([Style .BRIGHT , Fore .GREEN ] + list (args ) +
102
- [Style .RESET_ALL , Fore .RESET ]))
114
+ logger .info ('' .join ([Err_Style .BRIGHT , Err_Fore .GREEN ] + list (args ) +
115
+ [Err_Style .RESET_ALL , Err_Fore .RESET ]))
103
116
104
117
def info_notify (s ):
105
- info ('{}{}{}{}' .format (Style .BRIGHT , Fore .LIGHTBLUE_EX , s , Style .RESET_ALL ))
118
+ info ('{}{}{}{}' .format (Err_Style .BRIGHT , Err_Fore .LIGHTBLUE_EX , s , Err_Style .RESET_ALL ))
106
119
107
120
def pretty_log_dists (dists , log_func = info ):
108
121
infos = []
@@ -111,7 +124,7 @@ def pretty_log_dists(dists, log_func=info):
111
124
'includes recipes ({Fore.GREEN}{recipes}'
112
125
'{Style.RESET_ALL})' .format (
113
126
name = dist .name , recipes = ', ' .join (dist .recipes ),
114
- Fore = Fore , Style = Style ))
127
+ Fore = Err_Fore , Style = Err_Style ))
115
128
116
129
for line in infos :
117
130
log_func ('\t ' + line )
@@ -134,15 +147,15 @@ def shprint(command, *args, **kwargs):
134
147
short_string = string
135
148
if len (string ) > 100 :
136
149
short_string = string [:100 ] + '... (and {} more)' .format (len (string ) - 100 )
137
- logger .info (short_string + Style .RESET_ALL )
150
+ logger .info (short_string + Err_Style .RESET_ALL )
138
151
else :
139
- logger .debug (string + Style .RESET_ALL )
152
+ logger .debug (string + Err_Style .RESET_ALL )
140
153
141
154
output = command (* args , ** kwargs )
142
155
need_closing_newline = False
143
156
for line in output :
144
157
if logger .level > logging .DEBUG :
145
- string = '' .join ([Style .RESET_ALL , '\r ' , ' ' * 11 , 'working ... ' ,
158
+ string = '' .join ([Err_Style .RESET_ALL , '\r ' , ' ' * 11 , 'working ... ' ,
146
159
line [:100 ].replace ('\n ' , '' ).rstrip (), ' ...' ])
147
160
if len (string ) < 20 :
148
161
continue
@@ -224,12 +237,12 @@ def is_exe(fpath):
224
237
@contextlib .contextmanager
225
238
def current_directory (new_dir ):
226
239
cur_dir = getcwd ()
227
- logger .info ('' .join ((Fore .CYAN , '-> directory context ' , new_dir ,
228
- Fore .RESET )))
240
+ logger .info ('' .join ((Err_Fore .CYAN , '-> directory context ' , new_dir ,
241
+ Err_Fore .RESET )))
229
242
chdir (new_dir )
230
243
yield
231
- logger .info ('' .join ((Fore .CYAN , '<- directory context ' , cur_dir ,
232
- Fore .RESET )))
244
+ logger .info ('' .join ((Err_Fore .CYAN , '<- directory context ' , cur_dir ,
245
+ Err_Fore .RESET )))
233
246
chdir (cur_dir )
234
247
235
248
@@ -2587,13 +2600,13 @@ def recipes(self, args):
2587
2600
print ('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} '
2588
2601
'{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}'
2589
2602
'{version:<8}{Style.RESET_ALL}' .format (
2590
- recipe = recipe , Fore = Fore , Style = Style ,
2603
+ recipe = recipe , Fore = Out_Fore , Style = Out_Style ,
2591
2604
version = version ))
2592
2605
print (' {Fore.GREEN}depends: {recipe.depends}'
2593
- '{Fore.RESET}' .format (recipe = recipe , Fore = Fore ))
2606
+ '{Fore.RESET}' .format (recipe = recipe , Fore = Out_Fore ))
2594
2607
if recipe .conflicts :
2595
2608
print (' {Fore.RED}conflicts: {recipe.conflicts}'
2596
- '{Fore.RESET}' .format (recipe = recipe , Fore = Fore ))
2609
+ '{Fore.RESET}' .format (recipe = recipe , Fore = Out_Fore ))
2597
2610
else :
2598
2611
print ("{recipe.name:<12} {recipe.version:<8}" .format (
2599
2612
recipe = recipe ))
@@ -2605,9 +2618,9 @@ def bootstraps(self, args):
2605
2618
for bs in Bootstrap .list_bootstraps ():
2606
2619
bs = Bootstrap .get_bootstrap (bs , self .ctx )
2607
2620
print ('{Fore.BLUE}{Style.BRIGHT}{bs.name}{Style.RESET_ALL}' .format (
2608
- bs = bs , Fore = Fore , Style = Style ))
2621
+ bs = bs , Fore = Out_Fore , Style = Out_Style ))
2609
2622
print (' {Fore.GREEN}depends: {bs.recipe_depends}{Fore.RESET}' .format (
2610
- bs = bs , Fore = Fore ))
2623
+ bs = bs , Fore = Out_Fore ))
2611
2624
2612
2625
def clean_all (self , args ):
2613
2626
'''Delete all build components; the package cache, package builds,
@@ -2833,11 +2846,11 @@ def distributions(self, args):
2833
2846
2834
2847
if dists :
2835
2848
print ('{Style.BRIGHT}Distributions currently installed are:'
2836
- '{Style.RESET_ALL}' .format (Style = Style , Fore = Fore ))
2849
+ '{Style.RESET_ALL}' .format (Style = Out_Style , Fore = Out_Fore ))
2837
2850
pretty_log_dists (dists , print )
2838
2851
else :
2839
2852
print ('{Style.BRIGHT}There are no dists currently built.'
2840
- '{Style.RESET_ALL}' .format (Style = Style ))
2853
+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
2841
2854
2842
2855
def delete_dist (self , args ):
2843
2856
dist = self ._dist
@@ -2896,24 +2909,24 @@ def logcat(self, args):
2896
2909
def build_status (self , args ):
2897
2910
2898
2911
print ('{Style.BRIGHT}Bootstraps whose core components are probably already built:'
2899
- '{Style.RESET_ALL}' .format (Style = Style ))
2912
+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
2900
2913
for filen in os .listdir (join (self .ctx .build_dir , 'bootstrap_builds' )):
2901
2914
print (' {Fore.GREEN}{Style.BRIGHT}{filen}{Style.RESET_ALL}' .format (
2902
- filen = filen , Fore = Fore , Style = Style ))
2915
+ filen = filen , Fore = Out_Fore , Style = Out_Style ))
2903
2916
2904
2917
print ('{Style.BRIGHT}Recipes that are probably already built:'
2905
- '{Style.RESET_ALL}' .format (Style = Style ))
2918
+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
2906
2919
if exists (join (self .ctx .build_dir , 'other_builds' )):
2907
2920
for filen in sorted (os .listdir (join (self .ctx .build_dir , 'other_builds' ))):
2908
2921
name = filen .split ('-' )[0 ]
2909
2922
dependencies = filen .split ('-' )[1 :]
2910
2923
recipe_str = (' {Style.BRIGHT}{Fore.GREEN}{name}'
2911
2924
'{Style.RESET_ALL}' .format (
2912
- Style = Style , name = name , Fore = Fore ))
2925
+ Style = Out_Style , name = name , Fore = Out_Fore ))
2913
2926
if dependencies :
2914
2927
recipe_str += (' ({Fore.BLUE}with ' + ', ' .join (dependencies ) +
2915
- '{Fore.RESET})' ).format (Fore = Fore )
2916
- recipe_str += '{Style.RESET_ALL}' .format (Style = Style )
2928
+ '{Fore.RESET})' ).format (Fore = Out_Fore )
2929
+ recipe_str += '{Style.RESET_ALL}' .format (Style = Out_Style )
2917
2930
print (recipe_str )
2918
2931
2919
2932
0 commit comments