From 66ee96d1cc5a0e7823082e6c6abaa7d8f7f63909 Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Tue, 24 Nov 2015 14:50:52 +0100 Subject: [PATCH] [OpenWrt] Added file permission feature --- netjsonconfig/backends/openwrt/openwrt.py | 6 ++++-- netjsonconfig/backends/openwrt/schema.py | 4 ++++ tests/openwrt/test_backend.py | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/netjsonconfig/backends/openwrt/openwrt.py b/netjsonconfig/backends/openwrt/openwrt.py index 885ccd073..d9269b030 100644 --- a/netjsonconfig/backends/openwrt/openwrt.py +++ b/netjsonconfig/backends/openwrt/openwrt.py @@ -137,9 +137,10 @@ def _add_files(self, tar, timestamp): self._add_file(tar=tar, name=path, contents=contents, - timestamp=timestamp) + timestamp=timestamp, + mode=file_item.get('mode', '644')) - def _add_file(self, tar, name, contents, timestamp): + def _add_file(self, tar, name, contents, timestamp, mode='644'): """ adds a single file in tar object """ @@ -148,4 +149,5 @@ def _add_file(self, tar, name, contents, timestamp): info.size = len(contents) info.mtime = timestamp info.type = tarfile.REGTYPE + info.mode = int(mode, 8) # permissions converted to decimal notation tar.addfile(tarinfo=info, fileobj=byte_contents) diff --git a/netjsonconfig/backends/openwrt/schema.py b/netjsonconfig/backends/openwrt/schema.py index 624e3cebc..8108093cc 100644 --- a/netjsonconfig/backends/openwrt/schema.py +++ b/netjsonconfig/backends/openwrt/schema.py @@ -295,6 +295,10 @@ {"type": "string", "format": "textarea"}, {"type": "array"} ] + }, + "mode": { + "type": "string", + "maxLength": 4 } } } diff --git a/tests/openwrt/test_backend.py b/tests/openwrt/test_backend.py index 56261049d..c07aa815f 100644 --- a/tests/openwrt/test_backend.py +++ b/tests/openwrt/test_backend.py @@ -281,10 +281,12 @@ def test_file_inclusion(self): contents = tar.extractfile(crontab).read().decode() self.assertEqual(contents, o.config['files'][0]['contents']) self.assertNotEqual(crontab.mtime, 0) + self.assertEqual(crontab.mode, 420) # second file dummy = tar.getmember('etc/dummy.conf') contents = tar.extractfile(dummy).read().decode() self.assertEqual(contents, o.config['files'][1]['contents']) + self.assertEqual(dummy.mode, 420) # close and delete tar.gz file tar.close() os.remove('openwrt-config.tar.gz') @@ -312,3 +314,22 @@ def test_file_inclusion_list_contents(self): # close and delete tar.gz file tar.close() os.remove('openwrt-config.tar.gz') + + def test_file_permissions(self): + o = OpenWrt({ + "files": [ + { + "path": "/tmp/hello.sh", + "contents": "echo 'hello world'", + "mode": "0755" + } + ] + }) + o.generate() + tar = tarfile.open('openwrt-config.tar.gz', 'r:gz') + script = tar.getmember('tmp/hello.sh') + # check permissions + self.assertEqual(script.mode, 493) + # close and delete tar.gz file + tar.close() + os.remove('openwrt-config.tar.gz')