fandango.linos module

Description

#some Linux utilities

<pre> Executing shell commands and getting the stdout

The module subprocess must be used for that, instead of os.exec* or os.system.

import subprocess ps = subprocess.Popen(‘ps uax’,shell=True,stdout=subprocess.PIPE) grep1 = subprocess.Popen(‘grep -i hdbarchiver’,shell=True,stdin=ps.stdout,stdout=subprocess.PIPE) grep1.communicate() Out[77]: (‘sicilia 13698 0.0 0.0 51000 1552 ? Ss Feb23 0:00 SCREEN -dm -S HdbArchiver-palantir01_BO01_VC /homelocal/sicilia/ap sicilia 6343 0.0 0.0 50872 2748 pts/13 S+ 10:17 0:00 screen -r HdbArchiver-palantir01_BO01_VC ‘,

None)

</pre>

Classes

MyMachine

class fandango.linos.MyMachine[source]

This method identifies the current Machine (OS/Arch/Hostname/Kernel) using the platform module

Functions

get_memstats

fandango.linos.get_memstats(units='m')[source]

This method returns mem stats in Megabytes Remember that in linux buffers and cached memory should be considered FREE Dictionary returned is like: { ‘buffers’: ‘6’,

‘cached’: ‘557’, ‘free’: ‘16’, ‘shared’: ‘0’, ‘total’: ‘1002’, ‘used’: ‘986’ }

kill_process

fandango.linos.kill_process(process=None, signal=15)[source]

get_memory

fandango.linos.get_memory(pid=None, virtual=False)[source]

This function uses ‘/proc/pid/status’ to get the memory consumption of a process

is_dir

fandango.linos.is_dir(path)[source]

sysargs_to_dict

fandango.linos.sysargs_to_dict(args=None, defaults=[], trace=False, split=False, cast=True, lazy=True)[source]

It parses the command line arguments into an understandable dict defaults is the list of anonymous arguments to accept (would be False if not specified)

@param split: if True: args,kwargs are returned; if False then {None:[defaults],’option’:value} is returned instead

@param cast: will try to cast all strings to python types

@param lazy: will accept any A=B as an alternative to -A B

> command H=1 –option=value –parameter VALUE -test default_arg1 default_arg2

will returns

{H:1,option:value,parameter:VALUE,test:True,params:[default_arg1,default_arg2]}

getopt and argparse modules in python provide similar functionality, but are not available in all of our distributions

diffdir

fandango.linos.diffdir(origin, destination, caseless=False, checksize=True)[source]

expand_args_to_list

fandango.linos.expand_args_to_list(args, type_=<type 'int'>)[source]

it generates a list of args from a sequence like 1,3,5 or 3-7

findfolders

fandango.linos.findfolders(target='', parent='', filter_=True, printout=False)[source]

ping

fandango.linos.ping(ips, threaded=False, timeout=1)[source]

By Noah Gift’s, PyCon 2008 ips = [‘ivc%02d01’%(i+1) for i in range(16)] #ips = [“10.0.1.1”, “10.0.1.3”, “10.0.1.11”, “10.0.1.51”]

listdir

fandango.linos.listdir(folder, mask='.*', files=False, folders=False, links=False, caseless=True)[source]

get_cpu

fandango.linos.get_cpu(pid)[source]

Uses ps to get the CPU usage of a process by PID ; it will trigger exception of PID doesn’t exist

copydir

fandango.linos.copydir(origin, destination, timewait=0.1, overwrite=False)[source]

This method copies recursively a folder, creating subdirectories if needed and exiting at first error. Origin and destination must be existing folders. It includes a timewait between file copying

timefun

fandango.linos.timefun(f)[source]

This function allow to get time spent by some method calls, use timefun(lambda:f(args)) if needed

desktop_switcher

fandango.linos.desktop_switcher(period, event=None, iterations=2)[source]

It uses wmctrl to switch between all desktops with a period as specified. @param period Time between desktop changes @param event Event to stop the application @param iterations Number of cycles to execute, -1 for infinite

get_memory_usage

fandango.linos.get_memory_usage()[source]

This method returns the percentage of total memory used in this machine

get_process_pid

fandango.linos.get_process_pid(include='', exclude='grep|screen|kwrite')[source]

sendmail

fandango.linos.sendmail(subject, text, receivers, sender='', attachments=None, trace=False)[source]

expand_args_to_int_list

fandango.linos.expand_args_to_int_list(args)

shell_command

fandango.linos.shell_command(*commands, **keywords)[source]

