X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Ftools%2Fpresentation%2Fenvironment.py;h=ea4d94ad702917739c7da89ca5e745d34e78ff3f;hp=05376e0e095ef48dc21b45be128fbc859ca5d5d0;hb=95de51db79d23e57968e0d27a2a4458326a59639;hpb=eecad36d7d2275fa47fbcab40dbcf56108ab0a51 diff --git a/resources/tools/presentation/environment.py b/resources/tools/presentation/environment.py index 05376e0e09..ea4d94ad70 100644 --- a/resources/tools/presentation/environment.py +++ b/resources/tools/presentation/environment.py @@ -1,4 +1,4 @@ -# Copyright (c) 2017 Cisco and/or its affiliates. +# Copyright (c) 2018 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -21,10 +21,10 @@ import os import shutil import logging -from errors import PresentationError +from pal_errors import PresentationError -class Environment(object): +class Environment: """Setting of the environment: - set environment variables, - create directories. @@ -51,35 +51,6 @@ class Environment(object): """ return self._env - def _set_environment_variables(self): - """Set environment variables. - """ - logging.info("Setting the environment variables ...") - # logging.debug("Environment variables before:\n{}".format(os.environ)) - - count = 1 - - for var, value in self._env["configuration"].items(): - logging.debug(" {:3d} Setting the variable {} = {}". - format(count, var, value)) - os.environ[var] = str(value) - count += 1 - - for var, value in self._env["paths"].items(): - logging.debug(" {:3d} Setting the variable {} = {}". - format(count, var, value)) - os.environ[var] = str(value) - count += 1 - - for var, value in self._env["urls"].items(): - logging.debug(" {:3d} Setting the variable {} = {}". - format(count, var, value)) - os.environ[var] = str(value) - count += 1 - - # logging.debug("Environment variables after:\n{}".format(os.environ)) - logging.info("Done.") - def _make_dirs(self): """Create the directories specified in the 'make-dirs' part of 'environment' section in the specification file. @@ -89,40 +60,42 @@ class Environment(object): """ if self._force: - logging.info("Removing old build(s) ...") - for directory in self._env["build-dirs"]: - dir_to_remove = self._env["paths"][directory] + logging.info(u"Removing old build(s) ...") + for directory in self._env[u"build-dirs"]: + dir_to_remove = self._env[u"paths"][directory] if os.path.isdir(dir_to_remove): try: shutil.rmtree(dir_to_remove) - logging.info(" Removed: {}".format(dir_to_remove)) + logging.info(f" Removed: {dir_to_remove}") except OSError: - raise PresentationError("Cannot remove the directory " - "'{}'".format(dir_to_remove)) - logging.info("Done.") + raise PresentationError( + f"Cannot remove the directory {dir_to_remove}" + ) + logging.info(u"Done.") - logging.info("Making directories ...") + logging.info(u"Making directories ...") - for directory in self._env["make-dirs"]: - dir_to_make = self._env["paths"][directory] + for directory in self._env[u"make-dirs"]: + dir_to_make = self._env[u"paths"][directory] try: if os.path.isdir(dir_to_make): - logging.warning("The directory '{}' exists, skipping.". - format(dir_to_make)) + logging.warning( + f"The directory {dir_to_make} exists, skipping." + ) else: os.makedirs(dir_to_make) - logging.info(" Created: {}".format(dir_to_make)) + logging.info(f" Created: {dir_to_make}") except OSError: - raise PresentationError("Cannot make the directory '{}'". - format(dir_to_make)) + raise PresentationError( + f"Cannot make the directory {dir_to_make}" + ) - logging.info("Done.") + logging.info(u"Done.") def set_environment(self): """Set the environment. """ - self._set_environment_variables() self._make_dirs() @@ -131,27 +104,26 @@ def clean_environment(env): :param env: Environment specification. :type env: dict - :raises: PresentationError if it is not possible to remove a directory. """ - logging.info("Cleaning the environment ...") + logging.info(u"Cleaning the environment ...") - if not env["remove-dirs"]: # None or empty - logging.info(" No directories to remove.") + if not env[u"remove-dirs"]: # None or empty + logging.info(u" No directories to remove.") return - for directory in env["remove-dirs"]: - dir_to_remove = env["paths"][directory] - logging.info(" Removing the working directory {} ...". - format(dir_to_remove)) + for directory in env[u"remove-dirs"]: + dir_to_remove = env[u"paths"][directory] + logging.info(f" Removing the working directory {dir_to_remove} ...") if os.path.isdir(dir_to_remove): try: shutil.rmtree(dir_to_remove) - except OSError: - raise PresentationError("Cannot remove the directory '{}'". - format(dir_to_remove)) + except OSError as err: + logging.warning( + f"Cannot remove the directory {dir_to_remove}" + ) + logging.debug(str(err)) else: - logging.warning("The directory '{}' does not exist.". - format(dir_to_remove)) + logging.warning(f"The directory {dir_to_remove} does not exist.") - logging.info("Done.") + logging.info(u"Done.")