From 63e7ff18cd3d3af9b770531aeef341b6fc7b4555 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Sun, 23 Sep 2018 19:27:43 +0200 Subject: [PATCH 01/10] Fix in setup.py --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 20bebdd..ecfc219 100644 --- a/setup.py +++ b/setup.py @@ -12,8 +12,8 @@ if sys.version_info < (3, 3): # Pillow should be installed after if you need ImageField # If you install passlib and bcrypt, the password system will use bcrypt by default, if not, will use native crypt libc -setup(name='paramecio', - version='0.5.0', +setup(name='pastafariutils', + version='0.6.0', description='Utils for make *nix scripts.', long_description='Utils for make *nix scripts.', author='Antonio de la Rosa Caballero', From ad6fb7e28f962011c9b226b60c75362189bda729 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Sun, 23 Sep 2018 20:21:53 +0200 Subject: [PATCH 02/10] Fix in utils --- pastafariutils/unix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pastafariutils/unix.py b/pastafariutils/unix.py index db9a02a..64b0948 100644 --- a/pastafariutils/unix.py +++ b/pastafariutils/unix.py @@ -13,7 +13,7 @@ from subprocess import call, DEVNULL def add_user(user, password='', group='', user_directory='', shell='/usr/sbin/nologin'): if user_directory=='': - user_directory=user + user_directory='/home/'+user try: user_pwd=pwd.getpwnam(user) From 023ae5722599d28449d19845d78d48cc20cc6d5a Mon Sep 17 00:00:00 2001 From: absurdo Date: Fri, 26 May 2023 01:07:18 +0200 Subject: [PATCH 03/10] Fix in change password --- pastafariutils/unix.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pastafariutils/unix.py b/pastafariutils/unix.py index 64b0948..bc24d03 100644 --- a/pastafariutils/unix.py +++ b/pastafariutils/unix.py @@ -32,6 +32,8 @@ def add_user(user, password='', group='', user_directory='', shell='/usr/sbin/no if group!='': + # Buggy, need fix. + stat_group=os.stat('/home/%s' % user_directory) gid=stat_group.st_gid @@ -57,6 +59,8 @@ def change_password(user, new_password): if call("sudo echo \"%s:%s\" | chpasswd" % (user, new_password), shell=True, stdout=DEVNULL) > 0: return (True, 'Change password successfully') + else: + return (False, 'I cannot change password') except KeyError: From 6318ece7e15d8e1d95b2428fff7c254ec8d0a056 Mon Sep 17 00:00:00 2001 From: absurdo Date: Fri, 26 May 2023 01:37:39 +0200 Subject: [PATCH 04/10] Fix in unix.py --- pastafariutils/unix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pastafariutils/unix.py b/pastafariutils/unix.py index bc24d03..40f2fb4 100644 --- a/pastafariutils/unix.py +++ b/pastafariutils/unix.py @@ -56,11 +56,11 @@ def change_password(user, new_password): try: user_pwd=pwd.getpwnam(user) - if call("sudo echo \"%s:%s\" | chpasswd" % (user, new_password), shell=True, stdout=DEVNULL) > 0: + if call("sudo echo \"%s:%s\" | sudo chpasswd" % (user, new_password), shell=True, stdout=DEVNULL) > 0: return (True, 'Change password successfully') else: - return (False, 'I cannot change password') + return (False, 'I cannot change password, permissions?') except KeyError: From d4009d8f09635e78b985ce505485b790b8cdf5b0 Mon Sep 17 00:00:00 2001 From: absurdo Date: Fri, 26 May 2023 01:39:42 +0200 Subject: [PATCH 05/10] Fixes in unix.py --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index ecfc219..0de2ee6 100644 --- a/setup.py +++ b/setup.py @@ -5,15 +5,15 @@ import os from setuptools import setup, find_packages -if sys.version_info < (3, 3): - raise NotImplementedError("Sorry, you need at least Python 3.3 for use pastafariutils.") +if sys.version_info < (3, 6): + raise NotImplementedError("Sorry, you need at least Python 3.6 for use pastafariutils.") #import paramecio # Pillow should be installed after if you need ImageField # If you install passlib and bcrypt, the password system will use bcrypt by default, if not, will use native crypt libc setup(name='pastafariutils', - version='0.6.0', + version='0.6.1', description='Utils for make *nix scripts.', long_description='Utils for make *nix scripts.', author='Antonio de la Rosa Caballero', From 92cab96c85497e637b1767e129af4470a7aaa6f7 Mon Sep 17 00:00:00 2001 From: absurdo Date: Sun, 30 Jul 2023 22:58:25 +0200 Subject: [PATCH 06/10] Fix in add_user --- pastafariutils/unix.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pastafariutils/unix.py b/pastafariutils/unix.py index 40f2fb4..5286bf4 100644 --- a/pastafariutils/unix.py +++ b/pastafariutils/unix.py @@ -45,10 +45,12 @@ def add_user(user, password='', group='', user_directory='', shell='/usr/sbin/no if call(func_user, shell=True, stdout=DEVNULL) > 0: - return (True, '') + return (False, 'Error executing useradd command') else: - return (False, 'Error executing useradd command') + + return (True, '') + def change_password(user, new_password): @@ -58,9 +60,11 @@ def change_password(user, new_password): if call("sudo echo \"%s:%s\" | sudo chpasswd" % (user, new_password), shell=True, stdout=DEVNULL) > 0: - return (True, 'Change password successfully') - else: return (False, 'I cannot change password, permissions?') + + else: + return (True, 'Change password successfully') + except KeyError: @@ -69,10 +73,7 @@ def change_password(user, new_password): def del_user(user): if call("sudo userdel -r %s" % user, shell=True, stdout=DEVNULL, stderr=DEVNULL) > 0: - - return (True, 'Deleted user successfully') - - else: - return (False, '') + else: + return (True, 'Deleted user successfully') From 8e641c0f97e18049f0c54bf21cc702d6293d3c2a Mon Sep 17 00:00:00 2001 From: absurdo Date: Wed, 22 Nov 2023 22:34:57 +0100 Subject: [PATCH 07/10] Added packages --- pastafariutils/packages.py | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 pastafariutils/packages.py diff --git a/pastafariutils/packages.py b/pastafariutils/packages.py new file mode 100644 index 0000000..1658159 --- /dev/null +++ b/pastafariutils/packages.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import distro +from subprocess import call, DEVNULL + +def install_package(package: dict, extra_configurations={}): + + """A function for install packages for different distros. Now support, debian, ubuntu, rocky, almalinux, fedora, archlinux. + + Args: + package (dict): A dict with all packages names for every distro. + + Returns + + result_package_manager (bool): return false if install fail, if install is finished, return true. + """ + + linux_distro=distro.id() + + if linux_distro=='debian' or linux_distro=='ubuntu': + + if call('sudo DEBIAN_FRONTEND="noninteractive" apt-get install -y {}'.format(package[linux_distro]), shell=True) > 0: + print('Error, cannot install {}...'.format(package[linux_distro])) + return False + + elif linux_distro=='rocky' or linux_distro=='fedora' or linux_distro=='almalinux': + + if call("sudo dnf install -y {}", shell=True) > 0: + print('Error, cannot install {}...'.format(package[linux_distro])) + return False + if linux_distro=='arch': + + if call("sudo pacman -S --noconfirm {}".format(package[linux_distro]), shell=True) > 0: + print('Error, cannot install {}...'.format(package[linux_distro])) + exit(1) + +def change_config_file(config_file: dict): + + pass From b9f89db78fb10ce41fb416d1f63ad120c78b6d5f Mon Sep 17 00:00:00 2001 From: absurdo Date: Wed, 22 Nov 2023 22:47:12 +0100 Subject: [PATCH 08/10] Fix in install packages --- pastafariutils/packages.py | 4 ++++ setup.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pastafariutils/packages.py b/pastafariutils/packages.py index 1658159..542b811 100644 --- a/pastafariutils/packages.py +++ b/pastafariutils/packages.py @@ -17,6 +17,10 @@ def install_package(package: dict, extra_configurations={}): linux_distro=distro.id() + if not linux_distro in package: + print('Sorry, not package in {}'.format(linux_distro)) + return False + if linux_distro=='debian' or linux_distro=='ubuntu': if call('sudo DEBIAN_FRONTEND="noninteractive" apt-get install -y {}'.format(package[linux_distro]), shell=True) > 0: diff --git a/setup.py b/setup.py index 0de2ee6..84aaef4 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ if sys.version_info < (3, 6): # If you install passlib and bcrypt, the password system will use bcrypt by default, if not, will use native crypt libc setup(name='pastafariutils', - version='0.6.1', + version='0.6.2', description='Utils for make *nix scripts.', long_description='Utils for make *nix scripts.', author='Antonio de la Rosa Caballero', From 8cb53e725e361723c32c5d18e6239ab947a3d279 Mon Sep 17 00:00:00 2001 From: absurdo Date: Wed, 22 Nov 2023 23:02:18 +0100 Subject: [PATCH 09/10] Fix in install packages --- pastafariutils/packages.py | 8 +++++++- setup.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pastafariutils/packages.py b/pastafariutils/packages.py index 542b811..3f91065 100644 --- a/pastafariutils/packages.py +++ b/pastafariutils/packages.py @@ -29,7 +29,7 @@ def install_package(package: dict, extra_configurations={}): elif linux_distro=='rocky' or linux_distro=='fedora' or linux_distro=='almalinux': - if call("sudo dnf install -y {}", shell=True) > 0: + if call("sudo dnf install -y {}".format(package[linux_distro]), shell=True) > 0: print('Error, cannot install {}...'.format(package[linux_distro])) return False if linux_distro=='arch': @@ -41,3 +41,9 @@ def install_package(package: dict, extra_configurations={}): def change_config_file(config_file: dict): pass + +# A simple function for fill an array for every distros with the same package name. + +def fill_all_distros_package(): + + pass diff --git a/setup.py b/setup.py index 84aaef4..0eecd20 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ if sys.version_info < (3, 6): # If you install passlib and bcrypt, the password system will use bcrypt by default, if not, will use native crypt libc setup(name='pastafariutils', - version='0.6.2', + version='0.6.3', description='Utils for make *nix scripts.', long_description='Utils for make *nix scripts.', author='Antonio de la Rosa Caballero', From 1639c613ab272350a1defc8ba39eb0434fdde674 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Mon, 5 Feb 2024 23:50:17 +0100 Subject: [PATCH 10/10] Added linux --- pastafariutils/linux.py | 98 ++++++++++++++++++++++++++++++++++++++ pastafariutils/packages.py | 38 ++++++++++++--- pastafariutils/unix.py | 7 +++ 3 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 pastafariutils/linux.py diff --git a/pastafariutils/linux.py b/pastafariutils/linux.py new file mode 100644 index 0000000..bb06368 --- /dev/null +++ b/pastafariutils/linux.py @@ -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) + diff --git a/pastafariutils/packages.py b/pastafariutils/packages.py index 3f91065..2286990 100644 --- a/pastafariutils/packages.py +++ b/pastafariutils/packages.py @@ -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 {} diff --git a/pastafariutils/unix.py b/pastafariutils/unix.py index 5286bf4..781925c 100644 --- a/pastafariutils/unix.py +++ b/pastafariutils/unix.py @@ -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) +