Merge lp:~raharper/curtin/trunk-lp1731709-curthooks-write-files into lp:~curtin-dev/curtin/trunk

Proposed by Ryan Harper
Status: Merged
Merged at revision: 543
Proposed branch: lp:~raharper/curtin/trunk-lp1731709-curthooks-write-files
Merge into: lp:~curtin-dev/curtin/trunk
Diff against target: 125 lines (+75/-1)
4 files modified
curtin/commands/curthooks.py (+2/-0)
curtin/futil.py (+13/-0)
tests/unittests/helpers.py (+13/-0)
tests/unittests/test_curthooks.py (+47/-1)
To merge this branch: bzr merge lp:~raharper/curtin/trunk-lp1731709-curthooks-write-files
Reviewer Review Type Date Requested Status
Scott Moser (community) Approve
Server Team CI bot continuous-integration Approve
Review via email: mp+333714@code.launchpad.net

Description of the change

Re-add curthooks.write_files method for backwards compat

In revno 512, curthooks.write_files() was re-implemented under
curtin.futil.write_files and supports the original dictionary
format as well as other modes. This broke legacy images which used
hooks that invoked curthooks.write_files. This branch restores
the function and behavior while utilizing the change in implementation.

- Add warning when legacy path is called pointing to new module location
- Add unittests to exercise the code path.

LP: #1731709

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Scott Moser (smoser) wrote :

To my knowledge this did not occur in any officially supported image.
I believe it was a "custom image".

While we do not want to willy-nilly break things, we also have been
very un-careful about curtin usage as a library. We need to clean that
up, basically setting __all__ and only exposing methods we promise to keep.

If we do want to make this backwards compatible, I'd prefer to add a
warning also.

Revision history for this message
Scott Moser (smoser) wrote :

I'm not entirely opposed to fixing this.

However, I suggest we should put the actual definition of the backwards compatibility either:
a.) inside futil.write_files entirely and then in util:
    from futil import write_files

b.) inside futil._legacy_write_files and then in util, do
    from futil import _legacy_write_files as write_files

Then also, I'd prefer that we just unit test this.
We dont' need an integration test for it.

Lastly any way its called, we should warn with a deprecated warning.
http://www.arruda.blog.br/programacao/python-use-warnings/

Revision history for this message
Ryan Harper (raharper) wrote :

On Fri, Nov 17, 2017 at 1:25 PM, Scott Moser <email address hidden>
wrote:

> I'm not entirely opposed to fixing this.
>
> However, I suggest we should put the actual definition of the backwards
> compatibility either:
> a.) inside futil.write_files entirely and then in util:
> from futil import write_files
>
> b.) inside futil._legacy_write_files and then in util, do
> from futil import _legacy_write_files as write_files
>

This was a little confusing; what I think is reasonable
is to move curthooks.write_files (which is the legacy impl)
and move that to futil._legacy_write_files (and have it call write_files
directly)

And in curthooks, change the import as you show in (b).

in certain places where curthooks uses futil.write_files; those stay
but curthooks will have a 'write_files' attr which will point to
futil._legacy_write_files.

Do you still want to LOG a message/warning that users should instead use
futil.write_files instead of curthooks ?

>
> Then also, I'd prefer that we just unit test this.
>

We do.

> We dont' need an integration test for it.
>

I think it's a reasonable check.

>
> Lastly any way its called, we should warn with a deprecated warning.
> http://www.arruda.blog.br/programacao/python-use-warnings/
> --
> https://code.launchpad.net/~raharper/curtin/trunk-
> lp1731709-curthooks-write-files/+merge/333714
> You are the owner of lp:~raharper/curtin/trunk-lp1731709-curthooks-write-
> files.
>

Revision history for this message
Scott Moser (smoser) wrote :
Revision history for this message
Scott Moser (smoser) wrote :

Ryan and I talked about my MP linked above, and I think he was generally ok witih the suggested changes.

I think this is ready to go, but will wait for an "OK" from Ryan, or him pulling my branch into his.

If that is done, then I approve.
Just update the commit message appropriately (currently mentions vmtest tests)

544. By Ryan Harper

Move curthooks.write_files into futil._legacy_write_files

545. By Ryan Harper

merge from smoser write-files

