Advertisement
Guest User

Help needed to finish

a guest
Mar 10th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. login=xxxx
  4. pass=yyyy
  5. host=zzzz.com
  6. remote_dir="/remote/test"
  7. local_dir="/local/test"
  8.  
  9. import sqlite3
  10. import os
  11. import sys
  12. import subprocess
  13. from time import sleep
  14.  
  15. # Create the database if it doesn't exist.
  16. if not os.path.exists("seedbox-sync.sqlite"):
  17.  print("No database found, creating new one...")
  18.  con=sqlite3.connect("seedbox-sync.sqlite")
  19.  con.execute("create table rememberedFiles(filename varchar);")
  20. else:
  21.  con=sqlite3.connect("seedbox-sync.sqlite")
  22.  
  23. def isFileRemembered(filename):
  24.  cur=con.cursor()
  25.  cur.execute("select count(*) from rememberedFiles where filename=?",(filename,))
  26.  r=[row[0] for row in cur.fetchall()][0]
  27.  cur.close()
  28.  return r>0
  29.  
  30. def rememberFile(filename):
  31.  con.execute("insert into rememberedFiles values(?)",(filename,))
  32.  con.commit()
  33.  
  34. # The main loop.
  35. while True:
  36.  files=os.listdir(u""+remote_Dir) # If you call os.listdir() with a UTF-8 string the result will be an array of UTF-8 strings instead of ASCII. Needed for passing UTF-8 into sqlite3 for filenames with special characters.
  37.  
  38.  print("Sleeping for 10 seconds...")
  39.  sleep(10) # Sleep for 10 seconds between scans.
  40.  
  41.  for file in files:
  42.   if(isFileRemembered(file)):
  43.    # This file has been copied already.
  44.    print("Skipping file: "+file)
  45.    continue
  46.  
  47.   # Sync the file.
  48.   print("Syncing new file: "+file)
  49.   cmd=["mirror -c --log=sync.log "]
  50.   cmd.append(remote_Dir+"/"+file)
  51.   cmd.append(local_Dir+"/")
  52.   p=subprocess.Popen(cmd,shell=False)
  53.   if p.wait()==0:
  54.    rememberFile(file)
  55.    print("Synced & remembered: "+file)
  56.   else:
  57.    print("Failed to sync: "+file)
  58.  
  59. con.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement