Skip to content

Commit

Permalink
[OpenWrt] Added file permission feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Nov 24, 2015
1 parent 6b33a98 commit 66ee96d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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)
4 changes: 4 additions & 0 deletions netjsonconfig/backends/openwrt/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@
{"type": "string", "format": "textarea"},
{"type": "array"}
]
},
"mode": {
"type": "string",
"maxLength": 4
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/openwrt/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')

0 comments on commit 66ee96d

Please sign in to comment.