146b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang#!/usr/bin/python
246b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang# Copyright 2015 The Chromium Authors. All rights reserved.
346b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang# Use of this source code is governed by a BSD-style license that can be
446b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang# found in the LICENSE file.
546b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang
646b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wangimport contextlib
746b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wangimport os
88d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoiimport shutil
946b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wangimport tempfile
1046b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang
1146b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang
1246b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang@contextlib.contextmanager
138d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoidef TempDeploymentDir(paths, use_symlinks=True):
1446b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang  """Sets up and tears down a directory for deploying an app."""
158d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi  if use_symlinks:
168d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi    link_func = os.symlink
178d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi  else:
188d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi    link_func = _Copy
198d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi
2046b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang  try:
2146b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang    deployment_dir = tempfile.mkdtemp(prefix='deploy-')
228d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi    _PopulateDeploymentDir(deployment_dir, paths, link_func)
2346b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang    yield deployment_dir
2446b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang  finally:
258d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi    shutil.rmtree(deployment_dir)
2646b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang
2746b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang
288d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoidef _Copy(src, dst):
298d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi  if os.path.isdir(src):
308d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi    shutil.copytree(src, dst)
318d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi  else:
328d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi    shutil.copy2(src, dst)
3346b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang
3446b43bff003ceda46cf9a5d40a47f7674996d2e0Zhen Wang
358d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoidef _PopulateDeploymentDir(deployment_dir, paths, link_func):
368d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi  """Fills the deployment directory using the link_func specified."""
378d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi  for path in paths:
388d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi    destination = os.path.join(deployment_dir, os.path.basename(path))
398d2b206a675ec20ea07100c35df34e65ee1e45e8Ruchi Kandoi    link_func(path, destination)
40