aboutsummaryrefslogtreecommitdiff
blob: 224f05672b250b9fcbe4d8bda4d99cfea7f6a97a (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
# vim: set sw=4 sts=4 et :
# Copyright: 2008 Gentoo Foundation
# Author(s): Nirbheek Chauhan <nirbheek.chauhan@gmail.com>
# License: GPL-2
#
# Immortal lh!
#

import os, subprocess
import os.path as osp

class Fetchable(object):
    """class representing uri sources for a file and chksum information."""

    def __init__(self, filename=None, uri=[], chksums=None):
        """
        @param filename: filename...
        @param uri: either None (no uri),
            or a sequence of uri where the file is available
        @param chksums: either None (no chksum data),
            or a dict of chksum_type -> value for this file
        """
        self.uri = uri
        if chksums is None:
            self.chksums = {}
        else:
            self.chksums = chksums
        self.filename = filename
        if not self.filename:
            self.filename = osp.basename(self.uri[0])

    def __str__(self):
        return self.filename


class Fetcher(object):
    
    def __init__(self, tarballdir):
        """
        @param tarballdir: directory to download files to
        @type tarballdir: string

        @param command: shell command to fetch a file
        @type command: string
        """
        self.tarballdir = tarballdir
        self.command = '/usr/bin/wget -c -t 5 -T 60 --tries=%(tries)s --passive-ftp -O "'+tarballdir+'/%(file)s" "%(uri)s"'

    def fetch(self, target):
        """
        fetch a file

        @type target: L{Fetchable} instance
        @return: None if fetching failed,
                 else on-disk location of the file
        """

        if not isinstance(target, Fetchable):
            raise TypeError("target must be fetchable instance/derivative: %s" % target)

        #file_params = { 'mode': 0775 }
        fp = os.path.join(self.tarballdir, target.filename)
        filename = os.path.basename(fp)
        index = len(target.uri) -1
        while index >= 0:
            subprocess.check_call(self.command % { 'file': filename, 'uri': target.uri[index], 'tries': 5 }, shell=True)
            index -= 1

if __name__ == "__main__":
    import autotua
    job = autotua.Jobs().getjobs()[0]
    fetcher = Fetcher(tmpdir)
    fetcher.fetch(job.stage)