コード例 #1
0
 def fullSyncFromServer(self, fields, path):
     try:
         runHook("fullSyncStarted", 0)
         fields = urllib.urlencode(fields)
         src = urllib.urlopen(SYNC_URL + "fulldown", fields)
         (fd, tmpname) = tempfile.mkstemp(dir=os.path.dirname(path),
                                          prefix="fullsync")
         tmp = open(tmpname, "wb")
         decomp = zlib.decompressobj()
         cnt = 0
         while 1:
             data = src.read(CHUNK_SIZE)
             if not data:
                 tmp.write(decomp.flush())
                 break
             tmp.write(decomp.decompress(data))
             cnt += CHUNK_SIZE
             runHook("fullSyncProgress", "fromServer", cnt)
         src.close()
         tmp.close()
         os.close(fd)
         # if we were successful, overwrite old deck
         os.unlink(path)
         os.rename(tmpname, path)
         # reset the deck name
         c = sqlite.connect(path)
         c.execute("update decks set syncName = ?",
                   [checksum(path.encode("utf-8"))])
         c.commit()
         c.close()
     finally:
         runHook("fullSyncFinished")
コード例 #2
0
ファイル: sync.py プロジェクト: cornercase/anki
 def fullSyncFromServer(self, fields, path):
     try:
         runHook("fullSyncStarted", 0)
         fields = urllib.urlencode(fields)
         src = urllib.urlopen(SYNC_URL + "fulldown", fields)
         (fd, tmpname) = tempfile.mkstemp(dir=os.path.dirname(path), prefix="fullsync")
         tmp = open(tmpname, "wb")
         decomp = zlib.decompressobj()
         cnt = 0
         while 1:
             data = src.read(CHUNK_SIZE)
             if not data:
                 tmp.write(decomp.flush())
                 break
             tmp.write(decomp.decompress(data))
             cnt += CHUNK_SIZE
             runHook("fullSyncProgress", "fromServer", cnt)
         src.close()
         tmp.close()
         os.close(fd)
         # if we were successful, overwrite old deck
         os.unlink(path)
         os.rename(tmpname, path)
         # reset the deck name
         c = sqlite.connect(path)
         c.execute("update decks set syncName = ?", [checksum(path.encode("utf-8"))])
         c.commit()
         c.close()
     finally:
         runHook("fullSyncFinished")
コード例 #3
0
 def fullSyncFromLocal(self, fields, path):
     global sendProgressHook
     try:
         # write into a temporary file, since POST needs content-length
         src = open(path, "rb")
         (fd, name) = tempfile.mkstemp(prefix="oldanki")
         tmp = open(name, "w+b")
         # post vars
         for (key, value) in fields.items():
             tmp.write('--' + MIME_BOUNDARY + "\r\n")
             tmp.write('Content-Disposition: form-data; name="%s"\r\n' %
                       key)
             tmp.write('\r\n')
             tmp.write(value)
             tmp.write('\r\n')
         # file header
         tmp.write('--' + MIME_BOUNDARY + "\r\n")
         tmp.write(
             'Content-Disposition: form-data; name="deck"; filename="deck"\r\n'
         )
         tmp.write('Content-Type: application/octet-stream\r\n')
         tmp.write('\r\n')
         # data
         comp = zlib.compressobj()
         while 1:
             data = src.read(CHUNK_SIZE)
             if not data:
                 tmp.write(comp.flush())
                 break
             tmp.write(comp.compress(data))
         src.close()
         tmp.write('\r\n--' + MIME_BOUNDARY + '--\r\n\r\n')
         size = tmp.tell()
         tmp.seek(0)
         # open http connection
         runHook("fullSyncStarted", size)
         headers = {
             'Content-type':
             'multipart/form-data; boundary=%s' % MIME_BOUNDARY,
             'Content-length': str(size),
             'Host': SYNC_HOST,
         }
         req = urllib2.Request(SYNC_URL + "fullup?v=2", tmp, headers)
         try:
             sendProgressHook = fullSyncProgressHook
             res = urllib2.urlopen(req).read()
             assert res.startswith("OK")
             # update lastSync
             c = sqlite.connect(path)
             c.execute("update decks set lastSync = ?", (res[3:], ))
             c.commit()
             c.close()
         finally:
             sendProgressHook = None
             tmp.close()
             os.close(fd)
             os.unlink(name)
     finally:
         runHook("fullSyncFinished")
コード例 #4
0
ファイル: sync.py プロジェクト: cornercase/anki
 def fullSyncFromLocal(self, fields, path):
     global sendProgressHook
     try:
         # write into a temporary file, since POST needs content-length
         src = open(path, "rb")
         (fd, name) = tempfile.mkstemp(prefix="oldanki")
         tmp = open(name, "w+b")
         # post vars
         for (key, value) in fields.items():
             tmp.write("--" + MIME_BOUNDARY + "\r\n")
             tmp.write('Content-Disposition: form-data; name="%s"\r\n' % key)
             tmp.write("\r\n")
             tmp.write(value)
             tmp.write("\r\n")
         # file header
         tmp.write("--" + MIME_BOUNDARY + "\r\n")
         tmp.write('Content-Disposition: form-data; name="deck"; filename="deck"\r\n')
         tmp.write("Content-Type: application/octet-stream\r\n")
         tmp.write("\r\n")
         # data
         comp = zlib.compressobj()
         while 1:
             data = src.read(CHUNK_SIZE)
             if not data:
                 tmp.write(comp.flush())
                 break
             tmp.write(comp.compress(data))
         src.close()
         tmp.write("\r\n--" + MIME_BOUNDARY + "--\r\n\r\n")
         size = tmp.tell()
         tmp.seek(0)
         # open http connection
         runHook("fullSyncStarted", size)
         headers = {
             "Content-type": "multipart/form-data; boundary=%s" % MIME_BOUNDARY,
             "Content-length": str(size),
             "Host": SYNC_HOST,
         }
         req = urllib2.Request(SYNC_URL + "fullup?v=2", tmp, headers)
         try:
             sendProgressHook = fullSyncProgressHook
             res = urllib2.urlopen(req).read()
             assert res.startswith("OK")
             # update lastSync
             c = sqlite.connect(path)
             c.execute("update decks set lastSync = ?", (res[3:],))
             c.commit()
             c.close()
         finally:
             sendProgressHook = None
             tmp.close()
             os.close(fd)
             os.unlink(name)
     finally:
         runHook("fullSyncFinished")