1#!/usr/bin/python 2# Copyright 2015 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import contextlib 7import os 8import shutil 9import tempfile 10 11 12@contextlib.contextmanager 13def TempDeploymentDir(paths, use_symlinks=True): 14 """Sets up and tears down a directory for deploying an app.""" 15 if use_symlinks: 16 link_func = os.symlink 17 else: 18 link_func = _Copy 19 20 try: 21 deployment_dir = tempfile.mkdtemp(prefix='deploy-') 22 _PopulateDeploymentDir(deployment_dir, paths, link_func) 23 yield deployment_dir 24 finally: 25 shutil.rmtree(deployment_dir) 26 27 28def _Copy(src, dst): 29 if os.path.isdir(src): 30 shutil.copytree(src, dst) 31 else: 32 shutil.copy2(src, dst) 33 34 35def _PopulateDeploymentDir(deployment_dir, paths, link_func): 36 """Fills the deployment directory using the link_func specified.""" 37 for path in paths: 38 destination = os.path.join(deployment_dir, os.path.basename(path)) 39 link_func(path, destination) 40