Skip to content

Commit

Permalink
upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Apr 16, 2024
1 parent b2ddc52 commit 09d475d
Show file tree
Hide file tree
Showing 11 changed files with 473 additions and 349 deletions.
2 changes: 1 addition & 1 deletion profiwiki/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__="0.1.6"
__version__ = "0.1.6"
73 changes: 38 additions & 35 deletions profiwiki/docker.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
'''
"""
Created on 2023-04-01
@author: wf
'''
"""
import tempfile

from mwdocker.docker import DockerContainer
from python_on_whales import DockerException
import tempfile

class ProfiWikiContainer():

class ProfiWikiContainer:
"""
a profiwiki docker container wrapper
"""
def __init__(self,dc:DockerContainer):

def __init__(self, dc: DockerContainer):
"""
Args:
dc(DockerContainer): the to wrap
"""
self.dc=dc
def log_action(self,action:str):
self.dc = dc

def log_action(self, action: str):
"""
log the given action
Args:
action(str): the d
"""
if self.dc:
print(f"{action} {self.dc.kind} {self.dc.name}",flush=True)
print(f"{action} {self.dc.kind} {self.dc.name}", flush=True)
else:
print(f"{action}",flush=True)
def upload(self,text:str,path:str):
print(f"{action}", flush=True)

def upload(self, text: str, path: str):
"""
upload the given text to the given path
"""
with tempfile.NamedTemporaryFile() as tmp:
self.log_action(f"uploading {tmp.name} as {path} to ")
with open(tmp.name,"w") as text_file:
with open(tmp.name, "w") as text_file:
text_file.write(text)
self.dc.container.copy_to(tmp.name,path)
def killremove(self,volumes:bool=False):
self.dc.container.copy_to(tmp.name, path)

def killremove(self, volumes: bool = False):
"""
kill and remove me
Args:
volumes(bool): if True remove anonymous volumes associated with the container, default=True (to avoid e.g. passwords to get remembered / stuck
"""
Expand All @@ -55,42 +58,42 @@ def killremove(self,volumes:bool=False):
def start_cron(self):
"""
Starting periodic command scheduler: cron.
"""
self.dc.container.execute(["/usr/sbin/service","cron","start"],tty=True)
"""
self.dc.container.execute(["/usr/sbin/service", "cron", "start"], tty=True)

def install_plantuml(self):
"""
install plantuml to this container
"""
script="""#!/bin/bash
script = """#!/bin/bash
# install plantuml
# WF 2023-05-01
apt-get update
apt-get install -y plantuml
"""
# https://gabrieldemarmiesse.github.io/python-on-whales/docker_objects/containers/
script_path="/root/install_plantuml.sh"
script_path = "/root/install_plantuml.sh"
self.install_and_run_script(script, script_path)
pass
def install_and_run_script(self,script:str,script_path:str):

def install_and_run_script(self, script: str, script_path: str):
"""
install and run the given script
Args:
script(str): the source code of the script
script_path(str): the path to copy the script to and then execute
"""
self.upload(script,script_path)
self.upload(script, script_path)
# make executable
self.dc.container.execute(["chmod","+x",script_path])
self.dc.container.execute([script_path],tty=True)
self.dc.container.execute(["chmod", "+x", script_path])
self.dc.container.execute([script_path], tty=True)

def install_fontawesome(self):
"""
install fontawesome to this container
"""
script="""#!/bin/bash
script = """#!/bin/bash
# install fontawesome
# WF 2023-01-25
version=6.4.0
Expand All @@ -116,12 +119,12 @@ def install_fontawesome(self):
EOS
a2enconf font-awesome
"""
script_path="/root/install_fontawesome"
script_path = "/root/install_fontawesome"
self.install_and_run_script(script, script_path)
try:
self.dc.container.execute(["service","apache2","restart"])
self.dc.container.execute(["service", "apache2", "restart"])
except DockerException as e:
# we expect a SIGTERM
if not e.return_code==143:
if not e.return_code == 143:
raise e
pass
pass
32 changes: 17 additions & 15 deletions profiwiki/patch.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
'''
"""
Created on 2023-04-09
@author: wf
'''
"""
import re


class Patch:
"""
A class for patch a text file
"""

def __init__(self, file_path: str):
"""
Initializes a Patch instance with the file path file to be patched.
Args:
file_path (str): The file path of the PHP file to be patched.
"""
self.lines=[]
self.lines = []
self.file_path = file_path
# https://stackoverflow.com/a/3277516/1497139
with open(self.file_path, 'r', encoding='UTF-8') as file:
with open(self.file_path, "r", encoding="UTF-8") as file:
while line := file.readline():
self.lines.append(line.rstrip())

def save(self):
"""
save my lines
"""
with open(self.file_path, 'w') as f:
with open(self.file_path, "w") as f:
for line in self.lines:
f.write(f"{line}\n")

def patch_mediawiki_config_var(self, var_name: str, var_value: str) -> None:
"""
Patches a MediaWiki configuration variable in the PHP file with the given name and value.
Expand All @@ -50,21 +52,21 @@ def patch_mediawiki_config_var(self, var_name: str, var_value: str) -> None:

# Use fileinput to replace the matched line in the file
for i, line in enumerate(self.lines):
new_line=(re.sub(pattern, replacement, line))
self.lines[i]=new_line
def add_text(self,text:str,avoid_duplication:bool=True):
new_line = re.sub(pattern, replacement, line)
self.lines[i] = new_line

def add_text(self, text: str, avoid_duplication: bool = True):
"""
Adds text avoiding duplication if specified
Args:
text (str): the text to add
avoid_duplication(bool): if True avoid duplication of existing lines
"""
new_lines=text.split("\n")
new_lines = text.split("\n")
for new_line in new_lines:
do_add=True
do_add = True
if avoid_duplication:
do_add=not new_line in self.lines
do_add = not new_line in self.lines
if do_add:
self.lines.append(new_line)
self.lines.append(new_line)
Loading

0 comments on commit 09d475d

Please sign in to comment.