/usr/share/pyshared/allmydata/scripts/cli.py is in tahoe-lafs 1.9.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | import os.path, re, fnmatch
from twisted.python import usage
from allmydata.scripts.common import BaseOptions, get_aliases, get_default_nodedir, DEFAULT_ALIAS
from allmydata.util.encodingutil import argv_to_unicode, argv_to_abspath, quote_output
NODEURL_RE=re.compile("http(s?)://([^:]*)(:([1-9][0-9]*))?")
_default_nodedir = get_default_nodedir()
class VDriveOptions(BaseOptions):
optParameters = [
["node-directory", "d", None,
"Specify which Tahoe node directory should be used. The directory "
"should either contain a full Tahoe node, or a file named node.url "
"that points to some other Tahoe node. It should also contain a file "
"named '" + os.path.join('private', 'aliases') + "' which contains the "
"mapping from alias name to root dirnode URI." + (
_default_nodedir and (" [default: " + quote_output(_default_nodedir) + "]") or "")],
["node-url", "u", None,
"Specify the URL of the Tahoe gateway node, such as 'http://127.0.0.1:3456'. "
"This overrides the URL found in the --node-directory ."],
["dir-cap", None, None,
"Specify which dirnode URI should be used as the 'tahoe' alias."]
]
def postOptions(self):
if self['node-directory']:
self['node-directory'] = argv_to_abspath(self['node-directory'])
else:
self['node-directory'] = _default_nodedir
# compute a node-url from the existing options, put in self['node-url']
if self['node-url']:
if (not isinstance(self['node-url'], basestring)
or not NODEURL_RE.match(self['node-url'])):
msg = ("--node-url is required to be a string and look like "
"\"http://HOSTNAMEORADDR:PORT\", not: %r" %
(self['node-url'],))
raise usage.UsageError(msg)
else:
node_url_file = os.path.join(self['node-directory'], "node.url")
self['node-url'] = open(node_url_file, "r").read().strip()
if self['node-url'][-1] != "/":
self['node-url'] += "/"
aliases = get_aliases(self['node-directory'])
if self['dir-cap']:
aliases[DEFAULT_ALIAS] = self['dir-cap']
self.aliases = aliases # maps alias name to dircap
class MakeDirectoryOptions(VDriveOptions):
optParameters = [
("format", None, None, "Create a directory with the given format: SDMF or MDMF (case-insensitive)"),
]
def parseArgs(self, where=""):
self.where = argv_to_unicode(where)
if self['format']:
if self['format'].upper() not in ("SDMF", "MDMF"):
raise usage.UsageError("%s is an invalid format" % self['format'])
def getSynopsis(self):
return "Usage: %s mkdir [options] [REMOTE_DIR]" % (self.command_name,)
longdesc = """Create a new directory, either unlinked or as a subdirectory."""
class AddAliasOptions(VDriveOptions):
def parseArgs(self, alias, cap):
self.alias = argv_to_unicode(alias)
if self.alias.endswith(u':'):
self.alias = self.alias[:-1]
self.cap = cap
def getSynopsis(self):
return "Usage: %s add-alias [options] ALIAS[:] DIRCAP" % (self.command_name,)
longdesc = """Add a new alias for an existing directory."""
class CreateAliasOptions(VDriveOptions):
def parseArgs(self, alias):
self.alias = argv_to_unicode(alias)
if self.alias.endswith(u':'):
self.alias = self.alias[:-1]
def getSynopsis(self):
return "Usage: %s create-alias [options] ALIAS[:]" % (self.command_name,)
longdesc = """Create a new directory and add an alias for it."""
class ListAliasesOptions(VDriveOptions):
def getSynopsis(self):
return "Usage: %s list-aliases [options]" % (self.command_name,)
longdesc = """Display a table of all configured aliases."""
class ListOptions(VDriveOptions):
optFlags = [
("long", "l", "Use long format: show file sizes, and timestamps."),
("uri", "u", "Show file/directory URIs."),
("readonly-uri", None, "Show read-only file/directory URIs."),
("classify", "F", "Append '/' to directory names, and '*' to mutable."),
("json", None, "Show the raw JSON output."),
]
def parseArgs(self, where=""):
self.where = argv_to_unicode(where)
longdesc = """
List the contents of some portion of the grid.
When the -l or --long option is used, each line is shown in the
following format:
drwx <size> <date/time> <name in this directory>
where each of the letters on the left may be replaced by '-'.
If 'd' is present, it indicates that the object is a directory.
If the 'd' is replaced by a '?', the object type is unknown.
'rwx' is a Unix-like permissions mask: if the mask includes 'w',
then the object is writeable through its link in this directory
(note that the link might be replaceable even if the object is
not writeable through the current link).
The 'x' is a legacy of Unix filesystems. In Tahoe it is used
only to indicate that the contents of a directory can be listed.
Directories have no size, so their size field is shown as '-'.
Otherwise the size of the file, when known, is given in bytes.
The size of mutable files or unknown objects is shown as '?'.
The date/time shows when this link in the Tahoe filesystem was
last modified.
"""
class GetOptions(VDriveOptions):
def parseArgs(self, arg1, arg2=None):
# tahoe get FOO |less # write to stdout
# tahoe get tahoe:FOO |less # same
# tahoe get FOO bar # write to local file
# tahoe get tahoe:FOO bar # same
self.from_file = argv_to_unicode(arg1)
if arg2:
self.to_file = argv_to_unicode(arg2)
else:
self.to_file = None
if self.to_file == "-":
self.to_file = None
def getSynopsis(self):
return "Usage: %s get [options] REMOTE_FILE LOCAL_FILE" % (self.command_name,)
longdesc = """
Retrieve a file from the grid and write it to the local filesystem. If
LOCAL_FILE is omitted or '-', the contents of the file will be written to
stdout."""
def getUsage(self, width=None):
t = VDriveOptions.getUsage(self, width)
t += """
Examples:
% tahoe get FOO |less # write to stdout
% tahoe get tahoe:FOO |less # same
% tahoe get FOO bar # write to local file
% tahoe get tahoe:FOO bar # same
"""
return t
class PutOptions(VDriveOptions):
optFlags = [
("mutable", "m", "Create a mutable file instead of an immutable one (like --format=SDMF)"),
]
optParameters = [
("format", None, None, "Create a file with the given format: SDMF and MDMF for mutable, CHK (default) for immutable. (case-insensitive)"),
]
def parseArgs(self, arg1=None, arg2=None):
# see Examples below
if arg1 is not None and arg2 is not None:
self.from_file = argv_to_unicode(arg1)
self.to_file = argv_to_unicode(arg2)
elif arg1 is not None and arg2 is None:
self.from_file = argv_to_unicode(arg1) # might be "-"
self.to_file = None
else:
self.from_file = None
self.to_file = None
if self.from_file == u"-":
self.from_file = None
if self['format']:
if self['format'].upper() not in ("SDMF", "MDMF", "CHK"):
raise usage.UsageError("%s is an invalid format" % self['format'])
def getSynopsis(self):
return "Usage: %s put [options] LOCAL_FILE REMOTE_FILE" % (self.command_name,)
longdesc = """
Put a file into the grid, copying its contents from the local filesystem.
If REMOTE_FILE is missing, upload the file but do not link it into a
directory; also print the new filecap to stdout. If LOCAL_FILE is missing
or '-', data will be copied from stdin. REMOTE_FILE is assumed to start
with tahoe: unless otherwise specified."""
def getUsage(self, width=None):
t = VDriveOptions.getUsage(self, width)
t += """
Examples:
% cat FILE | tahoe put # create unlinked file from stdin
% cat FILE | tahoe put - # same
% tahoe put bar # create unlinked file from local 'bar'
% cat FILE | tahoe put - FOO # create tahoe:FOO from stdin
% tahoe put bar FOO # copy local 'bar' to tahoe:FOO
% tahoe put bar tahoe:FOO # same
% tahoe put bar MUTABLE-FILE-WRITECAP # modify the mutable file in-place
"""
return t
class CpOptions(VDriveOptions):
optFlags = [
("recursive", "r", "Copy source directory recursively."),
("verbose", "v", "Be noisy about what is happening."),
("caps-only", None,
"When copying to local files, write out filecaps instead of actual "
"data (only useful for debugging and tree-comparison purposes)."),
]
def parseArgs(self, *args):
if len(args) < 2:
raise usage.UsageError("cp requires at least two arguments")
self.sources = map(argv_to_unicode, args[:-1])
self.destination = argv_to_unicode(args[-1])
def getSynopsis(self):
return "Usage: %s cp [options] FROM.. TO" % (self.command_name,)
longdesc = """
Use 'tahoe cp' to copy files between a local filesystem and a Tahoe grid.
Any FROM/TO arguments that begin with an alias indicate Tahoe-side
files or non-file arguments. Directories will be copied recursively.
New Tahoe-side directories will be created when necessary. Assuming that
you have previously set up an alias 'home' with 'tahoe create-alias home',
here are some examples:
tahoe cp ~/foo.txt home: # creates tahoe-side home:foo.txt
tahoe cp ~/foo.txt /tmp/bar.txt home: # copies two files to home:
tahoe cp ~/Pictures home:stuff/my-pictures # copies directory recursively
You can also use a dircap as either FROM or TO target:
tahoe cp URI:DIR2-RO:ixqhc4kdbjxc7o65xjnveoewym:5x6lwoxghrd5rxhwunzavft2qygfkt27oj3fbxlq4c6p45z5uneq/blog.html ./ # copy Zooko's wiki page to a local file
This command still has some limitations: symlinks and special files
(device nodes, named pipes) are not handled very well. Arguments should
probably not have trailing slashes. 'tahoe cp' does not behave as much
like /bin/cp as you would wish, especially with respect to trailing
slashes.
"""
class UnlinkOptions(VDriveOptions):
def parseArgs(self, where):
self.where = argv_to_unicode(where)
def getSynopsis(self):
return "Usage: %s unlink [options] REMOTE_FILE" % (self.command_name,)
class RmOptions(UnlinkOptions):
def getSynopsis(self):
return "Usage: %s rm [options] REMOTE_FILE" % (self.command_name,)
class MvOptions(VDriveOptions):
def parseArgs(self, frompath, topath):
self.from_file = argv_to_unicode(frompath)
self.to_file = argv_to_unicode(topath)
def getSynopsis(self):
return "Usage: %s mv [options] FROM TO" % (self.command_name,)
longdesc = """
Use 'tahoe mv' to move files that are already on the grid elsewhere on
the grid, e.g., 'tahoe mv alias:some_file alias:new_file'.
If moving a remote file into a remote directory, you'll need to append a
'/' to the name of the remote directory, e.g., 'tahoe mv tahoe:file1
tahoe:dir/', not 'tahoe mv tahoe:file1 tahoe:dir'.
Note that it is not possible to use this command to move local files to
the grid -- use 'tahoe cp' for that.
"""
class LnOptions(VDriveOptions):
def parseArgs(self, frompath, topath):
self.from_file = argv_to_unicode(frompath)
self.to_file = argv_to_unicode(topath)
def getSynopsis(self):
return "Usage: %s ln [options] FROM_LINK TO_LINK" % (self.command_name,)
longdesc = """
Use 'tahoe ln' to duplicate a link (directory entry) already on the grid
to elsewhere on the grid. For example 'tahoe ln alias:some_file
alias:new_file'. causes 'alias:new_file' to point to the same object that
'alias:some_file' points to.
(The argument order is the same as Unix ln. To remember the order, you
can think of this command as copying a link, rather than copying a file
as 'tahoe cp' does. Then the argument order is consistent with that of
'tahoe cp'.)
When linking a remote file into a remote directory, you'll need to append
a '/' to the name of the remote directory, e.g. 'tahoe ln tahoe:file1
tahoe:dir/' (which is shorthand for 'tahoe ln tahoe:file1
tahoe:dir/file1'). If you forget the '/', e.g. 'tahoe ln tahoe:file1
tahoe:dir', the 'ln' command will refuse to overwrite the 'tahoe:dir'
directory, and will exit with an error.
Note that it is not possible to use this command to create links between
local and remote files.
"""
class BackupConfigurationError(Exception):
pass
class BackupOptions(VDriveOptions):
optFlags = [
("verbose", "v", "Be noisy about what is happening."),
("ignore-timestamps", None, "Do not use backupdb timestamps to decide whether a local file is unchanged."),
]
vcs_patterns = ('CVS', 'RCS', 'SCCS', '.git', '.gitignore', '.cvsignore',
'.svn', '.arch-ids','{arch}', '=RELEASE-ID',
'=meta-update', '=update', '.bzr', '.bzrignore',
'.bzrtags', '.hg', '.hgignore', '_darcs')
def __init__(self):
super(BackupOptions, self).__init__()
self['exclude'] = set()
def parseArgs(self, localdir, topath):
self.from_dir = argv_to_unicode(localdir)
self.to_dir = argv_to_unicode(topath)
def getSynopsis(self):
return "Usage: %s backup [options] FROM ALIAS:TO" % (self.command_name,)
def opt_exclude(self, pattern):
"""Ignore files matching a glob pattern. You may give multiple
'--exclude' options."""
g = argv_to_unicode(pattern).strip()
if g:
exclude = self['exclude']
exclude.add(g)
def opt_exclude_from(self, filepath):
"""Ignore file matching glob patterns listed in file, one per
line. The file is assumed to be in the argv encoding."""
abs_filepath = argv_to_abspath(filepath)
try:
exclude_file = file(abs_filepath)
except:
raise BackupConfigurationError('Error opening exclude file %s.' % quote_output(abs_filepath))
try:
for line in exclude_file:
self.opt_exclude(line)
finally:
exclude_file.close()
def opt_exclude_vcs(self):
"""Exclude files and directories used by following version control
systems: CVS, RCS, SCCS, Git, SVN, Arch, Bazaar(bzr), Mercurial,
Darcs."""
for pattern in self.vcs_patterns:
self.opt_exclude(pattern)
def filter_listdir(self, listdir):
"""Yields non-excluded childpaths in path."""
exclude = self['exclude']
exclude_regexps = [re.compile(fnmatch.translate(pat)) for pat in exclude]
for filename in listdir:
for regexp in exclude_regexps:
if regexp.match(filename):
break
else:
yield filename
longdesc = """
Add a versioned backup of the local FROM directory to a timestamped
subdirectory of the TO/Archives directory on the grid, sharing as many
files and directories as possible with earlier backups. Create TO/Latest
as a reference to the latest backup. Behaves somewhat like 'rsync -a
--link-dest=TO/Archives/(previous) FROM TO/Archives/(new); ln -sf
TO/Archives/(new) TO/Latest'."""
class WebopenOptions(VDriveOptions):
optFlags = [
("info", "i", "Open the t=info page for the file"),
]
def parseArgs(self, where=''):
self.where = argv_to_unicode(where)
def getSynopsis(self):
return "Usage: %s webopen [options] [ALIAS:PATH]" % (self.command_name,)
longdesc = """Open a web browser to the contents of some file or
directory on the grid. When run without arguments, open the Welcome
page."""
class ManifestOptions(VDriveOptions):
optFlags = [
("storage-index", "s", "Only print storage index strings, not pathname+cap."),
("verify-cap", None, "Only print verifycap, not pathname+cap."),
("repair-cap", None, "Only print repaircap, not pathname+cap."),
("raw", "r", "Display raw JSON data instead of parsed."),
]
def parseArgs(self, where=''):
self.where = argv_to_unicode(where)
def getSynopsis(self):
return "Usage: %s manifest [options] [ALIAS:PATH]" % (self.command_name,)
longdesc = """Print a list of all files and directories reachable from
the given starting point."""
class StatsOptions(VDriveOptions):
optFlags = [
("raw", "r", "Display raw JSON data instead of parsed"),
]
def parseArgs(self, where=''):
self.where = argv_to_unicode(where)
def getSynopsis(self):
return "Usage: %s stats [options] [ALIAS:PATH]" % (self.command_name,)
longdesc = """Print statistics about of all files and directories
reachable from the given starting point."""
class CheckOptions(VDriveOptions):
optFlags = [
("raw", None, "Display raw JSON data instead of parsed."),
("verify", None, "Verify all hashes, instead of merely querying share presence."),
("repair", None, "Automatically repair any problems found."),
("add-lease", None, "Add/renew lease on all shares."),
]
def parseArgs(self, where=''):
self.where = argv_to_unicode(where)
def getSynopsis(self):
return "Usage: %s check [options] [ALIAS:PATH]" % (self.command_name,)
longdesc = """
Check a single file or directory: count how many shares are available and
verify their hashes. Optionally repair the file if any problems were
found."""
class DeepCheckOptions(VDriveOptions):
optFlags = [
("raw", None, "Display raw JSON data instead of parsed."),
("verify", None, "Verify all hashes, instead of merely querying share presence."),
("repair", None, "Automatically repair any problems found."),
("add-lease", None, "Add/renew lease on all shares."),
("verbose", "v", "Be noisy about what is happening."),
]
def parseArgs(self, where=''):
self.where = argv_to_unicode(where)
def getSynopsis(self):
return "Usage: %s deep-check [options] [ALIAS:PATH]" % (self.command_name,)
longdesc = """
Check all files and directories reachable from the given starting point
(which must be a directory), like 'tahoe check' but for multiple files.
Optionally repair any problems found."""
subCommands = [
["mkdir", None, MakeDirectoryOptions, "Create a new directory."],
["add-alias", None, AddAliasOptions, "Add a new alias cap."],
["create-alias", None, CreateAliasOptions, "Create a new alias cap."],
["list-aliases", None, ListAliasesOptions, "List all alias caps."],
["ls", None, ListOptions, "List a directory."],
["get", None, GetOptions, "Retrieve a file from the grid."],
["put", None, PutOptions, "Upload a file into the grid."],
["cp", None, CpOptions, "Copy one or more files or directories."],
["unlink", None, UnlinkOptions, "Unlink a file or directory on the grid."],
["rm", None, RmOptions, "Unlink a file or directory on the grid (same as unlink)."],
["mv", None, MvOptions, "Move a file within the grid."],
["ln", None, LnOptions, "Make an additional link to an existing file or directory."],
["backup", None, BackupOptions, "Make target dir look like local dir."],
["webopen", None, WebopenOptions, "Open a web browser to a grid file or directory."],
["manifest", None, ManifestOptions, "List all files/directories in a subtree."],
["stats", None, StatsOptions, "Print statistics about all files/directories in a subtree."],
["check", None, CheckOptions, "Check a single file or directory."],
["deep-check", None, DeepCheckOptions, "Check all files/directories reachable from a starting point."],
]
def mkdir(options):
from allmydata.scripts import tahoe_mkdir
rc = tahoe_mkdir.mkdir(options)
return rc
def add_alias(options):
from allmydata.scripts import tahoe_add_alias
rc = tahoe_add_alias.add_alias(options)
return rc
def create_alias(options):
from allmydata.scripts import tahoe_add_alias
rc = tahoe_add_alias.create_alias(options)
return rc
def list_aliases(options):
from allmydata.scripts import tahoe_add_alias
rc = tahoe_add_alias.list_aliases(options)
return rc
def list(options):
from allmydata.scripts import tahoe_ls
rc = tahoe_ls.list(options)
return rc
def get(options):
from allmydata.scripts import tahoe_get
rc = tahoe_get.get(options)
if rc == 0:
if options.to_file is None:
# be quiet, since the file being written to stdout should be
# proof enough that it worked, unless the user is unlucky
# enough to have picked an empty file
pass
else:
print >>options.stderr, "%s retrieved and written to %s" % \
(options.from_file, options.to_file)
return rc
def put(options):
from allmydata.scripts import tahoe_put
rc = tahoe_put.put(options)
return rc
def cp(options):
from allmydata.scripts import tahoe_cp
rc = tahoe_cp.copy(options)
return rc
def unlink(options, command="unlink"):
from allmydata.scripts import tahoe_unlink
rc = tahoe_unlink.unlink(options, command=command)
return rc
def rm(options):
return unlink(options, command="rm")
def mv(options):
from allmydata.scripts import tahoe_mv
rc = tahoe_mv.mv(options, mode="move")
return rc
def ln(options):
from allmydata.scripts import tahoe_mv
rc = tahoe_mv.mv(options, mode="link")
return rc
def backup(options):
from allmydata.scripts import tahoe_backup
rc = tahoe_backup.backup(options)
return rc
def webopen(options, opener=None):
from allmydata.scripts import tahoe_webopen
rc = tahoe_webopen.webopen(options, opener=opener)
return rc
def manifest(options):
from allmydata.scripts import tahoe_manifest
rc = tahoe_manifest.manifest(options)
return rc
def stats(options):
from allmydata.scripts import tahoe_manifest
rc = tahoe_manifest.stats(options)
return rc
def check(options):
from allmydata.scripts import tahoe_check
rc = tahoe_check.check(options)
return rc
def deepcheck(options):
from allmydata.scripts import tahoe_check
rc = tahoe_check.deepcheck(options)
return rc
dispatch = {
"mkdir": mkdir,
"add-alias": add_alias,
"create-alias": create_alias,
"list-aliases": list_aliases,
"ls": list,
"get": get,
"put": put,
"cp": cp,
"unlink": unlink,
"rm": rm,
"mv": mv,
"ln": ln,
"backup": backup,
"webopen": webopen,
"manifest": manifest,
"stats": stats,
"check": check,
"deep-check": deepcheck,
}
|