Merge lp:~bac/lpsetup/addheaders into lp:lpsetup

Proposed by Brad Crittenden on 2012-07-03
Status: Merged
Approved by: Gary Poster on 2012-07-03
Approved revision: 42
Merged at revision: 41
Proposed branch: lp:~bac/lpsetup/addheaders
Merge into: lp:lpsetup
Diff against target: 308 lines (+86/-6)
11 files modified
README.rst (+1/-1)
lpsetup/subcommands/get.py (+3/-2)
lpsetup/subcommands/inithost.py (+5/-1)
lpsetup/subcommands/initlxc.py (+2/-0)
lpsetup/subcommands/install.py (+2/-0)
lpsetup/subcommands/lxcinstall.py (+5/-1)
lpsetup/templates/lp-setup-lxc-build (+2/-0)
lpsetup/templates/lp-setup-lxc-cleanup (+2/-0)
lpsetup/templates/lp-setup-lxc-test (+2/-0)
lpsetup/tests/test_utils.py (+38/-0)
lpsetup/utils.py (+24/-1)
To merge this branch: bzr merge lp:~bac/lpsetup/addheaders
Reviewer Review Type Date Requested Status
Gary Poster (community) 2012-07-03 Approve on 2012-07-03
Review via email: mp+113270@code.launchpad.net

Commit Message

Add headers to created or modified system files.

Description of the Change

Add headers to created or modified system files. Here's what one looks like:

"""
#!/bin/sh
# Copyright 2012 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# This file created at 2012-07-03T17:36:53.075414UTC by
# /home/bac/lpsetup/addheaders/lp-setup, version 0.2.2.

# Run the Launchpad build inside LXC.

# Assumptions:
# * This script is run as root.
"""

To post a comment you must log in.
lp:~bac/lpsetup/addheaders updated on 2012-07-03
41. By Brad Crittenden on 2012-07-03

Fixed merge conflict

42. By Brad Crittenden on 2012-07-03

Remove overly specific --2a specifier on bzr init command.

Gary Poster (gary) wrote :

