summaryrefslogtreecommitdiff
path: root/contrib/create_bzr_rollup.py
blob: f77db6f47aa1e39eb7519a22b05ec96565a91dca (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
"""\
This script runs after rsyncing bzr.
It checks the bzr version, and sees if there is a tarball and
zipfile that exist with that version.
If not, it creates them.
"""

import os, sys, tempfile

def sync(remote, local, verbose=False):
	"""Do the actual synchronization
	"""
	if verbose:
		status = os.system('rsync -av --delete "%s" "%s"' % (remote, local))
	else:
		status = os.system('rsync -a --delete "%s" "%s"' % (remote, local))
	return status==0

def create_tar_gz(local_dir, output_dir=None, verbose=False):
	import tarfile, bzrlib
	out_name = os.path.basename(local_dir) + '-' + str(bzrlib.Branch(local_dir).revno())
	final_path = os.path.join(output_dir, out_name + '.tar.gz')
	if os.path.exists(final_path):
		if verbose:
			print 'Output file already exists: %r' % final_path
		return
	fn, tmp_path=tempfile.mkstemp(suffix='.tar', prefix=out_name, dir=output_dir)
	os.close(fn)
	try:
		if verbose:
			print 'Creating %r (%r)' % (final_path, tmp_path)
		tar = tarfile.TarFile(name=tmp_path, mode='w')
		tar.add(local_dir, arcname=out_name, recursive=True)
		tar.close()

		if verbose:
			print 'Compressing...'
		if os.system('gzip "%s"' % tmp_path) != 0:
			raise ValueError('Failed to compress')
		tmp_path += '.gz'
                os.chmod(tmp_path, 0644)
		os.rename(tmp_path, final_path)
	except:
		os.remove(tmp_path)
		raise

def create_tar_bz2(local_dir, output_dir=None, verbose=False):
	import tarfile, bzrlib
	out_name = os.path.basename(local_dir) + '-' + str(bzrlib.Branch(local_dir).revno())
	final_path = os.path.join(output_dir, out_name + '.tar.bz2')
	if os.path.exists(final_path):
		if verbose:
			print 'Output file already exists: %r' % final_path
		return
	fn, tmp_path=tempfile.mkstemp(suffix='.tar', prefix=out_name, dir=output_dir)
	os.close(fn)
	try:
		if verbose:
			print 'Creating %r (%r)' % (final_path, tmp_path)
		tar = tarfile.TarFile(name=tmp_path, mode='w')
		tar.add(local_dir, arcname=out_name, recursive=True)
		tar.close()

		if verbose:
			print 'Compressing...'
		if os.system('bzip2 "%s"' % tmp_path) != 0:
			raise ValueError('Failed to compress')
		tmp_path += '.bz2'
                os.chmod(tmp_path, 0644)
		os.rename(tmp_path, final_path)
	except:
		os.remove(tmp_path)
		raise

def create_zip(local_dir, output_dir=None, verbose=False):
	import zipfile, bzrlib
	out_name = os.path.basename(local_dir) + '-' + str(bzrlib.Branch(local_dir).revno())
	final_path = os.path.join(output_dir, out_name + '.zip')
	if os.path.exists(final_path):
		if verbose:
			print 'Output file already exists: %r' % final_path
		return
	fn, tmp_path=tempfile.mkstemp(suffix='.zip', prefix=out_name, dir=output_dir)
	os.close(fn)
	try:
		if verbose:
			print 'Creating %r (%r)' % (final_path, tmp_path)
		zip = zipfile.ZipFile(file=tmp_path, mode='w')
		try:
			for root, dirs, files in os.walk(local_dir):
				for f in files:
					path = os.path.join(root, f)
					arcname = os.path.join(out_name, path[len(local_dir)+1:])
					zip.write(path, arcname=arcname)
		finally:
			zip.close()

                os.chmod(tmp_path, 0644)
		os.rename(tmp_path, final_path)
	except:
		os.remove(tmp_path)
		raise

def get_local_dir(remote, local):
	"""This returns the full path to the local directory where 
	the files are kept.

	rsync has the trick that if the source directory ends in a '/' then
	the file will be copied *into* the target. If it does not end in a slash,
	then the directory will be added into the target.
	"""
	if remote[-1:] == '/':
		return local
	# rsync paths are typically user@host:path/to/something
	# the reason for the split(':') is in case path doesn't contain a slash
	extra = remote.split(':')[-1].split('/')[-1]
	return os.path.join(local, extra)

def get_output_dir(output, local):
	if output:
		return output
	return os.path.dirname(os.path.realpath(local))


def main(args):
	import optparse
	p = optparse.OptionParser(usage='%prog [options] [remote] [local]'
		'\n  rsync the remote repository to the local directory'
		'\n  if remote is not given, it defaults to "bazaar-ng.org::bazaar-ng/bzr/bzr.dev"'
		'\n  if local is not given it defaults to "."')

        p.add_option('--verbose', action='store_true'
                , help="Describe the process")
	p.add_option('--no-tar-gz', action='store_false', dest='create_tar_gz', default=True
		, help="Don't create a gzip compressed tarfile.")
	p.add_option('--no-tar-bz2', action='store_false', dest='create_tar_bz2', default=True
		, help="Don't create a bzip2 compressed tarfile.")
	p.add_option('--no-zip', action='store_false', dest='create_zip', default=True
		, help="Don't create a zipfile.")
	p.add_option('--output-dir', default=None
		, help="Set the output location, default is just above the final local directory.")


	(opts, args) = p.parse_args(args)

	if len(args) < 1:
		remote = 'bazaar-ng.org::bazaar-ng/bzr/bzr.dev'
	else:
		remote = args[0]
	if len(args) < 2:
		local = '.'
	else:
		local = args[1]
	if len(args) > 2:
		print 'Invalid number of arguments, see --help for details.'

	if not sync(remote, local, verbose=opts.verbose):
		if opts.verbose:
			print '** rsync failed'
		return 1
	# Now we have the new update
	local_dir = get_local_dir(remote, local)

	output_dir = get_output_dir(opts.output_dir, local_dir)
	if opts.create_tar_gz:
		create_tar_gz(local_dir, output_dir=output_dir, verbose=opts.verbose)
	if opts.create_tar_bz2:
		create_tar_bz2(local_dir, output_dir=output_dir, verbose=opts.verbose)
	if opts.create_zip:
		create_zip(local_dir, output_dir=output_dir, verbose=opts.verbose)

	return 0
		
if __name__ == '__main__':
	sys.exit(main(sys.argv[1:]))