首先:我知道pyinotify。
我想要的是一个使用Dropbox上传到我的家庭服务器的服务。
我将在我的家庭服务器上有一个Dropbox的共享文件夹。每当共享该文件夹的其他人将任何内容放入该文件夹时,我希望我的家庭服务器等待它完全上载,并将所有文件移动到另一个文件夹,并从Dropbox文件夹中删除这些文件,从而节省Dropbox空间。
这里的问题是,我不能只是跟踪文件夹中的更改并立即移动文件,因为如果有人上传了一个大文件,Dropbox已经开始下载,因此会显示我家庭服务器上文件夹中的更改。
有什么变通方法吗?使用Dropbox API可以做到这一点吗?
我没有尝试过,但是Dropbox CLI version似乎有一个'filestatus‘方法来检查当前的文件状态。当我自己试过的时候,我会报告的。
发布于 2012-09-12 11:15:44
这是一个Ruby版本,它不会等待Dropbox空闲,因此实际上可以开始移动文件,同时它还在同步。此外,它还忽略了.
和..
。它实际上检查给定目录中每个文件的文件状态。
然后,我将以cronjob或在单独的screen中运行此脚本。
directory = "path/to/dir"
destination = "location/to/move/to"
Dir.foreach(directory) do |item|
next if item == '.' or item == '..'
fileStatus = `~/bin/dropbox.py filestatus #{directory + "/" + item}`
puts "processing " + item
if (fileStatus.include? "up to date")
puts item + " is up to date, starting to move file now."
# cp command here. Something along this line: `cp #{directory + "/" + item + destination}`
# rm command here. Probably you want to confirm that all copied files are correct by comparing md5 or something similar.
else
puts item + " is not up to date, moving on to next file."
end
end
这是完整的脚本,我最终得到了:
# runs in Ruby 1.8.x (ftools)
require 'ftools'
directory = "path/to/dir"
destination = "location/to/move/to"
Dir.glob(directory+"/**/*") do |item|
next if item == '.' or item == '..'
fileStatus = `~/bin/dropbox.py filestatus #{item}`
puts "processing " + item
puts "filestatus: " + fileStatus
if (fileStatus.include? "up to date")
puts item.split('/',2)[1] + " is up to date, starting to move file now."
`cp -r #{item + " " + destination + "/" + item.split('/',2)[1]}`
# remove file in Dropbox folder, if current item is not a directory and
# copied file is identical.
if (!File.directory?(item) && File.cmp(item, destination + "/" + item.split('/',2)[1]).to_s)
puts "remove " + item
`rm -rf #{item}`
end
else
puts item + " is not up to date, moving to next file."
end
end
发布于 2012-09-11 20:52:40
正如您在问题中提到的,有一个Python dropbox CLI客户端。它返回“空闲...”当它不主动处理文件时。我能想到的实现所需内容的最简单机制是while循环,它检查dropbox.py filestatus /home/directory/to/watch
的输出并执行内容的scp,如果成功,则删除内容。然后睡了五分钟左右。
类似于:
import time
from subprocess import check_call, check_output
DIR = "/directory/to/watch/"
REMOTE_DIR = "user@my_server.com:/folder"
While True:
if check_output(["dropbox.py", "status", DIR]) == "\nIdle...":
if check_call(["scp", "-r", DIR + "*", REMOTE_DIR]):
check_call(["rm", "-rf", DIR + "*"])
time.sleep(360)
当然,在测试这样的东西时我会非常小心,把错误的东西放在第二个check_call中,你可能会失去你的文件系统。
发布于 2012-09-11 22:02:49
你可以运行incrond,让它等待Dropbox文件夹中的IN_CLOSE_WRITE事件。那么它将仅在文件传输完成时被触发。
https://stackoverflow.com/questions/12338544
复制相似问题