99from __future__ import print_function
1010
1111import sys
12- from sys import stdout , platform
12+ from sys import stdout , stderr , platform
1313from os .path import (join , dirname , realpath , exists , isdir , basename ,
1414 expanduser , splitext )
1515from os import listdir , unlink , makedirs , environ , chdir , getcwd , walk , uname
4444import argparse
4545from appdirs import user_data_dir
4646import 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
4968else :
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
5771
5872user_dir = dirname (realpath (os .path .curdir ))
5973toolchain_dir = dirname (__file__ )
@@ -66,13 +80,13 @@ class LevelDifferentiatingFormatter(logging.Formatter):
6680 def format (self , record ):
6781 if record .levelno > 20 :
6882 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
7084 elif record .levelno > 10 :
7185 record .msg = '{}[INFO]{}: ' .format (
72- Style .BRIGHT , Style .RESET_ALL ) + record .msg
86+ Err_Style .BRIGHT , Err_Style .RESET_ALL ) + record .msg
7387 else :
7488 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
7690 return super (LevelDifferentiatingFormatter , self ).format (record )
7791
7892logger = logging .getLogger ('p4a' )
@@ -81,7 +95,7 @@ def format(self, record):
8195 # handler and reset the level
8296 logger .setLevel (logging .INFO )
8397 logger .touched = True
84- ch = logging .StreamHandler (stdout ) if sys . stdout . isatty () else logging . NullHandler ( )
98+ ch = logging .StreamHandler (stderr )
8599 formatter = LevelDifferentiatingFormatter ('%(message)s' )
86100 ch .setFormatter (formatter )
87101 logger .addHandler (ch )
@@ -92,20 +106,20 @@ def format(self, record):
92106
93107IS_PY3 = sys .version_info [0 ] >= 3
94108
95- info ('' .join ([Style .BRIGHT , Fore .RED ,
109+ info ('' .join ([Err_Style .BRIGHT , Err_Fore .RED ,
96110 '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 ,
99113 ('It should work (mostly), but you may experience '
100114 'missing features or bugs.' ),
101- Style .RESET_ALL ]))
115+ Err_Style .RESET_ALL ]))
102116
103117def 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 ]))
106120
107121def 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 ))
109123
110124def pretty_log_dists (dists , log_func = info ):
111125 infos = []
@@ -114,7 +128,7 @@ def pretty_log_dists(dists, log_func=info):
114128 'includes recipes ({Fore.GREEN}{recipes}'
115129 '{Style.RESET_ALL})' .format (
116130 name = dist .name , recipes = ', ' .join (dist .recipes ),
117- Fore = Fore , Style = Style ))
131+ Fore = Err_Fore , Style = Err_Style ))
118132
119133 for line in infos :
120134 log_func ('\t ' + line )
@@ -151,9 +165,9 @@ def shprint(command, *args, **kwargs):
151165
152166 # If logging is not in DEBUG mode, trim the command if necessary
153167 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 ))
155169 else :
156- logger .debug ('{}{}' .format (string , Style .RESET_ALL ))
170+ logger .debug ('{}{}' .format (string , Err_Style .RESET_ALL ))
157171
158172 need_closing_newline = False
159173 try :
@@ -165,7 +179,7 @@ def shprint(command, *args, **kwargs):
165179 msg = line .replace ('\n ' , ' ' ).replace ('\t ' , ' ' ).replace ('\b ' , ' ' ).rstrip ()
166180 if msg :
167181# 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 ))
169183 sys .stdout .flush ()
170184 need_closing_newline = True
171185 else :
@@ -265,25 +279,25 @@ def is_exe(fpath):
265279@contextlib .contextmanager
266280def current_directory (new_dir ):
267281 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 )))
270284 chdir (new_dir )
271285 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 )))
274288 chdir (cur_dir )
275289
276290@contextlib .contextmanager
277291def temp_directory ():
278292 temp_dir = mkdtemp ()
279293 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 )))
282296 yield temp_dir
283297 finally :
284298 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 )))
287301
288302
289303def cache_execution (f ):
@@ -2745,13 +2759,13 @@ def recipes(self, args):
27452759 print ('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} '
27462760 '{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}'
27472761 '{version:<8}{Style.RESET_ALL}' .format (
2748- recipe = recipe , Fore = Fore , Style = Style ,
2762+ recipe = recipe , Fore = Out_Fore , Style = Out_Style ,
27492763 version = version ))
27502764 print (' {Fore.GREEN}depends: {recipe.depends}'
2751- '{Fore.RESET}' .format (recipe = recipe , Fore = Fore ))
2765+ '{Fore.RESET}' .format (recipe = recipe , Fore = Out_Fore ))
27522766 if recipe .conflicts :
27532767 print (' {Fore.RED}conflicts: {recipe.conflicts}'
2754- '{Fore.RESET}' .format (recipe = recipe , Fore = Fore ))
2768+ '{Fore.RESET}' .format (recipe = recipe , Fore = Out_Fore ))
27552769 else :
27562770 print ("{recipe.name:<12} {recipe.version:<8}" .format (
27572771 recipe = recipe ))
@@ -2763,9 +2777,9 @@ def bootstraps(self, args):
27632777 for bs in Bootstrap .list_bootstraps ():
27642778 bs = Bootstrap .get_bootstrap (bs , self .ctx )
27652779 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 ))
27672781 print (' {Fore.GREEN}depends: {bs.recipe_depends}{Fore.RESET}' .format (
2768- bs = bs , Fore = Fore ))
2782+ bs = bs , Fore = Out_Fore ))
27692783
27702784 def clean_all (self , args ):
27712785 '''Delete all build components; the package cache, package builds,
@@ -2991,11 +3005,11 @@ def distributions(self, args):
29913005
29923006 if dists :
29933007 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 ))
29953009 pretty_log_dists (dists , print )
29963010 else :
29973011 print ('{Style.BRIGHT}There are no dists currently built.'
2998- '{Style.RESET_ALL}' .format (Style = Style ))
3012+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
29993013
30003014 def delete_dist (self , args ):
30013015 dist = self ._dist
@@ -3054,24 +3068,24 @@ def logcat(self, args):
30543068 def build_status (self , args ):
30553069
30563070 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 ))
30583072 for filen in os .listdir (join (self .ctx .build_dir , 'bootstrap_builds' )):
30593073 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 ))
30613075
30623076 print ('{Style.BRIGHT}Recipes that are probably already built:'
3063- '{Style.RESET_ALL}' .format (Style = Style ))
3077+ '{Style.RESET_ALL}' .format (Style = Out_Style ))
30643078 if exists (join (self .ctx .build_dir , 'other_builds' )):
30653079 for filen in sorted (os .listdir (join (self .ctx .build_dir , 'other_builds' ))):
30663080 name = filen .split ('-' )[0 ]
30673081 dependencies = filen .split ('-' )[1 :]
30683082 recipe_str = (' {Style.BRIGHT}{Fore.GREEN}{name}'
30693083 '{Style.RESET_ALL}' .format (
3070- Style = Style , name = name , Fore = Fore ))
3084+ Style = Out_Style , name = name , Fore = Out_Fore ))
30713085 if dependencies :
30723086 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 )
30753089 print (recipe_str )
30763090
30773091
0 commit comments