Python escribe cada vez más scripts para tareas administrativas en el sistema operativo Linux. Esta publicación tiene como objetivo presentar una herramienta para rastrear la ejecución de declaraciones de Python. Python es un lenguaje de programación dinámico orientado a objetos que se puede utilizar para desarrollar varios tipos de software. Ofrece un fuerte soporte para la integración con otros lenguajes y herramientas y viene con amplias bibliotecas estándar. En las distribuciones de Linux, Python se usa ampliamente para escribir herramientas administrativas, como el paquete de configuración de la impresora, etc.
Rastrear la ejecución de sentencias de Python y registrar todos los códigos en ejecución línea por línea es muy útil para localizar la causa de un problema de manera eficiente.
Afortunadamente, el paquete de python viene con una herramienta trace.py , que se puede utilizar para cumplir con esos requisitos. El archivo trace.py reside en el directorio /user/lib/python2.x, donde python2.x es la versión de python (por ejemplo, python2.3 y python2.4, etc.)
# rpm -ql python |grep trace.py /usr/lib/python2.3/trace.py /usr/lib/python2.3/trace.pyc /usr/lib/python2.3/trace.pyo
Por ejemplo, para rastrear /usr/sbin/printconf-backend, el comando es el siguiente:
# /usr/lib/python2.x/trace.py --trace /usr/sbin/printconf-backend
Mostrará toda la información de depuración y el código fuente del script de Python en la consola. Podemos grabar toda la salida de la siguiente manera.
# script /tmp/printerconf.log # /usr/lib/python2.x/trace.py --trace /usr/sbin/printconf-backend # exit #
Luego revise el /tmp/printerconf.log archivo.
Nota :Por defecto, trace.py no tiene permiso de ejecución. Por lo tanto, es necesario otorgar permiso de ejecución antes de realizar las instrucciones anteriores.Más opciones de Trace.py
Usando la opción –ayuda muestra información de uso en detalle para trace.py. Por ejemplo:
$ /usr/lib/python2.3/trace.py --help Usage: /usr/lib/python2.3/trace.py [OPTIONS][ARGS] Meta-options: --help Display this help then exit. --version Output version information then exit. Otherwise, exactly one of the following three options must be given: -t, --trace Print each line to sys.stdout before it is executed. -c, --count Count the number of times each line is executed and write the counts to .cover for each module executed, in the module's directory. See also `--coverdir', `--file', `--no-report' below. -l, --listfuncs Keep track of which functions are executed at least once and write the results to sys.stdout after the program exits. -r, --report Generate a report from a counts file; do not execute any code. `--file' must specify the results file to read, which must have been created in a previous run with `--count --file=FILE'. Modifiers: -f, --file= File to accumulate counts over several runs. -R, --no-report Do not generate the coverage report files. Useful if you want to accumulate over several runs. -C, --coverdir= Directory where the report files. The coverage report for . is written to file / / .cover. -m, --missing Annotate executable lines that were not executed with '>>>>>> '. -s, --summary Write a brief summary on stdout for each file. (Can only be used with --count or --report.) Filters, may be repeated multiple times: --ignore-module= Ignore the given module and its submodules (if it is a package). --ignore-dir= Ignore files in the given directory (multiple directories can be joined by os.pathsep).