File: create-reference-repo

package info (click to toggle)
obnam 1.21-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,032 kB
  • ctags: 2,505
  • sloc: python: 14,404; sh: 410; ansic: 252; makefile: 66; awk: 5
file content (135 lines) | stat: -rwxr-xr-x 4,508 bytes parent folder | download | duplicates (4)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# Copyright 2013-2014  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/>.
#
# =*= License: GPL-3+ =*=


description = '''\
Create a reference repository for an Obnam version.

Create a tar file containing an Obnam backup repository and some
summain(1) manifests. The repository contains two backup generations,
of test live data generated by ./mkfunnyfarm. The backups are made
using encryption and compression, using the test PGP keys in
./test-gpghome.

The point of this is to archive the tar file and use it to test that
future versions of Obnam can restore the data and otherwise use the
repository.

Only one command line argument: path to the Obnam executable.

'''


import cliapp
import logging
import os
import shutil
import tarfile
import tempfile


class CreateReferenceRepository(cliapp.Application):

    def add_settings(self):
        self.settings.string(
            ['obnam'],
            'which obnam to execute',
            metavar='CMD',
            default='./obnam')

        self.settings.string_list(
            ['obnam-argument', 'a'],
            'add ARG to obnam command line',
            metavar='ARG')

    def process_args(self, args):
        if args:
            raise cliapp.AppException('Wrong command line args')

        tempdir = tempfile.mkdtemp()
        live_dir = os.path.join(tempdir, 'data')
        repo_dir = os.path.join(tempdir, 'repo')

        logging.info('Live data: %s', live_dir)
        logging.info('Repository: %s', repo_dir)

        # First generation.
        self.create_live_data(live_dir)
        self.create_manifest(live_dir, os.path.join(tempdir, 'manifest-1'))
        self.backup(live_dir, repo_dir)

        # Second generation.
        self.modify_live_data(live_dir)
        self.create_manifest(live_dir, os.path.join(tempdir, 'manifest-2'))
        self.backup(live_dir, repo_dir)

        # Remove the live data. Everything will be tarred up.
        shutil.rmtree(live_dir)
        
        # Tar up.
        self.create_tar_file(tempdir)

    def create_live_data(self, dirname):
        logging.info('Creating live data in %s with funnyfarm', dirname)
        cliapp.runcmd(['./mkfunnyfarm', dirname])

    def modify_live_data(self, dirname):
        logging.info('Modifying live data in %s', dirname)
        filename = os.path.join(dirname, 'new-file.txt')
        with open(filename, 'w') as f:
            f.write('This file created for 2nd generation.\n')

    def create_manifest(self, dirname, manifest_name):
        logging.info('Creating manifest of %s in %s', dirname, manifest_name)
        cliapp.runcmd(
            ['summain', '--exclude=Ino', '--exclude=Dev', '--exclude=Uid',
             '--exclude=Username', '--exclude=Gid', '--exclude=Group',
             '-r', '--output', manifest_name, dirname])

    def backup(self, live_dir, repo_dir):
        logging.info('Backing up %s to %s', live_dir, repo_dir)
        env = dict(os.environ)
        env['GNUPGHOME'] = 'test-gpghome'

        argv = [
            self.settings['obnam'],
            '--no-default-config',
            '--quiet',
             '--weak-random',
             '--compress-with=deflate',
            '-r', repo_dir,
            ]

        # This encryption key is the one in test-gpghome.
        argv += ['--encrypt-with=3B1802F81B321347']

        argv += self.settings['obnam-argument']
        argv += ['backup', live_dir]

        logging.info('Command line: %s', repr(argv))
        cliapp.runcmd(argv, env=env)

    def create_tar_file(self, tempdir):
        logging.info('Creating tar file of %s to stdout', tempdir)
        with tarfile.TarFile(fileobj=self.output, mode='w') as f:
            for basename in os.listdir(tempdir):
                pathname = os.path.join(tempdir, basename)
                f.add(pathname, arcname=basename)


CreateReferenceRepository(description=description).run()