Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.84 KB | None | 0 0
  1. import os  # os is imported to create the folder
  2. import time
  3. from os import path  # path is used to check if the directory already exists in create_folder
  4. import shutil
  5. import smtplib
  6. #from taf.fdsw.rc import rc
  7. from email.mime.text import MIMEText
  8. from email.mime.multipart import MIMEMultipart
  9. from email.mime.base import MIMEBase
  10. from email import encoders
  11. import Credentials
  12. import logging
  13.  
  14. class SysTime(object):  # Systime() is used to calculate the current time and the current date
  15.  
  16.     def __init__(self):
  17.  
  18.         self.time = time.localtime()  # self.time holds the current time
  19.         self.date = str(time.strftime('%m.%d.%Y'))  # self.date holds the current date formatted into mm/dd/yy format
  20.  
  21.     def calculate_sys_time(self):  # self.time is formatted and output as a string
  22.  
  23.         current_time = time.strftime("%I:%M %p\n%a %x\n", self.time)
  24.         return str(current_time)
  25.  
  26.     def current_date(self):
  27.  
  28.         return self.date
  29.  
  30.  
  31. class FileWriter(object):  # FileWriter() creates the folder, file and writes the contents in the file
  32.  
  33.     def __init__(self):
  34.         self.folder_name_str = ' '
  35.         self.zip_file_name = ' '
  36.  
  37.     def create_folder(self, folder_name):
  38.         self.folder_name_str = str(folder_name)
  39.  
  40.         try:
  41.             os.mkdir(self.folder_name_str)
  42.  
  43.         except FileExistsError:
  44.             return
  45.  
  46.     def create_zip(self, file_name):
  47.         self.zip_file_name = file_name
  48.         shutil.make_archive(self.zip_file_name, 'zip', self.zip_file_name)
  49.         shutil.rmtree(self.folder_name_str)
  50.  
  51.     def write_to_file(self, file_name, file_contents):
  52.         with open(f'{self.folder_name_str}/{file_name}.txt', 'w') as b:
  53.             b.write(str(file_contents))
  54.  
  55.  
  56. class gnbMail(object):
  57.  
  58.     def __init__(self, user_email):
  59.         self.email_username = Credentials.EMAIL
  60.         self.email_password = Credentials.PASSWORD
  61.         self.subject = f'GNB Upgrade Results {SysTime().current_date()}'
  62.         self.msg = MIMEMultipart()
  63.         self.filename = ' '
  64.         self.user_email = user_email
  65.         self.sw_slot_one = rc_connection.get_sw_version_name(1)
  66.         self.sw_slot_two = rc_connection.get_sw_version_name(2)
  67.         self.body = f'Results from GNB Upgrade Script\n\n' \
  68.             f'Email Contains the results of attempt to upgrade the GNB ' \
  69.             f'and a .zip file containing logs if unsuccessful'
  70.         self.success = True
  71.         self.passed = f'\n\nUpgrade was successful\n\nSW#1:{self.sw_slot_one}\nSW#2:{self.sw_slot_two}'
  72.         self.failed = f'\n\nUpgrade was not unsuccessful, attached in a .txt files are the Python logs\n\n'\
  73.                       f'\nSW#1:{self.sw_slot_one}\nSW#2:{self.sw_slot_two}'
  74.  
  75.     def send_email(self, attach=None):
  76.         self.msg['From'] = self.email_username
  77.         self.msg['To'] = self.email_password
  78.         self.msg['Subject'] = self.subject
  79.  
  80.         if attach:
  81.             self.body += self.passed
  82.             self.msg.attach(MIMEText(self.body, 'plain'))
  83.         if not attach:
  84.             self.body += self.failed
  85.             print('not attach')
  86.             self.msg.attach(MIMEText(self.body, 'plain'))
  87.             self.filename = SysTime().current_date() + '.zip'
  88.             attachment = open(self.filename, 'rb')
  89.             part = MIMEBase('application', 'octet-stream')
  90.             part.set_payload(attachment.read())
  91.             encoders.encode_base64(part)
  92.             part.add_header('Content-Disposition', "attachment; filename= " + self.filename)
  93.  
  94.         text = self.msg.as_string()
  95.         server = smtplib.SMTP('smtp.gmail.com', 587)
  96.         server.starttls()
  97.         server.login(self.email_username, self.email_password)
  98.  
  99.         server.sendmail(self.email_username, self.user_email, text)
  100.         server.quit()
  101.  
  102. class gnb(object):
  103.     def gnb_upgrade(self):
  104.  
  105.         fw = FileWriter()
  106.         st = SysTime()
  107.         try:
  108.             #rc_connection.software_update(sw_slot_id = 2, sw_pkg_path = 'C:\_5G\AirScale-6.8451.82.zip', timeout = 500)
  109.             #rc_connection.trigger_software_update_activation()
  110.             return True
  111.         except:
  112.             fw.create_folder(st.current_date())
  113.             fw.write_to_file('results', 'failed')
  114.             fw.create_zip(st.current_date())
  115.             return False
  116.  
  117.  
  118.     def email_results(self, user_email):
  119.         g = gnb()
  120.         passed = g.gnb_upgrade()
  121.         ge = gnbMail(user_email)
  122.         ge.send_email(passed)
  123.  
  124. st = SysTime()
  125.  
  126. date_and_time = st.calculate_sys_time()
  127.  
  128. LOG_FILENAME = 'logs.txt'
  129. open(LOG_FILENAME, 'w').close()
  130. logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)
  131.  
  132. logging.debug('These were the errors when attempting to upgrade the GNB')
  133.  
  134. try:
  135.     ge = gnb()
  136.     ge.email_results('xicakidiy@five-plus.net')
  137. except:
  138.     logging.exception(f'\n\n{date_and_time}\n')
  139.     raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement