~store-reviewers/click-reviewers-tools/trunk

« back to all changes in this revision

Viewing changes to clickreviews/remote.py

  • Committer: Jamie Strandboge
  • Date: 2014-09-22 20:01:21 UTC
  • mfrom: (239.1.2 manual-review-flag)
  • Revision ID: jamie@ubuntu.com-20140922200121-yvvazt86rz835ymp
Ricardo Kirkner: updated frameworks.json using myapps api

Acked-By: Daniel Holbach

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
#
53
53
# Public
54
54
#
55
 
def get_remote_file(fn, url, data_dir=DATA_DIR):
 
55
def get_remote_data(url):
56
56
    try:
57
57
        f = request.urlopen(url)
58
58
    except (HTTPError, URLError) as error:
59
59
        abort('Data not retrieved because %s.' % error)
60
60
    except timeout:
61
61
        abort('Socket timed out.')
62
 
    html = f.read()
 
62
    if not f:
 
63
        abort()
 
64
    return f.read()
 
65
 
 
66
 
 
67
def get_remote_file_url(url):
 
68
    html = get_remote_data(url)
63
69
    # XXX: This is a hack and will be gone, as soon as myapps has an API for this.
64
70
    link = re.findall(b'<a href="(\S+?)">download file</a>', html)
65
71
    if not link:
68
74
        parse.urlparse(url).scheme,
69
75
        parse.urlparse(url).netloc,
70
76
        link[0].decode("utf-8"))
71
 
    f = request.urlopen(download_link)
72
 
    if not f:
73
 
        abort()
 
77
    return download_link
 
78
 
 
79
 
 
80
def get_remote_file(fn, url, data_dir=DATA_DIR):
 
81
    data = get_remote_data(url)
74
82
    if os.path.exists(fn):
75
83
        os.remove(fn)
76
84
    if not os.path.exists(os.path.dirname(fn)):
77
85
        os.makedirs(os.path.dirname(fn))
78
86
    with open(fn, 'bw') as local_file:
79
 
        local_file.write(f.read())
 
87
        local_file.write(data)
80
88
 
81
89
 
82
90
def read_cr_file(fn, url, local_copy_fn=None):