github.com/hwaf/hwaf@v0.0.0-20140814122253-5465f73b20f1/py-hwaftools/orch/util.py (about) 1 #!/usr/bin/env python 2 ''' 3 Utility functions 4 ''' 5 import re 6 7 try: from urllib import request 8 except: from urllib import urlopen 9 else: urlopen = request.urlopen 10 11 12 def string2list(string, delims=', '): 13 if not isinstance(string, type('')): 14 return string 15 return re.split('[%s]+'%delims, string) 16 17 def update_if(d, p, **kwds): 18 '''Return a copy of d updated with kwds. 19 20 If no such item is in d, set item from kwds, else call p(k,v) with 21 kwds item and only set if returns True. 22 23 ''' 24 if p is None: 25 p = lambda k,v: v is not None 26 d = dict(d) # copy 27 for k,v in kwds.items(): 28 if not k in d: 29 d[k] = v 30 continue 31 if p(k, v): 32 d[k] = v 33 34 return d 35 36 37 import subprocess 38 from subprocess import CalledProcessError 39 40 try: 41 from subprocess import check_output 42 except ImportError: 43 class CalledProcessError(Exception): 44 """This exception is raised when a process run by check_call() or 45 check_output() returns a non-zero exit status. 46 The exit status will be stored in the returncode attribute; 47 check_output() will also store the output in the output attribute. 48 """ 49 def __init__(self, returncode, cmd, output=None): 50 self.returncode = returncode 51 self.cmd = cmd 52 self.output = output 53 def __str__(self): 54 return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode) 55 56 def check_output(*popenargs, **kwargs): 57 r"""Run command with arguments and return its output as a byte string. 58 59 If the exit code was non-zero it raises a CalledProcessError. The 60 CalledProcessError object will have the return code in the returncode 61 attribute and output in the output attribute. 62 63 The arguments are the same as for the Popen constructor. Example: 64 65 >>> check_output(["ls", "-l", "/dev/null"]) 66 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' 67 68 The stdout argument is not allowed as it is used internally. 69 To capture standard error in the result, use stderr=STDOUT. 70 71 >>> check_output(["/bin/sh", "-c", 72 ... "ls -l non_existent_file ; exit 0"], 73 ... stderr=STDOUT) 74 'ls: non_existent_file: No such file or directory\n' 75 """ 76 if 'stdout' in kwargs: 77 raise ValueError('stdout argument not allowed, it will be overridden.') 78 process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) 79 output, unused_err = process.communicate() 80 retcode = process.poll() 81 if retcode: 82 cmd = kwargs.get("args") 83 if cmd is None: 84 cmd = popenargs[0] 85 raise CalledProcessError(retcode, cmd, output=output) 86 return output 87 88 89 def envvars(envtext): 90 ''' 91 Parse envtext as lines of 'name=value' pairs, return result as a 92 dictionary. Values beginning with '()' are allowed to span 93 multiple lines and expected to end with a closing brace. 94 ''' 95 def lines(text): 96 for line in text.split('\n'): 97 line = line.strip() 98 if not line: 99 continue 100 yield line 101 lines = lines(envtext) # this looses the first? 102 103 ret = dict() 104 for line in lines: 105 name, val = line.split('=',1) 106 if not val.startswith('()') or val.endswith('}'): 107 ret[name] = val 108 continue 109 110 # we have a multi-lined shell function, slurp until we get an ending '}' 111 val = [val] 112 for line in next(lines): 113 val.append(line) 114 if line.endswith('}'): 115 break 116 continue 117 ret[name] = '\n'.join(val) 118 return ret 119 120 def get_unpacker(filename, dirname = '.'): 121 if filename.endswith('.zip'): 122 return 'unzip -d %s %s' % (dirname, filename) 123 124 text2flags = {'.tar.gz':'xzf', '.tgz':'xzf', '.tar.bz2':'xjf', '.tar':'xf' } 125 for ext, flags in text2flags.items(): 126 if filename.endswith(ext): 127 return 'tar -C %s -%s %s' % (dirname, flags, filename) 128 return 'tar -C %s -xf %s' % (dirname, filename) 129