source: ogLive-Builder-Git/boottools/utils.py @ f002c56

browserbuild-browserdbusdeps-vadimfilebeat-installerimprove-versionlgromero-testsmainno-apt-moduleoggitoglive-ipv6pull-from-cloning-enginepy-seturlpybuilderresolvconfstunnel
Last change on this file since f002c56 was f002c56, checked in by Natalia Serrano <natalia.serrano@…>, 10 months ago

refs #597 use docker to create the oglive images

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import logging
2import subprocess
3import re
4import os
5import configparser
6import selectors
7
8logger = logging.getLogger ('boottools')
9
10def run (args):
11    stdout = stderr = ''
12    logger.debug ('run 10 args "{}"'.format (' '.join(args)))
13    sel = selectors.DefaultSelector()
14    p = subprocess.Popen (args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
15    sel.register (p.stdout.fileno(), selectors.EVENT_READ)
16    sel.register (p.stderr.fileno(), selectors.EVENT_READ)
17    while True:
18        events = sel.select()
19        #logger.debug (f'got {len(events)} events')
20        for key, _ in events:
21            if key.fileobj == p.stdout.fileno():
22                #logger.debug (f'reading from stdout')
23                line = p.stdout.readline()
24                if not line: break
25                stdout += line
26                logger.debug (line.rstrip())
27            elif key.fileobj == p.stderr.fileno():
28                #logger.debug (f'reading from stderr')
29                line = p.stderr.readline()
30                if not line: break
31                stderr += line
32                logger.warn (line.rstrip())
33        if p.poll() != None:
34            #logger.debug ('process exited, breaking loop')
35            break
36    sel.close()
37    #p.stdout.close()
38    #p.stderr.close()
39    #p.wait()
40    stdout = stdout.strip()
41    stderr = stderr.strip()
42
43    logger.debug (f'p.returncode {p.returncode}')
44    if p.returncode:
45        logger.error ('command "{}" failed with rc "{}"'.format (' '.join(args), p.returncode))
46
47        #logger.error ('stdout follows:')
48        #if stdout:
49        #    for i in stdout.split('\n'): logger.error ('  ' + i)
50
51        logger.error ('stderr follows:')
52        if stderr:
53            for i in stderr.split('\n'): logger.error ('  ' + i)
54
55        raise Exception ('command "{}" failed with rc "{}"'.format (' '.join(args), p.returncode))
56    return stdout, stderr
57
58def grep (regex, file):
59    with open (file, 'r') as f:
60        for l in f:
61            if (re.findall (regex, l)): return 1
62        return 0
63
64def is_mounted (mntpt):
65    return grep (mntpt, '/proc/mounts')
66
67def mount (dev, mntpt, opts=[]):
68    if not is_mounted (mntpt):
69        run (['mount', dev, mntpt] + opts)
70
71def umount (mntpt):
72    if (is_mounted (mntpt)):
73        run (['umount', mntpt])
74
75def read_config (fn):
76    if not os.path.exists (fn):
77        print (f'configuration file "{fn}" not found')
78        return
79    config = configparser.ConfigParser (comment_prefixes='#', inline_comment_prefixes='#')
80    config.read (fn)
81    return config
82
83def write_md5 (fn):
84    md5, _ = run (['md5sum', fn])
85    md5, rest = md5.split (' ', 1)
86    with open (f'{fn}.sum', 'w') as fd:
87        fd.write (md5 + '\n')
Note: See TracBrowser for help on using the repository browser.