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.rename(tmp_path, final_path)
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):
53
print 'Output file already exists: %r' % final_path
55
fn, tmp_path=tempfile.mkstemp(suffix='.tar', prefix=out_name, dir=output_dir)
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)
65
print 'Compressing...'
66
if os.system('bzip2 "%s"' % tmp_path) != 0:
67
raise ValueError('Failed to compress')
69
os.rename(tmp_path, final_path)
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):
80
print 'Output file already exists: %r' % final_path
82
fn, tmp_path=tempfile.mkstemp(suffix='.zip', prefix=out_name, dir=output_dir)
86
print 'Creating %r (%r)' % (final_path, tmp_path)
87
zip = zipfile.ZipFile(file=tmp_path, mode='w')
89
for root, dirs, files in os.walk(local_dir):
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)
97
os.rename(tmp_path, final_path)
102
def get_local_dir(remote, local):
103
"""This returns the full path to the local directory where
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.
110
if remote[-1:] == '/':
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)
117
def get_output_dir(output, local):
120
return os.path.dirname(os.path.realpath(local))
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 "."')
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.")
142
(opts, args) = p.parse_args(args)
145
remote = 'bazaar-ng.org::bazaar-ng/bzr/bzr.dev'
153
print 'Invalid number of arguments, see --help for details.'
155
if not sync(remote, local, verbose=opts.verbose):
157
print '** rsync failed'
159
# Now we have the new update
160
local_dir = get_local_dir(remote, local)
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)
168
create_zip(local_dir, output_dir=output_dir, verbose=opts.verbose)
172
if __name__ == '__main__':
173
sys.exit(main(sys.argv[1:]))