Skip to content

Commit 82cc5e5

Browse files
committed
[OpenWrt] Avoid error with "sysupgrade -r"
1 parent 30034a6 commit 82cc5e5

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

netjsonconfig/backends/openwrt/openwrt.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,21 @@ def generate(self, name='openwrt-config'):
141141
package_name = lines[0]
142142
text_contents = '\n'.join(lines[2:])
143143
byte_contents = BytesIO(text_contents.encode('utf8'))
144-
info = tarfile.TarInfo(name='/etc/config/{0}'.format(package_name))
144+
info = tarfile.TarInfo(name='etc/config/{0}'.format(package_name))
145145
info.size = len(text_contents)
146146
tar.addfile(tarinfo=info, fileobj=byte_contents)
147147
# insert additional files
148148
for file_item in self.config.get('files', []):
149149
contents = file_item['contents']
150+
path = file_item['path']
151+
# join lines if contents is a list
150152
if isinstance(contents, list):
151153
contents = '\n'.join(contents)
154+
# remove leading slashes from path
155+
if path.startswith('/'):
156+
path = path[1:]
152157
byte_contents = BytesIO(contents.encode('utf8'))
153-
info = tarfile.TarInfo(name=file_item['path'])
158+
info = tarfile.TarInfo(name=path)
154159
info.size = len(contents)
155160
tar.addfile(tarinfo=info, fileobj=byte_contents)
156161
# close archive

tests/openwrt/test_backend.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_generate(self):
142142
tar = tarfile.open('openwrt-config.tar.gz', 'r:gz')
143143
self.assertEqual(len(tar.getmembers()), 2)
144144
# network
145-
network = tar.getmember('/etc/config/network')
145+
network = tar.getmember('etc/config/network')
146146
contents = tar.extractfile(network).read().decode()
147147
expected = self._tabs("""config interface 'wlan0'
148148
option ifname 'wlan0'
@@ -152,7 +152,7 @@ def test_generate(self):
152152
""")
153153
self.assertEqual(contents, expected)
154154
# wireless
155-
wireless = tar.getmember('/etc/config/wireless')
155+
wireless = tar.getmember('etc/config/wireless')
156156
contents = tar.extractfile(wireless).read().decode()
157157
expected = self._tabs("""config wifi-device 'radio0'
158158
option channel '3'
@@ -281,11 +281,11 @@ def test_file_inclusion(self):
281281
tar = tarfile.open('openwrt-config.tar.gz', 'r:gz')
282282
self.assertEqual(len(tar.getmembers()), 2)
283283
# first file
284-
crontab = tar.getmember('/etc/crontabs/root')
284+
crontab = tar.getmember('etc/crontabs/root')
285285
contents = tar.extractfile(crontab).read().decode()
286286
self.assertEqual(contents, o.config['files'][0]['contents'])
287287
# second file
288-
dummy = tar.getmember('/etc/dummy.conf')
288+
dummy = tar.getmember('etc/dummy.conf')
289289
contents = tar.extractfile(dummy).read().decode()
290290
self.assertEqual(contents, o.config['files'][1]['contents'])
291291
# close and delete tar.gz file
@@ -309,7 +309,7 @@ def test_file_inclusion_list_contents(self):
309309
tar = tarfile.open('openwrt-config.tar.gz', 'r:gz')
310310
self.assertEqual(len(tar.getmembers()), 1)
311311
# check file
312-
crontab = tar.getmember('/root/.ssh/authorized_keys')
312+
crontab = tar.getmember('root/.ssh/authorized_keys')
313313
contents = tar.extractfile(crontab).read().decode()
314314
self.assertEqual(contents, '\n'.join(o.config['files'][0]['contents']))
315315
# close and delete tar.gz file

0 commit comments

Comments
 (0)