Changeset 2765

Show
Ignore:
Timestamp:
02/15/18 03:13:46 (10 months ago)
Author:
jwoithe
Message:

Minor python improvements to Scons scripts.

Replace older alternatives with subprocess (CheckJackdVer? should be more
efficient).

Directly use python ordering for tuples.

Stop importing unused modules.

Replace hand-written comparisons with startswith() (one was buggy).

Open and close files with context managers.

Patch from Nicolas Boulenguez with testing and minor fixes by Jonathan
Woithe.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/libffado/deb/SConscript

    r2764 r2765  
    2323 
    2424# from: http://www.qandr.org/quentin/writings/debscons.html 
    25 import os, shutil, sys 
     25import os 
     26 
    2627Import('env') # exported by parent SConstruct 
    2728 
     
    124125        DEBNAME, installed_size, DEBMAINT, DEBARCH, DEBVERSION, 
    125126        DEBDEPENDS, DEBDESC) 
    126     f = open(str(target[0]), 'w') 
    127     f.write(control_info) 
    128     f.close() 
     127    with open(str(target[0]), 'w') as f: 
     128        f.write(control_info) 
    129129 
    130130# We can generate the control file by calling make_control 
  • trunk/libffado/SConstruct

    r2764 r2765  
    2323# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
    2424# 
     25from __future__ import print_function 
    2526 
    2627FFADO_API_VERSION = "9" 
    2728FFADO_VERSION="2.4.9999" 
    2829 
    29 from subprocess import Popen, PIPE 
     30from subprocess import Popen, PIPE, check_output 
    3031import os 
    3132import re 
    3233import sys 
    3334from string import Template 
    34 import imp 
    3535import distutils.sysconfig 
    3636 
     
    157157def ConfigGuess( context ): 
    158158    context.Message( "Trying to find the system triple: " ) 
    159     ret = os.popen( "/bin/sh admin/config.guess" ).read()[:-1] 
     159    ret = check_output(("/bin/sh", "admin/config.guess")).rstrip() 
    160160    context.Result( ret ) 
    161161    return ret 
     
    220220version_re = re.compile(r'^(\d+)\.(\d+)\.(\d+)') 
    221221 
    222 def VersionInt(vers): 
    223     match = version_re.match(vers) 
    224     if not match: 
    225         return -1 
    226     (maj, min, patch) = match.group(1, 2, 3) 
    227     # For now allow "min" to run up to 65535.  "maj" and "patch" are 
    228     # restricted to 0-255. 
    229     return (int(maj) << 24) | (int(min) << 8) | int(patch) 
    230  
    231222def CheckJackdVer(): 
    232     # Suppress newline in python 2 and 3 
    233     sys.stdout.write('Checking jackd version...'
    234     sys.stdout.flush() 
    235     ret = Popen("which jackd >/dev/null 2>&1 && jackd --version | tail -n 1 | cut -d ' ' -f 3", shell=True, stdout=PIPE).stdout.read()[:-1].decode() 
    236     if (ret == "")
     223    print('Checking jackd version...', end='') 
     224    popen = Popen(("which", 'jackd'), stdout=PIPE, stderr=PIPE
     225    stdout, stderr = popen.communicate() 
     226    assert popen.returncode in (0, 1), "which returned a unexpected status" 
     227    if popen.returncode == 1
    237228        print("not installed") 
    238         return -1 
    239     else: 
    240         print(ret) 
    241     return VersionInt(ret) 
     229        return None 
     230    jackd = stdout.decode ().rstrip () 
     231    ret = check_output ((jackd, '--version')).decode() .rstrip () 
     232    ret = ret.split ('\n') [-1]; # Last line. 
     233    ret = ret.split () [2];      # Third field. 
     234    if not version_re.match (ret): 
     235        print("failed to parse version") 
     236        return None 
     237    print (ret) 
     238 
     239    # Trim off any "rc" (release candidate) components from the end of the 
     240    # version string 
     241    ret = ret.split ('rc')[0] 
     242    ret = ret.split ('.') 
     243    ret = map (int, ret) 
     244    return tuple (ret) 
    242245 
    243246if env['SERIALIZE_USE_EXPAT']: 
     
    297300    else: 
    298301        jackd_ver = CheckJackdVer() 
    299         if (jackd_ver != -1)
     302        if jackd_ver
    300303            # If jackd is unknown to pkg-config but is never-the-less 
    301304            # runnable, use the version number reported by it.  This means 
    302305            # users don't have to have jack development files present on 
    303306            # their system for this to work. 
    304             have_jack = (jackd_ver >= VersionInt('0.0.0')
    305             good_jack1 = (jackd_ver < VersionInt('1.9.0')) and (jackd_ver >= VersionInt('0.121.4')
    306             good_jack2 = (jackd_ver >= VersionInt('1.9.9')
     307            have_jack = jackd_ver >= (0, 0, 0
     308            good_jack1 = jackd_ver < (1, 9, 0) and jackd_ver >= (0, 121, 4
     309            good_jack2 = jackd_ver >= (1, 9, 9
    307310 
    308311    if env['ENABLE_SETBUFFERSIZE_API_VER'] == 'auto': 
     
    567570 
    568571#=== Begin Revised CXXFLAGS ========================================= 
    569 def outputof(*cmd): 
    570     """Run a command without running a shell, return cmd's stdout 
    571     """ 
    572     p = Popen(cmd, stdout=PIPE) 
    573     return p.communicate()[0] 
    574  
    575572def cpuinfo_kv(): 
    576573    """generator which reads lines from Linux /proc/cpuinfo and splits them 
    577574    into key:value tokens and yields (key, value) tuple. 
    578575    """ 
    579     f = open('/proc/cpuinfo', 'r') 
    580     for line in f: 
     576    with open('/proc/cpuinfo', 'r') as f: 
     577      for line in f: 
    581578        line = line.strip() 
    582579        if line: 
    583580            k,v = line.split(':', 1) 
    584581            yield (k.strip(), v.strip()) 
    585     f.close() 
    586  
    587582 
    588583class CpuInfo (object): 
     
    718713        # presumably if a person is running this script, they should have 
    719714        # a gcc toolchain installed... 
    720         x = outputof('objdump', '-Wi', real_exe
     715        x = check_output(('objdump', '-Wi', real_exe)
    721716        # should emit a line that looks like this: 
    722717        # /bin/mount:     file format elf32-i386 
     
    859854    print("Doing an optimized build...") 
    860855 
    861 env['REVISION'] = os.popen('svnversion .').read()[:-1] 
     856env['REVISION'] = check_output(('svnversion', '.',)).rstrip() 
    862857# This may be as simple as '89' or as complex as '4123:4184M'. 
    863858# We'll just use the last bit. 
     
    865860 
    866861# Assume an unversioned directory indicates a release. 
    867 if env['REVISION'][0:11] == 'Unversioned'
     862if env['REVISION'].startswith ('Unversioned')
    868863    env['REVISION'] = '' 
    869864 
    870865# try to circumvent localized versions 
    871 if len(env['REVISION']) >= 5 and env['REVISION'][0:6] == 'export'
     866if env['REVISION'].startswith ('export')
    872867    env['REVISION'] = '' 
    873868