bashhub 3.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,341 @@
1
+ # bash-preexec.sh -- Bash support for ZSH-like 'preexec' and 'precmd' functions.
2
+ # https://github.com/rcaloras/bash-preexec
3
+ #
4
+ #
5
+ # 'preexec' functions are executed before each interactive command is
6
+ # executed, with the interactive command as its argument. The 'precmd'
7
+ # function is executed before each prompt is displayed.
8
+ #
9
+ # Author: Ryan Caloras (ryan@bashhub.com)
10
+ # Forked from Original Author: Glyph Lefkowitz
11
+ #
12
+ # V0.4.1
13
+ #
14
+
15
+ # General Usage:
16
+ #
17
+ # 1. Source this file at the end of your bash profile so as not to interfere
18
+ # with anything else that's using PROMPT_COMMAND.
19
+ #
20
+ # 2. Add any precmd or preexec functions by appending them to their arrays:
21
+ # e.g.
22
+ # precmd_functions+=(my_precmd_function)
23
+ # precmd_functions+=(some_other_precmd_function)
24
+ #
25
+ # preexec_functions+=(my_preexec_function)
26
+ #
27
+ # 3. Consider changing anything using the DEBUG trap or PROMPT_COMMAND
28
+ # to use preexec and precmd instead. Preexisting usages will be
29
+ # preserved, but doing so manually may be less surprising.
30
+ #
31
+ # Note: This module requires two Bash features which you must not otherwise be
32
+ # using: the "DEBUG" trap, and the "PROMPT_COMMAND" variable. If you override
33
+ # either of these after bash-preexec has been installed it will most likely break.
34
+
35
+ # Avoid duplicate inclusion
36
+ if [[ "${__bp_imported:-}" == "defined" ]]; then
37
+ return 0
38
+ fi
39
+ __bp_imported="defined"
40
+
41
+ # Should be available to each precmd and preexec
42
+ # functions, should they want it. $? and $_ are available as $? and $_, but
43
+ # $PIPESTATUS is available only in a copy, $BP_PIPESTATUS.
44
+ # TODO: Figure out how to restore PIPESTATUS before each precmd or preexec
45
+ # function.
46
+ __bp_last_ret_value="$?"
47
+ BP_PIPESTATUS=("${PIPESTATUS[@]}")
48
+ __bp_last_argument_prev_command="$_"
49
+
50
+ __bp_inside_precmd=0
51
+ __bp_inside_preexec=0
52
+
53
+ # Initial PROMPT_COMMAND string that is removed from PROMPT_COMMAND post __bp_install
54
+ __bp_install_string=$'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install'
55
+
56
+ # Fails if any of the given variables are readonly
57
+ # Reference https://stackoverflow.com/a/4441178
58
+ __bp_require_not_readonly() {
59
+ local var
60
+ for var; do
61
+ if ! ( unset "$var" 2> /dev/null ); then
62
+ echo "bash-preexec requires write access to ${var}" >&2
63
+ return 1
64
+ fi
65
+ done
66
+ }
67
+
68
+ # Remove ignorespace and or replace ignoreboth from HISTCONTROL
69
+ # so we can accurately invoke preexec with a command from our
70
+ # history even if it starts with a space.
71
+ __bp_adjust_histcontrol() {
72
+ local histcontrol
73
+ histcontrol="${HISTCONTROL//ignorespace}"
74
+ # Replace ignoreboth with ignoredups
75
+ if [[ "$histcontrol" == *"ignoreboth"* ]]; then
76
+ histcontrol="ignoredups:${histcontrol//ignoreboth}"
77
+ fi;
78
+ export HISTCONTROL="$histcontrol"
79
+ }
80
+
81
+ # This variable describes whether we are currently in "interactive mode";
82
+ # i.e. whether this shell has just executed a prompt and is waiting for user
83
+ # input. It documents whether the current command invoked by the trace hook is
84
+ # run interactively by the user; it's set immediately after the prompt hook,
85
+ # and unset as soon as the trace hook is run.
86
+ __bp_preexec_interactive_mode=""
87
+
88
+ # Trims leading and trailing whitespace from $2 and writes it to the variable
89
+ # name passed as $1
90
+ __bp_trim_whitespace() {
91
+ local var=${1:?} text=${2:-}
92
+ text="${text#"${text%%[![:space:]]*}"}" # remove leading whitespace characters
93
+ text="${text%"${text##*[![:space:]]}"}" # remove trailing whitespace characters
94
+ printf -v "$var" '%s' "$text"
95
+ }
96
+
97
+
98
+ # Trims whitespace and removes any leading or trailing semicolons from $2 and
99
+ # writes the resulting string to the variable name passed as $1. Used for
100
+ # manipulating substrings in PROMPT_COMMAND
101
+ __bp_sanitize_string() {
102
+ local var=${1:?} text=${2:-} sanitized
103
+ __bp_trim_whitespace sanitized "$text"
104
+ sanitized=${sanitized%;}
105
+ sanitized=${sanitized#;}
106
+ __bp_trim_whitespace sanitized "$sanitized"
107
+ printf -v "$var" '%s' "$sanitized"
108
+ }
109
+
110
+ # This function is installed as part of the PROMPT_COMMAND;
111
+ # It sets a variable to indicate that the prompt was just displayed,
112
+ # to allow the DEBUG trap to know that the next command is likely interactive.
113
+ __bp_interactive_mode() {
114
+ __bp_preexec_interactive_mode="on";
115
+ }
116
+
117
+
118
+ # This function is installed as part of the PROMPT_COMMAND.
119
+ # It will invoke any functions defined in the precmd_functions array.
120
+ __bp_precmd_invoke_cmd() {
121
+ # Save the returned value from our last command, and from each process in
122
+ # its pipeline. Note: this MUST be the first thing done in this function.
123
+ __bp_last_ret_value="$?" BP_PIPESTATUS=("${PIPESTATUS[@]}")
124
+
125
+ # Don't invoke precmds if we are inside an execution of an "original
126
+ # prompt command" by another precmd execution loop. This avoids infinite
127
+ # recursion.
128
+ if (( __bp_inside_precmd > 0 )); then
129
+ return
130
+ fi
131
+ local __bp_inside_precmd=1
132
+
133
+ # Invoke every function defined in our function array.
134
+ local precmd_function
135
+ for precmd_function in "${precmd_functions[@]}"; do
136
+
137
+ # Only execute this function if it actually exists.
138
+ # Test existence of functions with: declare -[Ff]
139
+ if type -t "$precmd_function" 1>/dev/null; then
140
+ __bp_set_ret_value "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
141
+ # Quote our function invocation to prevent issues with IFS
142
+ "$precmd_function"
143
+ fi
144
+ done
145
+ }
146
+
147
+ # Sets a return value in $?. We may want to get access to the $? variable in our
148
+ # precmd functions. This is available for instance in zsh. We can simulate it in bash
149
+ # by setting the value here.
150
+ __bp_set_ret_value() {
151
+ return ${1:-}
152
+ }
153
+
154
+ __bp_in_prompt_command() {
155
+
156
+ local prompt_command_array
157
+ IFS=$'\n;' read -rd '' -a prompt_command_array <<< "$PROMPT_COMMAND"
158
+
159
+ local trimmed_arg
160
+ __bp_trim_whitespace trimmed_arg "${1:-}"
161
+
162
+ local command trimmed_command
163
+ for command in "${prompt_command_array[@]:-}"; do
164
+ __bp_trim_whitespace trimmed_command "$command"
165
+ if [[ "$trimmed_command" == "$trimmed_arg" ]]; then
166
+ return 0
167
+ fi
168
+ done
169
+
170
+ return 1
171
+ }
172
+
173
+ # This function is installed as the DEBUG trap. It is invoked before each
174
+ # interactive prompt display. Its purpose is to inspect the current
175
+ # environment to attempt to detect if the current command is being invoked
176
+ # interactively, and invoke 'preexec' if so.
177
+ __bp_preexec_invoke_exec() {
178
+
179
+ # Save the contents of $_ so that it can be restored later on.
180
+ # https://stackoverflow.com/questions/40944532/bash-preserve-in-a-debug-trap#40944702
181
+ __bp_last_argument_prev_command="${1:-}"
182
+ # Don't invoke preexecs if we are inside of another preexec.
183
+ if (( __bp_inside_preexec > 0 )); then
184
+ return
185
+ fi
186
+ local __bp_inside_preexec=1
187
+
188
+ # Checks if the file descriptor is not standard out (i.e. '1')
189
+ # __bp_delay_install checks if we're in test. Needed for bats to run.
190
+ # Prevents preexec from being invoked for functions in PS1
191
+ if [[ ! -t 1 && -z "${__bp_delay_install:-}" ]]; then
192
+ return
193
+ fi
194
+
195
+ if [[ -n "${COMP_LINE:-}" ]]; then
196
+ # We're in the middle of a completer. This obviously can't be
197
+ # an interactively issued command.
198
+ return
199
+ fi
200
+ if [[ -z "${__bp_preexec_interactive_mode:-}" ]]; then
201
+ # We're doing something related to displaying the prompt. Let the
202
+ # prompt set the title instead of me.
203
+ return
204
+ else
205
+ # If we're in a subshell, then the prompt won't be re-displayed to put
206
+ # us back into interactive mode, so let's not set the variable back.
207
+ # In other words, if you have a subshell like
208
+ # (sleep 1; sleep 2)
209
+ # You want to see the 'sleep 2' as a set_command_title as well.
210
+ if [[ 0 -eq "${BASH_SUBSHELL:-}" ]]; then
211
+ __bp_preexec_interactive_mode=""
212
+ fi
213
+ fi
214
+
215
+ if __bp_in_prompt_command "${BASH_COMMAND:-}"; then
216
+ # If we're executing something inside our prompt_command then we don't
217
+ # want to call preexec. Bash prior to 3.1 can't detect this at all :/
218
+ __bp_preexec_interactive_mode=""
219
+ return
220
+ fi
221
+
222
+ local this_command
223
+ this_command=$(
224
+ export LC_ALL=C
225
+ HISTTIMEFORMAT= builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //'
226
+ )
227
+
228
+ # Sanity check to make sure we have something to invoke our function with.
229
+ if [[ -z "$this_command" ]]; then
230
+ return
231
+ fi
232
+
233
+ # Invoke every function defined in our function array.
234
+ local preexec_function
235
+ local preexec_function_ret_value
236
+ local preexec_ret_value=0
237
+ for preexec_function in "${preexec_functions[@]:-}"; do
238
+
239
+ # Only execute each function if it actually exists.
240
+ # Test existence of function with: declare -[fF]
241
+ if type -t "$preexec_function" 1>/dev/null; then
242
+ __bp_set_ret_value ${__bp_last_ret_value:-}
243
+ # Quote our function invocation to prevent issues with IFS
244
+ "$preexec_function" "$this_command"
245
+ preexec_function_ret_value="$?"
246
+ if [[ "$preexec_function_ret_value" != 0 ]]; then
247
+ preexec_ret_value="$preexec_function_ret_value"
248
+ fi
249
+ fi
250
+ done
251
+
252
+ # Restore the last argument of the last executed command, and set the return
253
+ # value of the DEBUG trap to be the return code of the last preexec function
254
+ # to return an error.
255
+ # If `extdebug` is enabled a non-zero return value from any preexec function
256
+ # will cause the user's command not to execute.
257
+ # Run `shopt -s extdebug` to enable
258
+ __bp_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"
259
+ }
260
+
261
+ __bp_install() {
262
+ # Exit if we already have this installed.
263
+ if [[ "${PROMPT_COMMAND:-}" == *"__bp_precmd_invoke_cmd"* ]]; then
264
+ return 1;
265
+ fi
266
+
267
+ trap '__bp_preexec_invoke_exec "$_"' DEBUG
268
+
269
+ # Preserve any prior DEBUG trap as a preexec function
270
+ local prior_trap=$(sed "s/[^']*'\(.*\)'[^']*/\1/" <<<"${__bp_trap_string:-}")
271
+ unset __bp_trap_string
272
+ if [[ -n "$prior_trap" ]]; then
273
+ eval '__bp_original_debug_trap() {
274
+ '"$prior_trap"'
275
+ }'
276
+ preexec_functions+=(__bp_original_debug_trap)
277
+ fi
278
+
279
+ # Adjust our HISTCONTROL Variable if needed.
280
+ __bp_adjust_histcontrol
281
+
282
+ # Issue #25. Setting debug trap for subshells causes sessions to exit for
283
+ # backgrounded subshell commands (e.g. (pwd)& ). Believe this is a bug in Bash.
284
+ #
285
+ # Disabling this by default. It can be enabled by setting this variable.
286
+ if [[ -n "${__bp_enable_subshells:-}" ]]; then
287
+
288
+ # Set so debug trap will work be invoked in subshells.
289
+ set -o functrace > /dev/null 2>&1
290
+ shopt -s extdebug > /dev/null 2>&1
291
+ fi;
292
+
293
+ local existing_prompt_command
294
+ # Remove setting our trap install string and sanitize the existing prompt command string
295
+ existing_prompt_command="${PROMPT_COMMAND//$__bp_install_string[;$'\n']}" # Edge case of appending to PROMPT_COMMAND
296
+ existing_prompt_command="${existing_prompt_command//$__bp_install_string}"
297
+ __bp_sanitize_string existing_prompt_command "$existing_prompt_command"
298
+
299
+ # Install our hooks in PROMPT_COMMAND to allow our trap to know when we've
300
+ # actually entered something.
301
+ PROMPT_COMMAND=$'__bp_precmd_invoke_cmd\n'
302
+ if [[ -n "$existing_prompt_command" ]]; then
303
+ PROMPT_COMMAND+=${existing_prompt_command}$'\n'
304
+ fi;
305
+ PROMPT_COMMAND+='__bp_interactive_mode'
306
+
307
+ # Add two functions to our arrays for convenience
308
+ # of definition.
309
+ precmd_functions+=(precmd)
310
+ preexec_functions+=(preexec)
311
+
312
+ # Invoke our two functions manually that were added to $PROMPT_COMMAND
313
+ __bp_precmd_invoke_cmd
314
+ __bp_interactive_mode
315
+ }
316
+
317
+ # Sets an installation string as part of our PROMPT_COMMAND to install
318
+ # after our session has started. This allows bash-preexec to be included
319
+ # at any point in our bash profile.
320
+ __bp_install_after_session_init() {
321
+ # Make sure this is bash that's running this and return otherwise.
322
+ if [[ -z "${BASH_VERSION:-}" ]]; then
323
+ return 1;
324
+ fi
325
+
326
+ # bash-preexec needs to modify these variables in order to work correctly
327
+ # if it can't, just stop the installation
328
+ __bp_require_not_readonly PROMPT_COMMAND HISTCONTROL HISTTIMEFORMAT || return
329
+
330
+ local sanitized_prompt_command
331
+ __bp_sanitize_string sanitized_prompt_command "$PROMPT_COMMAND"
332
+ if [[ -n "$sanitized_prompt_command" ]]; then
333
+ PROMPT_COMMAND=${sanitized_prompt_command}$'\n'
334
+ fi;
335
+ PROMPT_COMMAND+=${__bp_install_string}
336
+ }
337
+
338
+ # Run our install so long as we're not delaying it.
339
+ if [[ -z "${__bp_delay_install:-}" ]]; then
340
+ __bp_install_after_session_init
341
+ fi;
@@ -0,0 +1,161 @@
1
+ #
2
+ # lib-bashhub.sh
3
+ # This file should contain the common bashhub
4
+ # shell functions between bash and zsh
5
+ #
6
+
7
+ __bh_path_add() {
8
+ if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
9
+ PATH="${PATH:+"$PATH:"}$1"
10
+ fi
11
+ }
12
+
13
+ #
14
+ # Checks if an element is present in an array.
15
+ #
16
+ # @param The element to check if present
17
+ # @param the array to check in
18
+ # @return 0 if present 1 otherwise
19
+ #
20
+ contains_element() {
21
+ local e
22
+ for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
23
+ return 1
24
+ }
25
+
26
+ # Make sure our bin directory is on our path
27
+ __bh_path_add "$HOME/.bashhub/bin"
28
+
29
+ #
30
+ # Function to be run by our preexec hook.
31
+ #
32
+ # Saves the directory this command is being executed in to track (cd-ing), and
33
+ # sets a variable so we know that a command was just executed and should be
34
+ # saved.
35
+ #
36
+ # GLOBALS:
37
+ # __BH_PWD The directory this command is being executed in
38
+ # __BH_SAVE_COMMAND The command that is being executed and to be saved.
39
+ #
40
+ # Arguments:
41
+ # $1 The command just entered, about to be executed.
42
+ #
43
+ __bh_preexec() {
44
+ __BH_PWD="$PWD"
45
+ __BH_SAVE_COMMAND="$1"
46
+ }
47
+
48
+ __bh_precmd() {
49
+
50
+ # Set this initially to properly catch the exit status.
51
+ __BH_EXIT_STATUS="$?"
52
+
53
+ local bashhub_dir
54
+ bashhub_dir=${BH_HOME_DIRECTORY:=~/.bashhub}
55
+
56
+ local command="$__BH_SAVE_COMMAND"
57
+
58
+ # Check if we need to process a command. If so, unset it as it will be
59
+ # processed and saved.
60
+ if [[ -n "$__BH_SAVE_COMMAND" ]]; then
61
+ unset __BH_SAVE_COMMAND
62
+ else
63
+ return 0
64
+ fi
65
+
66
+ if [[ -e "$bashhub_dir" ]]; then
67
+ (__bh_process_command "$command"&) >> "$bashhub_dir"/log.txt 2>&1
68
+ fi;
69
+ }
70
+
71
+ #
72
+ # Send our command to the server if everything
73
+ # looks good.
74
+ #
75
+ # @param A trimmed command from the command line
76
+ #
77
+ __bh_process_command() {
78
+
79
+ local bh_command
80
+ bh_command=$(__bh_trim_whitespace "$1")
81
+
82
+ # Sanity empty check
83
+ if [[ -z "$bh_command" ]]; then
84
+ return 0;
85
+ fi;
86
+
87
+ # Check to make sure bashhub is still installed. Otherwise, this will
88
+ # simply fail and spam the user that files dont exist.
89
+ if ! type "bashhub" &> /dev/null; then
90
+ return 0;
91
+ fi;
92
+
93
+ local process_id=$$
94
+
95
+ # This is non-standard across systems. GNU Date and BSD Date
96
+ # both convert to epoch differently. Using python for cross system
97
+ # compatibility.
98
+ local process_start_stamp
99
+ process_start_stamp=$(LC_ALL=C ps -p $$ -o lstart=)
100
+
101
+ local process_start=$(bashhub util parsedate "$process_start_stamp")
102
+ local working_directory="$__BH_PWD"
103
+ local exit_status="$__BH_EXIT_STATUS"
104
+
105
+ (bashhub save "$bh_command" "$working_directory" \
106
+ "$process_id" "$process_start" "$exit_status"&)
107
+ }
108
+
109
+ # Small function to check our Bashhub installation.
110
+ # It's added to our precmd functions. On its initial run
111
+ # it removes itself from the precmd function array.
112
+ # This means it runs exactly once.
113
+ __bh_check_bashhub_installation() {
114
+ local ret
115
+ ret=0
116
+ if [[ -n "$BASH_VERSION" && -n "$__bp_enable_subshells" && "$(trap)" != *"__bp_preexec_invoke_exec"* ]]; then
117
+ echo "Bashhub's preexec hook is being overriden and is not saving commands. Please resolve what may be holding the DEBUG trap."
118
+ ret=1
119
+ elif [[ ! -f "$BH_HOME_DIRECTORY/config" ]]; then
120
+ echo "Missing Bashhub config file. Please run 'bashhub setup' to generate one."
121
+ ret=2
122
+ elif ! grep -Fq "access_token" "$BH_HOME_DIRECTORY/config"; then
123
+ echo "Missing Bashhub access token. Please run 'bashhub setup' to re-login."
124
+ ret=3
125
+ elif ! grep -Fq "system_name" "$BH_HOME_DIRECTORY/config"; then
126
+ echo "Missing system name. Please run 'bashhub setup' to re-login."
127
+ ret=4
128
+ elif grep -Fq "save_commands = False" "$BH_HOME_DIRECTORY/config"; then
129
+ echo "Bashhub is currently disabled. Run 'bashhub on' to re-enable."
130
+ ret=5
131
+ fi;
132
+
133
+ # Remove from precmd_functions so it only runs once when the session starts.
134
+ local delete
135
+ delete=(__bh_check_bashhub_installation)
136
+ precmd_functions=( "${precmd_functions[@]/$delete}" )
137
+
138
+ return $ret
139
+ }
140
+
141
+ # Allows bashhub to manipulate session state by
142
+ # manipulating variables when invoked by precmd.
143
+ __bh_precmd_run_script() {
144
+ if [[ -e $BH_HOME_DIRECTORY/script.bh ]]; then
145
+ local command
146
+ command=$(head -n 1 "$BH_HOME_DIRECTORY/script.bh")
147
+ rm "$BH_HOME_DIRECTORY/script.bh"
148
+ eval "$command"
149
+ fi;
150
+ }
151
+
152
+ # Check our bashhub installation when the session starts.
153
+ precmd_functions+=(__bh_check_bashhub_installation)
154
+ precmd_functions+=(__bh_precmd_run_script)
155
+
156
+ __bh_trim_whitespace() {
157
+ local var=$@
158
+ var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
159
+ var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
160
+ echo -n "$var"
161
+ }
bashhub/shell_utils.py ADDED
@@ -0,0 +1,17 @@
1
+ import os
2
+ import dateutil.parser
3
+ from time import *
4
+ from .bashhub_globals import *
5
+
6
+
7
+ def get_session_information():
8
+ ppid = os.getppid()
9
+
10
+ # Non standard across systems GNU Date and BSD Date
11
+ # both convert to epoch differently. Need to use
12
+ # python date util to make standard.
13
+ start_time_command = "ps -p {0} -o lstart | sed -n 2p".format(ppid)
14
+ date_string = os.popen(start_time_command).read().strip()
15
+ date = dateutil.parser.parse(date_string)
16
+ start_time = int(mktime(date.timetuple())) * 1000
17
+ return (ppid, start_time)
bashhub/version.py ADDED
@@ -0,0 +1,5 @@
1
+ import platform
2
+
3
+ __version__ = '3.0.2'
4
+
5
+ version_str = 'Bashhub {0} (python {1})'.format(__version__, platform.python_version())
File without changes
bashhub/view/status.py ADDED
@@ -0,0 +1,25 @@
1
+ import dateutil.parser
2
+ import datetime
3
+ import humanize
4
+ from bashhub.bashhub_globals import BH_URL
5
+
6
+ status_view = """\
7
+ === Bashhub Status
8
+ {0}/{1}
9
+ Total Commands: {2}
10
+ Total Sessions: {3}
11
+ Total Systems: {4}
12
+ ===
13
+ Session PID {5} Started {6}
14
+ Commands In Session: {7}
15
+ Commands Today: {8}
16
+ """
17
+
18
+ def build_status_view(model):
19
+ date = datetime.datetime.fromtimestamp(model.session_start_time / 1000.0)
20
+ date_str = humanize.naturaltime(date)
21
+ return status_view.format(
22
+ BH_URL.rstrip('/'), model.username,
23
+ model.total_commands, model.total_sessions,
24
+ model.total_systems, model.session_name, date_str,
25
+ model.session_total_commands, model.total_commands_today)