0

Before I start just wants to tell you all that i'm a beginner in python :)

I'm running a python script via git bash while committing code changes(using git hook[commit-message]) and I wants to read some user input from git bash terminal and the same has to be sent to my python script where i'll validate that, I tried many options that are available in python modules like getpass, subprocess, msvcrt and others but nothing worked for me. So could someone guide me or show me the code how to achieve this?

Below is the one of the sample code I have used,

import sys
sys.stderr.write("Enter> ")
s=input("")
print(s)

and the output is below, actually the terminal control(stdin) doesn't wait to get user input instead it simply ended. Output of Git Bash terminal

Referred below links but nothing worked out for me :( bash script which executes a python script which prompts user GIT hook -> Python -> Bash: How to read user input?

Santhosh29
  • 79
  • 1
  • 3
  • 9
  • 1
    Why are you using `sys.stderr` anyway? It doesn't seem like an error. – TheTechRobo36414519 Jun 27 '20 at 16:22
  • You can use `bash script` to achieving this, if you interested tell me to post an answer – Peyman Majidi Jun 27 '20 at 16:33
  • 1
    You can get user input by`read` bash command then pass it to python script. Like: `read param` then `python3 myapp.py $param` – Peyman Majidi Jun 27 '20 at 16:41
  • @Peyman Majidi I'm really interested can you show me the example please, also I wants to make it clear that this python script will be called as part of git hook(commit-message) and the input I wants to read is a password of windows which will be used to authenticate the Jira portal, so ideally the prompt would come from python script so that I can read that value and call the Jira portal using requests module. I would like to have all these logic to be in Python script, is that possible? I appreciate your time on this :) – Santhosh29 Jun 27 '20 at 17:12
  • Check out my answer, and fill free for further details – Peyman Majidi Jun 28 '20 at 14:14

2 Answers2

2

I don’t think you want stderr. Try this:

s = input("Enter> ")
print(s)

Update: that doesn't seem to work for you. The other way to read input in Python is as follows:

>>> x = sys.stdin.readlines(1)
testing
>>> x
['testing\n']
>>> x[0].rstrip('\n')  # isolate the input
'testing'

However, this is probably a problem with the environment the script is being run in. Could you provide more detail in the question, so that we can reproduce your issue? Otherwise, the answer you find seems to solve your issue https://stackoverflow.com/a/46016718/13676619.

Thomas
  • 619
  • 3
  • 15
2

I suggest you read user-input via bash terminal then pass it as a parameter to your python script.

This is gonna be your python script file:

import sys

def main():
    if len(sys.argv) == 1:
        print(1) # break commiting proccess 
        return
    username = sys.argv[1]
    password = sys.argv[2]
 
    # kinda stuff to do before commiting (git hook) 

    if username == 'peyman' and password == '2020':
        print(0) # print 0 if everything works fine and you wanna continue commiting
    else:
        print(1) # print non-zero if something goes bad and you wanna break commiting proccess

if __name__ == "__main__":
    main()

and this is gonna be your pre-commit file in the .git/hook/ folder:

#!/bin/bash
echo "Username:"
exec < /dev/tty
read Username
echo "Password:"
read Password
x=$(python main.py $Username $Password)
exit $x

As you know, if pre-commit exit with 0, the committing process will execute, and if pre-commit return non-zero, committing will abort. so if you print(0) in your python script, the output set to exit command like this: exit 0

As you said all the process done in the python script file

I test it, it works, there you go~
Happy Coding

Peyman Majidi
  • 1,200
  • 1
  • 11
  • 25
  • Thanks @Peyman Majidi, At last your solution worked perfectly for me. As you said I have moved the bash script to commit-message hook where I read the password and send it to the python script. Only concern I have now is I had to provide absolute path like this `x=$(python /C/Users/sb21511/Documents/lmratesmarketdata/.git/hooks/Hello.py $1 $passvar)` I tried to give relative path but it did not work, I might do google for this to have relative path later. Once again thanks Peyman Majidi :) Have a nice day!!! – Santhosh29 Jun 28 '20 at 17:59
  • but you can add the *python script* **path** to the `$PATH` Environment variable, then It works, read this answer for more: https://stackoverflow.com/questions/9546324/adding-a-directory-to-the-path-environment-variable-in-windows – Peyman Majidi Jun 29 '20 at 03:48
  • in MyComputer->Properties->Advanced->Env Variables->Path. (https://docs.alfresco.com/4.2/tasks/fot-addpath.html) – Peyman Majidi Jun 29 '20 at 04:04
  • Sure @Peyman Majidi, let me take a look on this, Thanks once again. – Santhosh29 Jun 29 '20 at 04:58
  • 1
    Finally I used this approach `SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"` to take the python script directory instead of taking it from env $PATH. – Santhosh29 Jun 30 '20 at 07:11