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:pyinvoke [2017/11/28 09:41] – marc dexet | python:pyinvoke [2017/11/28 20:56] (Version actuelle) – marc dexet | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | |||
| ====== Invoke (pyinvoke) ====== | ====== Invoke (pyinvoke) ====== | ||
| - | Invoke est un **ordonnanceur** et un **exécuteur** de tâches. | + | [[http:// |
| Il repose sur la définition des tâches (//task//) via l' | Il repose sur la définition des tâches (//task//) via l' | ||
| Ligne 197: | Ligne 199: | ||
| - | ====== Aperçu de Invoke (pyinvoke) ====== | + | ===== Exemples |
| - | Invoke est un **ordonnanceur** et un **exécuteur** de tâches. | + | ==== Convertir en PDF des fichiers asciidoc ==== |
| - | Il repose sur la définition des tâches (//task//) via l' | + | Imaginons un projet avec |
| - | Ces tâches sont définies au sein d'un fichier | + | * un répertoire |
| + | * un répertoire | ||
| + | * un listing de fichiers dans //main/src// à mettre sous forme de document asciidoc | ||
| - | //tasks.py// | + | Décomposons le workflow. |
| - | <code python> | + | |
| - | from invoke import task | + | |
| - | @task | + | Il faut |
| - | def remove_all(ctx): | + | |
| - | | + | |
| - | </ | + | |
| - | <code bash> | + | * générer un document |
| - | $ invoke -l | + | * générer |
| - | Available tasks: | + | |
| - | + | ||
| - | remove-all | + | |
| - | + | ||
| - | $ invoke remove-all | + | |
| - | $ | + | |
| - | </code> | + | |
| - | + | ||
| - | ===== Mise en oeuvre ===== | + | |
| - | + | ||
| - | Création de l' | + | |
| - | + | ||
| - | <code bash> | + | |
| - | $ ~/Workdirs$ virtualenv -p python3 test_invoke | + | |
| - | Running virtualenv with interpreter / | + | |
| - | Using base prefix '/ | + | |
| - | New python executable in / | + | |
| - | Also creating executable in / | + | |
| - | Installing setuptools, pip, wheel...done. | + | |
| - | $ ~/Workdirs$ cd test_invoke/ | + | |
| - | $ ~/Workdirs/test_invoke$ source ./bin/activate | + | |
| - | </code> | + | |
| - | + | ||
| - | Installation de pyInvoke | + | |
| - | + | ||
| - | <code bash> | + | |
| - | $ ~/ | + | |
| - | Collecting invoke | + | |
| - | Downloading invoke-0.21.0-py3-none-any.whl (153kB) | + | |
| - | | + | |
| - | Installing collected packages: invoke | + | |
| - | Successfully installed invoke-0.21.0 | + | |
| - | + | ||
| - | </ | + | |
| - | + | ||
| - | ===== Créer une tâche ===== | + | |
| - | + | ||
| - | Pour créer une tâche il suffit | + | |
| - | + | ||
| - | | + | |
| - | * de créer une fonction qui prend //au moins// un // | + | |
| - | * et de l'annoter avec //@task// | + | |
| <code python> | <code python> | ||
| - | @task | + | from invoke import |
| - | def do_something(ctx): | + | import os |
| - | .... | + | import shutil |
| - | + | from mako.template import Template | |
| - | </ | + | |
| - | Une tâche peut exécuter une fonction python | + | work_dirname = '/tmp/project_doc' |
| - | **Appel de fonction** | ||
| - | <code python> | ||
| - | def do_it_right(): | ||
| - | // | ||
| - | |||
| - | @task | ||
| - | def do_something(ctx): | ||
| - | | ||
| - | | ||
| - | </ | ||
| - | **Appel de commande système** | + | def collectfilenames(path): |
| - | <code python> | + | """ |
| - | @task(pre=[clean_workdir, | + | |
| - | post=[deploy_artefact, | + | : |
| - | def build(ctx): | + | :return: full qualified filenames from this path |
| - | ctx.run('maven install' | + | """ |
| - | </ | + | path_list |
| + | for dirpath, _, files_name in os.walk(path): | ||
| + | for name in files_name: | ||
| + | path_list.append(" | ||
| + | | ||
| - | ===== Ordonnancement des tâches ===== | ||
| - | Un tâche peut nécessiter plusieurs taches en amont et/ou en aval du processus. | + | def recreate_dir(dirname): |
| + | """ | ||
| + | Remove and recreate dirname | ||
| + | :param dirname: | ||
| + | """ | ||
| + | if os.path.exists(dirname): | ||
| + | shutil.rmtree(dirname) | ||
| + | os.makedirs(dirname) | ||
| - | Pour modéliser cet enchaînement, | ||
| - | * ses prédécesseurs avec la propriété // | ||
| - | * et ses successeurs avec la propriété // | ||
| - | |||
| - | <code python> | ||
| - | @task(pre=[clean_workdir, | ||
| - | post=[deploy_artefact, | ||
| - | def build(ctx): | ||
| - | | ||
| - | </ | ||
| - | |||
| - | La succession des tâches s' | ||
| - | |||
| - | * une exception est levée | ||
| - | * une commande système ( // | ||
| - | |||
| - | **Exemple d' | ||
| - | <code python> | ||
| @task | @task | ||
| - | def get_last_commit(ctx): | + | def move_in_tempdir(ctx): |
| - | | + | |
| - | | + | |
| - | </code> | + | |
| - | <code bash> | ||
| - | $ invoke build | ||
| - | Clean workdir | ||
| - | Get last commit | ||
| - | Traceback (most recent call last): | ||
| - | File "/ | ||
| - | sys.exit(program.run()) | ||
| - | ... | ||
| - | File "/ | ||
| - | a = 10/0 | ||
| - | ZeroDivisionError: | ||
| - | </ | ||
| - | **Exemple d' | + | @task(pre=[move_in_tempdir]) |
| - | <code python> | + | def create_listing(ctx): |
| - | @task | + | |
| - | def get_last_commit(ctx): | + | |
| - | | + | |
| - | print('Get last commit') | + | |
| - | </code> | + | |
| - | <code bash> | ||
| - | $ invoke build | ||
| - | Clean workdir | ||
| - | hostname not found | ||
| - | </ | ||
| - | ==== Tâche par défaut ==== | + | @task(move_in_tempdir, |
| + | def generate_pdf(ctx): | ||
| + | with ctx.cd(work_dirname): | ||
| + | ctx.run(' | ||
| - | Pour désigner une tâche par défaut, il suffit de lui donner la propriété // | ||
| - | <code python> | + | @task(pre=[generate_pdf], |
| - | @task(default=True) | + | def copy_to_builddir(ctx): |
| - | def build(ctx): | + | |
| - | ... | + | |
| - | </code> | + | recreate_dir(dirname) |
| + | with ctx.cd(dirname): | ||
| + | ctx.run(' | ||
| - | ==== Exemple complet d' | ||
| - | |||
| - | <code python> | ||
| - | from invoke import task | ||
| - | |||
| - | @task | ||
| - | def clean_workdir(ctx): | ||
| - | print(' | ||
| - | |||
| - | @task | ||
| - | def get_last_commit(ctx): | ||
| - | print(' | ||
| - | |||
| - | @task | ||
| - | def compile_project(ctx): | ||
| - | print(' | ||
| - | |||
| - | @task | ||
| - | def deploy_artefact(ctx): | ||
| - | print(' | ||
| - | |||
| - | @task | ||
| - | def update_website(ctx): | ||
| - | print(' | ||
| - | |||
| - | @task( | ||
| - | post=[deploy_artefact, | ||
| - | def build(ctx): | ||
| - | | ||
| </ | </ | ||
| - | |||
| - | <code bash> | ||
| - | $ invoke build | ||
| - | Clean workdir | ||
| - | Get last commit | ||
| - | Commit | ||
| - | Building | ||
| - | Deploy | ||
| - | Update website | ||
| - | </ | ||
| - | |||
| ===== FAQ ===== | ===== FAQ ===== | ||
| - | ==== Comment récupérer le résultat d'une tâche ? ==== | ||
| - | Si une tâche doit retourner un résultat pour la tâche suivante, comment le passer ? | ||
| - | Apparemment il n'est pas possible de transmettre un résultat par l' | ||
| - | |||
| - | Le seul moyen est de peut-être utiliser un dictionnaire de stockage global. | ||
| - | |||
| - | |||
| - | <code python> | ||
| - | |||
| - | storage = {} | ||
| - | |||
| - | @task | ||
| - | def do_something(ctx): | ||
| - | | ||
| - | |||
| - | |||
| - | @task(pre=[do_something]) | ||
| - | def use_something(ctx): | ||
| - | bar = storage[' | ||
| - | </ | ||
| - | |||
| - | ==== Comment répondre à une commande interactive ? ==== | ||
| - | |||
| - | //Exemple tiré de la documentation officielle// | ||
| - | |||
| - | Supposons que nous ayons une commande qui demande des informations de façon interactive : | ||
| - | |||
| - | < | ||
| - | $ excitable-program | ||
| - | When you give the OK, I'm going to do the things. All of them!! | ||
| - | Are you ready? [Y/n] y | ||
| - | OK! I just did all sorts of neat stuff. You're welcome! Bye! | ||
| - | </ | ||
| - | |||
| - | Pour cela il faut utiliser un // | ||
| - | |||
| - | <code python> | ||
| - | responder = Responder(pattern=r" | ||
| - | ctx.run(" | ||
| - | </ | ||
| ==== Comment récupérer le résultat d'une tâche ? ==== | ==== Comment récupérer le résultat d'une tâche ? ==== | ||
| Ligne 479: | Ligne 321: | ||
| ctx.run(" | ctx.run(" | ||
| </ | </ | ||
| - | |||
| - | |||
| - | {{tag> | ||