i want to rewrite logcmd into python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Forum & Off Topic (https://python-forum.io/forum-23.html) +--- Forum: Bar (https://python-forum.io/forum-27.html) +--- Thread: i want to rewrite logcmd into python (/thread-2089.html) Pages:
1
2
|
i want to rewrite logcmd into python - Skaperen - Feb-18-2017 i have this bash script that is frying nilamo's mind that i want to rewrite into python 3: it's not in python,yet, it's still in bash: #!/bin/bash #---------------------------------------------------------------- # Copyright © 2007 - Philip Howard - All rights reserved # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public # License along with this program; if not, write to the # Free Software, Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. #---------------------------------------------------------------- # command logcmd # # purpose Execute a command with logging of output. # # syntax logcmd [options] command # # options -b -c --sh --bash # Execute a command string via the bash shell # Note that the command string is one argument # following all options. # # -d --dir --logdir # Output the log to a file with a generated name # in the specified directory. # # -i --ignore-environment # Do not use the inherited environment variables. # # -f --file --logfile # Output the log to the exact specified file. # # -n --name --logname # Output the log to a file with this name and time. # # -p --prefix # Output the log to a file the this prefix to name. # # -s --suffix # Output the log to a file the this suffix to name. # # author Phil Howard #---------------------------------------------------------------- self="${0}" if [[ "x${1}" = "x----dropenv" ]] ; then shift _dropenv_flag_=1 fi if [[ "x${1}" = "x----exec" ]] ; then if [[ -n "${EXEC_COMMANDS}" ]] ; then trap "echo SIGINT ; exit 1" INT trap "echo SIGTERM ; exit 1" TERM if [[ -n "${_dropenv_flag_}" ]] ; then _command_array_=( env -i ) else _command_array_=( ) fi _command_index_=0 _message_string_="" while [[ "${_command_index_}" -lt "${EXEC_COMMANDS}" ]] ; do eval '_one_argument_="${EXEC_COMMAND_'"${_command_index_}"'}"' case "${_one_argument_}" in ( *=* ) _message_string_="${_message_string_} '${_one_argument_}'" eval "export ${_one_argument_}" ;; ( * ) break ;; esac eval "unset EXEC_COMMAND_${_command_index_}" _command_index_=$[ $_command_index_ + 1 ] done while [[ "${_command_index_}" -lt "${EXEC_COMMANDS}" ]] ; do eval '_one_argument_="${EXEC_COMMAND_'"${_command_index_}"'}"' _message_string_="${_message_string_} '${_one_argument_}'" _command_array_=( "${_command_array_[@]}" "${_one_argument_}" ) eval "unset EXEC_COMMAND_${_command_index_}" _command_index_=$[ $_command_index_ + 1 ] done unset EXEC_COMMANDS _time_string_=$( exec date '+%H:%M:%S' ) echo "${_time_string_} [$$] EXECUTING:${_message_string_}" time "${_command_array_[@]}" _status_value_=$? _time_string_=$( exec date '+%H:%M:%S' ) echo "${_time_string_} [$$] FINISHED - status = ${_status_value_}" sleep 1 else echo "option ----exec used without EXEC_COMMANDS environment variable set" 1>&2 _status_value_=1 fi exit $_status_value_ fi if [[ "x${1}" = "x----bash" ]] ; then if [[ -n "${SHELL_COMMAND}" ]] ; then trap "echo SIGINT ; exit 1" INT trap "echo SIGTERM ; exit 1" TERM c="${SHELL_COMMAND}" unset SHELL_COMMAND t=$( exec date '+%H:%M:%S' ) echo "${t} [$$] EXECUTING: ${c}" if [[ -n "${_dropenv_flag_}" ]] ; then time env -i bash -c "${c}" s=$? else time bash -c "${c}" s=$? fi t=$( exec date '+%H:%M:%S' ) echo "${t} [$$] FINISHED - status = ${s}" sleep 1 else echo "option ----bash used without SHELL_COMMAND environment variable set" 1>&2 s=1 fi exit $s fi logdir="" logfile="" if [[ -z "${1}" ]] ; then echo "" 1>&2 echo "logcmd [-d logdirectory] command [args ...]" 1>&2 echo "logcmd [-f logfile] command [args ...]" 1>&2 echo "" 1>&2 exit 1 fi while [[ "x${1:0:1}" = "x-" ]] ; do case "x${1}" in ( x-b | x-c | x--sh | x--bash ) bashcmd=1 ;; ( x-d | x--dir | x--logdir ) shift; logdir="${1}" ;; ( x-d=* ) logdir="${1:3}" ;; ( x--dir=* ) logdir="${1:6}" ;; ( x--logdir=* ) logdir="${1:9}" ;; ( x-i | x--ignore-environment ) noenv=1 ;; ( x-f | x--file | x--logfile ) shift; logfile="${1}" ;; ( x-f=* ) logfile="${1:3}" ;; ( x--file=* ) logfile="${1:7}" ;; ( x--logfile=* ) logfile="${1:10}" ;; ( x-p | x--prefix ) shift; prefix="${1}" ;; ( x-p=* ) prefix="${1:3}" ;; ( x--prefix=* ) prefix="${1:9}" ;; ( x-s | x--suffix ) shift; suffix="${1}" ;; ( x-s=* ) suffix="${1:3}" ;; ( x--suffix=* ) suffix="${1:9}" ;; ( * ) echo "Unknown option: ${1}"; die=1 ;; esac shift done [[ -z "${die}" ]] || exit 1 pid=$$ tp=$( printf "%s-%06u" $( date '+%Y%m%d-%H%M%S' ) "${pid}" ) [[ -n "${prefix}" ]] && tp="${prefix}-${tp}" [[ -n "${suffix}" ]] && tp="${tp}-${suffix}" tp="${tp}.log" if [[ -n "${logfile}" ]] ; then if [[ -n "${prefix}" || -n "${suffix}" ]] ; then echo "Cannot use --logfile with either --prefix or --suffix" 1>&2 exit 1 fi fi if [[ -z "${logfile}" || "x${logfile}" = "x." ]] ; then logfile="${tp}" if [[ -z "${logdir}" ]] ; then for d in ${HOME}/{.,}cmdlog.d ${HOME}/{.,}cmdlog . /tmp ; do if [[ -d "${d}" && -w "${d}" ]] ; then logdir="${d}" break fi done unset d fi if [[ -z "${logdir}" ]] ; then echo "No log directory available" 1>&2 exit 1 fi export SCRIPT_LOGFILE="${logdir}/${logfile}" else if [[ -n "${logdir}" ]] ; then export SCRIPT_LOGFILE="${logdir}/${logfile}" else export SCRIPT_LOGFILE="${logfile}" fi fi unset logfile unset logdir if [[ $# -lt 1 ]] ; then echo "${0} [ -d <logdir> | -f <logfile> ] <command> ..." exit 1 fi if [[ -e "${SCRIPT_LOGFILE}" ]] ; then echo "Log file '${SCRIPT_LOGFILE}' already exists" exit 1 fi if [[ -n "${bashcmd}" ]] ; then if [[ $# -gt 1 ]] ; then shift echo "Extra arguments after shell command: $*" 1>&2 exit 1 fi export SHELL_COMMAND="${1}" unset bashcmd c="----bash" else i=0 for a in "$@" ; do eval "export EXEC_COMMAND_${i}="'"${a}"' i=$[ $i + 1 ] done export EXEC_COMMANDS="${i}" unset i unset a c="----exec" fi if [[ -n "${noenv}" ]] ; then c="----dropenv ${c}" fi d=$( exec dirname "${SCRIPT_LOGFILE}" ) [[ -d "${d}" ]] || mkdir -p "${d}" || exit 1 [[ -d "${d}" ]] || exit 1 unset d exec script -f -c "exec ${self} ${c}" "${SCRIPT_LOGFILE}" RE: i want to rewrite logcmd into python - sparkz_alot - Feb-18-2017 erm, couldn't you just use pythons 'logging' module? RE: i want to rewrite logcmd into python - nilamo - Feb-18-2017 (Feb-18-2017, 06:29 AM)Skaperen Wrote: i have this bash script that is frying nilamo's mind... To be fair, that's not incredibly difficult to do, though... RE: i want to rewrite logcmd into python - Skaperen - Feb-19-2017 (Feb-18-2017, 02:11 PM)sparkz_alot Wrote: erm, couldn't you just use pythons 'logging' module? perhaps i could. is it a command? (Feb-19-2017, 03:45 AM)Skaperen Wrote:(Feb-18-2017, 02:11 PM)sparkz_alot Wrote: erm, couldn't you just use pythons 'logging' module? it does not appear to be a command. what logcmd does is determine a file to do the logging in based on a date+time string with a prefix and/or suffix added, in a given directory, or a given exact path/file, and runs the screen command in a way to have it run the given command an write its output to that file. if the new version handles individual lines of output, my plan is to also have it store a timestamp for each line of output, also store any input that happens. the c version was going to do this, maybe the python version can. the command being run needs to have stdin/out/err be going through a pty, so a python module to set up ptys would be useful. a third party module might be ok in this case (i'll need to check it's code). if timstamps are added, i'll also need a script to just output the logged output and a script to output the output in a timed playback manner. if the original took an hour to run, then the timed playback will take an hour. i use logcmd a lot every day. RE: i want to rewrite logcmd into python - wavic - Feb-19-2017 Hmmm! Do you know ttyrec program for Linux... You run it, you get the prompt back and all commands you type on this tty window are recorded with their output all of that with timestamps. Then you can replay all session with another command. I think seen its code will be helpfull a lot. RE: i want to rewrite logcmd into python - Skaperen - Feb-19-2017 (Feb-19-2017, 07:16 AM)wavic Wrote: Hmmm! Do you know ttyrec program for Linux... You run it, you get the prompt back and all commands you type on this tty window are recorded with their output all of that with timestamps. Then you can replay all session with another command. I think seen its code will be helpfull a lot. i am not familiar with it. it may be useful. even if i do my own, being compatible with it can have pluses. thanks! RE: i want to rewrite logcmd into python - wavic - Feb-19-2017 I admit that I 've never used it but it's great for showing some terminal tricks to other people. I think recorded session file is a plain text and can be studied RE: i want to rewrite logcmd into python - Skaperen - Feb-20-2017 i think i will at least start to develop these things in python (python3) i need experience in things like this. right now i am trying to pick scripts based on what can be useful to release to the public. some stuff i have done in c could be redone in python, too. if i can figure out how to call the pivot_root() syscall (unique to linux) there are a couple of interesting things i want to try doing in python on linux. RE: i want to rewrite logcmd into python - wavic - Feb-20-2017 Hah! I just read about it. Is it possible new_root to be a remote directory mounted somewhere on the local file system ? RE: i want to rewrite logcmd into python - Skaperen - Feb-20-2017 (Feb-20-2017, 05:50 AM)wavic Wrote: Hah! I just read about it. Is it possible new_root to be a remote directory mounted somewhere on the local file system ?new_root must already be mounted, be a type supported as root (most filesystem types are), and have a place for put_old to end up. so, yes! but then, anything not under new_root cannot be accessed. and it only affects the process it is executed in. i ran across someone mentioned that there existed a way to call a dynamic library symbol (name) after the library was loaded. "pivot_root" is in libc. if you are wanting to mount a network filesystem as root ... it is done in lots of places. i did it (in c) in the cable set top boxes i worked on. |