summaryrefslogtreecommitdiff
path: root/genbackupdata
blob: e2394fa397b4151ef154e78f98d76504493a0687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
# Copyright 2011  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import cliapp
import os
import ttystatus

import genbackupdatalib


class GenbackupdataApp(cliapp.Application):

    def add_settings(self):
        self.settings.bytesize(
            ['create', 'c'],
            'how much data to create (default: %default)')
        self.settings.bytesize(
            ['file-size'],
            'size of one file',
            default=16 * 1024)
        self.settings.bytesize(
            ['chunk-size'],
            'generate data in chunks of this size',
            default=16 * 1024)
        self.settings.integer(
            ['depth'],
            'depth of directory tree',
            default=3)
        self.settings.integer(
            ['max-files'],
            'max files/dirs per dir',
            default=128)
        self.settings.integer(
            ['seed'],
            'seed for random number generator',
            default=0)
        self.settings.boolean(
            ['quiet'],
            'do not report progress')

    def process_args(self, args):
        outputdir = args[0]
        bytes = self.settings['create']
        self.gen = genbackupdatalib.DataGenerator(self.settings['seed'])
        self.names = genbackupdatalib.NameGenerator(
            outputdir, self.settings['depth'], self.settings['max-files'])

        self.setup_ttystatus(outputdir)
        self.status['total'] = bytes
        while bytes > 0:
            n = min(self.settings['file-size'], bytes)
            self.create_file(n)
            bytes -= n
        self.status.flush()
        self.status.finish()

    def create_file(self, bytes):
        '''Generate one output file.'''

        file_size = self.settings['file-size']
        chunk_size = self.settings['chunk-size']
        pathname = self.names.new()
        dirname = os.path.dirname(pathname)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        with open(pathname, 'wb') as f:
            while bytes >= chunk_size:
                self.write_bytes(f, chunk_size)
                bytes -= chunk_size
            if bytes > 0:
                self.write_bytes(f, bytes)

    def write_bytes(self, f, bytes):
        chunk = self.gen.generate(bytes)
        f.write(chunk)
        self.status['written'] += bytes

    def setup_ttystatus(self, outputdir):
        self.status = ttystatus.TerminalStatus(period=0.1)
        if self.settings['quiet']:
            self.status.disable()
        if hasattr(self.status, 'start_new_line'):
            self.status.format(
                'Generating backup data: %Pathname(outputdir)\n'
                '%ByteSize(written) of %ByteSize(total) '
                '%PercentDone(written,total) (%ByteSpeed(written))\n'
                '%RemainingTime(written,total) %ProgressBar(written,total)')
        else:
            self.status.format(
                'Generating %ByteSize(written) of %ByteSize(total) '
                '%PercentDone(written,total) (%ByteSpeed(written))')

        self.status['written'] = 0
        self.status['total'] = 0
        self.status['outputdir'] = outputdir


if __name__ == '__main__':
    GenbackupdataApp().run()