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