|
14 | 14 | # limitations under the License. |
15 | 15 | # |
16 | 16 | import logging |
17 | | -import os |
18 | | -import re |
19 | | -from collections import namedtuple |
20 | 17 | from pathlib import Path |
21 | | -from typing import Optional |
| 18 | +from typing import Callable |
22 | 19 |
|
23 | 20 | logger = logging.getLogger("ucc_gen") |
24 | | -FileUpdater = namedtuple("FileUpdater", ["path_segments", "function"]) |
25 | 21 |
|
| 22 | +internal_root_dir = Path(__file__).resolve().parent |
26 | 23 |
|
27 | | -_base_html_pattern = re.compile( |
28 | | - r"<script\s+src=\"\${make_url\(page_path\)}\"\s*>\s*</script>" |
29 | | -) |
30 | 24 |
|
| 25 | +def _load_canonical_template(template_name: str) -> str: |
| 26 | + return ( |
| 27 | + internal_root_dir / "package" / "appserver" / "templates" / template_name |
| 28 | + ).read_text() |
31 | 29 |
|
32 | | -def _handle_base_html_update(content: str) -> Optional[str]: |
33 | | - matches = _base_html_pattern.findall(content) |
34 | 30 |
|
35 | | - if not matches: |
36 | | - return None |
| 31 | +def _is_legacy_base_html(content: str) -> bool: |
| 32 | + legacy_markers = ( |
| 33 | + "cherrypy.request.path_info", |
| 34 | + "${make_url('/config?autoload=1')}", |
| 35 | + "__splunkd_partials__ = ${json_decode(splunkd)};", |
| 36 | + '<script type="module" src="${make_url(page_path)}"></script>', |
| 37 | + '<script src="${make_url(page_path)}"></script>', |
| 38 | + 'page_path = "/static/app/" + app_name + "/js/build/entry_page.js"', |
| 39 | + ) |
| 40 | + return any(marker in content for marker in legacy_markers) |
37 | 41 |
|
38 | | - for result in matches: |
39 | | - content = content.replace( |
40 | | - result, '<script type="module" src="${make_url(page_path)}"></script>' |
41 | | - ) |
42 | 42 |
|
43 | | - return content |
| 43 | +def _is_legacy_redirect_html(content: str) -> bool: |
| 44 | + legacy_markers = ( |
| 45 | + "cherrypy.request.path_info", |
| 46 | + "${make_url('/config?autoload=1')}", |
| 47 | + "__splunkd_partials__ = ${json_decode(splunkd)};", |
| 48 | + "${ta.name}", |
| 49 | + "${ta.version}", |
| 50 | + 'page_path = "/static/app/" + app_name + "/js/build/${ta.name}_redirect_page.${ta.version}.js"', |
| 51 | + ) |
| 52 | + return any(marker in content for marker in legacy_markers) |
44 | 53 |
|
45 | 54 |
|
46 | | -def handle_package_files_update(path: str) -> None: |
47 | | - files_to_update = [ |
48 | | - FileUpdater(("appserver", "templates", "base.html"), _handle_base_html_update) |
49 | | - ] |
| 55 | +def _migrate_template_if_needed( |
| 56 | + package_dir: str, |
| 57 | + relative_path: str, |
| 58 | + template_name: str, |
| 59 | + detector: Callable[[str], bool], |
| 60 | +) -> None: |
| 61 | + file_path = Path(package_dir) / relative_path |
| 62 | + if not file_path.is_file(): |
| 63 | + return |
50 | 64 |
|
51 | | - for file_updater in files_to_update: |
52 | | - relative_path = os.path.join(*file_updater.path_segments) |
53 | | - file_path = os.path.join(path, *file_updater.path_segments) |
| 65 | + current_content = file_path.read_text() |
| 66 | + if current_content == _load_canonical_template(template_name): |
| 67 | + return |
54 | 68 |
|
55 | | - if not os.path.isfile(file_path): |
56 | | - continue |
| 69 | + if detector(current_content): |
| 70 | + file_path.write_text(_load_canonical_template(template_name)) |
| 71 | + logger.info( |
| 72 | + "File '%s' exists in the package directory and was updated to the " |
| 73 | + "current static UCC template to remove legacy Mako/CherryPy usage.", |
| 74 | + relative_path, |
| 75 | + ) |
| 76 | + return |
57 | 77 |
|
58 | | - path_obj = Path(file_path) |
59 | | - original_content = path_obj.read_text() |
60 | | - output_content = file_updater.function(original_content) |
| 78 | + logger.warning( |
| 79 | + "File '%s' exists in the package directory and uses a custom template. " |
| 80 | + "UCC left it unchanged because it could not be safely migrated " |
| 81 | + "automatically.", |
| 82 | + relative_path, |
| 83 | + ) |
61 | 84 |
|
62 | | - if output_content is None or original_content == output_content: |
63 | | - continue |
64 | 85 |
|
65 | | - path_obj.write_text(output_content) |
66 | | - logger.info( |
67 | | - f"File '{relative_path}' exists in the package directory and its content needed to be updated by UCC." |
68 | | - ) |
| 86 | +def handle_package_files_update(path: str) -> None: |
| 87 | + _migrate_template_if_needed( |
| 88 | + path, |
| 89 | + "appserver/templates/base.html", |
| 90 | + "base.html", |
| 91 | + _is_legacy_base_html, |
| 92 | + ) |
| 93 | + _migrate_template_if_needed( |
| 94 | + path, |
| 95 | + "appserver/templates/redirect.html", |
| 96 | + "redirect.html", |
| 97 | + _is_legacy_redirect_html, |
| 98 | + ) |
0 commit comments