File: fetch_libevent.py

package info (click to toggle)
python-gevent 0.13.6-1%2Bnmu3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,324 kB
  • sloc: python: 13,296; makefile: 95; ansic: 37
file content (60 lines) | stat: -rwxr-xr-x 1,733 bytes parent folder | download
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
#! /usr/bin/env python

"""download and extract the libevent source archive
"""

import sys
import os
import urllib
import tarfile
import StringIO
import tempfile
import shutil
import copy

try:
    from hashlib import md5
except ImportError:
    from md5 import md5


# python 2.4's tarfile doesn't have extractall.
def extractall(self, path="."):
    for tarinfo in self:
        if tarinfo.isdir():
            # Extract directories with a safe mode.
            tarinfo = copy.copy(tarinfo)
            tarinfo.mode = 0700
        self.extract(tarinfo, path)


def download_and_extract(url, digest):
    assert url.endswith(".tar.gz"), "can only download .tar.gz files"
    dst = os.path.abspath("libevent-src")

    if os.path.exists(dst):
        sys.exit("Error: path %s already exists" % dst)

    tmpdir = tempfile.mkdtemp(prefix="tmp-libevent-src", dir=".")
    try:
        dirname = os.path.join(tmpdir, url.split("/")[-1][:-len(".tar.gz")])
        print "downloading libevent source from %s" % url
        tgz = urllib.urlopen(url).read()
        if md5(tgz).hexdigest() != digest:
            sys.exit("Error: wrong md5 sum")

        print "extracting to %s" % dst
        tf = tarfile.open("libevent-src.tar.gz",
                          fileobj=StringIO.StringIO(tgz),
                          mode='r:gz')
        extractall(tf, tmpdir)
        os.rename(dirname, dst)
        print "setup.py will now build libevent and link with it statically"
    finally:
        shutil.rmtree(tmpdir)

if __name__ == '__main__':
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    download_and_extract(
        "http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz",
        "a00e037e4d3f9e4fe9893e8a2d27918c")