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