summaryrefslogtreecommitdiffstats
path: root/feed/sse_grab.py
blob: 095081e3cfa3fd2020dd798c0e2b5b0fc9615609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python

import sys, urllib2, os, socket, httplib

import sse_tar, sse_lock
from sse_defs import *
from sse_config import *

socket.setdefaulttimeout(SSE_SOCKET_TIMEOUT)

def grab_archive(meta, recid, provider_id = SSE_PROVIDER_NONE):
    
    try:
        os.mkdir("%s/download" % HOME)
    except:
        pass
    
    fn = os.path.join(HOME, "download", recid)
    lock_fn = fn + ".lock"

    if not sse_lock.try_lock_file(lock_fn):
        print "Skipping locked archive '%s'." % recid
        return

    try:
        download = False

        try:
            f = open(fn+".release", "r")
        except IOError:
            download = True
        else:
            download = f.read(SSE_BLOCK_SIZE) != meta["version"]
            f.close()

        try:
            archive_url = meta["archive-url-download"]
        except KeyError:
            archive_url = meta["archive-url"]

        if not download:
            print "File %s up-to-date." % archive_url
            return

        print "Downloading %s..." % archive_url

        dst = None
        src = None
        
        try:
            dst = file(fn, "w")
            src = urllib2.urlopen(archive_url)
            m = 0

            while True:

                if m > SSE_MAX_DOWNLOAD:
                    os.unlink(fn)
                    print "WARNING: Archive too large, ignoring."
                    return

                data = src.read(SSE_BLOCK_SIZE)

                if len(data) <= 0:
                    break
        
                dst.write(data)
                m += len(data)

            dst.close()
            del src

        except KeyboardInterrupt, e:
            raise e
        
        except Exception, e:
            os.unlink(fn)
            print "WARNING: Failed to download %s: %s!" % (archive_url, e)
            return

        sse_tar.process_archive(fn, meta, recid, provider_id)
        os.unlink(fn)

        try:
            f = open(fn+".release", "w")
        except:
            os.unlink(fn)
        
        f.write(meta["version"])
        f.close()

    finally:
        sse_lock.unlock_file(lock_fn)