~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-08-04 19:31:20 UTC
  • Revision ID: mbp@sourcefrog.net-20050804193119-5be8d5233c4bef96
- move in tutorial from web site
  needs more updates

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