Exemplo n.º 1
0
def get_hosts(ip_range=None):
    '''Get LAN alive hosts.
    '''
    if not ip_range:
        ip_range = ['%s.0/24' % ip.rsplit('.', 1)[0] for ip in get_ips()]
    elif not isinstance(ip_range, (list, tuple)):
        ip_range = [ip_range]

    res = []
    for ip_range_ in ip_range:
        stdout, stderr, return_code = popen(['fping', '-a', '-A', '-r1', '-g', ip_range_])
        if return_code is not None:
            res += stdout

    return list(set(res))
Exemplo n.º 2
0
def is_file_open(file, check_mtime=True, mtime_delta=5):
    '''Return True if the file is open.
    If file is a directory, recursively check all the files inside.

    :param file: file or directory
    :param check_mtime: check the file modified time
    '''
    if not os.path.exists(file):
        return False
    for file_ in iter_files(file):
        if popen(['lsof', file_])[-1] == 0:
            return True

    if check_mtime:
        def get_stat():
            return dict([(f, os.stat(f).st_mtime) for f in iter_files(file)])
        data0 = get_stat()
        time.sleep(mtime_delta)
        data1 = get_stat()
        return data0 != data1

    return False
Exemplo n.º 3
0
def parse(file):
    res = {}

    cmd = ['mediainfo', '-language=raw', '-f', file]
    stdout, stderr, return_code = popen(cmd)
    if return_code is None:
        raise MediainfoError('failed to run command "%s"' % ' '.join(cmd))
    elif return_code != 0:
        logger.error('failed to parse file %s: %s, %s', file, stdout, stderr)
        return res

    cat = None
    for line in stdout:
        fields = re.split(r'\s*:\s*', line.decode('utf-8').lower())

        if len(fields) == 1:
            cat = fields[0]
            if cat:
                res[cat] = {}
        elif cat and len(fields) == 2:
            if not res[cat].get(fields[0]):
                res[cat][fields[0]] = fields[1]

    return res
Exemplo n.º 4
0
    def unpack(self, remove_src=True, remove_failed=True):
        '''Unpack the archive in its directory.

        :return: processed files list (including multipart files)
        '''
        ext = self.ext.lower()
        stdout, stderr, returncode = popen(ARCHIVE_DEF[ext] + [self.file], cwd=self.path)
        if returncode != 0:
            if remove_failed and ext in RE_EXTRACT_ERRORS:
                if [l for l in stderr if RE_EXTRACT_ERRORS[ext].search(l)]:
                    remove_src = True
                    logger.info('failed to extract %s: bad archive', self.file)
                else:
                    remove_src = False
                    logger.error('failed to extract %s: %s, %s', self.file, stdout, stderr)

        processed = self.get_multipart_files()

        # Remove files
        if remove_src:
            for processed_file in processed:
                remove_file(processed_file)

        return processed
Exemplo n.º 5
0
def _service(svc, param):
    cmd = 'service %s %s' % (svc, param)
    stdout, stderr, returncode = popen(cmd)
    return ' '.join((stdout or []) + (stderr or [])), returncode
Exemplo n.º 6
0
def _service(svc, param):
    cmd = 'service %s %s' % (svc, param)
    stdout, stderr, returncode = popen(cmd)
    return ' '.join((stdout or []) + (stderr or [])), returncode