Skip to content

Commit

Permalink
Moved generate method details to _generate_contents
Browse files Browse the repository at this point in the history
Different backends can customize the generated content with this method
to avoid duplication of code.
  • Loading branch information
nemesifier committed Dec 17, 2015
1 parent 3624dcd commit a0b1373
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
25 changes: 9 additions & 16 deletions netjsonconfig/backends/openwisp/openwisp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import re
import gzip
import tarfile
from io import BytesIO

from jinja2 import Environment, PackageLoader

Expand Down Expand Up @@ -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:
Expand All @@ -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
53 changes: 35 additions & 18 deletions netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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', []):
Expand All @@ -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)
Expand Down

0 comments on commit a0b1373

Please sign in to comment.