- fixed up style check
- modified docstring in ftutil._legacy_write_files

Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Scott Moser (smoser) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'curtin/commands/curthooks.py'
2--- curtin/commands/curthooks.py 2017-10-05 18:59:05 +0000
3+++ curtin/commands/curthooks.py 2017-11-27 18:50:41 +0000
4@@ -36,6 +36,8 @@
5
6 from . import populate_one_subcmd
7
8+write_files = futil._legacy_write_files # LP: #1731709
9+
10 CMD_ARGUMENTS = (
11 ((('-t', '--target'),
12 {'help': 'operate on target. default is env[TARGET_MOUNT_POINT]',
13
14=== modified file 'curtin/futil.py'
15--- curtin/futil.py 2017-07-28 19:01:38 +0000
16+++ curtin/futil.py 2017-11-27 18:50:41 +0000
17@@ -18,6 +18,7 @@
18 import grp
19 import pwd
20 import os
21+import warnings
22
23 from .util import write_file, target_path
24 from .log import LOG
25@@ -101,3 +102,15 @@
26 content=info.get('content', ''),
27 owner=info.get('owner', "-1:-1"),
28 perms=info.get('permissions', info.get('perms', "0644")))
29+
30+
31+def _legacy_write_files(cfg, base_dir=None):
32+ """Backwards compatibility for curthooks.write_files (LP: #1731709)
33+ It needs to work like:
34+ curthooks.write_files(cfg, target)
35+ cfg is a 'cfg' dictionary with a 'write_files' entry in it.
36+ """
37+ warnings.warn(
38+ "write_files use from curtin.util is deprecated. "
39+ "Please use curtin.futil.write_files.", DeprecationWarning)
40+ return write_files(cfg.get('write_files', {}), base_dir=base_dir)
41
42=== modified file 'tests/unittests/helpers.py'
43--- tests/unittests/helpers.py 2017-08-03 19:31:49 +0000
44+++ tests/unittests/helpers.py 2017-11-27 18:50:41 +0000
45@@ -79,3 +79,16 @@
46 if _dir is None:
47 _dir = self.tmp_dir()
48 return os.path.normpath(os.path.abspath(os.path.join(_dir, path)))
49+
50+
51+def dir2dict(startdir, prefix=None):
52+ flist = {}
53+ if prefix is None:
54+ prefix = startdir
55+ for root, dirs, files in os.walk(startdir):
56+ for fname in files:
57+ fpath = os.path.join(root, fname)
58+ key = fpath[len(prefix):]
59+ with open(fpath, "r") as fp:
60+ flist[key] = fp.read()
61+ return flist
62
63=== modified file 'tests/unittests/test_curthooks.py'
64--- tests/unittests/test_curthooks.py 2017-08-03 18:14:51 +0000
65+++ tests/unittests/test_curthooks.py 2017-11-27 18:50:41 +0000
66@@ -5,7 +5,7 @@
67 from curtin import util
68 from curtin import config
69 from curtin.reporter import events
70-from .helpers import CiTestCase
71+from .helpers import CiTestCase, dir2dict
72
73
74 class TestGetFlashKernelPkgs(CiTestCase):
75@@ -761,4 +761,50 @@
76 curthooks.detect_required_packages({'network': {'version': 3}})
77
78
79+class TestCurthooksWriteFiles(CiTestCase):
80+ def test_handle_write_files_empty(self):
81+ """ Test curthooks.write_files returns for empty config """
82+ tmpd = self.tmp_dir()
83+ ret = curthooks.write_files({}, tmpd)
84+ self.assertEqual({}, dir2dict(tmpd, prefix=tmpd))
85+ self.assertIsNone(ret)
86+
87+ def test_handle_write_files(self):
88+ """ Test curthooks.write_files works as it used to """
89+ tmpd = self.tmp_dir()
90+ cfg = {'file1': {'path': '/etc/default/hello.txt',
91+ 'content': "Hello World!\n"},
92+ 'foobar': {'path': '/sys/wark', 'content': "Engauge!\n"}}
93+ curthooks.write_files({'write_files': cfg}, tmpd)
94+ self.assertEqual(
95+ dict((cfg[i]['path'], cfg[i]['content']) for i in cfg.keys()),
96+ dir2dict(tmpd, prefix=tmpd))
97+
98+ @patch('curtin.commands.curthooks.futil.target_path')
99+ @patch('curtin.commands.curthooks.futil.write_finfo')
100+ def test_handle_write_files_finfo(self, mock_write_finfo, mock_tp):
101+ """ Validate that futils.write_files handles target_path correctly """
102+ cc_target = "/tmpXXXX/random/dir/used/by/maas"
103+ cfg = {
104+ 'file1': {
105+ 'path': '/etc/default/hello.txt',
106+ 'content': "Hello World!\n",
107+ },
108+ }
109+ mock_tp.side_effect = [
110+ cc_target + cfg['file1']['path'],
111+ ]
112+
113+ expected_cfg = {
114+ 'file1': {
115+ 'path': '/etc/default/hello.txt',
116+ 'content': cfg['file1']['content']},
117+ }
118+ curthooks.write_files({'write_files': cfg}, cc_target)
119+ mock_write_finfo.assert_called_with(
120+ content=expected_cfg['file1']['content'], owner='-1:-1',
121+ path=cc_target + expected_cfg['file1']['path'],
122+ perms='0644')
123+
124+
125 # vi: ts=4 expandtab syntax=python

Subscribers

People subscribed via source and target branches