Python Forum
i want to rewrite logcmd into python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i want to rewrite logcmd into python
#1
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/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}"
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
erm, couldn't you just use pythons 'logging' module?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
(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...
Reply
#4
(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?

perhaps i could.  is it a command?

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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(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!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
Hah! I just read about it. Is it possible new_root to be a remote directory mounted somewhere on the local file system ?  Big Grin
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(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 ?  Big Grin
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  i want to rewrite tarnet into python Skaperen 11 10,085 Feb-21-2017, 03:47 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020