~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to contrib/create_bzr_rollup.py

  • Committer: Martin Pool
  • Date: 2005-06-06 04:15:44 UTC
  • Revision ID: mbp@sourcefrog.net-20050606041544-83be94eb35eef7de
- script to create rollups, from John

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""\
 
3
This script runs after rsyncing bzr.
 
4
It checks the bzr version, and sees if there is a tarball and
 
5
zipfile that exist with that version.
 
6
If not, it creates them.
 
7
"""
 
8
 
 
9
import os, sys, tempfile
 
10
 
 
11
def sync(remote, local, verbose=False):
 
12
        """Do the actual synchronization
 
13
        """
 
14
        if verbose:
 
15
                status = os.system('rsync -av --delete "%s" "%s"' % (remote, local))
 
16
        else:
 
17
                status = os.system('rsync -a --delete "%s" "%s"' % (remote, local))
 
18
        return status==0
 
19
 
 
20
def create_tar_gz(local_dir, output_dir=None, verbose=False):
 
21
        import tarfile, bzrlib
 
22
        out_name = os.path.basename(local_dir) + '-' + str(bzrlib.Branch(local_dir).revno())
 
23
        final_path = os.path.join(output_dir, out_name + '.tar.gz')
 
24
        if os.path.exists(final_path):
 
25
                if verbose:
 
26
                        print 'Output file already exists: %r' % final_path
 
27
                return
 
28
        fn, tmp_path=tempfile.mkstemp(suffix='.tar', prefix=out_name, dir=output_dir)
 
29
        os.close(fn)
 
30
        try:
 
31
                if verbose:
 
32
                        print 'Creating %r (%r)' % (final_path, tmp_path)
 
33
                tar = tarfile.TarFile(name=tmp_path, mode='w')
 
34
                tar.add(local_dir, arcname=out_name, recursive=True)
 
35
                tar.close()
 
36
 
 
37
                if verbose:
 
38
                        print 'Compressing...'
 
39
                if os.system('gzip "%s"' % tmp_path) != 0:
 
40
                        raise ValueError('Failed to compress')
 
41
                tmp_path += '.gz'
 
42
                os.rename(tmp_path, final_path)
 
43
        except:
 
44
                os.remove(tmp_path)
 
45
                raise
 
46
 
 
47
def create_tar_bz2(local_dir, output_dir=None, verbose=False):
 
48
        import tarfile, bzrlib
 
49
        out_name = os.path.basename(local_dir) + '-' + str(bzrlib.Branch(local_dir).revno())
 
50
        final_path = os.path.join(output_dir, out_name + '.tar.bz2')
 
51
        if os.path.exists(final_path):
 
52
                if verbose:
 
53
                        print 'Output file already exists: %r' % final_path
 
54
                return
 
55
        fn, tmp_path=tempfile.mkstemp(suffix='.tar', prefix=out_name, dir=output_dir)
 
56
        os.close(fn)
 
57
        try:
 
58
                if verbose:
 
59
                        print 'Creating %r (%r)' % (final_path, tmp_path)
 
60
                tar = tarfile.TarFile(name=tmp_path, mode='w')
 
61
                tar.add(local_dir, arcname=out_name, recursive=True)
 
62
                tar.close()
 
63
 
 
64
                if verbose:
 
65
                        print 'Compressing...'
 
66
                if os.system('bzip2 "%s"' % tmp_path) != 0:
 
67
                        raise ValueError('Failed to compress')
 
68
                tmp_path += '.bz2'
 
69
                os.rename(tmp_path, final_path)
 
70
        except:
 
71
                os.remove(tmp_path)
 
72
                raise
 
73
 
 
74
def create_zip(local_dir, output_dir=None, verbose=False):
 
75
        import zipfile, bzrlib
 
76
        out_name = os.path.basename(local_dir) + '-' + str(bzrlib.Branch(local_dir).revno())
 
77
        final_path = os.path.join(output_dir, out_name + '.zip')
 
78
        if os.path.exists(final_path):
 
79
                if verbose:
 
80
                        print 'Output file already exists: %r' % final_path
 
81
                return
 
82
        fn, tmp_path=tempfile.mkstemp(suffix='.zip', prefix=out_name, dir=output_dir)
 
83
        os.close(fn)
 
84
        try:
 
85
                if verbose:
 
86
                        print 'Creating %r (%r)' % (final_path, tmp_path)
 
87
                zip = zipfile.ZipFile(file=tmp_path, mode='w')
 
88
                try:
 
89
                        for root, dirs, files in os.walk(local_dir):
 
90
                                for f in files:
 
91
                                        path = os.path.join(root, f)
 
92
                                        arcname = os.path.join(out_name, path[len(local_dir)+1:])
 
93
                                        zip.write(path, arcname=arcname)
 
94
                finally:
 
95
                        zip.close()
 
96
 
 
97
                os.rename(tmp_path, final_path)
 
98
        except:
 
99
                os.remove(tmp_path)
 
100
                raise
 
101
 
 
102
def get_local_dir(remote, local):
 
103
        """This returns the full path to the local directory where 
 
