Advertisement
pjmakey2

clarion sorter

Jan 20th, 2012
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.38 KB | None | 0 0
  1. #!/usr/bin/python
  2. #author: peter (pjl@hpc.com.py)
  3.  
  4. import email
  5. from email.MIMEText import MIMEText
  6. from email.MIMEMultipart import MIMEMultipart
  7. from email.MIMEBase import MIMEBase
  8. from email import encoders
  9. import mimetypes
  10. import smtplib
  11. import subprocess
  12. import re
  13. import os
  14. import sys
  15. import time
  16. import shutil
  17.  
  18. dirproduccion = "/mnt/acosi/ARCHIVOS"
  19. pathbkp = "/tmp/bkp"
  20. prodsorter = "/tmp/sorter/dm"
  21. prod=re.compile("/mnt/acosi/ARCHIVOS")
  22. dats=re.compile(".*\.dat$")
  23. indi=re.compile(".*\.k.*?$")
  24. mem=re.compile(".*\.mem$")
  25. eje=re.compile(".*\.exe$")
  26. comando = "cp"
  27. parametros = "-v"
  28.  
  29. def control():
  30.     tiempoco = time.time()
  31.     subprocess.call("lsof +D /media/usuarios/aco/sistemas/ARCHIVOS | egrep \".*\.exe$|.*\.EXE$\"",
  32.             shell=True,
  33.             stdout=open("/tmp/_Sistemasabiertos.log","w"),
  34.             stderr=open("/tmp/errorlsoft.log","w"))
  35.     tam = os.path.getsize("/tmp/_Sistemasabiertos.log")
  36.     while tam > 0:
  37.         subprocess.call("lsof +D /media/usuarios/aco/sistemas/ARCHIVOS | egrep \".*\.exe$|.*\.EXE$\"",
  38.                 shell=True,
  39.                 stdout=open("/tmp/_Sistemasabiertos.log","w"),
  40.                 stderr=open("/tmp/errorlsoft.log","w"))
  41.         tam = os.path.getsize("/tmp/_Sistemasabiertos.log")
  42.         tiempoac = time.time()
  43.         diffloat = (tiempoac - tiempoco)
  44.         difint = int(diffloat)
  45.         if difint > 18300
  46.             informar('Sorter can\'t be done',
  47.                 '''Program's run's\n''',
  48.                 "/tmp/_Sistemasabiertos.log")
  49.             sys.exit(1)
  50.  
  51. def informar(situacion,cuerpo,abjunto):
  52.     desde = "xxx@xxx"
  53.     hasta = "xxx@xxx"
  54.     asunto = situacion
  55.     cuerpo = cuerpo
  56.     abjunto =  abjunto
  57.  
  58.     m = MIMEMultipart()
  59.     m["To"] = hasta
  60.     m["From"] = desde
  61.     m["Subject"] = asunto
  62.  
  63.     m.attach(MIMEText(cuerpo))
  64.  
  65.     msg = MIMEBase('application', 'octet-stream')
  66.  
  67.     msg.set_payload(open(abjunto, 'rb').read())
  68.     encoders.encode_base64(msg)
  69.     msg.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(abjunto))
  70.     m.attach(msg)
  71.    
  72.     s = smtplib.SMTP("smtp.gmail.com", 587)
  73.     s.set_debuglevel(1)
  74.     s.ehlo()
  75.     s.starttls()
  76.     s.ehlo()
  77.     s.login(desde,"xxx")
  78.     s.sendmail(desde, hasta, m.as_string())
  79.     s.close()
  80.  
  81. def crearseguro():
  82.     try:
  83.         shutil.copytree(dirproduccion, "/tmp/bkp")  
  84.     except EnvironmentError, e:
  85.          escritura = "can not copy %s a /tmp/bkp \n %s" % (dirproduccion, e)
  86.          log = open("/tmp/errorcrearseguro.log", "a")
  87.          log.write(escritura)
  88.          log.close()
  89.          informar("Sorter fail",
  90.                    '''Fail backup of the production data''',
  91.                    "/tmp/errorcrearseguro.log")
  92.          sys.exit(1)   
  93.     else:
  94.          escritura = "%s --> /tmp/bkp \n" % dirproduccion
  95.          log = open("/tmp/crearseguro.log", "a")
  96.          log.write(escritura)
  97.          log.close()
  98.  
  99. def crearpaths(dirpath):
  100.     if not os.path.isdir(dirpath):
  101.         os.makedirs(dirpath)
  102.  
  103. def creararbol(paths):
  104.     if prod.search(paths):
  105.         path = paths
  106.         for dirpath, dirname, arcnam in os.walk(path):
  107.             for arc in arcnam:
  108.                 if dats.search(arc.lower()):
  109.                                     dirs = "/tmp/sorter/dm" + dirpath
  110.                     crearpaths(dirs)
  111.                     archivo = dirpath + "/"  + arc
  112.                     try:
  113.                             shutil.copy(archivo, dirs)  
  114.                     except EnvironmentError, e:
  115.                         escritura = "can not copy %s a %s \n %s" % (archivo, dirs, e)
  116.                         log = open("/tmp/errorcreararboldats.log", "a")
  117.                         log.write(escritura)
  118.                         log.close()
  119.                         informar("fail .dat copy",
  120.                               '''sorter fails in dat copy''',
  121.                               "/tmp/errorcreararboldats.log")
  122.                         sys.exit(1)
  123.                     else:
  124.                         escritura = "%s --> %s \n" % (archivo, dirs)
  125.                         log = open("/tmp/creararboldats.log", "a")
  126.                         log.write(escritura)
  127.                         log.close()
  128.  
  129.                 elif indi.search(arc.lower()):
  130.                                     dirs = "/tmp/sorter/k" + dirpath
  131.                     crearpaths(dirs)
  132.                     archivo = dirpath + "/"  + arc
  133.                     try:
  134.                             shutil.copy(archivo, dirs)  
  135.                     except EnvironmentError, e:
  136.                         escritura = "can not copy %s a %s \n %s" % (archivo, dirs, e)
  137.                         log = open("/tmp/errorcreararbolk00.log", "a")
  138.                         log.write(escritura)
  139.                         log.close()
  140.                         informar("fail K data copy",
  141.                               '''Fail of the k data''',
  142.                               "/tmp/errorcreararbolk00.log")
  143.                         sys.exit(1)
  144.                     else:
  145.                         escritura = "%s --> %s \n" % (archivo, dirs)
  146.                         log = open("/tmp/creararbolk00.log", "a")
  147.                         log.write(escritura)
  148.                         log.close()    
  149.                    
  150.                 elif mem.search(arc.lower()):
  151.                                     dirs = "/tmp/sorter/dm" + dirpath
  152.                     crearpaths(dirs)
  153.                     archivo = dirpath + "/"  + arc
  154.                     try:
  155.                             shutil.copy(archivo, dirs)  
  156.                     except EnvironmentError, e:
  157.                         escritura = "can copy %s a %s \n %s" % (archivo, dirs, e)
  158.                         log = open("/tmp/errorcreararbolmem.log", "a")
  159.                         log.write(escritura)
  160.                         log.close()
  161.                         informar("Fail .mem files",
  162.                               '''Sorter fails in the .mem files copy''',
  163.                               "/tmp/errorcreararbolmem.log")
  164.                         sys.exit(1)
  165.                     else:
  166.                         escritura = "%s --> %s \n" % (archivo, dirs)
  167.                         log = open("/tmp/creararbolmem.log", "a")
  168.                         log.write(escritura)
  169.                         log.close()
  170.  
  171.                 elif eje.search(arc.lower()):
  172.                                     dirs = "/tmp/sorter/e" + dirpath
  173.                     crearpaths(dirs)
  174.                     archivo = dirpath + "/"  + arc
  175.                     try:
  176.                             shutil.copy(archivo, dirs)  
  177.                     except EnvironmentError, e:
  178.                         escritura = "can not copy %s a %s \n %s" % (archivo, dirs, e)
  179.                         log = open("/tmp/errorcreararboleje.log", "a")
  180.                         log.write(escritura)
  181.                         log.close()
  182.                         informar("Sorter Fail",
  183.                               '''Can copy exe files''',
  184.                               "/tmp/errorcreararboleje.log")
  185.                         sys.exit(1)
  186.                     else:
  187.                         escritura = "%s --> %s \n" % (archivo, dirs)
  188.                         log = open("/tmp/creararboleje.log", "a")
  189.                         log.write(escritura)
  190.                         log.close()
  191.     else:
  192.         ejecutarSor(prodsorter)
  193.  
  194. def ejecutarSor(dirsorter):
  195.     for dirpath, dirname, arcnam in os.walk(dirsorter):
  196.         if dirpath == "/tmp/sorter/dm/ARCHIVOS/":
  197.             pass
  198.         else:
  199.             dirpath = dirpath.replace("/","\\\\\\")
  200.             comando = "dosemu -input \"g:\\r\" -input \" cd " + dirpath + " \\r\" -input \" dir \\r\" -input \" csrt.exe \*.DAT /v \\r\" -input \" exitemu \\r\""
  201.             subprocess.call(comando,shell=True)
  202.  
  203.  
  204. if __name__ == "__main__":
  205.     control()
  206.     crearseguro()
  207.     creararbol(dirproduccion)
  208.     creararbol(prodsorter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement