Skip to content

Instantly share code, notes, and snippets.

@Pixelsuft
Last active September 29, 2021 09:28
Show Gist options
  • Select an option

  • Save Pixelsuft/9b61990dd6c1702dc2e397c81636daec to your computer and use it in GitHub Desktop.

Select an option

Save Pixelsuft/9b61990dd6c1702dc2e397c81636daec to your computer and use it in GitHub Desktop.
Running pip without creating process
'''
MIT License
Copyright (c) 2021 Pixelsuft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import sys
from pip._internal.cli.main import main as pip_main
backup = (sys.argv, sys.exit)
def fake_exit(*args, **kwargs) -> None:
pass
def hook_sys(args: tuple) -> None:
sys.argv = list(args)
sys.exit = fake_exit
def unhook_sys() -> None:
sys.argv = backup[0]
sys.exit = backup[1]
def parse_args(cmd_string: str) -> list:
result_str = ''
work = str(cmd_string).strip()
can_pass_space = False
for i in range(len(work)):
if work[i] == '\"':
if can_pass_space == False:
can_pass_space = True
result_str += '\n'
else:
can_pass_space = False
elif work[i] == ' ':
if can_pass_space == False:
result_str += '\n'
else:
result_str += ' '
else:
result_str += work[i]
result = []
for i in result_str.split('\n'):
if not i == '':
result.append(i)
return result
def check_cmd(cmd: list) -> list:
result = cmd
return result
def run_pip_command(cmd) -> any:
if not cmd:
return True
if type(cmd) == str:
cmd = parse_args(cmd)
cmd = tuple(check_cmd(list(cmd)))
hook_sys(cmd)
error = None
try:
pip_main()
except Exception as e:
error = e
unhook_sys()
return error if error else False
def main() -> bool:
try:
while True:
print('>>> ', end='')
cmd = input()
if not cmd.lower().strip():
break
result = run_pip_command(cmd)
if result and not type(result) == bool:
print(f'Error: {result}')
return False
except KeyboardInterrupt:
return True
if __name__ == '__main__':
print('Pip hooker by pixelsuft')
if main():
print('Ctrl+C Pressed, aborting...\n', end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment