File: objpull.py

package info (click to toggle)
python-sphobjinv 2.3.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,328 kB
  • sloc: python: 4,194; makefile: 29; sh: 20
file content (61 lines) | stat: -rw-r--r-- 1,605 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
61
# Quickie script for refreshing the local objects.inv cache
# OVERWRITES EXISTING FILES, WITH PRE-DELETION



def pullobjs():

    import os
    import urllib.request as urlrq

    import certifi

    # Open conf.py, retrieve content and compile
    with open(os.path.join(os.pardir, 'conf.py'), 'r') as f:
        confcode = compile(f.read(), 'conf.py', 'exec')

    # Execute conf.py into the global namespace (I know, sloppy)
    exec(confcode, globals())

    # Iterate intersphinx_mapping from conf.py to retrieve the objects.inv files
    # Make use of the conf.py 'isphx_objstr' substitution string, too
    for n, t in intersphinx_mapping.items():

        print('{0}:\n'.format(n) + '-' * 16)

        try:
            os.remove(isphx_objstr.format(n))
        except FileNotFoundError:
            pass # No big deal

        try:
            resp = urlrq.urlopen(t[0] + '/objects.inv', cafile=certifi.where())
        except Exception as e:
            print('HTTP request failed:\n' + str(e) + '\n')
            continue
        else:
            print('... located ...')

        try:
            b_s = resp.read()
        except Exception as e:
            print('Download failed:\n' + str(e) + '\n')
            continue
        else:
            print('... downloaded ...')

        try:
            with open(isphx_objstr.format(n), 'wb') as f:
                f.write(b_s)
        except Exception as e:
            print('Write failed:\n' + str(e) + '\n')
            continue
        else:
            print('... done.')

        print('')


if __name__ == '__main__':

    pullobjs()