pymtt
 All Classes Namespaces Files Functions Variables Groups
Copytree.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from builtins import str
3 #!/usr/bin/env python
4 #
5 # Copyright (c) 2015-2018 Intel, Inc. All rights reserved.
6 # $COPYRIGHT$
7 #
8 # Additional copyrights may follow
9 #
10 # $HEADER$
11 #
12 
13 import shutil
14 import distutils.dir_util
15 import os
16 from BaseMTTUtility import *
17 
18 from sys import version_info
19 if version_info[0] == 3:
20  if version_info[1] <= 3:
21  from imp import reload
22  else :
23  from importlib import reload
24 ## @addtogroup Utilities
25 # @{
26 # @section Copytree
27 # Copy a directory tree from source to the same relative loation under the MTT scratch directory
28 # @param src The top directory of the tree to be copied
29 # @param preserve_symlinks Preserve symlinks instead of copying the contents
30 # @param preserve_directory Copies directory instead of contents
31 # @}
33  def __init__(self):
34  BaseMTTUtility.__init__(self)
35  self.options = {}
36  self.options['src'] = (None, "The top directory of the tree to be copied")
37  self.options['preserve_symlinks'] = ("0", "Preserve symlinks instead of copying the contents")
38  self.options['preserve_directory'] = ("0", "Copies directory instead of contents")
39 
40  def print_name(self):
41  return "Copytree"
42 
43  def print_options(self, testDef, prefix):
44  lines = testDef.printOptions(self.options)
45  for line in lines:
46  print(prefix + line)
47  return
48 
49  def execute(self, log, keyvals, testDef):
50  testDef.logger.verbose_print("Copytree Execute")
51  # parse any provided options - these will override the defaults
52  cmds = {}
53  testDef.parseOptions(log, self.options, keyvals, cmds)
54  # if they didn't provide a src, then we can't do anything
55 
56  try:
57  if cmds['src'] is None:
58  log['status'] = 1
59  log['stderr'] = "Src directory not specified"
60  return
61  except KeyError:
62  log['status'] = 1
63  log['stderr'] = "Src directory not specified"
64  return
65  # define the dst directory
66  dst = os.path.join(testDef.options['scratchdir'], log['section'].replace(":","_"))
67  # record the location
68  log['location'] = dst
69  # Check if already exists to skip if ASIS is set
70  try:
71  if cmds['asis'] and os.path.exists(dst) and os.path.isdir(dst):
72  testDef.logger.verbose_print("As-Is location " + dst + " exists and is a directory")
73  log['status'] = 0
74  return
75  except KeyError:
76  pass
77  # perform the copy
78  try:
79  # Cleanup the target directory if it exists
80  if os.path.exists(dst):
81  shutil.rmtree(dst)
82  os.mkdir(dst)
83  for srcpath in cmds['src'].split(','):
84  srcpath = srcpath.strip()
85  reload(distutils.dir_util)
86  if cmds['preserve_directory'] != "0":
87  subdst = os.path.join(dst,os.path.basename(srcpath))
88  if os.path.exists(subdst):
89  shutil.rmtree(subdst)
90  os.mkdir(subdst)
91  distutils.dir_util.copy_tree(srcpath, subdst, preserve_symlinks=int(cmds['preserve_symlinks']))
92  else:
93  distutils.dir_util.copy_tree(srcpath, dst, preserve_symlinks=int(cmds['preserve_symlinks']))
94  log['status'] = 0
95  except (os.error, shutil.Error, \
96  distutils.errors.DistutilsFileError) as e:
97  log['status'] = 1
98  log['stderr'] = str(e)
99  return