Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os # os is imported to create the folder
- import time
- from os import path # path is used to check if the directory already exists in create_folder
- import shutil
- import smtplib
- #from taf.fdsw.rc import rc
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- from email.mime.base import MIMEBase
- from email import encoders
- import Credentials
- import logging
- class SysTime(object): # Systime() is used to calculate the current time and the current date
- def __init__(self):
- self.time = time.localtime() # self.time holds the current time
- self.date = str(time.strftime('%m.%d.%Y')) # self.date holds the current date formatted into mm/dd/yy format
- def calculate_sys_time(self): # self.time is formatted and output as a string
- current_time = time.strftime("%I:%M %p\n%a %x\n", self.time)
- return str(current_time)
- def current_date(self):
- return self.date
- class FileWriter(object): # FileWriter() creates the folder, file and writes the contents in the file
- def __init__(self):
- self.folder_name_str = ' '
- self.zip_file_name = ' '
- def create_folder(self, folder_name):
- self.folder_name_str = str(folder_name)
- try:
- os.mkdir(self.folder_name_str)
- except FileExistsError:
- return
- def create_zip(self, file_name):
- self.zip_file_name = file_name
- shutil.make_archive(self.zip_file_name, 'zip', self.zip_file_name)
- shutil.rmtree(self.folder_name_str)
- def write_to_file(self, file_name, file_contents):
- with open(f'{self.folder_name_str}/{file_name}.txt', 'w') as b:
- b.write(str(file_contents))
- class gnbMail(object):
- def __init__(self, user_email):
- self.email_username = Credentials.EMAIL
- self.email_password = Credentials.PASSWORD
- self.subject = f'GNB Upgrade Results {SysTime().current_date()}'
- self.msg = MIMEMultipart()
- self.filename = ' '
- self.user_email = user_email
- self.sw_slot_one = rc_connection.get_sw_version_name(1)
- self.sw_slot_two = rc_connection.get_sw_version_name(2)
- self.body = f'Results from GNB Upgrade Script\n\n' \
- f'Email Contains the results of attempt to upgrade the GNB ' \
- f'and a .zip file containing logs if unsuccessful'
- self.success = True
- self.passed = f'\n\nUpgrade was successful\n\nSW#1:{self.sw_slot_one}\nSW#2:{self.sw_slot_two}'
- self.failed = f'\n\nUpgrade was not unsuccessful, attached in a .txt files are the Python logs\n\n'\
- f'\nSW#1:{self.sw_slot_one}\nSW#2:{self.sw_slot_two}'
- def send_email(self, attach=None):
- self.msg['From'] = self.email_username
- self.msg['To'] = self.email_password
- self.msg['Subject'] = self.subject
- if attach:
- self.body += self.passed
- self.msg.attach(MIMEText(self.body, 'plain'))
- if not attach:
- self.body += self.failed
- print('not attach')
- self.msg.attach(MIMEText(self.body, 'plain'))
- self.filename = SysTime().current_date() + '.zip'
- attachment = open(self.filename, 'rb')
- part = MIMEBase('application', 'octet-stream')
- part.set_payload(attachment.read())
- encoders.encode_base64(part)
- part.add_header('Content-Disposition', "attachment; filename= " + self.filename)
- text = self.msg.as_string()
- server = smtplib.SMTP('smtp.gmail.com', 587)
- server.starttls()
- server.login(self.email_username, self.email_password)
- server.sendmail(self.email_username, self.user_email, text)
- server.quit()
- class gnb(object):
- def gnb_upgrade(self):
- fw = FileWriter()
- st = SysTime()
- try:
- #rc_connection.software_update(sw_slot_id = 2, sw_pkg_path = 'C:\_5G\AirScale-6.8451.82.zip', timeout = 500)
- #rc_connection.trigger_software_update_activation()
- return True
- except:
- fw.create_folder(st.current_date())
- fw.write_to_file('results', 'failed')
- fw.create_zip(st.current_date())
- return False
- def email_results(self, user_email):
- g = gnb()
- passed = g.gnb_upgrade()
- ge = gnbMail(user_email)
- ge.send_email(passed)
- st = SysTime()
- date_and_time = st.calculate_sys_time()
- LOG_FILENAME = 'logs.txt'
- open(LOG_FILENAME, 'w').close()
- logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)
- logging.debug('These were the errors when attempting to upgrade the GNB')
- try:
- ge = gnb()
- ge.email_results('xicakidiy@five-plus.net')
- except:
- logging.exception(f'\n\n{date_and_time}\n')
- raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement