def test_simple_webkit(): cmd = "python {webkit} '{url}'".format(webkit=cfg.SIMPLE_WEBKIT, url=URL) text = process.get_simple_cmd_output(cmd) assert '<h1 id="message">Hi Crowbar!</h1>' in text # call it again: text = process.get_simple_cmd_output(cmd) assert '<h1 id="message">Hi Crowbar!</h1>' in text
def get_wmctrl_output(): """ Parses the output of wmctrl and returns a list of ordered dicts. """ assert fs.which("wmctrl"), "the program wmctrl was not found." # cmd = "wmctrl -lGpx" lines = [ line for line in process.get_simple_cmd_output(cmd).split("\n") if line ] res = [] for line in lines: # 0x05e000c7 0 4402 2562 298 638 540 truecrypt.Truecrypt jabba-uplink TrueCrypt pieces = line.split() d = OrderedDict() #d['wid'] = int(pieces[0], 16) # converted to decimal d['wid'] = pieces[0] d['desktop'] = int(pieces[1]) d['pid'] = int(pieces[2]) d['geometry'] = [int(x) for x in pieces[3:7]] d['window_class'] = pieces[7] d['client_machine_name'] = pieces[8] d['window_title'] = ' '.join(pieces[9:]) res.append(d) # return res
def get_screen_resolution(): """ Screen resolution (as a tuple). """ result = [x for x in process.get_simple_cmd_output(cfg.XRANDR).split('\n') if '*' in x][0] result = tuple([int(x) for x in result.split()[0].split('x')]) return result
def get_wmctrl_output(): """ Parses the output of wmctrl and returns a list of ordered dicts. """ assert fs.which("wmctrl"), "the program wmctrl was not found." # cmd = "wmctrl -lGpx" lines = [line for line in process.get_simple_cmd_output(cmd).split("\n") if line] res = [] for line in lines: # 0x05e000c7 0 4402 2562 298 638 540 truecrypt.Truecrypt jabba-uplink TrueCrypt pieces = line.split() d = OrderedDict() #d['wid'] = int(pieces[0], 16) # converted to decimal d['wid'] = pieces[0] d['desktop'] = int(pieces[1]) d['pid'] = int(pieces[2]) d['geometry'] = [int(x) for x in pieces[3:7]] d['window_class'] = pieces[7] d['client_machine_name'] = pieces[8] d['window_title'] = ' '.join(pieces[9:]) res.append(d) # return res
def get_video_summary(video_file): """Get a one-line summary of the video file. Example: 'VIDEO: [WMV3] 320x240 24bpp 1000.000 fps 386.0 kbps (47.1 kbyte/s)' """ cmd = video_info.format(video_file) output = process.get_simple_cmd_output(cmd) return re.findall("VIDEO\:.*", output)[0]
def ping(host, cnt=1): """Ping a URL and return the average ping time.""" cmd = 'ping -c {cnt} {url}'.format(url=host, cnt=cnt) output = [x for x in process.get_simple_cmd_output(cmd).split('\n') if x] result = re.search('min/avg/max/mdev = (.*)/(.*)/(.*)/(.*) ms', output[-1]) if result: return float('{0:.2f}'.format(float(result.group(1)))) else: return None
def get_video_info(video_file): """Get info about a video. The info is returned by mplayer. The result is a dictionary whose keys start with 'ID_'. """ cmd = video_info.format(video_file) output = process.get_simple_cmd_output(cmd) return dict(re.findall('(ID_.*)=(.*)', output))
def get_screen_resolution(): """ Screen resolution (as a tuple). """ result = [ x for x in process.get_simple_cmd_output(cfg.XRANDR).split('\n') if '*' in x ][0] result = tuple([int(x) for x in result.split()[0].split('x')]) return result
def get_window_title_by_id(wid): assert fs.which("xwininfo"), "the program xwininfo was not found." # result = get_simple_cmd_output('xwininfo -id {id}'.format(id=wid)) for line in StringIO(result): line = line.rstrip("\n") match = re.search(r'^xwininfo: Window id:.*"(.*)"$', line) if match: return match.group(1) # return None
def get_page_with_cookies_using_wget(url): """Get the content of a cookies-protected page. The page is downloaded with wget. Cookies are passed to wget.""" cookies = get_cookies_in_text(get_host(url)) fs.store_content_in_file(cookies, cfg.COOKIES_TXT, overwrite=True) OPTIONS = "--cookies=on --load-cookies={0} --keep-session-cookies".format(cfg.COOKIES_TXT) cmd = "{wget} {options} '{url}' -qO-".format(wget=cfg.WGET, options=OPTIONS, url=url) page = process.get_simple_cmd_output(cmd) os.unlink(cfg.COOKIES_TXT) return page
def fping(host, cnt=1): """ Get the avg ping time of a host (in msec). Instead of ping we use the command fping. """ host = host.split(':')[0] cmd = "fping {host} -C {cnt} -q".format(host=host, cnt=cnt) res = [float(x) for x in process.get_simple_cmd_output(cmd).strip().split(':')[-1].split() if x != '-'] if len(res) > 0: return sum(res) / len(res) else: return None
def get_active_window_id(hexa=False): """ Window ID of the active window. The return value is a string. By default, the ID is in decimal format. If hexa is True, the return value is hexadecimal. In both cases, the return value is a string. """ assert fs.which("xdotool"), "the program xdotool was not found." # wid = get_simple_cmd_output('xdotool getactivewindow').strip() if not hexa: return wid else: return hex(int(wid))
def get_page_with_cookies_using_wget(url): """Get the content of a cookies-protected page. The page is downloaded with wget. Cookies are passed to wget.""" cookies = get_cookies_in_text(get_host(url)) fs.store_content_in_file(cookies, cfg.COOKIES_TXT, overwrite=True) OPTIONS = "--cookies=on --load-cookies={0} --keep-session-cookies".format( cfg.COOKIES_TXT) cmd = "{wget} {options} '{url}' -qO-".format(wget=cfg.WGET, options=OPTIONS, url=url) page = process.get_simple_cmd_output(cmd) os.unlink(cfg.COOKIES_TXT) return page
def fping(host, cnt=1): """ Get the avg ping time of a host (in msec). Instead of ping we use the command fping. """ host = host.split(':')[0] cmd = "fping {host} -C {cnt} -q".format(host=host, cnt=cnt) res = [ float(x) for x in process.get_simple_cmd_output(cmd).strip().split(':') [-1].split() if x != '-' ] if len(res) > 0: return sum(res) / len(res) else: return None
def html_to_text(html, method=cfg.LYNX): """Convert an HTML source to text format. Two methods are available: (1) with lynx, (2) with html2text.py. The return value is a string.""" temp = tempfile.NamedTemporaryFile(prefix='tmp', suffix='.html', dir='/tmp', delete=False) fs.store_content_in_file(html, temp.name, overwrite=True) if method == cfg.LYNX: cmd = "{lynx} {html} -dump".format(lynx=cfg.LYNX, html=temp.name) elif method == cfg.HTML2TEXT: cmd = "python {html2text} {html}".format(html2text=cfg.HTML2TEXT, html=temp.name) else: print >>sys.stderr, "Warning! Unknown method is used in web.html_to_text." os.unlink(temp.name) return None text = process.get_simple_cmd_output(cmd) os.unlink(temp.name) return text
def html_to_text(html, method=cfg.LYNX): """Convert an HTML source to text format. Two methods are available: (1) with lynx, (2) with html2text.py. The return value is a string.""" temp = tempfile.NamedTemporaryFile(prefix='tmp', suffix='.html', dir='/tmp', delete=False) fs.store_content_in_file(html, temp.name, overwrite=True) if method == cfg.LYNX: cmd = "{lynx} {html} -dump".format(lynx=cfg.LYNX, html=temp.name) elif method == cfg.HTML2TEXT: cmd = "python {html2text} {html}".format(html2text=cfg.HTML2TEXT, html=temp.name) else: print >> sys.stderr, "Warning! Unknown method is used in web.html_to_text." os.unlink(temp.name) return None text = process.get_simple_cmd_output(cmd) os.unlink(temp.name) return text
def get_wallpaper(): """Get the path of the file that is set as wallpaper.""" cmd = 'gsettings get org.gnome.desktop.background picture-uri' uri = process.get_simple_cmd_output(cmd) return uri.replace("'", "")
def read_clipboard(): """Read content of 'clipboard'.""" cmd = 'xsel -bo' return process.get_simple_cmd_output(cmd)
def read_primary(): """Read content of 'primary'.""" cmd = 'xsel -po' return process.get_simple_cmd_output(cmd)
def get_gnome_session_version(): version = process.get_simple_cmd_output('gnome-session --version') return int(version.split()[1][0]) # main version number
def get_js_page(url): """Get a page with Webkit, i.e. evaluate embedded Javascripts.""" cmd = "python {webkit} '{url}'".format(webkit=cfg.SIMPLE_WEBKIT, url=url) text = process.get_simple_cmd_output(cmd) return text
def test_get_unix_date(self): # Unix command 'date' date = process.get_simple_cmd_output('date').replace('\n', '') # pure Python 'date' python = dat.get_unix_date() assert date == python
def test_get_simple_cmd_output(): res = process.get_simple_cmd_output("echo -n Ubuntu") assert res == 'Ubuntu'
def get_wallpaper(): """Get the path of the file that is set as wallpaper.""" cmd = "gconftool-2 --get /desktop/gnome/background/picture_filename" return process.get_simple_cmd_output(cmd)