[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Adding a 'Scripts' section to our examples page



commit c26169071a6a5de9c9c5bf2e9ef185e84355ea9f
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Mar 25 10:10:08 2014 -0700

    Adding a 'Scripts' section to our examples page
    
    Coderman suggested a cookbook section on #tor-dev. I like this idea. On
    occasion I write one-off scripts for specific tasks. These would make fantastic
    examples for new Stem users, so starting a section on our examples page for
    them.
    
    I recall writing another script for 'list the contact information of all
    outdated relays' for a ticket. I'll dig that up later and add it.
---
 docs/contents.rst                                 |    2 +
 docs/tutorials/double_double_toil_and_trouble.rst |   14 +++++
 docs/tutorials/examples/compare_flags.rst         |   60 +++++++++++++++++++++
 3 files changed, 76 insertions(+)

diff --git a/docs/contents.rst b/docs/contents.rst
index fbb0ecd..d9b6159 100644
--- a/docs/contents.rst
+++ b/docs/contents.rst
@@ -12,6 +12,8 @@ Contents
    tutorials/mirror_mirror_on_the_wall
    tutorials/double_double_toil_and_trouble
 
+   tutorials/examples/compare_flags
+
    change_log
    download
 
diff --git a/docs/tutorials/double_double_toil_and_trouble.rst b/docs/tutorials/double_double_toil_and_trouble.rst
index 0da647a..3dabc1a 100644
--- a/docs/tutorials/double_double_toil_and_trouble.rst
+++ b/docs/tutorials/double_double_toil_and_trouble.rst
@@ -5,6 +5,9 @@ Below is a listing of scripts and applications that use Stem. If you have
 something you would like to have included on this page then `let me know
 <https://www.atagar.com/contact/>`_!
 
+Applications
+============
+
 .. Image Sources:
    
    * Arm
@@ -47,3 +50,14 @@ something you would like to have included on this page then `let me know
 `ExitMap <https://github.com/NullHypothesis/exitmap>`_                                                      Scanner for malicious or misconfigured Tor exits.
 =========================================================================================================== ==========
 
+Scripts
+=======
+
+Descriptors
+-----------
+
+* `Comparing Directory Authority Flags <examples/compare_flags.html>`_
+
+  Compares the votes of two directory authorities, in this case moria1 and
+  maatuska, with a special interest in the 'Running' flag.
+
diff --git a/docs/tutorials/examples/compare_flags.rst b/docs/tutorials/examples/compare_flags.rst
new file mode 100644
index 0000000..b0f7fc6
--- /dev/null
+++ b/docs/tutorials/examples/compare_flags.rst
@@ -0,0 +1,60 @@
+Comparing Directory Authority Flags
+===================================
+
+Compares the votes of two directory authorities, in this case moria1 and
+maatuska, with a special interest in the 'Running' flag.
+
+::
+
+  % python compare_flags.py 
+  maatuska has the Running flag but moria1 doesn't: 92FCB6748A40E6088E22FBAB943AB2DD743EA818
+  maatuska has the Running flag but moria1 doesn't: 6871F682350BA931838C0EC1E4A23044DAE06A73
+  maatuska has the Running flag but moria1 doesn't: E2BB13AA2F6960CD93ABE5257A825687F3973C62
+  moria1 has the Running flag but maatuska doesn't: 546C54E2A89D88E0794D04AECBF1AC8AC9DA81DE
+  moria1 has the Running flag but maatuska doesn't: DCAEC3D069DC39AAE43D13C8AF31B5645E05ED61
+  ...
+
+::
+
+  from stem.descriptor import DocumentHandler, remote
+
+  # Query all authority votes asynchronously.
+
+  downloader = remote.DescriptorDownloader(document_handler = DocumentHandler.DOCUMENT)
+  queries = {}
+
+  for name, authority in remote.get_authorities().items():
+    if authority.v3ident is None:
+      continue  # authority doens't vote if it lacks a v3ident
+
+    queries[name] = downloader.get_vote(authority)
+
+  # Wait for the votes to finish being downloaded, this produces a dictionary of
+  # authority nicknames to their vote.
+
+  votes = dict((name, query.run()[0]) for (name, query) in queries.items())
+
+  # Get a superset of all the fingerprints in all the votes.
+
+  all_fingerprints = set()
+
+  for vote in votes.values():
+    all_fingerprints.update(vote.routers.keys())
+
+  # Finally, compare moria1's votes to maatuska.
+
+  for fingerprint in all_fingerprints:
+    moria1_vote = votes['moria1'].routers.get(fingerprint)
+    maatuska_vote = votes['maatuska'].routers.get(fingerprint)
+
+    if not moria1_vote and not maatuska_vote:
+      print "both moria1 and maatuska haven't voted about %s" % fingerprint
+    elif not moria1_vote:
+      print "moria1 hasn't voted about %s" % fingerprint
+    elif not maatuska_vote:
+      print "maatuska hasn't voted about %s" % fingerprint
+    elif 'Running' in moria1_vote.flags and 'Running' not in maatuska_vote.flags:
+      print "moria1 has the Running flag but maatuska doesn't: %s" % fingerprint
+    elif 'Running' in maatuska_vote.flags and 'Running' not in moria1_vote.flags:
+      print "maatuska has the Running flag but moria1 doesn't: %s" % fingerprint
+

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits