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 , splitext )
15
15
from os import listdir , unlink , makedirs , environ , chdir , getcwd , walk , uname
44
44
import argparse
45
45
from appdirs import user_data_dir
46
46
import sh
47
- if sys .stdout .isatty ():
48
- from colorama import Style , Fore
47
+
48
+ from colorama import Style as Colo_Style , Fore as Colo_Fore
49
+ from collections import defaultdict
50
+ class colorama_shim (object ):
51
+ def __init__ (self ):
52
+ self ._dict = defaultdict (str )
53
+ def __getattr__ (self , key ):
54
+ return self ._dict [key ]
55
+ Style = Fore = colorama_shim ()
56
+ Null_Style = Null_Fore = colorama_shim ()
57
+
58
+ if stdout .isatty ():
59
+ Out_Style = Colo_Style
60
+ Out_Fore = Colo_Fore
61
+ else :
62
+ Out_Style = Null_Style
63
+ Out_Fore = Null_Fore
64
+
65
+ if stderr .isatty ():
66
+ Err_Style = Colo_Style
67
+ Err_Fore = Colo_Fore
49
68
else :
50
- from collections import defaultdict
51
- class colorama_shim (object ):
52
- def __init__ (self ):
53
- self ._dict = defaultdict (str )
54
- def __getattr__ (self , key ):
55
- return self ._dict [key ]
56
- Style = Fore = colorama_shim ()
69
+ Err_Style = Null_Style
70
+ Err_Fore = Null_Fore
57
71
58
72
user_dir = dirname (realpath (os .path .curdir ))
59
73
toolchain_dir = dirname (__file__ )
@@ -66,13 +80,13 @@ class LevelDifferentiatingFormatter(logging.Formatter):
66
80
def format (self , record ):
67
81
if record .levelno > 20 :
68
82
record .msg = '{}{}[WARNING]{}{}: ' .format (
69
- Style .BRIGHT , Fore .RED , Fore .RESET , Style .RESET_ALL ) + record .msg
83
+ Err_Style .BRIGHT , Err_Fore .RED , Err_Fore .RESET , Err_Style .RESET_ALL ) + record .msg
70
84
elif record .levelno > 10 :
71
85
record .msg = '{}[INFO]{}: ' .format (
72
- Style .BRIGHT , Style .RESET_ALL ) + record .msg
86
+ Err_Style .BRIGHT , Err_Style .RESET_ALL ) + record .msg
73
87
else :
74
88
record .msg = '{}{}[DEBUG]{}{}: ' .format (
75
- Style .BRIGHT , Fore .LIGHTBLACK_EX , Fore .RESET , Style .RESET_ALL ) + record .msg
89
+ Err_Style .BRIGHT , Err_Fore .LIGHTBLACK_EX , Err_Fore .RESET , Err_Style .RESET_ALL ) + record .msg
76
90
return super (LevelDifferentiatingFormatter , self ).format (record )
77
91
78
92
logger = logging .getLogger ('p4a' )
@@ -81,7 +95,7 @@ def format(self, record):
81
95
# handler and reset the level
82
96
logger .setLevel (logging .INFO )
83
97
logger .touched = True
84
- ch = logging .StreamHandler (stdout ) if sys . stdout . isatty () else logging . NullHandler ( )
98
+ ch = logging .StreamHandler (stderr )
85
99
formatter = LevelDifferentiatingFormatter ('%(message)s' )
86
100
ch .setFormatter (formatter )
87
101
logger .addHandler (ch )
@@ -92,20 +106,20 @@ def format(self, record):
92
106
93
107
IS_PY3 = sys .version_info [0 ] >= 3
94
108
95
- info ('' .join ([Style .BRIGHT , Fore .RED ,
109
+ info ('' .join ([Err_Style .BRIGHT , Err_Fore .RED ,
96
110
'This python-for-android revamp is an experimental alpha release!' ,
97
- Style .RESET_ALL ]))
98
- info ('' .join ([Fore .RED ,
111
+ Err_Style .RESET_ALL ]))
112
+ info ('' .join ([Err_Fore .RED ,
99
113
('It should work (mostly), but you may experience '
100
114
'missing features or bugs.' ),
101
- Style .RESET_ALL ]))
115
+ Err_Style .RESET_ALL ]))
102
116
103
117
def info_main (* args ):
104
- logger .info ('' .join ([Style .BRIGHT , Fore .GREEN ] + list (args ) +
105
- [Style .RESET_ALL , Fore .RESET ]))
118
+ logger .info ('' .join ([Err_Style .BRIGHT , Err_Fore .GREEN ] + list (args ) +
119
+ [Err_Style .RESET_ALL , Err_Fore .RESET ]))
106
120
107
121
def info_notify (s ):
108
- info ('{}{}{}{}' .format (Style .BRIGHT , Fore .LIGHTBLUE_EX , s , Style .RESET_ALL ))
122
+ info ('{}{}{}{}' .format (Err_Style .BRIGHT , Err_Fore .LIGHTBLUE_EX , s , Err_Style .RESET_ALL ))
109
123
110
124
def pretty_log_dists (dists , log_func = info ):
111
125
infos = []
@@ -114,7 +128,7 @@ def pretty_log_dists(dists, log_func=info):
114
128
'includes recipes ({Fore.GREEN}{recipes}'
115
129
'{Style.RESET_ALL})' .format (
116
130
name = dist .name , recipes = ', ' .join (dist .recipes ),
117
- Fore = Fore , Style = Style ))
131
+ Fore = Err_Fore , Style = Err_Style ))
118
132
119
133
for line in infos :
120
134
log_func ('\t ' + line )
@@ -151,9 +165,9 @@ def shprint(command, *args, **kwargs):
151
165
152
166
# If logging is not in DEBUG mode, trim the command if necessary
153
167
if logger .level > logging .DEBUG :
154
- logger .info ('{}{}' .format (shorten_string (string , columns - 12 ), Style .RESET_ALL ))
168
+ logger .info ('{}{}' .format (shorten_string (string , columns - 12 ), Err_Style .RESET_ALL ))
155
169
else :
156
- logger .debug ('{}{}' .format (string , Style .RESET_ALL ))
170
+ logger .debug ('{}{}' .format (string , Err_Style .RESET_ALL ))
157
171
158
172
need_closing_newline = False
159
173
try :
@@ -165,7 +179,7 @@ def shprint(command, *args, **kwargs):
165
179
msg = line .replace ('\n ' , ' ' ).replace ('\t ' , ' ' ).replace ('\b ' , ' ' ).rstrip ()
166
180
if msg :
167
181
# if len(msg) > msg_width: msg = msg[:(msg_width - 3)] + '...'
168
- sys .stdout .write ('{}\r {}{:<{width}}' .format (Style .RESET_ALL , msg_hdr , shorten_string (msg , msg_width ), width = msg_width ))
182
+ sys .stdout .write ('{}\r {}{:<{width}}' .format (Err_Style .RESET_ALL , msg_hdr , shorten_string (msg , msg_width ), width = msg_width ))
169
183
sys .stdout .flush ()
170
184
need_closing_newline = True
171
185
else :
@@ -265,25 +279,25 @@ def is_exe(fpath):
265
279
@contextlib .contextmanager
266
280
def current_directory (new_dir ):
267
281
cur_dir = getcwd ()
268
- logger .info ('' .join ((Fore .CYAN , '-> directory context ' , new_dir ,
269
- Fore .RESET )))
282
+ logger .info ('' .join ((Err_Fore .CYAN , '-> directory context ' , new_dir ,
283
+ Err_Fore .RESET )))
270
284
chdir (new_dir )
271
285
yield
272
- logger .info ('' .join ((Fore .CYAN , '<- directory context ' , cur_dir ,
273
- Fore .RESET )))
286
+ logger .info ('' .join ((Err_Fore .CYAN , '<- directory context ' , cur_dir ,
287
+ Err_Fore .RESET )))
274
288
chdir (cur_dir )
275
289
276
290
@contextlib .contextmanager
277
291
def temp_directory ():
278
292
temp_dir = mkdtemp ()
279
293
try :
280
- logger .debug ('' .join ((Fore .CYAN , ' + temp directory used ' , temp_dir ,
281
- Fore .RESET )))
294
+ logger .debug ('' .join ((Err_Fore .CYAN , ' + temp directory used ' , temp_dir ,
295
+ Err_Fore .RESET )))
282
296
yield temp_dir
283
297
finally :
284
298
shutil .rmtree (temp_dir )
285
- logger .debug ('' .join ((Fore .CYAN , ' - temp directory deleted ' , temp_dir ,
286
- Fore .RESET )))
299
+ logger .debug ('' .join ((Err_Fore .CYAN , ' - temp directory deleted ' , temp_dir ,
300
+ Err_Fore .RESET )))
287
301
288
302
289
303
def cache_execution (f ):
@@ -2745,13 +2759,13 @@ def recipes(self, args):
2745
2759
print ('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} '
2746
2760
'{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}'
2747
2761
'{version:<8}{Style.RESET_ALL}' .format (
2748
- recipe = recipe , Fore = Fore , Style = Style ,
2762
+ recipe = recipe , Fore = Out_Fore , Style = Out_Style ,
2749
2763
version = version ))
2750
2764
print (' {Fore.GREEN}depends: {recipe.depends}'
2751
- '{Fore.RESET}' .format (recipe = recipe , Fore = Fore ))
2765
+ '{Fore.RESET}' .format (recipe = recipe , Fore = Out_Fore ))
2752
2766
if recipe .conflicts :
2753
2767
print (' {Fore.RED}conflicts: {recipe.conflicts}'
2754
- '{Fore.RESET}' .format (recipe = recipe , Fore = Fore ))
2768
+ '{Fore.RESET}' .format (recipe = recipe , Fore = Out_Fore ))
2755
2769
else :
2756
2770
print ("{recipe.name:<12} {recipe.version:<8}" .format (
2757
2771
recipe = recipe ))
@@ -2763,9 +2777,9 @@ def bootstraps(self, args):
2763
2777
for bs in Bootstrap .list_bootstraps ():
2764
2778
bs = Bootstrap .get_bootstrap (bs , self .ctx )
2765
2779
print ('{Fore.BLUE}{Style.BRIGHT}{bs.name}{Style.RESET_ALL}' .format (
2766
- bs = bs , Fore = Fore , Style = Style ))
2780
+ bs = bs , Fore = Out_Fore , Style = Out_Style ))
2767
2781
print (' {Fore.GREEN}depends: {bs.recipe_depends}{Fore.RESET}' .format (
2768
- bs = bs , Fore = Fore ))
2782
+ bs = bs , Fore = Out_Fore ))
2769
2783
2770
2784
def clean_all (self , args ):
2771
2785
'''Delete all build components; the package cache, package builds,
@@ -2991,11 +3005,11 @@ def distributions(self, args):
2991
3005
2992
3006
if dists :
2993
3007
print ('{Style.BRIGHT}Distributions currently installed are:'
2994
- '{Style.RESET_ALL}' .format (Style = Style , Fore = Fore ))
3008
+ '{Style.RESET_ALL}' .format (Style = Out_Style , Fore = Out_Fore ))
2995
3009
pretty_log_dists (dists , print )
2996
3010
else :
2997
3011
print ('{Style.BRIGHT}There are no dists currently built.'
2998
- '{Style.RESET_ALL}' .format (Style = Style ))
3012
+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
2999
3013
3000
3014
def delete_dist (self , args ):
3001
3015
dist = self ._dist
@@ -3054,24 +3068,24 @@ def logcat(self, args):
3054
3068
def build_status (self , args ):
3055
3069
3056
3070
print ('{Style.BRIGHT}Bootstraps whose core components are probably already built:'
3057
- '{Style.RESET_ALL}' .format (Style = Style ))
3071
+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
3058
3072
for filen in os .listdir (join (self .ctx .build_dir , 'bootstrap_builds' )):
3059
3073
print (' {Fore.GREEN}{Style.BRIGHT}{filen}{Style.RESET_ALL}' .format (
3060
- filen = filen , Fore = Fore , Style = Style ))
3074
+ filen = filen , Fore = Out_Fore , Style = Out_Style ))
3061
3075
3062
3076
print ('{Style.BRIGHT}Recipes that are probably already built:'
3063
- '{Style.RESET_ALL}' .format (Style = Style ))
3077
+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
3064
3078
if exists (join (self .ctx .build_dir , 'other_builds' )):
3065
3079
for filen in sorted (os .listdir (join (self .ctx .build_dir , 'other_builds' ))):
3066
3080
name = filen .split ('-' )[0 ]
3067
3081
dependencies = filen .split ('-' )[1 :]
3068
3082
recipe_str = (' {Style.BRIGHT}{Fore.GREEN}{name}'
3069
3083
'{Style.RESET_ALL}' .format (
3070
- Style = Style , name = name , Fore = Fore ))
3084
+ Style = Out_Style , name = name , Fore = Out_Fore ))
3071
3085
if dependencies :
3072
3086
recipe_str += (' ({Fore.BLUE}with ' + ', ' .join (dependencies ) +
3073
- '{Fore.RESET})' ).format (Fore = Fore )
3074
- recipe_str += '{Style.RESET_ALL}' .format (Style = Style )
3087
+ '{Fore.RESET})' ).format (Fore = Out_Fore )
3088
+ recipe_str += '{Style.RESET_ALL}' .format (Style = Out_Style )
3075
3089
print (recipe_str )
3076
3090
3077
3091
0 commit comments