Thank you! Looks great.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'README.rst'
2--- README.rst 2012-06-27 10:17:46 +0000
3+++ README.rst 2012-07-03 18:22:19 +0000
4@@ -22,7 +22,7 @@
5
6 lp-setup install
7
8-If instead tou want to install the Launchpad environment inside an LXC::
9+If instead you want to install the Launchpad environment inside an LXC::
10
11 lp-setup lxc-install
12
13
14=== modified file 'lpsetup/subcommands/get.py'
15--- lpsetup/subcommands/get.py 2012-06-27 09:10:56 +0000
16+++ lpsetup/subcommands/get.py 2012-07-03 18:22:19 +0000
17@@ -84,9 +84,10 @@
18 """Create a repo for the Launchpad code and retrieve it."""
19 # TODO Add --no-trees handling.
20 with su(user):
21- # Set up the repository.
22+ # Set up the repository, but only if it needs it.
23 mkdirs(directory)
24- call('bzr', 'init-repo', '--2a', directory)
25+ if not os.path.exists(os.path.join(directory, '.bzr')):
26+ call('bzr', 'init-repo', directory)
27 # Set up the codebase.
28 checkout_dir = os.path.join(directory, LP_CHECKOUT)
29 setup_codebase(user, checkout_dir, dependencies_dir, valid_ssh_keys)
30
31=== modified file 'lpsetup/subcommands/inithost.py'
32--- lpsetup/subcommands/inithost.py 2012-07-03 16:45:18 +0000
33+++ lpsetup/subcommands/inithost.py 2012-07-03 18:22:19 +0000
34@@ -37,7 +37,10 @@
35 LP_PACKAGES,
36 SSH_KEY_NAME,
37 )
38-from lpsetup.utils import call
39+from lpsetup.utils import (
40+ call,
41+ get_file_header,
42+ )
43
44
45 def write_file_contents(filename, contents, mode):
46@@ -120,6 +123,7 @@
47 (auth_file, public_key, 'a'),
48 (known_hosts, known_host_content, 'a'),
49 ]:
50+ contents = "{0}\n{1}".format(get_file_header(), contents)
51 write_file_contents(filename, contents, mode)
52 os.chmod(ssh_key_path, 0600)
53 # Set up bzr and Launchpad authentication.
54
55=== modified file 'lpsetup/subcommands/initlxc.py'
56--- lpsetup/subcommands/initlxc.py 2012-06-28 17:22:45 +0000
57+++ lpsetup/subcommands/initlxc.py 2012-07-03 18:22:19 +0000
58@@ -41,6 +41,7 @@
59 from lpsetup.utils import (
60 call,
61 get_container_path,
62+ get_file_header,
63 get_lxc_gateway,
64 lxc_stopped,
65 render_to_file,
66@@ -79,6 +80,7 @@
67 'Error: LXC bridge interface not found.')
68 content = LXC_OPTIONS.format(interface=lxc_gateway)
69 with open(LXC_CONFIG_TEMPLATE, 'w') as f:
70+ f.write(get_file_header() + '\n')
71 f.write(content)
72 # Creating container.
73 call(
74
75=== modified file 'lpsetup/subcommands/install.py'
76--- lpsetup/subcommands/install.py 2012-06-27 09:10:56 +0000
77+++ lpsetup/subcommands/install.py 2012-07-03 18:22:19 +0000
78@@ -40,6 +40,7 @@
79 from lpsetup.utils import (
80 call,
81 ConfigParser,
82+ get_file_header,
83 )
84
85
86@@ -92,6 +93,7 @@
87 for option, value in options.items():
88 parser.set(section, option, value.format(**context))
89 with open(path, 'w') as f:
90+ f.write(get_file_header() + '\n')
91 parser.write(f)
92
93
94
95=== modified file 'lpsetup/subcommands/lxcinstall.py'
96--- lpsetup/subcommands/lxcinstall.py 2012-06-28 17:04:14 +0000
97+++ lpsetup/subcommands/lxcinstall.py 2012-07-03 18:22:19 +0000
98@@ -30,6 +30,7 @@
99 install,
100 )
101 from lpsetup.utils import (
102+ get_file_header,
103 render_to_file,
104 sshlxc as ssh,
105 this_command,
106@@ -38,11 +39,13 @@
107
108 def create_scripts(lxc_name, ssh_key_path, user):
109 """Create scripts to update the Launchpad environment and run tests."""
110+
111 context = {
112 'lxcip': LXC_IP_COMMAND[0],
113 'lxc_name': lxc_name,
114 'ssh_key_path': ssh_key_path,
115 'user': user,
116+ 'installation_header': get_file_header(initial_indent=''),
117 }
118 scripts = []
119 # Generate the scripts.
120@@ -56,6 +59,7 @@
121 sudoers_contents = '{user} ALL = (ALL) NOPASSWD: {scripts}\n'.format(
122 user=user, scripts=', '. join(scripts))
123 with open(sudoers_file, 'w') as sudoers:
124+ sudoers.write(get_file_header() + '\n')
125 sudoers.write(sudoers_contents)
126 # The sudoers must have this mode or it will be ignored.
127 os.chmod(sudoers_file, 0440)
128@@ -72,7 +76,7 @@
129 # Use ssh to call this script from inside the container.
130 args = [
131 'install', '-u', user, '-s', 'setup_apt', 'setup_launchpad',
132- '-d', dependencies_dir, '-c', directory
133+ '-d', dependencies_dir, '-c', directory,
134 ]
135 cmd = this_command(directory, args)
136 ssh(lxc_name, cmd, key=ssh_key_path)
137
138=== modified file 'lpsetup/templates/lp-setup-lxc-build'
139--- lpsetup/templates/lp-setup-lxc-build 2012-05-24 17:25:39 +0000
140+++ lpsetup/templates/lp-setup-lxc-build 2012-07-03 18:22:19 +0000
141@@ -2,6 +2,8 @@
142 # Copyright 2012 Canonical Ltd. This software is licensed under the
143 # GNU Affero General Public License version 3 (see the file LICENSE).
144
145+# {installation_header}
146+
147 # Run the Launchpad build inside LXC.
148
149 # Assumptions:
150
151=== modified file 'lpsetup/templates/lp-setup-lxc-cleanup'
152--- lpsetup/templates/lp-setup-lxc-cleanup 2012-04-10 17:16:25 +0000
153+++ lpsetup/templates/lp-setup-lxc-cleanup 2012-07-03 18:22:19 +0000
154@@ -2,6 +2,8 @@
155 # Copyright 2012 Canonical Ltd. This software is licensed under the
156 # GNU Affero General Public License version 3 (see the file LICENSE).
157
158+# {installation_header}
159+
160 # Cleanup remnants of LXC containers from previous runs.
161
162 # Runs of LXC may leave cruft laying around that interferes with
163
164=== modified file 'lpsetup/templates/lp-setup-lxc-test'
165--- lpsetup/templates/lp-setup-lxc-test 2012-06-27 17:59:09 +0000
166+++ lpsetup/templates/lp-setup-lxc-test 2012-07-03 18:22:19 +0000
167@@ -2,6 +2,8 @@
168 # Copyright 2012 Canonical Ltd. This software is licensed under the
169 # GNU Affero General Public License version 3 (see the file LICENSE).
170
171+# {installation_header}
172+
173 # Test Launchpad using LXC ephemeral instances.
174
175 # Assumptions:
176
177=== modified file 'lpsetup/tests/test_utils.py'
178--- lpsetup/tests/test_utils.py 2012-05-15 13:53:23 +0000
179+++ lpsetup/tests/test_utils.py 2012-07-03 18:22:19 +0000
180@@ -4,6 +4,7 @@
181
182 """Tests for the utils module."""
183
184+from datetime import datetime
185 import getpass
186 import os
187 import shutil
188@@ -12,6 +13,7 @@
189 import tempfile
190 import unittest
191
192+from lpsetup import get_version
193 from lpsetup.settings import (
194 LXC_LP_DIR_PATTERN,
195 LXC_LP_TEST_DIR_PATTERN,
196@@ -20,6 +22,7 @@
197 from lpsetup.utils import (
198 ConfigParser,
199 get_container_path,
200+ get_file_header,
201 get_lxc_gateway,
202 get_network_interfaces,
203 get_running_containers,
204@@ -63,6 +66,41 @@
205 get_container_path('mycontainer', 'home'))
206
207
208+class GetFileHeaderTest(unittest.TestCase):
209+
210+ def setUp(self):
211+ self.version = get_version()
212+ self.t = datetime(1969, 7, 20, 20, 18)
213+ self.fake_program = 'fake_program'
214+
215+ def test_getfileheader_time(self):
216+ header = get_file_header(program=self.fake_program, now=self.t)
217+ expected = '# This file created at {}UTC'.format(self.t.isoformat())
218+ self.assertTrue(header.startswith(expected))
219+
220+ def test_getfileheader_executable(self):
221+ header = get_file_header(program=self.fake_program, now=self.t)
222+ expected = 'by ' + self.fake_program
223+ self.assertIn(expected, header)
224+
225+ def test_getfileheader_version(self):
226+ version = '{}.'.format(get_version())
227+ header = get_file_header(program=self.fake_program, now=self.t)
228+ self.assertTrue(header.endswith(version))
229+
230+ def test_getfileheader_indents(self):
231+ long_program_name = "/opt/usr/local/bin/program/name/really/long.rb"
232+ initial = '* '
233+ subsequent = '** '
234+ header = get_file_header(
235+ program=long_program_name, now=self.t,
236+ initial_indent=initial, subsequent_indent=subsequent)
237+ lines = header.split('\n')
238+ self.assertTrue(lines[0].startswith(initial))
239+ for line in lines[1:]:
240+ self.assertTrue(line.startswith(subsequent))
241+
242+
243 class GetLXCGatewayTest(unittest.TestCase):
244
245 interfaces = ('eth0', 'virbr0', 'lxcbr0', 'lo')
246
247=== modified file 'lpsetup/utils.py'
248--- lpsetup/utils.py 2012-06-22 15:01:49 +0000
249+++ lpsetup/utils.py 2012-07-03 18:22:19 +0000
250@@ -8,6 +8,7 @@
251 __all__ = [
252 'call',
253 'get_container_path',
254+ 'get_file_header',
255 'get_lxc_gateway',
256 'get_network_interfaces',
257 'get_running_containers',
258@@ -22,6 +23,7 @@
259 ]
260
261 from ConfigParser import RawConfigParser
262+from datetime import datetime
263 from functools import (
264 partial,
265 wraps,
266@@ -32,6 +34,7 @@
267 import subprocess
268 import shutil
269 import sys
270+import textwrap
271 import time
272
273 from shelltoolbox import (
274@@ -41,7 +44,10 @@
275 ssh,
276 )
277
278-from lpsetup import exceptions
279+from lpsetup import (
280+ exceptions,
281+ get_version,
282+ )
283 from lpsetup.settings import (
284 LXC_LP_DIR_PATTERN,
285 LXC_LP_TEST_DIR_PATTERN,
286@@ -143,6 +149,23 @@
287 lxc_stopped = partial(lxc_in_state, 'STOPPED')
288
289
290+def get_file_header(program=sys.argv[0], now=None,
291+ initial_indent='# ', subsequent_indent='# '):
292+ if now is None:
293+ now = datetime.utcnow()
294+ header = (
295+ 'This file created at {date}UTC by {program}, '
296+ 'version {version}.'.format(
297+ date=datetime.isoformat(now),
298+ program=program,
299+ version=get_version(),
300+ )
301+ )
302+ return textwrap.fill(
303+ header, initial_indent=initial_indent,
304+ subsequent_indent=subsequent_indent)
305+
306+
307 def render_to_file(template_name, context, dest, templates_dir=None):
308 """Render `template_name` using `context`. Write the result inside `dest`.
309

Subscribers

People subscribed via source and target branches