Created
          February 5, 2020 23:23 
        
      - 
      
 - 
        
Save mmasser95/ec00060c1ac1eacba9216fdeba9e00ee to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/python3.7 | |
| from threading import Thread, Lock | |
| from queue import Queue | |
| from io import open as iopen | |
| from argparse import ArgumentParser | |
| import subprocess | |
| QE = Queue() | |
| LK = Lock() | |
| TH = [] | |
| EXFLAG = False | |
| class Worker(Thread): | |
| def __init__(self): | |
| Thread.__init__(self) | |
| def run(self): | |
| while not EXFLAG: | |
| LK.acquire() | |
| if not QE.empty(): | |
| k = QE.get() | |
| LK.release() | |
| k = k.split(";") | |
| try: | |
| print(f"{'-'*20}Migrando {k[4]}{'-'*20}") | |
| cmd = subprocess.run(["./imapsync", f'--host1 "{k[0]}"', f'--host2 "{k[3]}"', f'--user1 "{k[1]}"', | |
| f'--user2 "{k[4]}"', f'--password1 "{k[2]}"', f'--password2 "{k[5]}"'], | |
| universal_newlines=True, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE) | |
| if cmd.returncode == 0: | |
| print(f"{'-'*20}Migrando {k[4]}{'-'*20}") | |
| else: | |
| print(f"{'-'*20}Error migrando {k[4]}{'-'*20}") | |
| except Exception as e: | |
| print(e) | |
| else: | |
| LK.release() | |
| def parseBuzon(fil): | |
| with iopen(fil, 'r', encoding="utf-8") as f: | |
| return [i.strip() for i in f.readlines() if not i.startswith("#")] | |
| if __name__ == "__main__": | |
| ap = ArgumentParser(description="", epilog="") | |
| ap.add_argument("-t", type=int, default=5, help="Num. de Threads (Def. 5)") | |
| ap.add_argument("-f", type=str, default="buzones.txt", | |
| help="Nombre del fichero. (Def. buzones.txt)") | |
| p = ap.parse_args() | |
| for i in range(p.t): | |
| t = Worker() | |
| t.start() | |
| TH.append(t) | |
| LK.acquire() | |
| for i in parseBuzon(p.f): | |
| QE.put(i) | |
| LK.release() | |
| while not QE.empty(): | |
| pass | |
| EXFLAG = True | |
| for t in TH: | |
| t.join() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment