python:les_modules
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
python:les_modules [2013/05/31 23:13] – [string] root | python:les_modules [2024/12/20 12:41] (Version actuelle) – [Fabric] admin | ||
---|---|---|---|
Ligne 4: | Ligne 4: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | |||
+ | ===== Chemin des modules ===== | ||
+ | * Dans l' | ||
+ | <code bash> | ||
+ | export PYTHONPATH=/ | ||
+ | </ | ||
+ | |||
+ | * Dans le code python : | ||
+ | <code python> | ||
+ | # | ||
+ | import os, sys | ||
+ | sys.path.append(os.path.join(os.path.dirname(__file__), | ||
+ | # | ||
+ | </ | ||
===== optparse ===== | ===== optparse ===== | ||
Ligne 184: | Ligne 198: | ||
# | # | ||
- | from fabric.api import * | + | import sys, os, getpass |
- | from fabric.colors import * | + | #import sys, os, getpass, multiprocessing |
+ | |||
+ | try: | ||
+ | | ||
+ | from fabric.colors import * | ||
+ | except: | ||
+ | print(' | ||
+ | sys.exit(1) | ||
+ | |||
+ | sys.tracebacklimit = 0 | ||
+ | |||
+ | output[' | ||
+ | output[' | ||
+ | output[' | ||
+ | output[' | ||
+ | output[' | ||
+ | |||
+ | #env.user = ' | ||
+ | # | ||
- | env.hosts = [' | ||
- | env.user = ' | ||
- | env.password = ' | ||
- | # | ||
# | # | ||
- | | + | # |
- | ' | + | # ' |
- | } | + | #} |
#env.shell = "/ | #env.shell = "/ | ||
+ | # | ||
+ | env.disable_known_hosts = True | ||
+ | # | ||
+ | # | ||
# | # | ||
- | # | + | #env.timeout = 10 #Network ssh connection timeout, in seconds. |
- | 'web': ['www1', 'www2', 'www3'], | + | # |
- | 'dns': ['ns1', 'ns2'] | + | env.linewise = True |
+ | env.warn_only = True | ||
+ | |||
+ | env.role_default = ' | ||
+ | |||
+ | env.roledefs = { | ||
+ | 'eth': ['svr1', 'svr2', 'svr3', ' | ||
+ | 'adm': ['svr1-adm', 'svr2-adm', | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
} | } | ||
- | env.disable_known_hosts = True | ||
- | env.parralel = True | ||
- | env.pool_size = 0 #Sets the number of concurrent processes to use when executing tasks in parallel. | ||
- | env.timeout = 10 #Network ssh connection timeout, in seconds. | ||
- | env.command_timeout = 10 | ||
- | # | ||
- | hide(' | ||
- | def remote_info(): | + | env.roledefs['web_adm'] += env.roledefs['imm'] |
- | with hide('running', ' | + | |
- | print(yellow(" | + | |
- | a = run('uname -a') | + | |
- | fastprint(a, | + | |
- | def local_info(): | + | if env.hosts == [] and env.roles == []: |
- | local(' | + | env.hosts = env.roledefs[env.role_default] |
+ | |||
+ | #list_all = [] | ||
+ | #for server in env.roledefs.values(): | ||
+ | # | ||
+ | # | ||
+ | |||
+ | def VERIF_IPMI_PASSWORD(): | ||
+ | | ||
+ | globals()[' | ||
+ | |||
+ | if ' | ||
+ | IPMI_PASSWORD = getpass.getpass(prompt=' | ||
+ | globals()[' | ||
+ | |||
+ | @task | ||
+ | @serial | ||
+ | def lr(): | ||
+ | """ | ||
+ | for role in env.roledefs: | ||
+ | if role == env.role_default: | ||
+ | print(" | ||
+ | else: | ||
+ | print(" | ||
+ | sys.exit() | ||
+ | |||
+ | # | ||
+ | @task | ||
+ | def cmd(cmd, show = None): | ||
+ | """ | ||
+ | #with show(' | ||
+ | try: | ||
+ | out = run(cmd) | ||
+ | except Exception as err: | ||
+ | sys.stdout.write(yellow(" | ||
+ | print(red(err)) | ||
+ | return | ||
+ | |||
+ | if show == None: | ||
+ | for line in out.splitlines(): | ||
+ | sys.stdout.write(yellow(" | ||
+ | print(line) | ||
+ | |||
+ | if out.failed: | ||
+ | sys.stdout.write(yellow(" | ||
+ | print(red(' | ||
+ | elif show == ' | ||
+ | sys.stdout.write(yellow(" | ||
+ | if out.failed: | ||
+ | print(red(' | ||
+ | else: | ||
+ | print(green(' | ||
+ | |||
+ | if show == ' | ||
+ | print(yellow(' | ||
+ | print(' | ||
+ | else: | ||
+ | print(" | ||
+ | sys.exit(1) | ||
+ | |||
+ | @task | ||
+ | def send_file(src, | ||
+ | """ | ||
+ | if dst == None: | ||
+ | dst = src | ||
+ | |||
+ | |||
+ | if mode == None: | ||
+ | put(src, dst) | ||
+ | else: | ||
+ | try: | ||
+ | int(mode) | ||
+ | except: | ||
+ | print("' | ||
+ | sys.exit(1) | ||
+ | |||
+ | put(src, dst, mode=mode) | ||
+ | @task | ||
+ | def reboot(wait = 0): | ||
+ | """ | ||
+ | if os.getlogin() != ' | ||
+ | print(' | ||
+ | sys.exit(1) | ||
+ | |||
+ | try: | ||
+ | int(wait) | ||
+ | except: | ||
+ | print("' | ||
+ | sys.exit(1) | ||
+ | |||
+ | reboot(wait = wait) | ||
+ | |||
+ | @serial | ||
+ | @task | ||
+ | def power(state): | ||
+ | """ | ||
+ | if not env.host in env.roledefs[' | ||
+ | print(' | ||
+ | print(' | ||
+ | sys.exit(1) | ||
+ | |||
+ | if state not in [' | ||
+ | print(" | ||
+ | sys.exit(1) | ||
+ | |||
+ | VERIF_IPMI_PASSWORD() | ||
+ | |||
+ | out = local(" | ||
+ | |||
+ | if out.failed: | ||
+ | sys.stdout.write(yellow(" | ||
+ | print(red(' | ||
+ | print(out.stderr) | ||
+ | sys.exit(1) | ||
+ | else: | ||
+ | for line in out.splitlines(): | ||
+ | sys.stdout.write(yellow(" | ||
+ | print(line) | ||
+ | |||
+ | @serial | ||
+ | @task | ||
+ | def console(action=' | ||
+ | """ | ||
+ | if len(env.hosts) != 1: | ||
+ | print(' | ||
+ | sys.exit(1) | ||
+ | |||
+ | if not env.host in env.roledefs[' | ||
+ | print(' | ||
+ | print(' | ||
+ | sys.exit(1) | ||
+ | |||
+ | VERIF_IPMI_PASSWORD() | ||
+ | |||
+ | os.system(" | ||
+ | |||
+ | @serial | ||
+ | @task | ||
+ | def web_adm(): | ||
+ | """ | ||
+ | if len(env.hosts) != 1: | ||
+ | print(' | ||
+ | sys.exit(1) | ||
+ | |||
+ | if os.getenv(' | ||
+ | print(' | ||
+ | sys.exit(1) | ||
+ | |||
+ | if env.host not in env.roledefs[' | ||
+ | print(" | ||
+ | sys.exit(1) | ||
+ | |||
+ | os.system(' | ||
</ | </ | ||
+ | |||
+ | ===== readline ===== | ||
+ | |||
+ | <code python readline.py> | ||
+ | import readline, signal | ||
+ | prompt = 'GigiX > ' | ||
+ | |||
+ | def signal_handler(signal, | ||
+ | |||
+ | input(prompt) | ||
+ | return | ||
+ | |||
+ | signal.signal(signal.SIGINT, | ||
+ | readline.parse_and_bind(" | ||
+ | |||
+ | class completer: | ||
+ | def __init__(self, | ||
+ | self.tree = tree | ||
+ | |||
+ | def _parser(self, | ||
+ | if (search is None) or (callable(search) == True): | ||
+ | return [] | ||
+ | elif len(line) == 0: | ||
+ | return [x for x in search] | ||
+ | elif len(line) == 1 and readline.get_line_buffer()[-1] != ' ': | ||
+ | return ['%s ' % x for x in search if x.startswith(line[0])] | ||
+ | else: | ||
+ | return self._parser(line[1: | ||
+ | return [] | ||
+ | |||
+ | def complete(self, | ||
+ | line = readline.get_line_buffer().split() | ||
+ | results = self._parser(line, | ||
+ | return results[state] | ||
+ | |||
+ | def linux_sles_12(): | ||
+ | print(' | ||
+ | |||
+ | tree = { | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | }, | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | }, | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | }, | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | }, | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | readline.set_completer(completer(tree).complete) | ||
+ | l = '' | ||
+ | while l != (' | ||
+ | try: | ||
+ | l = input(prompt) | ||
+ | eval(" | ||
+ | except EOFError: | ||
+ | print(' | ||
+ | break | ||
+ | except: | ||
+ | continue | ||
+ | </ | ||
+ | |||
===== Twisted ===== | ===== Twisted ===== | ||
Ligne 245: | Ligne 527: | ||
import ctypes, ctypes.util | import ctypes, ctypes.util | ||
- | ctypes.cdll.LoadLibrary(ctypes.util.find_library(' | + | libc=ctypes.cdll.LoadLibrary(ctypes.util.find_library(' |
libc.printf(" | libc.printf(" | ||
</ | </ | ||
Ligne 494: | Ligne 776: | ||
(6, ' | (6, ' | ||
(7, ' | (7, ' | ||
+ | </ | ||
+ | |||
+ | * cycle : | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ... | ||
+ | ... | ||
</ | </ | ||
===== string ===== | ===== string ===== | ||
Ligne 515: | Ligne 817: | ||
>>> | >>> | ||
[(' | [(' | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== operator ===== | ||
+ | Trie par rapport a un index : | ||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | [(' | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== dbus ===== | ||
+ | |||
+ | Dbus permet d’interagir avec les autres programmes identifiés par DBUS comme knotify, le service de notifications KDE. | ||
+ | |||
+ | Pour voir la liste des services DBUS, lancer la commande qdbus : | ||
+ | < | ||
+ | gigi@debian: | ||
+ | | ||
+ | </ | ||
+ | |||
+ | <code python knotify.py> | ||
+ | # | ||
+ | import sys, dbus | ||
+ | |||
+ | knotify = dbus.SessionBus().get_object(" | ||
+ | try: | ||
+ | title, text = sys.argv[1: | ||
+ | except: | ||
+ | print ' | ||
+ | sys.exit(1) | ||
+ | |||
+ | knotify.event(" | ||
</ | </ |
python/les_modules.1370041992.txt.gz · Dernière modification : 2013/05/31 23:13 de root