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 errors import PresentationError
27 class Environment(object):
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("Removing old build(s) ...")
64 for directory in self._env["build-dirs"]:
65 dir_to_remove = self._env["paths"][directory]
66 if os.path.isdir(dir_to_remove):
68 shutil.rmtree(dir_to_remove)
69 logging.info(" Removed: {}".format(dir_to_remove))
71 raise PresentationError("Cannot remove the directory "
72 "'{}'".format(dir_to_remove))
75 logging.info("Making directories ...")
77 for directory in self._env["make-dirs"]:
78 dir_to_make = self._env["paths"][directory]
80 if os.path.isdir(dir_to_make):
81 logging.warning("The directory '{}' exists, skipping.".
84 os.makedirs(dir_to_make)
85 logging.info(" Created: {}".format(dir_to_make))
87 raise PresentationError("Cannot make the directory '{}'".
92 def set_environment(self):
93 """Set the environment.
99 def clean_environment(env):
100 """Clean the environment.
102 :param env: Environment specification.
104 :raises: PresentationError if it is not possible to remove a directory.
107 logging.info("Cleaning the environment ...")
109 if not env["remove-dirs"]: # None or empty
110 logging.info(" No directories to remove.")
113 for directory in env["remove-dirs"]:
114 dir_to_remove = env["paths"][directory]
115 logging.info(" Removing the working directory {} ...".
116 format(dir_to_remove))
117 if os.path.isdir(dir_to_remove):
119 shutil.rmtree(dir_to_remove)
120 except OSError as err:
121 logging.warning("Cannot remove the directory '{}'".
122 format(dir_to_remove))
123 logging.debug(str(err))
125 logging.warning("The directory '{}' does not exist.".
126 format(dir_to_remove))
128 logging.info("Done.")