Ejemplo n.º 1
0
 def _status(self, directory, path):
     global state
     if state is None:
         state = library_state.BzrLibraryState(ui=ui.SilentUIFactory,
                                               trace=trace.DefaultConfig())
     buf = CoerceIO()
     w = workingtree.WorkingTree.open(directory)
     status.show_tree_status(w,
                             specific_files=[path] if path else None,
                             to_file=buf,
                             short=True)
     raw = buf.getvalue()
     if not raw.strip():
         return
     if path:
         ans = raw[:2]
         if ans == 'I ':  # Ignored
             ans = None
         return ans
     dirtied = untracked = ' '
     for line in raw.splitlines():
         if len(line) > 1 and line[1] in 'ACDMRIN':
             dirtied = 'D'
         elif line and line[0] == '?':
             untracked = 'U'
     ans = dirtied + untracked
     return ans if ans.strip() else None
 def test_trace_context(self):
     tracer = fixtures.RecordingContextManager()
     ui = _mod_ui.SilentUIFactory()
     state = library_state.BzrLibraryState(ui=ui, trace=tracer)
     state.__enter__()
     try:
         self.assertEqual(['__enter__'], tracer._calls)
     finally:
         state.__exit__(None, None, None)
         self.assertEqual(['__enter__', '__exit__'], tracer._calls)
 def test_ui_is_used(self):
     ui = _mod_ui.SilentUIFactory()
     state = library_state.BzrLibraryState(
         ui=ui, trace=fixtures.RecordingContextManager())
     orig_ui = _mod_ui.ui_factory
     state.__enter__()
     try:
         self.assertEqual(ui, _mod_ui.ui_factory)
     finally:
         state.__exit__(None, None, None)
         self.assertEqual(orig_ui, _mod_ui.ui_factory)
Ejemplo n.º 4
0
def initialize(setup_ui=True, stdin=None, stdout=None, stderr=None):
    """Set up everything needed for normal use of bzrlib.

    Most applications that embed bzrlib, including bzr itself, should call
    this function to initialize various subsystems.  

    More options may be added in future so callers should use named arguments.

    The object returned by this function can be used as a contex manager
    through the 'with' statement to automatically shut down when the process
    is finished with bzrlib.  However (from bzr 2.4) it's not necessary to
    separately enter the context as well as starting bzr: bzrlib is ready to
    go when this function returns.

    :param setup_ui: If true (default) use a terminal UI; otherwise 
        some other ui_factory must be assigned to `bzrlib.ui.ui_factory` by
        the caller.
    :param stdin, stdout, stderr: If provided, use these for terminal IO;
        otherwise use the files in `sys`.
    :return: A context manager for the use of bzrlib. The __exit__
        should be called by the caller before exiting their process or
        otherwise stopping use of bzrlib. Advanced callers can use
        BzrLibraryState directly.
    """
    from bzrlib import library_state, trace
    if setup_ui:
        import bzrlib.ui
        stdin = stdin or sys.stdin
        stdout = stdout or sys.stdout
        stderr = stderr or sys.stderr
        ui_factory = bzrlib.ui.make_ui_for_terminal(stdin, stdout, stderr)
    else:
        ui_factory = None
    tracer = trace.DefaultConfig()
    state = library_state.BzrLibraryState(ui=ui_factory, trace=tracer)
    # Start automatically in case people don't realize this returns a context.
    state._start()
    return state