104
        the files are kept.
 
105
 
 
106
        rsync has the trick that if the source directory ends in a '/' then
 
107
        the file will be copied *into* the target. If it does not end in a slash,
 
108
        then the directory will be added into the target.
 
109
        """
 
110
        if remote[-1:] == '/':
 
111
                return local
 
112
        # rsync paths are typically user@host:path/to/something
 
113
        # the reason for the split(':') is in case path doesn't contain a slash
 
114
        extra = remote.split(':')[-1].split('/')[-1]
 
115
        return os.path.join(local, extra)
 
116
 
 
117
def get_output_dir(output, local):
 
118
        if output:
 
119
                return output
 
120
        return os.path.dirname(os.path.realpath(local))
 
121
 
 
122
 
 
123
def main(args):
 
124
        import optparse
 
125
        p = optparse.OptionParser(usage='%prog [options] [remote] [local]'
 
126
                '\n  rsync the remote repository to the local directory'
 
127
                '\n  if remote is not given, it defaults to "bazaar-ng.org::bazaar-ng/bzr/bzr.dev"'
 
128
                '\n  if local is not given it defaults to "."')
 
129
 
 
130
        p.add_option('--verbose', action='store_true'
 
131
                , help="Describe the process")
 
132
        p.add_option('--no-tar-gz', action='store_false', dest='create_tar_gz', default=True
 
133
                , help="Don't create a gzip compressed tarfile.")
 
134
        p.add_option('--no-tar-bz2', action='store_false', dest='create_tar_bz2', default=True
 
135
                , help="Don't create a bzip2 compressed tarfile.")
 
136
        p.add_option('--no-zip', action='store_false', dest='create_zip', default=True
 
137
                , help="Don't create a zipfile.")
 
138
        p.add_option('--output-dir', default=None
 
139
                , help="Set the output location, default is just above the final local directory.")
 
140
 
 
141
 
 
142
        (opts, args) = p.parse_args(args)
 
143
 
 
144
        if len(args) < 1:
 
145
                remote = 'bazaar-ng.org::bazaar-ng/bzr/bzr.dev'
 
146
        else:
 
147
                remote = args[0]
 
148
        if len(args) < 2:
 
149
                local = '.'
 
150
        else:
 
151
                local = args[1]
 
152
        if len(args) > 2:
 
153
                print 'Invalid number of arguments, see --help for details.'
 
154
 
 
155
        if not sync(remote, local, verbose=opts.verbose):
 
156
                if opts.verbose:
 
157
                        print '** rsync failed'
 
158
                return 1
 
159
        # Now we have the new update
 
160
        local_dir = get_local_dir(remote, local)
 
161
 
 
162
        output_dir = get_output_dir(opts.output_dir, local_dir)
 
163
        if opts.create_tar_gz:
 
164
                create_tar_gz(local_dir, output_dir=output_dir, verbose=opts.verbose)
 
165
        if opts.create_tar_bz2:
 
166
                create_tar_bz2(local_dir, output_dir=output_dir, verbose=opts.verbose)
 
167
        if opts.create_zip:
 
168
                create_zip(local_dir, output_dir=output_dir, verbose=opts.verbose)
 
169
 
 
170
        return 0
 
171
                
 
172
if __name__ == '__main__':
 
173
        sys.exit(main(sys.argv[1:]))
 
174