File: cache_fallback_directories.py

package info (click to toggle)
python-stem 1.7.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 5,768 kB
  • sloc: python: 29,441; java: 312; makefile: 125; sh: 17
file content (55 lines) | stat: -rwxr-xr-x 1,986 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
# Copyright 2016-2018, Damian Johnson and The Tor Project
# See LICENSE for licensing information

"""
Caches tor's latest fallback directories.
"""

import re
import sys

import stem.descriptor.remote
import stem.util.system

try:
  # account for urllib's change between python 2.x and 3.x
  import urllib.request as urllib
except ImportError:
  import urllib2 as urllib

GITWEB_MAN_LOG = 'https://gitweb.torproject.org/tor.git/log/src/or/fallback_dirs.inc'
FALLBACK_DIR_LINK = "href='/tor.git/commit/src/or/fallback_dirs.inc\?id=([^']*)'"

if __name__ == '__main__':
  try:
    fallback_dir_page = urllib.urlopen(GITWEB_MAN_LOG).read()
    fallback_dir_commit = re.search(FALLBACK_DIR_LINK, fallback_dir_page).group(1)
  except:
    print("Unable to determine the latest commit to edit tor's fallback directories: %s" % sys.exc_info()[1])
    sys.exit(1)

  try:
    stem_commit = stem.util.system.call('git rev-parse HEAD')[0]
  except IOError as exc:
    print("Unable to determine stem's current commit: %s" % exc)
    sys.exit(1)

  print('Latest tor commit editing fallback directories: %s' % fallback_dir_commit)
  print('Current stem commit: %s' % stem_commit)
  print('')

  cached_fallback_directories = stem.descriptor.remote.FallbackDirectory.from_cache()
  latest_fallback_directories = stem.descriptor.remote.FallbackDirectory.from_remote()

  if cached_fallback_directories == latest_fallback_directories:
    print('Fallback directories are already up to date, nothing to do.')
    sys.exit(0)

  # all fallbacks have the same header metadata, so just picking one

  headers = latest_fallback_directories.values()[0].header if latest_fallback_directories else None

  print('Differences detected...\n')
  print(stem.descriptor.remote._fallback_directory_differences(cached_fallback_directories, latest_fallback_directories))
  stem.descriptor.remote.FallbackDirectory._write(latest_fallback_directories, fallback_dir_commit, stem_commit, headers)