]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/stats_gatherer.py
bdb357660043307b61d0cdba6aeeaf1cb772f1bd
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / stats_gatherer.py
1
2 import os, sys
3 from allmydata.scripts.common import BasedirOptions
4 from allmydata.util.assertutil import precondition
5 from allmydata.util.encodingutil import listdir_unicode, quote_output
6
7 class CreateStatsGathererOptions(BasedirOptions):
8     default_nodedir = None
9
10     def getSynopsis(self):
11         return "Usage:  %s create-stats-gatherer [options] NODEDIR" % (self.command_name,)
12
13
14 stats_gatherer_tac = """
15 # -*- python -*-
16
17 from allmydata import stats
18 from twisted.application import service
19
20 verbose = True
21 g = stats.StatsGathererService(verbose=verbose)
22
23 application = service.Application('allmydata_stats_gatherer')
24 g.setServiceParent(application)
25 """
26
27
28 def create_stats_gatherer(config, out=sys.stdout, err=sys.stderr):
29     basedir = config['basedir']
30     # This should always be called with an absolute Unicode basedir.
31     precondition(isinstance(basedir, unicode), basedir)
32
33     if os.path.exists(basedir):
34         if listdir_unicode(basedir):
35             print >>err, "The base directory %s is not empty." % quote_output(basedir)
36             print >>err, "To avoid clobbering anything, I am going to quit now."
37             print >>err, "Please use a different directory, or empty this one."
38             return -1
39         # we're willing to use an empty directory
40     else:
41         os.mkdir(basedir)
42     f = open(os.path.join(basedir, "tahoe-stats-gatherer.tac"), "wb")
43     f.write(stats_gatherer_tac)
44     f.close()
45     return 0
46
47 subCommands = [
48     ["create-stats-gatherer", None, CreateStatsGathererOptions, "Create a stats-gatherer service."],
49 ]
50
51 dispatch = {
52     "create-stats-gatherer": create_stats_gatherer,
53     }
54
55