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.
 
 
9
import os, sys, tempfile
 
 
11
def sync(remote, local, verbose=False):
 
 
12
        """Do the actual synchronization
 
 
15
                status = os.system('rsync -av --delete "%s" "%s"' % (remote, local))
 
 
17
                status = os.system('rsync -a --delete "%s" "%s"' % (remote, local))
 
 
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):
 
 
26
                        print 'Output file already exists: %r' % final_path
 
 
28
        fn, tmp_path=tempfile.mkstemp(suffix='.tar', prefix=out_name, dir=output_dir)
 
 
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)
 
 
38
                        print 'Compressing...'
 
 
39
                if os.system('gzip "%s"' % tmp_path) != 0:
 
 
40
                        raise ValueError('Failed to compress')
 
 
42
                os.chmod(tmp_path, 0644)
 
 
43
                os.rename(tmp_path, final_path)
 
 
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):
 
 
54
                        print 'Output file already exists: %r' % final_path
 
 
56
        fn, tmp_path=tempfile.mkstemp(suffix='.tar', prefix=out_name, dir=output_dir)
 
 
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)
 
 
66
                        print 'Compressing...'
 
 
67
                if os.system('bzip2 "%s"' % tmp_path) != 0:
 
 
68
                        raise ValueError('Failed to compress')
 
 
70
                os.chmod(tmp_path, 0644)
 
 
71
                os.rename(tmp_path, final_path)
 
 
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):
 
 
82
                        print 'Output file already exists: %r' % final_path
 
 
84
        fn, tmp_path=tempfile.mkstemp(suffix='.zip', prefix=out_name, dir=output_dir)
 
 
88
                        print 'Creating %r (%r)' % (final_path, tmp_path)
 
 
89
                zip = zipfile.ZipFile(file=tmp_path, mode='w')
 
 
91
                        for root, dirs, files in os.walk(local_dir):
 
 
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)
 
 
99
                os.chmod(tmp_path, 0644)
 
 
100
                os.rename(tmp_path, final_path)
 
 
105
def get_local_dir(remote, local):
 
 
106
        """This returns the full path to the local directory where 
 
 
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.
 
 
113
        if remote[-1:] == '/':
 
 
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)
 
 
120
def get_output_dir(output, local):
 
 
123
        return os.path.dirname(os.path.realpath(local))
 
 
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 "."')
 
 
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.")
 
 
145
        (opts, args) = p.parse_args(args)
 
 
148
                remote = 'bazaar-ng.org::bazaar-ng/bzr/bzr.dev'
 
 
156
                print 'Invalid number of arguments, see --help for details.'
 
 
158
        if not sync(remote, local, verbose=opts.verbose):
 
 
160
                        print '** rsync failed'
 
 
162
        # Now we have the new update
 
 
163
        local_dir = get_local_dir(remote, local)
 
 
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)
 
 
171
                create_zip(local_dir, output_dir=output_dir, verbose=opts.verbose)
 
 
175
if __name__ == '__main__':
 
 
176
        sys.exit(main(sys.argv[1:]))