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