diff --git a/netjsonconfig/backends/openwisp/openwisp.py b/netjsonconfig/backends/openwisp/openwisp.py index 4069674a2..87d1ddf1b 100644 --- a/netjsonconfig/backends/openwisp/openwisp.py +++ b/netjsonconfig/backends/openwisp/openwisp.py @@ -1,7 +1,4 @@ import re -import gzip -import tarfile -from io import BytesIO from jinja2 import Environment, PackageLoader @@ -151,9 +148,16 @@ def generate(self): :returns: in-memory tar.gz archive, instance of ``BytesIO`` """ + return super(OpenWisp, self).generate() + + def _generate_contents(self, tar): + """ + Adds configuration files to tarfile instance. + + :param tar: tarfile instance + :returns: None + """ uci = self.render(files=False) - tar_bytes = BytesIO() - tar = tarfile.open(fileobj=tar_bytes, mode='w') # create a list with all the packages (and remove empty entries) packages = re.split('package ', uci) if '' in packages: @@ -177,14 +181,3 @@ def generate(self): self._add_openvpn_scripts() # add tc_script self._add_tc_script() - # add files resulting archive - self._add_files(tar) - # close archive - tar.close() - tar_bytes.seek(0) - gzip_bytes = BytesIO() - gz = gzip.GzipFile(fileobj=gzip_bytes, mode='wb', mtime=0) - gz.write(tar_bytes.getvalue()) - gz.close() - gzip_bytes.seek(0) # set pointer to beginning of stream - return gzip_bytes diff --git a/netjsonconfig/backends/openwrt/openwrt.py b/netjsonconfig/backends/openwrt/openwrt.py index 249d71e9e..c2adca207 100644 --- a/netjsonconfig/backends/openwrt/openwrt.py +++ b/netjsonconfig/backends/openwrt/openwrt.py @@ -154,22 +154,10 @@ def generate(self): :returns: in-memory tar.gz archive, instance of ``BytesIO`` """ - uci = self.render(files=False) tar_bytes = BytesIO() tar = tarfile.open(fileobj=tar_bytes, mode='w') - # create a list with all the packages (and remove empty entries) - packages = re.split('package ', uci) - if '' in packages: - packages.remove('') - # for each package create a file with its contents in /etc/config - for package in packages: - lines = package.split('\n') - package_name = lines[0] - text_contents = '\n'.join(lines[2:]) - self._add_file(tar=tar, - name='etc/config/{0}'.format(package_name), - contents=text_contents) - self._add_files(tar) + self._generate_contents(tar) + self._process_files(tar) tar.close() tar_bytes.seek(0) # set pointer to beginning of stream # `mtime` parameter of gzip file must be 0, otherwise any checksum operation @@ -183,6 +171,27 @@ def generate(self): gzip_bytes.seek(0) # set pointer to beginning of stream return gzip_bytes + def _generate_contents(self, tar): + """ + Adds configuration files to tarfile instance. + + :param tar: tarfile instance + :returns: None + """ + uci = self.render(files=False) + # create a list with all the packages (and remove empty entries) + packages = re.split('package ', uci) + if '' in packages: + packages.remove('') + # for each package create a file with its contents in /etc/config + for package in packages: + lines = package.split('\n') + package_name = lines[0] + text_contents = '\n'.join(lines[2:]) + self._add_file(tar=tar, + name='etc/config/{0}'.format(package_name), + contents=text_contents) + def write(self, name, path='./'): """ Like ``generate`` but writes to disk. @@ -199,10 +208,12 @@ def write(self, name, path='./'): f.write(byte_object.getvalue()) f.close() - def _add_files(self, tar): + def _process_files(self, tar): """ - adds files specified in self.config['files'] - in specified tar object + Adds files specified in self.config['files'] to tarfile instance. + + :param tar: tarfile instance + :returns: None """ # insert additional files for file_item in self.config.get('files', []): @@ -221,7 +232,13 @@ def _add_files(self, tar): def _add_file(self, tar, name, contents, mode=DEFAULT_FILE_MODE): """ - adds a single file in tar object + Adds a single file in tarfile instance. + + :param tar: tarfile instance + :param name: string representing filename or path + :param contents: string representing file contents + :param mode: string representing file mode, defaults to 644 + :returns: None """ byte_contents = BytesIO(contents.encode('utf8')) info = tarfile.TarInfo(name=name)