~mgorven/ibid/irc-quit-343790

« back to all changes in this revision

Viewing changes to ibid/utils.py

  • Committer: Stefano Rivera
  • Date: 2009-03-05 14:49:50 UTC
  • mfrom: (551.6.11 rfc-plugin-330864)
  • Revision ID: stefano@rivera.za.net-20090305144950-xp8xmoj8u693lsqq
RFC Parser with file downloading and caching infrastructure.
https://code.edge.launchpad.net/~stefanor/ibid/rfc-plugin-330864/+merge/4040

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
import os
3
3
import os.path
4
4
import re
 
5
import time
 
6
import urllib2
 
7
 
 
8
import ibid
5
9
 
6
10
def ago(delta, units=None):
7
11
        parts = []
31
35
    entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
32
36
    return entity_re.subn(substitute_entity, string)[0]
33
37
 
 
38
def cacheable_download(url, cachefile):
 
39
        """Download url to cachefile if it's modified since cachefile.
 
40
        Specify cachefile in the form pluginname/cachefile.
 
41
        Returns complete path to downloaded file."""
 
42
 
 
43
        # We do allow absolute paths, for people who know what they are doing,
 
44
        # but the common use case should be pluginname/cachefile.
 
45
        if cachefile[0] not in (os.sep, os.altsep):
 
46
                cachedir = ibid.config.plugins['cachedir']
 
47
                if not cachedir:
 
48
                        cachedir = os.path.join(ibid.options['base'], 'cache')
 
49
                elif cachedir[0] == "~":
 
50
                        cachedir = os.path.expanduser(cachedir)
 
51
 
 
52
                plugindir = os.path.join(cachedir, os.path.dirname(cachefile))
 
53
                if not os.path.isdir(plugindir):
 
54
                        os.makedirs(plugindir)
 
55
        
 
56
                cachefile = os.path.join(cachedir, cachefile)
 
57
 
 
58
        exists = os.path.isfile(cachefile)
 
59
 
 
60
        req = urllib2.Request(url)
 
61
 
 
62
        if exists:
 
63
                modified = os.path.getmtime(cachefile)
 
64
                modified = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(modified))
 
65
                req.add_header("If-Modified-Since", modified)
 
66
 
 
67
        try:
 
68
                connection = urllib2.urlopen(req)
 
69
        except urllib2.HTTPError, e:
 
70
                if e.code == 304 and exists:
 
71
                        return cachefile
 
72
                else:
 
73
                        raise
 
74
        
 
75
        # Download into a temporary file, in case something goes wrong
 
76
        downloadfile = os.path.join(plugindir, ".download." + os.path.basename(cachefile))
 
77
        outfile = file(downloadfile, "wb")
 
78
        buf = "x"
 
79
        while len(buf) > 0:
 
80
                buf = connection.read(1024)
 
81
                outfile.write(buf)
 
82
        
 
83
        outfile.close()
 
84
 
 
85
        try:
 
86
                os.rename(downloadfile, cachefile)
 
87
        except OSError:
 
88
                # Are we on a system that doesn't support atomic renames?
 
89
                os.remove(cachefile)
 
90
                os.rename(downloadfile, cachefile)
 
91
        
 
92
        return cachefile
 
93
 
34
94
def file_in_path(program):
35
95
        path = os.environ.get("PATH", os.defpath).split(os.pathsep)
36
96
        path = [os.path.join(dir, program) for dir in path]