Add an app builder

This commit is contained in:
Valentin Niess
2020-04-02 00:10:20 +02:00
parent 83ee9e5f0c
commit f23a2dc25f
10 changed files with 320 additions and 42 deletions

View File

@@ -0,0 +1,10 @@
__all__ = ['decode']
def decode(s):
'''Decode Python 3 bytes as str
'''
try:
return s.decode()
except AttributeError:
return str(s)

View File

@@ -2,20 +2,13 @@ import os
import re
import subprocess
from .compat import decode
from .log import debug
__all__ = ['ldd', 'system']
def _decode(s):
'''Decode Python 3 bytes as str
'''
try:
return s.decode()
except AttributeError:
return s
def system(*args):
'''System call with capturing output
@@ -27,13 +20,13 @@ def system(*args):
stderr=subprocess.PIPE)
out, err = p.communicate()
if err:
err = _decode(err)
err = decode(err)
stripped = [line for line in err.split(os.linesep)
if line and not line.startswith('fuse: warning:')]
if stripped:
raise RuntimeError(err)
return str(_decode(out).strip())
return str(decode(out).strip())
_ldd_pattern = re.compile('=> (.+) [(]0x')

View File

@@ -0,0 +1,41 @@
import os
import re
import shutil
from .fs import make_tree
from .log import debug
__all__ = ['copy_template', 'load_template']
_template_pattern = re.compile('[{][{][ ]*([^{} ]+)[ ]*[}][}]')
def load_template(path, **kwargs):
'''Load a template file and substitue keywords
'''
with open(path) as f:
template = f.read()
def matcher(m):
tag = m.group(1)
try:
return kwargs[tag]
except KeyError:
return tag
return _template_pattern.sub(matcher, template)
def copy_template(path, destination, **kwargs):
'''Copy a template file and substitue keywords
'''
txt = load_template(path, **kwargs)
debug('COPY', '%s as %s', os.path.basename(path), destination)
make_tree(os.path.dirname(destination))
with open(destination, 'w') as f:
f.write(txt)
shutil.copymode(path, destination)

View File

@@ -1,4 +1,8 @@
import os
try:
from urllib.request import urlopen as _urlopen
except ImportError:
from urllib2 import urlopen as _urlopen
try:
from urllib.request import urlretrieve as _urlretrieve
except ImportError:
@@ -8,7 +12,15 @@ except ImportError:
from .log import debug
__all__ = ['urlretrieve']
__all__ = ['urlopen', 'urlretrieve']
def urlopen(url, *args, **kwargs):
'''Open a remote file
'''
baseurl, urlname = os.path.split(url)
debug('DOWNLOAD', '%s from %s', baseurl, urlname)
return _urlopen(url, *args, **kwargs)
def urlretrieve(url, filename=None):