2019-03-24 13:14:00 +00:00
|
|
|
from __future__ import print_function
|
2015-10-01 21:40:10 +00:00
|
|
|
|
|
|
|
import os
|
2015-11-12 23:16:28 +00:00
|
|
|
import urllib
|
2015-10-20 16:13:58 +00:00
|
|
|
from types import NoneType
|
2019-03-24 14:04:00 +00:00
|
|
|
from six.moves import cStringIO as StringIO
|
2015-11-12 23:16:28 +00:00
|
|
|
from datetime import datetime
|
2017-01-19 22:39:53 +00:00
|
|
|
import json
|
2015-11-12 23:16:28 +00:00
|
|
|
|
2015-10-20 16:13:58 +00:00
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
from twisted.python import usage
|
|
|
|
|
2015-10-20 16:13:58 +00:00
|
|
|
from allmydata.util.assertutil import precondition
|
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
from .common import BaseOptions, BasedirOptions, get_aliases
|
|
|
|
from .cli import MakeDirectoryOptions, LnOptions, CreateAliasOptions
|
|
|
|
import tahoe_mv
|
2015-12-02 15:54:01 +00:00
|
|
|
from allmydata.util.encodingutil import argv_to_abspath, argv_to_unicode, to_str, \
|
|
|
|
quote_local_unicode_path
|
2016-02-09 15:30:06 +00:00
|
|
|
from allmydata.scripts.common_http import do_http, BadResponse
|
2015-10-01 21:40:10 +00:00
|
|
|
from allmydata.util import fileutil
|
|
|
|
from allmydata import uri
|
2016-02-03 19:38:16 +00:00
|
|
|
from allmydata.util.abbreviate import abbreviate_space, abbreviate_time
|
2017-08-23 21:34:11 +00:00
|
|
|
from allmydata.frontends.magic_folder import load_magic_folders
|
|
|
|
from allmydata.frontends.magic_folder import save_magic_folders
|
|
|
|
from allmydata.frontends.magic_folder import maybe_upgrade_magic_folders
|
2015-10-01 21:40:10 +00:00
|
|
|
|
2015-11-12 23:16:28 +00:00
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
INVITE_SEPARATOR = "+"
|
|
|
|
|
|
|
|
class CreateOptions(BasedirOptions):
|
2017-08-23 21:34:11 +00:00
|
|
|
nickname = None # NOTE: *not* the "name of this magic-folder"
|
2015-10-20 13:52:00 +00:00
|
|
|
local_dir = None
|
|
|
|
synopsis = "MAGIC_ALIAS: [NICKNAME LOCAL_DIR]"
|
2016-12-15 02:49:47 +00:00
|
|
|
optParameters = [
|
|
|
|
("poll-interval", "p", "60", "How often to ask for updates"),
|
2017-08-23 21:34:11 +00:00
|
|
|
("name", "n", "default", "The name of this magic-folder"),
|
2016-12-15 02:49:47 +00:00
|
|
|
]
|
2017-08-23 21:34:11 +00:00
|
|
|
description = (
|
|
|
|
"Create a new magic-folder. If you specify NICKNAME and "
|
|
|
|
"LOCAL_DIR, this client will also be invited and join "
|
|
|
|
"using the given nickname. A new alias (see 'tahoe list-aliases') "
|
|
|
|
"will be added with the master folder's writecap."
|
|
|
|
)
|
2016-12-15 02:49:47 +00:00
|
|
|
|
2015-10-20 13:52:00 +00:00
|
|
|
def parseArgs(self, alias, nickname=None, local_dir=None):
|
2015-10-01 21:40:10 +00:00
|
|
|
BasedirOptions.parseArgs(self)
|
2015-10-20 16:13:58 +00:00
|
|
|
alias = argv_to_unicode(alias)
|
|
|
|
if not alias.endswith(u':'):
|
2015-10-01 21:40:10 +00:00
|
|
|
raise usage.UsageError("An alias must end with a ':' character.")
|
|
|
|
self.alias = alias[:-1]
|
2015-10-20 16:13:58 +00:00
|
|
|
self.nickname = None if nickname is None else argv_to_unicode(nickname)
|
2016-12-15 02:49:47 +00:00
|
|
|
try:
|
|
|
|
if int(self['poll-interval']) <= 0:
|
|
|
|
raise ValueError("should be positive")
|
|
|
|
except ValueError:
|
|
|
|
raise usage.UsageError(
|
|
|
|
"--poll-interval must be a positive integer"
|
|
|
|
)
|
2015-10-23 22:13:43 +00:00
|
|
|
|
|
|
|
# Expand the path relative to the current directory of the CLI command, not the node.
|
|
|
|
self.local_dir = None if local_dir is None else argv_to_abspath(local_dir, long_path=False)
|
|
|
|
|
2015-10-20 13:52:00 +00:00
|
|
|
if self.nickname and not self.local_dir:
|
|
|
|
raise usage.UsageError("If NICKNAME is specified then LOCAL_DIR must also be specified.")
|
2015-10-23 22:13:43 +00:00
|
|
|
node_url_file = os.path.join(self['node-directory'], u"node.url")
|
2015-10-01 21:40:10 +00:00
|
|
|
self['node-url'] = fileutil.read(node_url_file).strip()
|
|
|
|
|
|
|
|
def _delegate_options(source_options, target_options):
|
|
|
|
target_options.aliases = get_aliases(source_options['node-directory'])
|
|
|
|
target_options["node-url"] = source_options["node-url"]
|
|
|
|
target_options["node-directory"] = source_options["node-directory"]
|
2017-08-23 21:34:11 +00:00
|
|
|
target_options["name"] = source_options["name"]
|
2015-10-01 21:40:10 +00:00
|
|
|
target_options.stdin = StringIO("")
|
|
|
|
target_options.stdout = StringIO()
|
|
|
|
target_options.stderr = StringIO()
|
|
|
|
return target_options
|
|
|
|
|
|
|
|
def create(options):
|
2015-10-20 16:13:58 +00:00
|
|
|
precondition(isinstance(options.alias, unicode), alias=options.alias)
|
|
|
|
precondition(isinstance(options.nickname, (unicode, NoneType)), nickname=options.nickname)
|
|
|
|
precondition(isinstance(options.local_dir, (unicode, NoneType)), local_dir=options.local_dir)
|
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
# make sure we don't already have a magic-folder with this name before we create the alias
|
|
|
|
maybe_upgrade_magic_folders(options["node-directory"])
|
|
|
|
folders = load_magic_folders(options["node-directory"])
|
|
|
|
if options['name'] in folders:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Already have a magic-folder named '{}'".format(options['name']), file=options.stderr)
|
2017-08-23 21:34:11 +00:00
|
|
|
return 1
|
|
|
|
|
|
|
|
# create an alias; this basically just remembers the cap for the
|
|
|
|
# master directory
|
2015-10-01 21:40:10 +00:00
|
|
|
from allmydata.scripts import tahoe_add_alias
|
|
|
|
create_alias_options = _delegate_options(options, CreateAliasOptions())
|
|
|
|
create_alias_options.alias = options.alias
|
|
|
|
|
|
|
|
rc = tahoe_add_alias.create_alias(create_alias_options)
|
|
|
|
if rc != 0:
|
2019-03-24 13:14:00 +00:00
|
|
|
print(create_alias_options.stderr.getvalue(), file=options.stderr)
|
2015-10-01 21:40:10 +00:00
|
|
|
return rc
|
2019-03-24 13:14:00 +00:00
|
|
|
print(create_alias_options.stdout.getvalue(), file=options.stdout)
|
2015-10-01 21:40:10 +00:00
|
|
|
|
|
|
|
if options.nickname is not None:
|
2019-03-24 13:14:00 +00:00
|
|
|
print(u"Inviting myself as client '{}':".format(options.nickname), file=options.stdout)
|
2015-10-01 21:40:10 +00:00
|
|
|
invite_options = _delegate_options(options, InviteOptions())
|
|
|
|
invite_options.alias = options.alias
|
|
|
|
invite_options.nickname = options.nickname
|
2017-08-23 21:34:11 +00:00
|
|
|
invite_options['name'] = options['name']
|
2015-10-01 21:40:10 +00:00
|
|
|
rc = invite(invite_options)
|
|
|
|
if rc != 0:
|
2019-03-24 13:14:00 +00:00
|
|
|
print(u"magic-folder: failed to invite after create\n", file=options.stderr)
|
|
|
|
print(invite_options.stderr.getvalue(), file=options.stderr)
|
2015-10-01 21:40:10 +00:00
|
|
|
return rc
|
|
|
|
invite_code = invite_options.stdout.getvalue().strip()
|
2019-03-24 13:14:00 +00:00
|
|
|
print(u" created invite code", file=options.stdout)
|
2015-10-01 21:40:10 +00:00
|
|
|
join_options = _delegate_options(options, JoinOptions())
|
2016-12-15 05:20:45 +00:00
|
|
|
join_options['poll-interval'] = options['poll-interval']
|
2017-08-23 21:34:11 +00:00
|
|
|
join_options.nickname = options.nickname
|
2015-10-20 13:52:00 +00:00
|
|
|
join_options.local_dir = options.local_dir
|
2015-10-20 20:19:04 +00:00
|
|
|
join_options.invite_code = invite_code
|
2015-10-01 21:40:10 +00:00
|
|
|
rc = join(join_options)
|
|
|
|
if rc != 0:
|
2019-03-24 13:14:00 +00:00
|
|
|
print(u"magic-folder: failed to join after create\n", file=options.stderr)
|
|
|
|
print(join_options.stderr.getvalue(), file=options.stderr)
|
2015-10-01 21:40:10 +00:00
|
|
|
return rc
|
2019-03-24 13:14:00 +00:00
|
|
|
print(u" joined new magic-folder", file=options.stdout)
|
|
|
|
print(
|
2017-08-23 21:34:11 +00:00
|
|
|
u"Successfully created magic-folder '{}' with alias '{}:' "
|
|
|
|
u"and client '{}'\nYou must re-start your node before the "
|
|
|
|
u"magic-folder will be active."
|
2019-03-24 13:14:00 +00:00
|
|
|
.format(options['name'], options.alias, options.nickname), file=options.stdout)
|
2017-08-23 21:34:11 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
class ListOptions(BasedirOptions):
|
|
|
|
description = (
|
|
|
|
"List all magic-folders this client has joined"
|
|
|
|
)
|
|
|
|
optFlags = [
|
|
|
|
("json", "", "Produce JSON output")
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def list_(options):
|
|
|
|
folders = load_magic_folders(options["node-directory"])
|
|
|
|
if options["json"]:
|
|
|
|
_list_json(options, folders)
|
|
|
|
return 0
|
|
|
|
_list_human(options, folders)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def _list_json(options, folders):
|
|
|
|
"""
|
|
|
|
List our magic-folders using JSON
|
|
|
|
"""
|
|
|
|
info = dict()
|
|
|
|
for name, details in folders.items():
|
|
|
|
info[name] = {
|
|
|
|
u"directory": details["directory"],
|
|
|
|
}
|
2019-03-24 13:14:00 +00:00
|
|
|
print(json.dumps(info), file=options.stdout)
|
2015-10-01 21:40:10 +00:00
|
|
|
return 0
|
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
|
|
|
|
def _list_human(options, folders):
|
|
|
|
"""
|
|
|
|
List our magic-folders for a human user
|
|
|
|
"""
|
|
|
|
if folders:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("This client has the following magic-folders:", file=options.stdout)
|
2017-08-23 21:34:11 +00:00
|
|
|
biggest = max([len(nm) for nm in folders.keys()])
|
|
|
|
fmt = " {:>%d}: {}" % (biggest, )
|
|
|
|
for name, details in folders.items():
|
2019-03-24 13:14:00 +00:00
|
|
|
print(fmt.format(name, details["directory"]), file=options.stdout)
|
2017-08-23 21:34:11 +00:00
|
|
|
else:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("No magic-folders", file=options.stdout)
|
2017-08-23 21:34:11 +00:00
|
|
|
|
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
class InviteOptions(BasedirOptions):
|
|
|
|
nickname = None
|
|
|
|
synopsis = "MAGIC_ALIAS: NICKNAME"
|
|
|
|
stdin = StringIO("")
|
2017-08-23 21:34:11 +00:00
|
|
|
optParameters = [
|
|
|
|
("name", "n", "default", "The name of this magic-folder"),
|
|
|
|
]
|
|
|
|
description = (
|
|
|
|
"Invite a new participant to a given magic-folder. The resulting "
|
|
|
|
"invite-code that is printed is secret information and MUST be "
|
|
|
|
"transmitted securely to the invitee."
|
|
|
|
)
|
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
def parseArgs(self, alias, nickname=None):
|
|
|
|
BasedirOptions.parseArgs(self)
|
2015-10-20 16:13:58 +00:00
|
|
|
alias = argv_to_unicode(alias)
|
|
|
|
if not alias.endswith(u':'):
|
2015-10-01 21:40:10 +00:00
|
|
|
raise usage.UsageError("An alias must end with a ':' character.")
|
|
|
|
self.alias = alias[:-1]
|
2015-10-20 16:13:58 +00:00
|
|
|
self.nickname = argv_to_unicode(nickname)
|
2015-10-23 22:13:43 +00:00
|
|
|
node_url_file = os.path.join(self['node-directory'], u"node.url")
|
2015-10-01 21:40:10 +00:00
|
|
|
self['node-url'] = open(node_url_file, "r").read().strip()
|
|
|
|
aliases = get_aliases(self['node-directory'])
|
|
|
|
self.aliases = aliases
|
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
def invite(options):
|
2015-10-20 16:13:58 +00:00
|
|
|
precondition(isinstance(options.alias, unicode), alias=options.alias)
|
|
|
|
precondition(isinstance(options.nickname, unicode), nickname=options.nickname)
|
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
from allmydata.scripts import tahoe_mkdir
|
|
|
|
mkdir_options = _delegate_options(options, MakeDirectoryOptions())
|
|
|
|
mkdir_options.where = None
|
|
|
|
|
|
|
|
rc = tahoe_mkdir.mkdir(mkdir_options)
|
|
|
|
if rc != 0:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("magic-folder: failed to mkdir\n", file=options.stderr)
|
2015-10-01 21:40:10 +00:00
|
|
|
return rc
|
2015-10-20 16:16:32 +00:00
|
|
|
|
|
|
|
# FIXME this assumes caps are ASCII.
|
2015-10-01 21:40:10 +00:00
|
|
|
dmd_write_cap = mkdir_options.stdout.getvalue().strip()
|
2015-10-20 16:16:32 +00:00
|
|
|
dmd_readonly_cap = uri.from_string(dmd_write_cap).get_readonly().to_string()
|
2015-10-01 21:40:10 +00:00
|
|
|
if dmd_readonly_cap is None:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("magic-folder: failed to diminish dmd write cap\n", file=options.stderr)
|
2015-10-01 21:40:10 +00:00
|
|
|
return 1
|
|
|
|
|
|
|
|
magic_write_cap = get_aliases(options["node-directory"])[options.alias]
|
2015-10-20 16:16:32 +00:00
|
|
|
magic_readonly_cap = uri.from_string(magic_write_cap).get_readonly().to_string()
|
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
# tahoe ln CLIENT_READCAP COLLECTIVE_WRITECAP/NICKNAME
|
|
|
|
ln_options = _delegate_options(options, LnOptions())
|
2015-10-20 16:16:32 +00:00
|
|
|
ln_options.from_file = unicode(dmd_readonly_cap, 'utf-8')
|
|
|
|
ln_options.to_file = u"%s/%s" % (unicode(magic_write_cap, 'utf-8'), options.nickname)
|
2015-10-01 21:40:10 +00:00
|
|
|
rc = tahoe_mv.mv(ln_options, mode="link")
|
|
|
|
if rc != 0:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("magic-folder: failed to create link\n", file=options.stderr)
|
|
|
|
print(ln_options.stderr.getvalue(), file=options.stderr)
|
2015-10-01 21:40:10 +00:00
|
|
|
return rc
|
|
|
|
|
2015-10-20 16:16:32 +00:00
|
|
|
# FIXME: this assumes caps are ASCII.
|
2019-03-24 13:14:00 +00:00
|
|
|
print("%s%s%s" % (magic_readonly_cap, INVITE_SEPARATOR, dmd_write_cap), file=options.stdout)
|
2015-10-01 21:40:10 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
class JoinOptions(BasedirOptions):
|
|
|
|
synopsis = "INVITE_CODE LOCAL_DIR"
|
|
|
|
dmd_write_cap = ""
|
|
|
|
magic_readonly_cap = ""
|
2016-12-15 02:49:47 +00:00
|
|
|
optParameters = [
|
|
|
|
("poll-interval", "p", "60", "How often to ask for updates"),
|
2017-08-23 21:34:11 +00:00
|
|
|
("name", "n", "default", "Name of the magic-folder"),
|
2016-12-15 02:49:47 +00:00
|
|
|
]
|
|
|
|
|
2015-10-20 11:08:14 +00:00
|
|
|
def parseArgs(self, invite_code, local_dir):
|
|
|
|
BasedirOptions.parseArgs(self)
|
2015-10-23 21:04:08 +00:00
|
|
|
|
2016-12-15 02:49:47 +00:00
|
|
|
try:
|
|
|
|
if int(self['poll-interval']) <= 0:
|
|
|
|
raise ValueError("should be positive")
|
|
|
|
except ValueError:
|
|
|
|
raise usage.UsageError(
|
|
|
|
"--poll-interval must be a positive integer"
|
|
|
|
)
|
2015-10-23 21:04:08 +00:00
|
|
|
# Expand the path relative to the current directory of the CLI command, not the node.
|
|
|
|
self.local_dir = None if local_dir is None else argv_to_abspath(local_dir, long_path=False)
|
2015-10-20 20:19:04 +00:00
|
|
|
self.invite_code = to_str(argv_to_unicode(invite_code))
|
2015-10-01 21:40:10 +00:00
|
|
|
|
|
|
|
def join(options):
|
2015-10-20 20:19:04 +00:00
|
|
|
fields = options.invite_code.split(INVITE_SEPARATOR)
|
|
|
|
if len(fields) != 2:
|
|
|
|
raise usage.UsageError("Invalid invite code.")
|
|
|
|
magic_readonly_cap, dmd_write_cap = fields
|
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
maybe_upgrade_magic_folders(options["node-directory"])
|
|
|
|
existing_folders = load_magic_folders(options["node-directory"])
|
2015-10-01 21:40:10 +00:00
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
if options['name'] in existing_folders:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("This client already has a magic-folder named '{}'".format(options['name']), file=options.stderr)
|
2015-12-02 15:54:01 +00:00
|
|
|
return 1
|
2015-11-05 15:56:31 +00:00
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
db_fname = os.path.join(
|
|
|
|
options["node-directory"],
|
|
|
|
u"private",
|
|
|
|
u"magicfolder_{}.sqlite".format(options['name']),
|
|
|
|
)
|
|
|
|
if os.path.exists(db_fname):
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Database '{}' already exists; not overwriting".format(db_fname), file=options.stderr)
|
2017-08-23 21:34:11 +00:00
|
|
|
return 1
|
2015-10-23 21:05:34 +00:00
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
folder = {
|
|
|
|
u"directory": options.local_dir.encode('utf-8'),
|
|
|
|
u"collective_dircap": magic_readonly_cap,
|
|
|
|
u"upload_dircap": dmd_write_cap,
|
|
|
|
u"poll_interval": options["poll-interval"],
|
|
|
|
}
|
|
|
|
existing_folders[options["name"]] = folder
|
|
|
|
|
|
|
|
save_magic_folders(options["node-directory"], existing_folders)
|
2015-10-01 21:40:10 +00:00
|
|
|
return 0
|
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
|
2015-11-05 16:24:52 +00:00
|
|
|
class LeaveOptions(BasedirOptions):
|
2017-08-23 21:34:11 +00:00
|
|
|
synopsis = "Remove a magic-folder and forget all state"
|
|
|
|
optParameters = [
|
|
|
|
("name", "n", "default", "Name of magic-folder to leave"),
|
|
|
|
]
|
|
|
|
|
2015-11-05 16:24:52 +00:00
|
|
|
|
|
|
|
def leave(options):
|
2015-11-10 15:24:44 +00:00
|
|
|
from ConfigParser import SafeConfigParser
|
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
existing_folders = load_magic_folders(options["node-directory"])
|
2015-11-10 15:24:44 +00:00
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
if not existing_folders:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("No magic-folders at all", file=options.stderr)
|
2017-08-23 21:34:11 +00:00
|
|
|
return 1
|
|
|
|
|
|
|
|
if options["name"] not in existing_folders:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("No such magic-folder '{}'".format(options["name"]), file=options.stderr)
|
2017-08-23 21:34:11 +00:00
|
|
|
return 1
|
|
|
|
|
|
|
|
privdir = os.path.join(options["node-directory"], u"private")
|
|
|
|
db_fname = os.path.join(privdir, u"magicfolder_{}.sqlite".format(options["name"]))
|
|
|
|
|
|
|
|
# delete from YAML file and re-write it
|
|
|
|
del existing_folders[options["name"]]
|
|
|
|
save_magic_folders(options["node-directory"], existing_folders)
|
|
|
|
|
|
|
|
# delete the database file
|
|
|
|
try:
|
|
|
|
fileutil.remove(db_fname)
|
|
|
|
except Exception as e:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Warning: unable to remove %s due to %s: %s"
|
|
|
|
% (quote_local_unicode_path(db_fname), e.__class__.__name__, str(e)), file=options.stderr)
|
2017-08-23 21:34:11 +00:00
|
|
|
|
|
|
|
# if this was the last magic-folder, disable them entirely
|
|
|
|
if not existing_folders:
|
|
|
|
parser = SafeConfigParser()
|
|
|
|
parser.read(os.path.join(options["node-directory"], u"tahoe.cfg"))
|
|
|
|
parser.remove_section("magic_folder")
|
|
|
|
with open(os.path.join(options["node-directory"], u"tahoe.cfg"), "w") as f:
|
|
|
|
parser.write(f)
|
2015-11-10 15:24:44 +00:00
|
|
|
|
2016-02-01 23:30:42 +00:00
|
|
|
return 0
|
2015-12-02 15:54:01 +00:00
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
|
2015-11-12 14:47:25 +00:00
|
|
|
class StatusOptions(BasedirOptions):
|
|
|
|
synopsis = ""
|
|
|
|
stdin = StringIO("")
|
2017-08-23 21:34:11 +00:00
|
|
|
optParameters = [
|
|
|
|
("name", "n", "default", "Name for the magic-folder to show status"),
|
|
|
|
]
|
2015-11-12 23:16:28 +00:00
|
|
|
|
2015-11-12 14:47:25 +00:00
|
|
|
def parseArgs(self):
|
|
|
|
BasedirOptions.parseArgs(self)
|
|
|
|
node_url_file = os.path.join(self['node-directory'], u"node.url")
|
2015-11-12 23:16:28 +00:00
|
|
|
with open(node_url_file, "r") as f:
|
|
|
|
self['node-url'] = f.read().strip()
|
|
|
|
|
|
|
|
|
2016-05-23 20:48:07 +00:00
|
|
|
def _get_json_for_fragment(options, fragment, method='GET', post_args=None):
|
2015-11-12 23:16:28 +00:00
|
|
|
nodeurl = options['node-url']
|
|
|
|
if nodeurl.endswith('/'):
|
|
|
|
nodeurl = nodeurl[:-1]
|
|
|
|
|
|
|
|
url = u'%s/%s' % (nodeurl, fragment)
|
2016-05-23 20:48:07 +00:00
|
|
|
if method == 'POST':
|
|
|
|
if post_args is None:
|
|
|
|
raise ValueError("Must pass post_args= for POST method")
|
|
|
|
body = urllib.urlencode(post_args)
|
|
|
|
else:
|
|
|
|
body = ''
|
|
|
|
if post_args is not None:
|
|
|
|
raise ValueError("post_args= only valid for POST method")
|
|
|
|
resp = do_http(method, url, body=body)
|
2015-11-12 23:16:28 +00:00
|
|
|
if isinstance(resp, BadResponse):
|
|
|
|
# specifically NOT using format_http_error() here because the
|
|
|
|
# URL is pretty sensitive (we're doing /uri/<key>).
|
|
|
|
raise RuntimeError(
|
|
|
|
"Failed to get json from '%s': %s" % (nodeurl, resp.error)
|
|
|
|
)
|
|
|
|
|
|
|
|
data = resp.read()
|
2017-01-19 22:39:53 +00:00
|
|
|
parsed = json.loads(data)
|
2016-05-23 20:48:07 +00:00
|
|
|
if parsed is None:
|
2015-11-12 23:16:28 +00:00
|
|
|
raise RuntimeError("No data from '%s'" % (nodeurl,))
|
|
|
|
return parsed
|
|
|
|
|
|
|
|
|
|
|
|
def _get_json_for_cap(options, cap):
|
|
|
|
return _get_json_for_fragment(
|
|
|
|
options,
|
|
|
|
'uri/%s?t=json' % urllib.quote(cap),
|
|
|
|
)
|
|
|
|
|
|
|
|
def _print_item_status(item, now, longest):
|
|
|
|
paddedname = (' ' * (longest - len(item['path']))) + item['path']
|
|
|
|
if 'failure_at' in item:
|
|
|
|
ts = datetime.fromtimestamp(item['started_at'])
|
2016-02-03 19:38:16 +00:00
|
|
|
prog = 'Failed %s (%s)' % (abbreviate_time(now - ts), ts)
|
2015-11-12 23:16:28 +00:00
|
|
|
elif item['percent_done'] < 100.0:
|
|
|
|
if 'started_at' not in item:
|
|
|
|
prog = 'not yet started'
|
|
|
|
else:
|
|
|
|
so_far = now - datetime.fromtimestamp(item['started_at'])
|
|
|
|
if so_far.seconds > 0.0:
|
|
|
|
rate = item['percent_done'] / so_far.seconds
|
|
|
|
if rate != 0:
|
|
|
|
time_left = (100.0 - item['percent_done']) / rate
|
|
|
|
prog = '%2.1f%% done, around %s left' % (
|
|
|
|
item['percent_done'],
|
2016-02-03 19:38:16 +00:00
|
|
|
abbreviate_time(time_left),
|
2015-11-12 23:16:28 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
time_left = None
|
|
|
|
prog = '%2.1f%% done' % (item['percent_done'],)
|
|
|
|
else:
|
|
|
|
prog = 'just started'
|
|
|
|
else:
|
|
|
|
prog = ''
|
|
|
|
for verb in ['finished', 'started', 'queued']:
|
|
|
|
keyname = verb + '_at'
|
|
|
|
if keyname in item:
|
|
|
|
when = datetime.fromtimestamp(item[keyname])
|
2016-02-03 19:38:16 +00:00
|
|
|
prog = '%s %s' % (verb, abbreviate_time(now - when))
|
2015-11-12 23:16:28 +00:00
|
|
|
break
|
|
|
|
|
2019-03-24 13:14:00 +00:00
|
|
|
print(" %s: %s" % (paddedname, prog))
|
2015-11-12 14:47:25 +00:00
|
|
|
|
2017-08-23 21:34:11 +00:00
|
|
|
|
2015-11-12 14:47:25 +00:00
|
|
|
def status(options):
|
2015-11-12 23:16:28 +00:00
|
|
|
nodedir = options["node-directory"]
|
2017-08-23 21:34:11 +00:00
|
|
|
stdout, stderr = options.stdout, options.stderr
|
|
|
|
magic_folders = load_magic_folders(os.path.join(options["node-directory"]))
|
|
|
|
|
2016-08-08 22:11:15 +00:00
|
|
|
with open(os.path.join(nodedir, u'private', u'api_auth_token'), 'rb') as f:
|
|
|
|
token = f.read()
|
2015-11-12 23:16:28 +00:00
|
|
|
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Magic-folder status for '{}':".format(options["name"]), file=stdout)
|
2017-08-23 21:34:11 +00:00
|
|
|
|
|
|
|
if options["name"] not in magic_folders:
|
|
|
|
raise Exception(
|
|
|
|
"No such magic-folder '{}'".format(options["name"])
|
|
|
|
)
|
|
|
|
|
|
|
|
dmd_cap = magic_folders[options["name"]]["upload_dircap"]
|
|
|
|
collective_readcap = magic_folders[options["name"]]["collective_dircap"]
|
|
|
|
|
2016-08-08 22:11:15 +00:00
|
|
|
# do *all* our data-retrievals first in case there's an error
|
2015-11-12 23:16:28 +00:00
|
|
|
try:
|
2016-08-08 22:11:15 +00:00
|
|
|
dmd_data = _get_json_for_cap(options, dmd_cap)
|
|
|
|
remote_data = _get_json_for_cap(options, collective_readcap)
|
|
|
|
magic_data = _get_json_for_fragment(
|
|
|
|
options,
|
|
|
|
'magic_folder?t=json',
|
|
|
|
method='POST',
|
|
|
|
post_args=dict(
|
|
|
|
t='json',
|
2017-08-23 21:34:11 +00:00
|
|
|
name=options["name"],
|
2016-08-08 22:11:15 +00:00
|
|
|
token=token,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
except Exception as e:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("failed to retrieve data: %s" % str(e), file=stderr)
|
2016-08-08 22:11:15 +00:00
|
|
|
return 2
|
|
|
|
|
|
|
|
for d in [dmd_data, remote_data, magic_data]:
|
|
|
|
if isinstance(d, dict) and 'error' in d:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Error from server: %s" % d['error'], file=stderr)
|
|
|
|
print("This means we can't retrieve the remote shared directory.", file=stderr)
|
2016-08-08 22:11:15 +00:00
|
|
|
return 3
|
|
|
|
|
|
|
|
captype, dmd = dmd_data
|
|
|
|
if captype != 'dirnode':
|
2019-03-24 13:14:00 +00:00
|
|
|
print("magic_folder_dircap isn't a directory capability", file=stderr)
|
2016-08-08 22:11:15 +00:00
|
|
|
return 2
|
2015-11-12 23:16:28 +00:00
|
|
|
|
|
|
|
now = datetime.now()
|
|
|
|
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Local files:", file=stdout)
|
2015-11-12 23:16:28 +00:00
|
|
|
for (name, child) in dmd['children'].items():
|
|
|
|
captype, meta = child
|
|
|
|
status = 'good'
|
|
|
|
size = meta['size']
|
|
|
|
created = datetime.fromtimestamp(meta['metadata']['tahoe']['linkcrtime'])
|
|
|
|
version = meta['metadata']['version']
|
2016-02-03 19:38:16 +00:00
|
|
|
nice_size = abbreviate_space(size)
|
|
|
|
nice_created = abbreviate_time(now - created)
|
2015-11-12 23:16:28 +00:00
|
|
|
if captype != 'filenode':
|
2019-03-24 13:14:00 +00:00
|
|
|
print("%20s: error, should be a filecap" % name, file=stdout)
|
2015-11-12 23:16:28 +00:00
|
|
|
continue
|
2019-03-24 13:14:00 +00:00
|
|
|
print(" %s (%s): %s, version=%s, created %s" % (name, nice_size, status, version, nice_created), file=stdout)
|
2015-11-12 23:16:28 +00:00
|
|
|
|
2019-03-24 13:14:00 +00:00
|
|
|
print(file=stdout)
|
|
|
|
print("Remote files:", file=stdout)
|
2016-08-08 22:11:15 +00:00
|
|
|
|
|
|
|
captype, collective = remote_data
|
2015-11-12 23:16:28 +00:00
|
|
|
for (name, data) in collective['children'].items():
|
|
|
|
if data[0] != 'dirnode':
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Error: '%s': expected a dirnode, not '%s'" % (name, data[0]), file=stdout)
|
|
|
|
print(" %s's remote:" % name, file=stdout)
|
2015-11-12 23:16:28 +00:00
|
|
|
dmd = _get_json_for_cap(options, data[1]['ro_uri'])
|
2016-08-08 22:11:15 +00:00
|
|
|
if isinstance(dmd, dict) and 'error' in dmd:
|
2019-03-24 13:14:00 +00:00
|
|
|
print(" Error: could not retrieve directory", file=stdout)
|
2016-08-08 22:11:15 +00:00
|
|
|
continue
|
2015-11-12 23:16:28 +00:00
|
|
|
if dmd[0] != 'dirnode':
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Error: should be a dirnode", file=stdout)
|
2015-11-12 23:16:28 +00:00
|
|
|
continue
|
|
|
|
for (n, d) in dmd[1]['children'].items():
|
|
|
|
if d[0] != 'filenode':
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Error: expected '%s' to be a filenode." % (n,), file=stdout)
|
2015-11-12 23:16:28 +00:00
|
|
|
|
|
|
|
meta = d[1]
|
|
|
|
status = 'good'
|
|
|
|
size = meta['size']
|
|
|
|
created = datetime.fromtimestamp(meta['metadata']['tahoe']['linkcrtime'])
|
|
|
|
version = meta['metadata']['version']
|
2016-02-03 19:38:16 +00:00
|
|
|
nice_size = abbreviate_space(size)
|
|
|
|
nice_created = abbreviate_time(now - created)
|
2019-03-24 13:14:00 +00:00
|
|
|
print(" %s (%s): %s, version=%s, created %s" % (n, nice_size, status, version, nice_created), file=stdout)
|
2015-11-12 23:16:28 +00:00
|
|
|
|
2016-08-08 22:11:15 +00:00
|
|
|
if len(magic_data):
|
|
|
|
uploads = [item for item in magic_data if item['kind'] == 'upload']
|
|
|
|
downloads = [item for item in magic_data if item['kind'] == 'download']
|
|
|
|
longest = max([len(item['path']) for item in magic_data])
|
2015-11-12 23:16:28 +00:00
|
|
|
|
2017-12-30 23:19:41 +00:00
|
|
|
# maybe gate this with --show-completed option or something?
|
|
|
|
uploads = [item for item in uploads if item['status'] != 'success']
|
|
|
|
downloads = [item for item in downloads if item['status'] != 'success']
|
2015-11-12 23:16:28 +00:00
|
|
|
|
|
|
|
if len(uploads):
|
2019-03-24 13:14:00 +00:00
|
|
|
print()
|
|
|
|
print("Uploads:", file=stdout)
|
2015-11-12 23:16:28 +00:00
|
|
|
for item in uploads:
|
|
|
|
_print_item_status(item, now, longest)
|
|
|
|
|
|
|
|
if len(downloads):
|
2019-03-24 13:14:00 +00:00
|
|
|
print()
|
|
|
|
print("Downloads:", file=stdout)
|
2015-11-12 23:16:28 +00:00
|
|
|
for item in downloads:
|
|
|
|
_print_item_status(item, now, longest)
|
|
|
|
|
2016-08-08 22:11:15 +00:00
|
|
|
for item in magic_data:
|
2015-11-12 23:16:28 +00:00
|
|
|
if item['status'] == 'failure':
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Failed:", item, file=stdout)
|
2015-11-12 23:16:28 +00:00
|
|
|
|
2015-11-05 16:24:52 +00:00
|
|
|
return 0
|
|
|
|
|
2015-11-12 23:16:28 +00:00
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
class MagicFolderCommand(BaseOptions):
|
|
|
|
subCommands = [
|
|
|
|
["create", None, CreateOptions, "Create a Magic Folder."],
|
|
|
|
["invite", None, InviteOptions, "Invite someone to a Magic Folder."],
|
|
|
|
["join", None, JoinOptions, "Join a Magic Folder."],
|
2015-11-05 16:24:52 +00:00
|
|
|
["leave", None, LeaveOptions, "Leave a Magic Folder."],
|
2016-12-06 19:01:51 +00:00
|
|
|
["status", None, StatusOptions, "Display status of uploads/downloads."],
|
2017-08-23 21:34:11 +00:00
|
|
|
["list", None, ListOptions, "List Magic Folders configured in this client."],
|
2015-10-01 21:40:10 +00:00
|
|
|
]
|
2016-05-23 20:56:06 +00:00
|
|
|
optFlags = [
|
|
|
|
["debug", "d", "Print full stack-traces"],
|
|
|
|
]
|
2017-08-23 21:34:11 +00:00
|
|
|
description = (
|
|
|
|
"A magic-folder has an owner who controls the writecap "
|
|
|
|
"containing a list of nicknames and readcaps. The owner can invite "
|
|
|
|
"new participants. Every participant has the writecap for their "
|
|
|
|
"own folder (the corresponding readcap is in the master folder). "
|
|
|
|
"All clients download files from all other participants using the "
|
|
|
|
"readcaps contained in the master magic-folder directory."
|
|
|
|
)
|
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
def postOptions(self):
|
|
|
|
if not hasattr(self, 'subOptions'):
|
|
|
|
raise usage.UsageError("must specify a subcommand")
|
|
|
|
def getSynopsis(self):
|
2017-08-23 21:34:11 +00:00
|
|
|
return "Usage: tahoe [global-options] magic-folder"
|
2015-10-01 21:40:10 +00:00
|
|
|
def getUsage(self, width=None):
|
|
|
|
t = BaseOptions.getUsage(self, width)
|
2017-08-23 21:34:11 +00:00
|
|
|
t += (
|
|
|
|
"Please run e.g. 'tahoe magic-folder create --help' for more "
|
|
|
|
"details on each subcommand.\n"
|
|
|
|
)
|
2015-10-01 21:40:10 +00:00
|
|
|
return t
|
|
|
|
|
|
|
|
subDispatch = {
|
|
|
|
"create": create,
|
|
|
|
"invite": invite,
|
|
|
|
"join": join,
|
2015-11-05 16:24:52 +00:00
|
|
|
"leave": leave,
|
2015-11-12 23:16:28 +00:00
|
|
|
"status": status,
|
2017-08-23 21:34:11 +00:00
|
|
|
"list": list_,
|
2015-10-01 21:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def do_magic_folder(options):
|
|
|
|
so = options.subOptions
|
|
|
|
so.stdout = options.stdout
|
|
|
|
so.stderr = options.stderr
|
|
|
|
f = subDispatch[options.subCommand]
|
2016-05-23 20:56:06 +00:00
|
|
|
try:
|
|
|
|
return f(so)
|
|
|
|
except Exception as e:
|
2019-03-24 13:14:00 +00:00
|
|
|
print("Error: %s" % (e,), file=options.stderr)
|
2016-05-23 20:56:06 +00:00
|
|
|
if options['debug']:
|
|
|
|
raise
|
2015-10-01 21:40:10 +00:00
|
|
|
|
|
|
|
subCommands = [
|
|
|
|
["magic-folder", None, MagicFolderCommand,
|
|
|
|
"Magic Folder subcommands: use 'tahoe magic-folder' for a list."],
|
|
|
|
]
|
|
|
|
|
|
|
|
dispatch = {
|
|
|
|
"magic-folder": do_magic_folder,
|
|
|
|
}
|