1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
16 Setting of the environment according to the specification specified in the
17 specification YAML file.
24 from pal_errors import PresentationError
28 """Setting of the environment:
29 - set environment variables,
33 def __init__(self, env, force=False):
36 :param env: Environment specification.
37 :param force: If True, remove old build(s) if present.
46 def environment(self):
49 :returns: Environment settings.
55 """Create the directories specified in the 'make-dirs' part of
56 'environment' section in the specification file.
58 :raises: PresentationError if it is not possible to remove or create a
63 logging.info(u"Removing old build(s) ...")
64 for directory in self._env[u"build-dirs"]:
65 dir_to_remove = self._env[u"paths"][directory]
66 if os.path.isdir(dir_to_remove):
68 shutil.rmtree(dir_to_remove)
69 logging.info(f" Removed: {dir_to_remove}")
71 raise PresentationError(
72 f"Cannot remove the directory {dir_to_remove}"
74 logging.info(u"Done.")
76 logging.info(u"Making directories ...")
78 for directory in self._env[u"make-dirs"]:
79 dir_to_make = self._env[u"paths"][directory]
81 if os.path.isdir(dir_to_make):
83 f"The directory {dir_to_make} exists, skipping."
86 os.makedirs(dir_to_make)
87 logging.info(f" Created: {dir_to_make}")
89 raise PresentationError(
90 f"Cannot make the directory {dir_to_make}"
93 logging.info(u"Done.")
95 def set_environment(self):
96 """Set the environment.
102 def clean_environment(env):
103 """Clean the environment.
105 :param env: Environment specification.
109 logging.info(u"Cleaning the environment ...")
111 if not env[u"remove-dirs"]: # None or empty
112 logging.info(u" No directories to remove.")
115 for directory in env[u"remove-dirs"]:
116 dir_to_remove = env[u"paths"][directory]
117 logging.info(f" Removing the working directory {dir_to_remove} ...")
118 if os.path.isdir(dir_to_remove):
120 shutil.rmtree(dir_to_remove)
121 except OSError as err:
123 f"Cannot remove the directory {dir_to_remove}"
125 logging.debug(str(err))
127 logging.warning(f"The directory {dir_to_remove} does not exist.")
129 logging.info(u"Done.")