message: Support `--workdir`.
diff: @@ -149,21 +149,28 @@ class Drake:
  def __init__(self,
  root = None,
  jobs = None,
  + workdir = None,
  kill_builders_on_failure = False,
  use_mtime = None,
  adjust_mtime = None,
  adjust_mtime_future = None,
  adjust_mtime_second = None,
  ):
  - if root is None:
  - root = drake.Path('.')
  + root = Path(root) if root is not None else Path.dot
  + with contextlib.ExitStack() as ctx:
  + if workdir is not None:
  + self.__workdir = drake.Path(workdir)
  + ctx.enter_context(self.__workdir)
  + root = root.without_prefix(self.__workdir).canonize()
  + else:
  + self.__workdir = None
  self.__jobs = 1
  self.__jobs_lock = None
  self.__kill_builders_on_failure = kill_builders_on_failure
  self.__nodes = {}
  self.__prefix = drake.Path('.')
  self.__scheduler = Scheduler(policy = sched.DepthFirst())
  - self.__source = drake.Path(root)
  + self.__source = root
  self.__use_mtime = self.__option(
  'MTIME', True, use_mtime)
  self.__adjust_mtime = self.__option(
  @@ -229,6 +236,9 @@ class Drake:
  _OS.system(cmd)
  def run(self, *cfg, **kwcfg):
  + with contextlib.ExitStack() as ctx:
  + if self.__workdir is not None:
  + ctx.enter_context(self.__workdir)
  start = time.time()
  try:
  g = self.__globals
  @@ -241,6 +251,8 @@ class Drake:
  '-j' : lambda j: self.jobs_set(j),
  '--help' : help,
  '-h' : help,
  + '--workdir': lambda w: None,
  + '-w' : lambda w: None,
  '--complete-modes': complete_modes,
  '--complete-options': complete_options,
  '--complete-nodes': complete_nodes,
  @@ -1087,12 +1099,12 @@ class Path:
  pass
  def __enter__(self):
  - self.__saved_cwd.append(os.getcwd())
  - os.chdir(str(self))
  + self.__saved_cwd.append(_OS.getcwd())
  + _OS.chdir(str(self))
  return self
  def __exit__(self, exc_type, exc_value, traceback):
  - os.chdir(self.__saved_cwd.pop())
  + _OS.chdir(self.__saved_cwd.pop())
  Path.dot = Path('.')
  Path.dotdot = Path('..')
  @@ -3104,7 +3116,18 @@ def add_default_node(node):
  def run(root, *cfg, **kwcfg):
  - with Drake(root) as d:
  + args = sys.argv[1:]
  + workdir = None
  + for arg in args:
  + if workdir is True:
  + workdir = arg
  + if arg in ['--workdir', '-w']:
  + if workdir is not None:
  + raise Exception('duplicate option {}'.format(arg))
  + workdir = True
  + if workdir is True:
  + raise Exception('missing argument for option {}'.format(arg))
  + with Drake(root = root, workdir = workdir) as d:
  d.run(*cfg, **kwcfg)
  