infocalypse
 
(Steve Dougherty)
2013-06-07: Add WoT fn-pull support; default WoT truster.

Add WoT fn-pull support; default WoT truster. * Set up with fn-setupwot or fn-setup without --nowot. Takes --truster. * Add --truster override for fn-pull. Currently fn-pull with --wot uses WoT only to resolve the base key, but it would be desirable to be able to resolve the repo edition and handle its redundancy level via the WoT vcs XML.

diff --git a/infocalypse/__init__.py b/infocalypse/__init__.py
--- a/infocalypse/__init__.py
+++ b/infocalypse/__init__.py
@@ -512,9 +512,14 @@ def infocalypse_pull(ui_, repo, **opts):
         request_uri = get_uri_from_hash(ui_, repo, params, stored_cfg)
     elif opts['wot']:
         import wot
-        truster = stored_cfg.get_wot_identity(
-            stored_cfg.get_dir_insert_uri(repo.root))
-        # TODO: Require repo name, not full path - look it up from the XML.
+        if opts['truster']:
+            truster = opts['truster']
+        else :
+            truster = stored_cfg.get_wot_identity(
+                stored_cfg.get_dir_insert_uri(repo.root))
+        # TODO: Require repo name, not full path as part of the --wot. Look
+        # it up from the XML.
+        # TODO: Insert XML.
 
         # Expecting <id stuff>/reponame.R1/edition
         wot_id, repo_path = opts['wot'].split('/', 1)
@@ -780,11 +785,24 @@ def infocalypse_setup(ui_, **opts):
     else:
         ui_.status("Skipped FMS configuration because --nofms was set.\n")
 
+    if not opts['nowot']:
+        import wot
+        wot.execute_setup_wot(ui_, opts)
+    else:
+        ui_.status("Skipped WoT configuration because --nowot was set.\n")
+
 def infocalypse_setupfms(ui_, **opts):
     """ Setup or modify the fms configuration. """
     # REQUIRES config file.
     execute_setupfms(ui_, opts)
 
+
+# TODO: Why ui with trailing underscore? Is there a global "ui" somewhere?
+def infocalypse_setupwot(ui_, **opts):
+    import wot
+    wot.execute_setup_wot(ui_, opts)
+
+
 #----------------------------------------------------------"
 def do_archive_create(ui_, opts, params, stored_cfg):
     """ fn-archive --create."""
@@ -885,6 +903,8 @@ FMS_OPTS = [('', 'fmshost', '', 'fms hos
             ('', 'fmsport', 0, 'fms port'),
 ]
 
+WOT_OPTS = [('', 'truster', '', 'WoT identity to use when looking up others'),
+]
 
 AGGRESSIVE_OPT = [('', 'aggressive', None, 'aggressively search for the '
                    + 'latest USK index'),]
@@ -897,10 +917,9 @@ cmdtable = {
                 [('', 'uri', '', 'request URI to pull from'),
                  ('', 'hash', [], 'repo hash of repository to pull from'),
                  ('', 'wot', '', 'WoT nick@key/repo to pull from'),
-                 # TODO: Might want --truster override. (Previously set in
-                 # fn-create)
                  ('', 'onlytrusted', None, 'only use repo announcements from '
                   + 'known users')]
+                + WOT_OPTS
                 + FCP_OPTS
                 + NOSEARCH_OPT
                 + AGGRESSIVE_OPT,
@@ -994,6 +1013,7 @@ cmdtable = {
                   ('', 'nofms', None, 'skip FMS configuration'),
                   ('', 'fmsid', '', "fmsid (only part before '@'!)"),
                   ('', 'timeout', 30, "fms socket timeout in seconds")]
+                 + WOT_OPTS
                  + FCP_OPTS
                  + FMS_OPTS,
                 "[options]"),
@@ -1004,6 +1024,11 @@ cmdtable = {
                  + FMS_OPTS,
                 "[options]"),
 
+    "fn-setupwot": (infocalypse_setupwot,
+                    FCP_OPTS +
+                    WOT_OPTS,
+                    "[options]"),
+
     "fn-archive": (infocalypse_archive,
                   [('', 'uri', '', 'Request URI for --pull, Insert URI ' +
                     'for --create, --push'),
@@ -1026,3 +1051,4 @@ commands.norepo += ' fn-setup'
 commands.norepo += ' fn-setupfms'
 commands.norepo += ' fn-genkey'
 commands.norepo += ' fn-archive'
+commands.norepo += ' fn-setupwot'
diff --git a/infocalypse/config.py b/infocalypse/config.py
--- a/infocalypse/config.py
+++ b/infocalypse/config.py
@@ -206,7 +206,11 @@ class Config:
         Return the WoT identity associated with the request USK,
         or the default if none is set.
         """
-        return self.wot_identities[normalize(for_usk_or_id)]
+        repo_id = normalize(for_usk_or_id)
+        if repo_id in self.wot_identities:
+            return self.wot_identities[repo_id]
+        else:
+            return self.defaults['DEFAULT_TRUSTER']
 
     # Hmmm... really nescessary?
     def get_dir_insert_uri(self, repo_dir):
@@ -282,6 +286,9 @@ class Config:
             cfg.fmsread_groups = (parser.get('primary','fmsread_groups').
                                   strip().split('|'))
 
+        if parser.has_option('primary', 'default_truster'):
+            cfg.defaults['DEFAULT_TRUSTER'] = parser.get('primary',
+                                                         'default_truster')
 
 
     # Hmmm... would be better to detect_and_fix_default_bug()
@@ -379,6 +386,8 @@ class Config:
         parser.set('primary', 'fmsnotify_group',
                    cfg.defaults['FMSNOTIFY_GROUP'])
         parser.set('primary', 'fmsread_groups', '|'.join(cfg.fmsread_groups))
+        parser.set('primary', 'default_truster',
+                   cfg.defaults['DEFAULT_TRUSTER'])
 
         parser.add_section('index_values')
         for repo_id in cfg.version_table:
diff --git a/infocalypse/wot.py b/infocalypse/wot.py
--- a/infocalypse/wot.py
+++ b/infocalypse/wot.py
@@ -1,7 +1,36 @@
 import fcp
+from config import Config
 
 # Support for querying WoT for own identities and identities meeting various
 # criteria.
+# TODO: "cmds" suffix to module name to fit fms, arc, inf?
+
+def execute_setup_wot(ui_, opts):
+    cfg = Config.from_ui(ui_)
+    wot_id = opts['truster']
+
+    # TODO: Code for wot_id parsing duplicated between here and WoT pull.
+    nickname_prefix = ''
+    key_prefix=''
+    # Could be nick@key, nick, @key
+    split = wot_id.split('@')
+    nickname_prefix = split[0]
+
+    if len(split) == 2:
+        key_prefix = split[1]
+
+    # TODO: Support key
+    response = resolve_local_identity(ui_, nickname_prefix=nickname_prefix)
+
+    if response is None:
+        return
+
+    ui_.status("Setting default truster to {0}@{1}\n".format(
+        response['Nickname'],
+        response['Identity']))
+
+    cfg.defaults['DEFAULT_TRUSTER'] = response['Identity']
+    Config.to_file(cfg)
 
 def resolve_local_identity(ui, nickname_prefix=None):
     """