1#!/usr/bin/env python
2# Copyright 2015-2019, Damian Johnson and The Tor Project
3# See LICENSE for licensing information
4
5"""
6Caches tor's latest manual content. Run this to pick new man page changes.
7"""
8
9import re
10import sys
11
12import stem.manual
13import stem.util.system
14
15try:
16  # account for urllib's change between python 2.x and 3.x
17  import urllib.request as urllib
18except ImportError:
19  import urllib2 as urllib
20
21GITWEB_MAN_LOG = 'https://gitweb.torproject.org/tor.git/log/doc/tor.1.txt'
22MAN_LOG_LINK = "href='/tor.git/commit/doc/tor.1.txt\\?id=([^']*)'"
23
24if __name__ == '__main__':
25  try:
26    man_log_page = urllib.urlopen(GITWEB_MAN_LOG).read()
27    man_commit = re.search(MAN_LOG_LINK, man_log_page).group(1)
28  except:
29    print("Unable to determine the latest commit to edit tor's man page: %s" % sys.exc_info()[1])
30    sys.exit(1)
31
32  try:
33    stem_commit = stem.util.system.call('git rev-parse HEAD')[0]
34  except IOError as exc:
35    print("Unable to determine stem's current commit: %s" % exc)
36    sys.exit(1)
37
38  print('Latest tor commit editing man page: %s' % man_commit)
39  print('Current stem commit: %s' % stem_commit)
40  print('')
41
42  try:
43    cached_manual = stem.manual.Manual.from_cache()
44    db_schema = cached_manual.schema
45  except stem.manual.SchemaMismatch as exc:
46    cached_manual, db_schema = None, exc.database_schema
47  except IOError:
48    cached_manual, db_schema = None, None  # local copy has been deleted
49
50  if db_schema != stem.manual.SCHEMA_VERSION:
51    print('Cached database schema is out of date (was %s, but current version is %s)' % (db_schema, stem.manual.SCHEMA_VERSION))
52    cached_manual = None
53
54  latest_manual = stem.manual.Manual.from_remote()
55
56  if cached_manual:
57    if cached_manual == latest_manual:
58      print('Manual information is already up to date, nothing to do.')
59      sys.exit(0)
60
61    print('Differences detected...\n')
62    print(stem.manual._manual_differences(cached_manual, latest_manual))
63
64  latest_manual.man_commit = man_commit
65  latest_manual.stem_commit = stem_commit
66  latest_manual.save(stem.manual.CACHE_PATH)
67