Debian Codegraph @

testing/clean_skipfails.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 import logging
6 import os
7 import sys
8 from pathlib import Path
9 from typing import List, Optional, Set
10
11 from manifestparser.manifestparser import ManifestParser
12 from manifestparser.toml import alphabetize_toml_str, remove_skip_if
13 from moztest.resolve import TestResolver
14 from tomlkit.toml_document import TOMLDocument
15
16 ERROR = "error"
17
18
19 class CleanSkipfails:
20
21 def __init__(
22 self,
23 command_context=None,
24 manifest_search_path: str = "",
25 os_name: Optional[str] = None,
26 os_version: Optional[str] = None,
27 processor: Optional[str] = None,
28 ) -> None:
29 self.command_context = command_context
30 if self.command_context is not None:
31 self.topsrcdir = self.command_context.topsrcdir
32 else:
33 self.topsrcdir = Path(__file__).parent.parent
34 self.topsrcdir = os.path.normpath(self.topsrcdir)
35 self.component = "clean-skip-fails"
36
37 self.manifest_search_path = manifest_search_path
38 self.os_name = os_name
39 self.os_version = os_version
40 self.processor = processor
41
42 def error(self, e):
43 if self.command_context is not None:
44 self.command_context.log(
45 logging.ERROR, self.component, {ERROR: str(e)}, "ERROR: {error}"
46 )
47 else:
48 print(f"ERROR: {e}", file=sys.stderr, flush=True)
49
50 def info(self, e):
51 if self.command_context is not None:
52 self.command_context.log(
53 logging.INFO, self.component, {ERROR: str(e)}, "INFO: {error}"
54 )
55 else:
56 print(f"INFO: {e}", file=sys.stderr, flush=True)
57
58 def full_path(self, filename: str):
59 """Returns full path for the relative filename"""
60
61 return os.path.join(self.topsrcdir, os.path.normpath(filename.split(":")[-1]))
62
63 def isdir(self, filename: str):
64 """Returns True if filename is a directory"""
65
66 return os.path.isdir(self.full_path(filename))
67
68 def run(self):
69 if self.os_name is None and self.os_version is None and self.processor is None:
70 self.error("Needs at least --os, --os_version or --processor to be set.")
71 return
72
73 manifest_path_set = self.get_manifest_paths()
74 parser = ManifestParser(use_toml=True, document=True)
75 for manifest_path in manifest_path_set:
76 parser.read(manifest_path)
77 manifest: TOMLDocument = parser.source_documents[
78 os.path.abspath(manifest_path)
79 ]
80 has_removed_items = remove_skip_if(
81 manifest, self.os_name, self.os_version, self.processor
82 )
83 manifest_str = alphabetize_toml_str(manifest)
84 if len(manifest_str) > 0:
85 fp = open(manifest_path, "w", encoding="utf-8", newline="\n")
86 fp.write(manifest_str)
87 fp.close()
88 removed_condition: List[str] = []
89 if self.os_name is not None:
90 removed_condition.append(f"'os == {self.os_name}'")
91 if self.os_version is not None:
92 removed_condition.append(f"'os_version == {self.os_version}'")
93 if self.processor is not None:
94 removed_condition.append(f"'processor == {self.processor}'")
95 if has_removed_items:
96 self.info(
97 f'Removed skip-if conditions for {", ".join(removed_condition)} in manifest: "{manifest_path}"'
98 )
99 else:
100 self.info(
101 f'Did not find skip-if conditions to remove for {", ".join(removed_condition)} in manifest: "{manifest_path}"'
102 )
103
104 def get_manifest_paths(self) -> Set[str]:
105 resolver = TestResolver.from_environment(cwd=self.manifest_search_path)
106 if self.isdir(self.manifest_search_path):
107 tests = list(resolver.resolve_tests(paths=[self.manifest_search_path]))
108 manifest_paths = set(t["manifest"] for t in tests)
109 else:
110 myPath = Path(".").parent
111 manifest_paths = set(
112 [
113 str(x).replace("\\", "/")
114 for x in myPath.glob(self.manifest_search_path)
115 ]
116 )
117 return manifest_paths