Line length: Fix recent merges
[csit.git] / resources / tools / presentation / static_content.py
1 # Copyright (c) 2021 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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Static content
15
16 Process the static content stored in the git.
17 """
18
19 import logging
20
21 from os import makedirs
22 from os.path import isdir
23 from shutil import rmtree, copytree, Error
24
25 from pal_errors import PresentationError
26
27
28 def prepare_static_content(spec):
29     """Prepare the static content which is stored in the git.
30
31     :param spec: Specification read from the specification file.
32     :type spec: Specification
33     :raises PresentationError: If it is not possible to process the static
34         content.
35     """
36
37     src = spec.static.get(u"src-path", None)
38     dst = spec.static.get(u"dst-path", None)
39     if src is None or dst is None:
40         logging.warning(u"No static content specified, skipping")
41         return
42
43     # Copy all the static content to the build directory:
44     logging.info(u"Copying the static content ...")
45     logging.info(f"  Source:      {src}")
46     logging.info(f"  Destination: {dst}")
47
48     try:
49         if isdir(dst):
50             rmtree(dst)
51
52         copytree(src, dst)
53
54         makedirs(spec.environment[u"paths"][u"DIR[WORKING,SRC,STATIC]"])
55
56     except (Error, OSError) as err:
57         raise PresentationError(
58             u"Not possible to process the static content.",
59             repr(err)
60         )
61
62     logging.info(u"Done.")