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
|
#!/usr/bin/python
import contextlib
import os
import shutil
import subprocess
import sys
import tempfile
import optparse
# Use the util functions from cloudinit
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(
sys.argv[0]), os.pardir, os.pardir))
if os.path.exists(os.path.join(possible_topdir, "cloudinit", "__init__.py")):
sys.path.insert(0, possible_topdir)
from cloudinit import util
def find_versioned_files():
(stdout, _stderr) = util.subp(['bzr', 'ls', '--versioned', '--recursive'])
fns = [fn for fn in stdout.splitlines()
if fn and not fn.startswith('.')]
fns.sort()
return fns
def copy(fn, where_to, verbose):
if verbose:
print("Copying %r --> %r" % (fn, where_to))
if os.path.isfile(fn):
shutil.copy(fn, where_to)
elif os.path.isdir(fn) and not os.path.isdir(where_to):
os.makedirs(where_to)
else:
raise RuntimeError("Do not know how to copy %s" % (fn))
def main():
parser = optparse.OptionParser()
parser.add_option("-f", "--file", dest="filename",
help="write archive to FILE", metavar="FILE")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="show verbose messaging")
(options, args) = parser.parse_args()
base_fn = options.filename
if not base_fn:
(stdout, _stderr) = util.subp(['bzr', 'revno'])
revno = stdout.strip()
cmd = [sys.executable,
util.abs_join(os.pardir, 'tools', 'read-version')]
(stdout, _stderr) = util.subp(cmd)
version = stdout.strip()
base_fn = 'cloud-init-%s-%s' % (version, revno)
with util.tempdir() as tdir:
util.ensure_dir(util.abs_join(tdir, base_fn))
arch_fn = '%s.tar.gz' % (base_fn)
with util.chdir(os.pardir):
fns = find_versioned_files()
for fn in fns:
copy(fn, util.abs_join(tdir, base_fn, fn),
verbose=options.verbose)
arch_full_fn = util.abs_join(tdir, arch_fn)
cmd = ['tar', '-czvf', arch_full_fn, '-C', tdir, base_fn]
if options.verbose:
print("Creating an archive from directory %r to %r" %
(util.abs_join(tdir, base_fn), arch_full_fn))
util.subp(cmd, capture=(not options.verbose))
shutil.move(util.abs_join(tdir, arch_fn),
util.abs_join(os.getcwd(), arch_fn))
print(os.path.abspath(arch_fn))
return 0
if __name__ == '__main__':
sys.exit(main())
|