Skip to content

Commit e442620

Browse files
committed
refactor(logs): introduce FileWrapper
1 parent 614984b commit e442620

File tree

2 files changed

+81
-23
lines changed

2 files changed

+81
-23
lines changed

mwutil/files.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import re
2+
import subprocess
3+
from abc import ABC
4+
5+
from mwutil.exec import run_container_command
6+
from mwutil.local_config import MWUtilConfig
7+
8+
9+
def compile_path_template(config: MWUtilConfig, template: str) -> str:
10+
replacements = {
11+
"base": config.basedir.absolute(),
12+
"config": config.configdir.absolute(),
13+
"core": config.coredir.absolute(),
14+
"dumps": config.dumpdir.absolute(),
15+
}
16+
17+
return template.format(**replacements)
18+
19+
class FileWrapper(ABC):
20+
21+
@staticmethod
22+
def from_path(config: MWUtilConfig, path_template: str) -> 'FileWrapper':
23+
path = compile_path_template(config, path_template)
24+
pattern = re.compile("^([A-Za-z0-9+.-]+)://")
25+
match = pattern.match(path)
26+
if not match:
27+
return HostFile(path)
28+
else:
29+
container_name = match.group(1)
30+
container_path = path[len(container_name) + 3:]
31+
return ContainerFile(container_name, container_path, config)
32+
33+
def read(self):
34+
raise NotImplementedError("Subclasses must implement this method")
35+
36+
def stream_to_stdout(self):
37+
raise NotImplementedError("Subclasses must implement this method")
38+
39+
40+
class HostFile(FileWrapper):
41+
def __init__(self, path: str):
42+
self.path = path
43+
44+
def read(self):
45+
with open(self.path, 'r') as file:
46+
return file.read()
47+
48+
def stream_to_stdout(self):
49+
subprocess.run(["cat", self.path], check=True)
50+
51+
class ContainerFile(FileWrapper):
52+
def __init__(self, container_name: str, path: str, config: MWUtilConfig):
53+
self.container_name = container_name
54+
self.path = path
55+
self.config = config
56+
57+
def read(self):
58+
result = run_container_command(
59+
self.config,
60+
['cat', self.path],
61+
container_name=self.container_name,
62+
capture_output=True,
63+
text=True,
64+
exec_options=["-u", "root"]
65+
)
66+
if result.returncode != 0:
67+
raise Exception(f"Failed to read file {self.path} from container {self.container_name}")
68+
return result.stdout
69+
70+
def stream_to_stdout(self):
71+
run_container_command(
72+
self.config,
73+
['cat', self.path],
74+
container_name=self.container_name,
75+
capture_output=False,
76+
text=True,
77+
exec_options=["-u", "root"]
78+
)

mwutil/modules/logs.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import subprocess
21
from argparse import ArgumentParser, Namespace
3-
from pathlib import Path
42

3+
from mwutil.files import FileWrapper
54
from mwutil.local_config import MWUtilConfig
65
from mwutil.module import MWUtilModule
76

@@ -26,27 +25,8 @@ def populate_subparser(self, parser: ArgumentParser, config: MWUtilConfig):
2625
choices=self.FILES.keys()
2726
)
2827

29-
parser.add_argument(
30-
"-m",
31-
"--method",
32-
type=str,
33-
choices=["cat", "python"],
34-
default="cat"
35-
)
36-
3728
def execute(self, config: MWUtilConfig, args: Namespace):
3829
file = args.file
39-
file_path = self.get_file_path(config, file)
40-
if args.method == "cat":
41-
subprocess.run(["cat", file_path], check=True)
42-
elif args.method == "python":
43-
with open(file_path, "r", encoding="utf-8") as f:
44-
print(f.read())
45-
46-
def get_file_path(self, config: MWUtilConfig, file: str) -> Path:
47-
replacements = {
48-
"core": config.coredir.absolute()
49-
}
50-
5130
template = self.FILES.get(file)
52-
return Path(template.format(**replacements))
31+
file = FileWrapper.from_path(config, template)
32+
file.stream_to_stdout()

0 commit comments

Comments
 (0)