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