Added linux

This commit is contained in:
Antonio de la Rosa 2024-02-05 23:50:17 +01:00
parent 8cb53e725e
commit 1639c613ab
3 changed files with 136 additions and 7 deletions

98
pastafariutils/linux.py Normal file
View file

@ -0,0 +1,98 @@
#!/usr/bin/epython3
import os
import distro
from subprocess import call, DEVNULL
import re
import json
def shell_command(command):
if call(command, shell=True) > 0:
print('Error: cannot execute command')
return False
def check_distro(arr_command):
distro_id=distro.id()
if not distro_id in arr_command:
print("Sorry, you don't have a patch for this distro\n\n")
return False
else:
return distro_id
def install_package(package):
distro_id=distro.id()
if distro_id=='debian' or distro_id=='ubuntu':
return shell_command('sudo DEBIAN_FRONTEND="noninteractive" apt-get install -y {}'.format(package[distro]))
elif distro_id=='fedora' or distro_id=='almalinux' or distro_id=='rocky':
return shell_command('sudo dnf install -y {}'.format(package[distro]))
elif distro_id=='arch':
return shell_command('sudo pacman -S --noconfirm {}'.format(package[distro]))
def patch_file(original_file, patch_file):
distro_id=check_distro(original_file)
if not distro_id:
return False
return shell_command("sudo patch {} < {}".format(original_file[distro], patch_file[distro]))
def systemd_service(action, service):
distro_id=check_distro(service)
if not distro_id:
return False
return shell_command('sudo systemctl {} {}'.format(action, service[distro]))
def exec(command):
distro_id=check_distro(command)
if not distro_id:
return False
return shell_command(command[distro])
def sed(arr_sed):
distro_id=check_distro(arr_sed)
if not distro_id:
return False
return shell_command("sudo sed -i s/{}/{}/g".format(arr_sed[distro][0], arr_sed[distro][1]))
def json_log(message, error=0, status=0, progress=0, no_progress=0, return_message=0):
log={'error': error, 'status': status, 'progress': progress, 'no_progress': no_progress, 'message': message}
if not return_message:
print(json.dumps(log))
else:
return json.dumps(log)

View file

@ -1,7 +1,9 @@
#!/usr/bin/env python3
import os
import distro
from subprocess import call, DEVNULL
import re
def install_package(package: dict, extra_configurations={}):
@ -10,7 +12,7 @@ def install_package(package: dict, extra_configurations={}):
Args:
package (dict): A dict with all packages names for every distro.
Returns
Returns:
result_package_manager (bool): return false if install fail, if install is finished, return true.
"""
@ -36,14 +38,36 @@ def install_package(package: dict, extra_configurations={}):
if call("sudo pacman -S --noconfirm {}".format(package[linux_distro]), shell=True) > 0:
print('Error, cannot install {}...'.format(package[linux_distro]))
exit(1)
return False
def change_config_file(config_file: dict):
pass
# Method for patch files using patch utility
def apply_patch(original_file: dict, patch_file: dict):
linux_distro=distro.id()
# patch originalAmigo.sh < parche.patch
if not linux_distro in original_file or not linux_distro in patch_file):
print('Error, not exists original file and patch files for this distro'))
return False
if call("sudo patch {} < {}".format(original_file[linux_distro], patch_file[linux_distro]), shell=True) > 0:
print('Error, cannot patch {}...'.format(original_file[linux_distro]))
return False
# A simple function for fill an array for every distros with the same package name.
def fill_all_distros_package():
def fill_all_distros_str(name):
pass
fill_str={}
for distro in ['debian', 'ubuntu', 'rocky', 'almalinux', 'fedora', 'arch']:
fill_str[distro]=name
return fill_str
# A simple function for get a json return value
def return_json_value():
return {}

View file

@ -77,3 +77,10 @@ def del_user(user):
else:
return (True, 'Deleted user successfully')
def mkdir_sh(directory):
if call("sudo mkdir -p %s" % directory, shell=True) > 0:
return (False, '')
else:
return (True, 'Created directory successfully %s' % directory)