def test_non_ascii_subprocess_output(self): out = StringIO() sandbox = ConfigureSandbox({}, {}, ['configure'], out, out) sandbox.include_file(mozpath.join(topsrcdir, 'build', 'moz.configure', 'util.configure')) sandbox.include_file(mozpath.join(topsrcdir, 'python', 'mozbuild', 'mozbuild', 'test', 'configure', 'data', 'subprocess.configure')) status = 0 try: sandbox.run() except SystemExit as e: status = e.code self.assertEquals(status, 0) quote_char = "'" if getpreferredencoding().lower() == 'utf-8': quote_char = '\u00B4'.encode('utf-8') self.assertEquals(out.getvalue().strip(), quote_char)
def test_non_ascii_subprocess_output(self): out = StringIO() sandbox = ConfigureSandbox({}, {}, [], out, out) sandbox.include_file(mozpath.join(topsrcdir, 'build', 'moz.configure', 'util.configure')) sandbox.include_file(mozpath.join(topsrcdir, 'python', 'mozbuild', 'mozbuild', 'test', 'configure', 'data', 'subprocess.configure')) status = 0 try: sandbox.run() except SystemExit as e: status = e.code self.assertEquals(status, 0) quote_char = "'" if getpreferredencoding().lower() == 'utf-8': quote_char = '\u00B4'.encode('utf-8') self.assertEquals(out.getvalue().strip(), quote_char)
def __init__(self, config, environ=os.environ, argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr, logger=None): dict.__setitem__(self, '__builtins__', self.BUILTINS) self._environ = dict(environ) self._paths = [] self._all_paths = set() self._templates = set() # Associate SandboxDependsFunctions to DependsFunctions. self._depends = OrderedDict() self._seen = set() # Store the @imports added to a given function. self._imports = {} self._options = OrderedDict() # Store raw option (as per command line or environment) for each Option self._raw_options = OrderedDict() # Store options added with `imply_option`, and the reason they were # added (which can either have been given to `imply_option`, or # inferred. Their order matters, so use a list. self._implied_options = [] # Store all results from _prepare_function self._prepared_functions = set() # Queue of functions to execute, with their arguments self._execution_queue = [] # Store the `when`s associated to some options. self._conditions = {} # A list of conditions to apply as a default `when` for every *_impl() self._default_conditions = [] self._helper = CommandLineHelper(environ, argv) assert isinstance(config, dict) self._config = config # Tracks how many templates "deep" we are in the stack. self._template_depth = 0 logging.addLevelName(TRACE, 'TRACE') if logger is None: logger = moz_logger = logging.getLogger('moz.configure') logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(levelname)s: %(message)s') handler = ConfigureOutputHandler(stdout, stderr) handler.setFormatter(formatter) queue_debug = handler.queue_debug logger.addHandler(handler) else: assert isinstance(logger, logging.Logger) moz_logger = None @contextmanager def queue_debug(): yield self._logger = logger # Some callers will manage to log a bytestring with characters in it # that can't be converted to ascii. Make our log methods robust to this # by detecting the encoding that a producer is likely to have used. encoding = getpreferredencoding() def wrapped_log_method(logger, key): method = getattr(logger, key) def wrapped(*args, **kwargs): out_args = [ six.ensure_text(arg, encoding=encoding or 'utf-8') if isinstance(arg, six.binary_type) else arg for arg in args ] return method(*out_args, **kwargs) return wrapped log_namespace = { k: wrapped_log_method(logger, k) for k in ('debug', 'info', 'warning', 'error') } log_namespace['queue_debug'] = queue_debug self.log_impl = ReadOnlyNamespace(**log_namespace) self._help = None self._help_option = self.option_impl( '--help', help='print this message', category=HELP_OPTIONS_CATEGORY) self._seen.add(self._help_option) self._always = DependsFunction(self, lambda: True, []) self._never = DependsFunction(self, lambda: False, []) if self._value_for(self._help_option): self._help = HelpFormatter(argv[0]) self._help.add(self._help_option) elif moz_logger: handler = logging.FileHandler('config.log', mode='w', delay=True, encoding='utf-8') handler.setFormatter(formatter) logger.addHandler(handler)
def __init__(self, config, environ=os.environ, argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr, logger=None): dict.__setitem__(self, '__builtins__', self.BUILTINS) self._paths = [] self._all_paths = set() self._templates = set() # Associate SandboxDependsFunctions to DependsFunctions. self._depends = OrderedDict() self._seen = set() # Store the @imports added to a given function. self._imports = {} self._options = OrderedDict() # Store raw option (as per command line or environment) for each Option self._raw_options = OrderedDict() # Store options added with `imply_option`, and the reason they were # added (which can either have been given to `imply_option`, or # inferred. Their order matters, so use a list. self._implied_options = [] # Store all results from _prepare_function self._prepared_functions = set() # Queue of functions to execute, with their arguments self._execution_queue = [] # Store the `when`s associated to some options. self._conditions = {} # A list of conditions to apply as a default `when` for every *_impl() self._default_conditions = [] self._helper = CommandLineHelper(environ, argv) assert isinstance(config, dict) self._config = config if logger is None: logger = moz_logger = logging.getLogger('moz.configure') logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(levelname)s: %(message)s') handler = ConfigureOutputHandler(stdout, stderr) handler.setFormatter(formatter) queue_debug = handler.queue_debug logger.addHandler(handler) else: assert isinstance(logger, logging.Logger) moz_logger = None @contextmanager def queue_debug(): yield # Some callers will manage to log a bytestring with characters in it # that can't be converted to ascii. Make our log methods robust to this # by detecting the encoding that a producer is likely to have used. encoding = getpreferredencoding() def wrapped_log_method(logger, key): method = getattr(logger, key) if not encoding: return method def wrapped(*args, **kwargs): out_args = [ arg.decode(encoding) if isinstance(arg, str) else arg for arg in args ] return method(*out_args, **kwargs) return wrapped log_namespace = { k: wrapped_log_method(logger, k) for k in ('debug', 'info', 'warning', 'error') } log_namespace['queue_debug'] = queue_debug self.log_impl = ReadOnlyNamespace(**log_namespace) self._help = None self._help_option = self.option_impl('--help', help='print this message') self._seen.add(self._help_option) self._always = DependsFunction(self, lambda: True, []) self._never = DependsFunction(self, lambda: False, []) if self._value_for(self._help_option): self._help = HelpFormatter(argv[0]) self._help.add(self._help_option) elif moz_logger: handler = logging.FileHandler('config.log', mode='w', delay=True) handler.setFormatter(formatter) logger.addHandler(handler)
def __init__(self, config, environ=os.environ, argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr, logger=None): dict.__setitem__(self, '__builtins__', self.BUILTINS) self._paths = [] self._all_paths = set() self._templates = set() # Store the real function and its dependencies, behind each # DependsFunction generated from @depends. self._depends = {} self._seen = set() # Store the @imports added to a given function. self._imports = {} self._options = OrderedDict() # Store raw option (as per command line or environment) for each Option self._raw_options = OrderedDict() # Store options added with `imply_option`, and the reason they were # added (which can either have been given to `imply_option`, or # inferred. Their order matters, so use a list. self._implied_options = [] # Store all results from _prepare_function self._prepared_functions = set() # Queue of functions to execute, with their arguments self._execution_queue = [] self._helper = CommandLineHelper(environ, argv) assert isinstance(config, dict) self._config = config if logger is None: logger = moz_logger = logging.getLogger('moz.configure') logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(levelname)s: %(message)s') handler = ConfigureOutputHandler(stdout, stderr) handler.setFormatter(formatter) queue_debug = handler.queue_debug logger.addHandler(handler) else: assert isinstance(logger, logging.Logger) moz_logger = None @contextmanager def queue_debug(): yield # Some callers will manage to log a bytestring with characters in it # that can't be converted to ascii. Make our log methods robust to this # by detecting the encoding that a producer is likely to have used. encoding = getpreferredencoding() def wrapped_log_method(logger, key): method = getattr(logger, key) if not encoding: return method def wrapped(*args, **kwargs): out_args = [ arg.decode(encoding) if isinstance(arg, str) else arg for arg in args ] return method(*out_args, **kwargs) return wrapped log_namespace = { k: wrapped_log_method(logger, k) for k in ('debug', 'info', 'warning', 'error') } log_namespace['queue_debug'] = queue_debug self.log_impl = ReadOnlyNamespace(**log_namespace) self._help = None self._help_option = self.option_impl('--help', help='print this message') self._seen.add(self._help_option) if self._value_for(self._help_option): self._help = HelpFormatter(argv[0]) self._help.add(self._help_option) elif moz_logger: handler = logging.FileHandler('config.log', mode='w', delay=True) handler.setFormatter(formatter) logger.addHandler(handler)