Advertisement
Guest User

wrapper_script

a guest
Mar 30th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import arcpy, os, smtplib, socket, shutil
  3. from datetime import datetime
  4. from email.MIMEMultipart import MIMEMultipart
  5. from email.MIMEText import MIMEText
  6. from email.MIMEBase import MIMEBase
  7. from email import encoders
  8. from shutil import copytree, ignore_patterns
  9.  
  10. try:
  11.     path = arcpy.GetParameterAsText(0)
  12.     szazezer = arcpy.GetParameter(1)
  13.     tbx_path, selected_tool = os.path.split(path) # Split so you have the toolbox and tool name
  14.     tbx = arcpy.ImportToolbox(tbx_path) # import the toolbox
  15.     tool = getattr(tbx, selected_tool) # get the tool by name from the toolbox
  16.     result = tool() # run the tool with all the required parameters
  17.     uzi = arcpy.GetMessages()
  18.     status = result.status # Will be 4 on successful completion
  19. except Exception as e:
  20.     arcpy.GetMessages(2) # get only the error messages (alternately just use the exception object)
  21.  
  22. # valtozok
  23. hostname = socket.gethostname()
  24. IP = socket.gethostbyname(hostname)
  25. tool_subject = str(tool)[10:14]
  26. maxSeverity = arcpy.GetMaxSeverity()
  27. messageCount = arcpy.GetMessageCount()
  28. time = str(datetime.now())[:19]
  29. fromaddr = "some@email.com"
  30. toaddr = "anotherone@email.com"
  31. msg = MIMEMultipart()
  32. msg['From'] = fromaddr
  33. msg['To'] = toaddr
  34. msg['Subject'] = str(result) + " " + tool_subject + " (" + str(szazezer) + ") lefutott: " + str(status) + " " + str(time) + ": " + str(messageCount) + "db uzenet, a legdurvabb: " + str(maxSeverity)
  35. body = "tematika: " + str(tool)[10:15] + "\n" + "utem: " + str(tool)[13:15] + "\n" + "100e: " + szazezer + "\n" + "computer: " + hostname + "\n" + "IP: " + IP + "\n\n" + uzi
  36. msg.attach(MIMEText(body, 'plain'))
  37.  
  38. # attachement
  39. filename = r"pontok_tmodel.dbf"
  40. attachment = open(r"c:\arcGIS_2_email\input_folder\pontok_tmodel.dbf", "rb")
  41. part = MIMEBase('application', 'octet-stream')
  42. part.set_payload((attachment).read())
  43. encoders.encode_base64(part)
  44. part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
  45. msg.attach(part)
  46.  
  47. # send email
  48. server = smtplib.SMTP('1.1.1.1', 25)
  49. server.starttls()
  50. server.login("username", "password")
  51. text = msg.as_string()
  52. server.sendmail(fromaddr, toaddr, text)
  53. server.quit()
  54.  
  55. #play music
  56. import subprocess  
  57. subprocess.check_output(["cmd", "/c", "start", r"c:\arcGIS_2_email\zene.bat"])  
  58.  
  59. #backup files
  60. time2 = str(datetime.now())[:10] + "_" + str(datetime.now())[11:-13] + str(datetime.now())[14:-10]
  61. source = r"c:\arcGIS_2_email\output_folder"
  62. backup1 = r"d:\_..._ide"
  63. backup2 = tool_subject + "_" + time2
  64. backup = backup1 + "\\" + backup2
  65. shutil.copytree(source, backup, ignore=ignore_patterns('*.prj', '*.*p*'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement