daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest May 23rd, 2016 157 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import pdb
  2. #pdb.set_trace()
  3. import sqlite3
  4. import os
  5. import sys
  6. import subprocess
  7. from time import sleep
  8. from os import system
  9.  
  10. from os import popen
  11. import posix
  12. import pipes
  13. import shlex
  14. ########################
  15. destssh='user@hostname'
  16. syncdir='/home/syncdir.sh'
  17. syncfile='/home/syncfile.sh'
  18. ########################
  19. if len(sys.argv)<3:
  20.  print("Please run with atleast two parameters.")
  21.  exit(1)
  22.  
  23. sourceDir=sys.argv[-2].rstrip("/") #enables "python seedbox-sync.py /source /destination"
  24. destDir=sys.argv[-1].rstrip("/")   #see above. Unnecessary because it is inserted directly
  25.  
  26. # Create the database if it doesn't exist.
  27. if not os.path.exists("seedbox-sync.sqlite"):
  28.  print("No database found, creating new one...")
  29.  con=sqlite3.connect("seedbox-sync.sqlite")
  30.  con.execute("create table rememberedFiles(filename varchar);")
  31. else:
  32.  con=sqlite3.connect("seedbox-sync.sqlite")
  33.  
  34. def isFileRemembered(filename):
  35.  cur=con.cursor()
  36.  cur.execute("select count(*) from rememberedFiles where filename=?",(filename,))
  37.  r=[row[0] for row in cur.fetchall()][0]
  38.  cur.close()
  39.  return r>0
  40.  
  41. def rememberFile(filename):
  42.  con.execute("insert into rememberedFiles values(?)",(filename,))
  43.  con.commit()
  44.  
  45. # The main loop.
  46. while True:
  47.  files=os.listdir(u""+sourceDir) # 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 chara$
  48.  
  49.  # print("Sleeping for 30 Seconds...")
  50.  sleep(1)
  51.  
  52.  for file in files:
  53.   if(isFileRemembered(file)):
  54.    # This file has been copied already.
  55.    # print("Skipping file: "+file)
  56.    continue
  57. #  pdb.set_trace()
  58.   # Sync the file.
  59.   print("Syncing new file: "+file)
  60.   if(os.path.isdir(file)):
  61.         system("ssh %(destssh)s 'sh %(syncdir)s %(sourceDir)s/%(file)s %(destDir)s'" % locals())
  62.   else:
  63.         system("ssh %(destssh)s 'sh %(syncfile)s %(sourceDir)s/%(file)s %(destDir)s'" % locals())
  64. #  if p.wait()==0:
  65.   system("exit")
  66.   rememberFile(file)
  67.   print("Synced & remembered: "+file)
  68. #  else:
  69. #   print("Failed to sync: "+file)
  70.  
  71.  
  72. con.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Pastebin PRO 'AUTUMN SPECIAL'!
Get 60% OFF Pastebin PRO accounts!
 
Top