Replace tpo git repository URL by gitlab
[stem.git] / cache_fallback_directories.py
blob8fe425d1a1bf8bacb04e4ce2c4c17fa93aa2bfc5
1 #!/usr/bin/env python
2 # Copyright 2016-2020, Damian Johnson and The Tor Project
3 # See LICENSE for licensing information
5 """
6 Caches tor's latest fallback directories.
7 """
9 import re
10 import sys
11 import urllib.request
13 import stem.directory
14 import stem.util.system
16 GITWEB_FALLBACK_LOG = 'https://gitweb.torproject.org/tor.git/log/src/app/config/fallback_dirs.inc'
17 FALLBACK_DIR_LINK = b"href='/tor.git/commit/src/app/config/fallback_dirs.inc\\?id=([^']*)'"
19 if __name__ == '__main__':
20 try:
21 fallback_dir_page = urllib.request.urlopen(GITWEB_FALLBACK_LOG).read()
22 fallback_dir_commit = re.search(FALLBACK_DIR_LINK, fallback_dir_page).group(1).decode('utf-8')
23 except:
24 print("Unable to determine the latest commit to edit tor's fallback directories: %s" % sys.exc_info()[1])
25 sys.exit(1)
27 try:
28 stem_commit = stem.util.system.call('git rev-parse HEAD')[0]
29 except OSError as exc:
30 print("Unable to determine stem's current commit: %s" % exc)
31 sys.exit(1)
33 print('Latest tor commit editing fallback directories: %s' % fallback_dir_commit)
34 print('Current stem commit: %s' % stem_commit)
35 print('')
37 cached_fallback_directories = stem.directory.Fallback.from_cache()
38 latest_fallback_directories = stem.directory.Fallback.from_remote()
40 if cached_fallback_directories == latest_fallback_directories:
41 print('Fallback directories are already up to date, nothing to do.')
42 sys.exit(0)
44 # all fallbacks have the same header metadata, so just picking one
46 headers = list(latest_fallback_directories.values())[0].header if latest_fallback_directories else None
48 print('Differences detected...\n')
49 print(stem.directory._fallback_directory_differences(cached_fallback_directories, latest_fallback_directories))
50 stem.directory.Fallback._write(latest_fallback_directories, fallback_dir_commit, stem_commit, headers)