From 538616f7594d806a1f1661e2156571dd82cab965 Mon Sep 17 00:00:00 2001 From: Madhuri Agarwal Date: Fri, 15 Dec 2023 16:50:42 +0530 Subject: [PATCH 1/7] first commit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fe8ed44b..364d7d13 100644 --- a/README.md +++ b/README.md @@ -281,3 +281,4 @@ If you have any feedback or ideas to improve this project, feel free to contact Reeha's Github +# python-mini-projects From 35e7599ac6739c4b74b73e6bfb69d02edbd8d46f Mon Sep 17 00:00:00 2001 From: Madhuri Agarwal Date: Fri, 15 Dec 2023 17:15:54 +0530 Subject: [PATCH 2/7] Update calculator.py Updating Operator subtraction in smart_calculator --- Smart_Calculator/calculator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Smart_Calculator/calculator.py b/Smart_Calculator/calculator.py index 2c46b4eb..58692966 100644 --- a/Smart_Calculator/calculator.py +++ b/Smart_Calculator/calculator.py @@ -59,7 +59,7 @@ def calculate(): list.insert(END,'something went wrong please enter again') operations = {'ADD':add,'ADDITION':add, 'SUM':add, 'PLUS':add, - 'SUB':sub, 'DIFFERENCE':sub, 'MINUS': sub, 'SUBTRACT':sub, 'DIFF':sub, + 'SUB':sub, 'DIFFERENCE':sub, 'MINUS': sub, 'SUBTRACT':sub, 'DIFF':sub, 'SUBTRACTION':sub, 'LCM':lcm, 'HCF':hcf, 'PRODUCT':mul, 'MULTIPLICATION':mul, 'MULTIPLY':mul, 'DIVISION':div, 'DIV':div, 'DIVIDE':div, 'MOD':mod,'REMAINDER':mod, 'MODULUS':mod} From f4919c4cabac879836a6ac391b824509ce09582b Mon Sep 17 00:00:00 2001 From: Madhuri Agarwal Date: Wed, 24 Jan 2024 12:40:09 +0530 Subject: [PATCH 3/7] New unit added for volume calculation --- Converter/converter_values.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Converter/converter_values.py b/Converter/converter_values.py index 3cc9c104..79b42f47 100644 --- a/Converter/converter_values.py +++ b/Converter/converter_values.py @@ -31,6 +31,7 @@ cubic m : m3 litre: l milliliter : ml +kilolitre : kl MASS : M ---------------------------------------------------------------- gram : g @@ -98,7 +99,8 @@ "ml":10**(6) , "l":10**(3) , "in3":61023.7441 , - "ft3":35.31467} + "ft3":35.31467 , + "kl":1} M= {"g":1 , "kg":10**(-3) , From a1d6a3b83e54e1bb8919c3240b32d41468e9db77 Mon Sep 17 00:00:00 2001 From: Madhuri Agarwal Date: Thu, 1 Feb 2024 16:49:48 +0530 Subject: [PATCH 4/7] New functionality for temperature conversion added --- Converter/converter_values.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Converter/converter_values.py b/Converter/converter_values.py index 79b42f47..d0ef3a78 100644 --- a/Converter/converter_values.py +++ b/Converter/converter_values.py @@ -50,6 +50,11 @@ week : w year : y +TEMPERATURE : temp ---------------------------------------------------------------- +kelvin : K +Celsius : C +Fahrenheit : F + ''', "help":''' @@ -117,3 +122,6 @@ "min":1440 , "sec":86400 } +temp= {"K":310.15 , + "C":37 , + "F":98.6 } From fe700e719670341ba5e98af8972a3ed5faa53d6d Mon Sep 17 00:00:00 2001 From: Madhuri Agarwal Date: Thu, 1 Feb 2024 16:51:24 +0530 Subject: [PATCH 5/7] New functionality for temperature conversion added --- Converter/converter_values.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Converter/converter_values.py b/Converter/converter_values.py index d0ef3a78..cb2921f0 100644 --- a/Converter/converter_values.py +++ b/Converter/converter_values.py @@ -122,6 +122,6 @@ "min":1440 , "sec":86400 } -temp= {"K":310.15 , - "C":37 , - "F":98.6 } +temp= {"K":274.15 , + "C":1 , + "F":33.8 } From c9bfcb77e434bcb8bd7d6128b4bf1d496ef67cd6 Mon Sep 17 00:00:00 2001 From: Madhuri Agarwal Date: Thu, 1 Feb 2024 16:53:37 +0530 Subject: [PATCH 6/7] New functionality for temperature conversion added --- Converter/converter_values.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Converter/converter_values.py b/Converter/converter_values.py index cb2921f0..f2b335b6 100644 --- a/Converter/converter_values.py +++ b/Converter/converter_values.py @@ -50,7 +50,7 @@ week : w year : y -TEMPERATURE : temp ---------------------------------------------------------------- +TEMPERATURE : t ---------------------------------------------------------------- kelvin : K Celsius : C Fahrenheit : F @@ -122,6 +122,6 @@ "min":1440 , "sec":86400 } -temp= {"K":274.15 , +t= {"K":274.15 , "C":1 , "F":33.8 } From e703395e3c0ce95c7c84ba41f9c47ba82f02d3b9 Mon Sep 17 00:00:00 2001 From: Madhuri Agarwal Date: Tue, 27 Feb 2024 12:40:02 +0530 Subject: [PATCH 7/7] New feature for error and logs handling --- Error and Logs Handling/README.md | 31 +++++ .../error_logging_handling.py | 108 ++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 Error and Logs Handling/README.md create mode 100644 Error and Logs Handling/error_logging_handling.py diff --git a/Error and Logs Handling/README.md b/Error and Logs Handling/README.md new file mode 100644 index 00000000..7b836698 --- /dev/null +++ b/Error and Logs Handling/README.md @@ -0,0 +1,31 @@ +![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99) +![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103) +[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/ndleah) +[![View Repositories](https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub)](https://github.com/ndleah?tab=repositories) + +# Execute Shell Command + +## 🛠️ Description +Python code is written to perform below 3 functionalities: +1. Execute shell command and get the output and error. +2. Logging of all steps +3. Archiving of logs + +## ⚙️ Languages or Frameworks Used +Language used is Python. There are no dependencies. + +## 🌟 User Inputs +There are 3 inputs required from user before executing the code: +1. Email Id +2. Log path +3. Archive Log path + +Make sure to provide correct path for log, archive log and email_id in python file before executing it. + +## 🌟 How to run +Simply import required funtions in your Python script and execute it. + +## Author Name + +[Madhuri Agarwal](https://github.com/magar51) + diff --git a/Error and Logs Handling/error_logging_handling.py b/Error and Logs Handling/error_logging_handling.py new file mode 100644 index 00000000..eb133a99 --- /dev/null +++ b/Error and Logs Handling/error_logging_handling.py @@ -0,0 +1,108 @@ +"""This script aims at creating log file and sending it on email""" +import os +import sys +import glob +import logging +import datetime +from shutil import move +import subprocess + + +"""Generic error handling module""" +def error(status, error_msg, module_name, return_code): + + if status.lower() == 'error': + logging.error('!!! Error !!!') + logging.error('Error executing module : %s', module_name) + logging.error(error_msg) + logging.error('Module return code : %s', return_code) + logging.error('Exiting script with exit(1) !!!') + email_log_file(log_file, status='failure') + sys.exit(1) + + if status.lower() == 'warning': + logging.warning('!!! Warning !!!') + logging.warning('Error executing module : %s', module_name) + logging.warning(error_msg) + logging.warning('Module return code : %s', return_code) + logging.warning('Script execution will continue !!!') + + +"""Generic Logging function """ +def initiate_logging(): + + dt_now = datetime.datetime.today() + log_file = '' + 'testing' + dt_now.strftime('_%Y%m%d_%H%M%S') + '.log' + log_file_pattern = '' + 'testing' + '*' + '.log' + log_file_list = glob.glob(log_file_pattern) + + for file in log_file_list: + if os.path.exists(file): + dest_file = '' + file.split('/')[-1] + if os.path.exists(dest_file): + os.remove(dest_file) + move(file, '') + + logging.basicConfig(filename=log_file, level=logging.DEBUG, + format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%Y-%m-%d %H:%M:%S') + + return log_file + + +"""Executes the provided unix shell command """ +def execute_shell_command(command): + + try: + proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) + out, err = proc.communicate() + return_code = proc.returncode + if err: + print(str(err)) + return out, return_code + except Exception as err: + print("Exception Occurred while executing module : %s", str(err)) + return 105 + + +"""Emails the status of job execution along with log file as attachment""" +def email_log_file(log_file, status): + + if status == 'success': + body = 'Execution successful...PFA log file' + subject = 'Execution successful - testing' + + email_cmd = """echo "{b}" | mailx -s "{s}" -a "{l}" "{to}" """.format(b=body, s=subject, l=log_file, to='') + logging.info('Email Command :- %s', email_cmd) + os.system(email_cmd) + else: + body = 'Execution Failed...PFA log file' + subject = '!!! Failure - testing !!!' + + email_cmd = """echo "{b}" | mailx -s "{s}" -a "{l}" "{to}" """.format(b=body, s=subject, l=log_file, to='') + logging.info('Email Command :- %s', email_cmd) + os.system(email_cmd) + + +# Start of Execution +if __name__ == '__main__': + + log_file = initiate_logging() + + logging.info('!!! Execution Begins !!!') + + command ='echo Madhuri' + result, status = execute_shell_command(command) + + if status != 0: + logging.info("Exception in running shell command") + STATUS = 'error' + ERROR_MSG = '!!! Error !!! Execution failed !!!' + MODULE_NAME = 'Execution Script' + RETURN_CODE = '' + error(STATUS, ERROR_MSG, MODULE_NAME, RETURN_CODE) + else: + command_result = result.decode('utf8').rstrip("\r\n") + logging.info(command_result) + + email_log_file(log_file, status='success') +# End of Execution