Executes a list of commands linking their stdin and stdouts @param commands each argument is interpreted as a command @param split returns stdout as a file.readlines() result @return the last stdout result

expand_args_to_str_list

fandango.linos.expand_args_to_str_list(args)

get_file_size

fandango.linos.get_file_size(path)[source]

arg_to_bool

fandango.linos.arg_to_bool(arg)[source]

file_exists

fandango.linos.file_exists(path)[source]

check_process

fandango.linos.check_process(pid)[source]

get_free_memory

fandango.linos.get_free_memory(units='m')[source]

KillEmAll

fandango.linos.KillEmAll(klass)[source]

raw autodoc

fandango.linos.KillEmAll(klass)[source]
class fandango.linos.MyMachine[source]

Bases: fandango.objects.Struct

This method identifies the current Machine (OS/Arch/Hostname/Kernel) using the platform module

fandango.linos.arg_to_bool(arg)[source]
fandango.linos.check_process(pid)[source]
fandango.linos.copydir(origin, destination, timewait=0.1, overwrite=False)[source]

This method copies recursively a folder, creating subdirectories if needed and exiting at first error. Origin and destination must be existing folders. It includes a timewait between file copying

fandango.linos.desktop_switcher(period, event=None, iterations=2)[source]

It uses wmctrl to switch between all desktops with a period as specified. @param period Time between desktop changes @param event Event to stop the application @param iterations Number of cycles to execute, -1 for infinite

fandango.linos.diffdir(origin, destination, caseless=False, checksize=True)[source]
fandango.linos.expand_args_to_int_list(args)
fandango.linos.expand_args_to_list(args, type_=<type 'int'>)[source]

it generates a list of args from a sequence like 1,3,5 or 3-7

fandango.linos.expand_args_to_str_list(args)
fandango.linos.file_exists(path)[source]
fandango.linos.findfolders(target='', parent='', filter_=True, printout=False)[source]
fandango.linos.get_cpu(pid)[source]

Uses ps to get the CPU usage of a process by PID ; it will trigger exception of PID doesn’t exist

fandango.linos.get_file_size(path)[source]
fandango.linos.get_free_memory(units='m')[source]
fandango.linos.get_memory(pid=None, virtual=False)[source]

This function uses ‘/proc/pid/status’ to get the memory consumption of a process

fandango.linos.get_memory_usage()[source]

This method returns the percentage of total memory used in this machine

fandango.linos.get_memstats(units='m')[source]

This method returns mem stats in Megabytes Remember that in linux buffers and cached memory should be considered FREE Dictionary returned is like: { ‘buffers’: ‘6’,

‘cached’: ‘557’, ‘free’: ‘16’, ‘shared’: ‘0’, ‘total’: ‘1002’, ‘used’: ‘986’ }
fandango.linos.get_process_pid(include='', exclude='grep|screen|kwrite')[source]
fandango.linos.is_dir(path)[source]
fandango.linos.is_link(path)[source]
fandango.linos.kill_process(process=None, signal=15)[source]
fandango.linos.listdir(folder, mask='.*', files=False, folders=False, links=False, caseless=True)[source]
fandango.linos.ping(ips, threaded=False, timeout=1)[source]

By Noah Gift’s, PyCon 2008 ips = [‘ivc%02d01’%(i+1) for i in range(16)] #ips = [“10.0.1.1”, “10.0.1.3”, “10.0.1.11”, “10.0.1.51”]

fandango.linos.sendmail(subject, text, receivers, sender='', attachments=None, trace=False)[source]
fandango.linos.shell_command(*commands, **keywords)[source]

Executes a list of commands linking their stdin and stdouts @param commands each argument is interpreted as a command @param split returns stdout as a file.readlines() result @return the last stdout result

fandango.linos.sysargs_to_dict(args=None, defaults=[], trace=False, split=False, cast=True, lazy=True)[source]

It parses the command line arguments into an understandable dict defaults is the list of anonymous arguments to accept (would be False if not specified)

@param split: if True: args,kwargs are returned; if False then {None:[defaults],’option’:value} is returned instead

@param cast: will try to cast all strings to python types

@param lazy: will accept any A=B as an alternative to -A B

> command H=1 –option=value –parameter VALUE -test default_arg1 default_arg2

will returns

{H:1,option:value,parameter:VALUE,test:True,params:[default_arg1,default_arg2]}

getopt and argparse modules in python provide similar functionality, but are not available in all of our distributions

fandango.linos.timefun(f)[source]

This function allow to get time spent by some method calls, use timefun(lambda:f(args)) if needed