Skip to main content

A collection of python functions made by zh.

Project description

zhmiscellanygsudo,

An extremely useful and unique fully-featured wrapper of gsudo for python.

Introduction
Usage examples
Documentation


Introduction

This package was made to once and for all solve the common issue of rerunning python scripts and pyinstaller binaries as admin, and so much more! Even able to execute processes as SYSTEM or even TrustedInstaller! (SYSTEM is higher then admin, and TrustedInstaller is the highest privilege level on Windows machines!)

Take this example; if I have a script prints some useful info, that happens to need to be run as admin whenever it's called, but I don't want to always call it from an admin shell. The only way to solve this before was to run the script in as admin in a new window, but now all my useful information that our imaginary script just printed out is all in that new window! What to do? Well, by default on Windows, it is actually impossible to run a process as admin and keep its console output inside the same window. But not anymore! With functions such as zhmiscellanygsudo.admin_subprocess.Popen() we can all have the best of both worlds!

Supports use with pyinstaller and other python packagers!

Can be installed with pip install zhmiscellanygsudo

The git repository for this package can be found here. The docs also look nicer on github.

If you want to reach out, you may add me on discord at @z_h_ or join my server.

Usage examples


Re-running as admin and retaining console window:

import zhmiscellanygsudo
# Are we admin? Lets find out.
print(zhmiscellanygsudo.is_admin())
# If we're not, this script/executable will be re ran as admin, all the while retaining the same console window, as if by magic!
zhmiscellanygsudo.rerun_as_admin(keep_same_console=True)  # keeping the same console window is default behavior if not specified
# we can be confident that we will only get to this line once the script is running with admin.
print("Hey, I'm running as admin now!")

Running another script with the highest possible privileges:

import zhmiscellanygsudo
zhmiscellanygsudo.admin_subprocess.run('python another_script.py', run_as_TrustedInstaller=True)  # the same high privilege arguments are available for the rerun function too

Documentation:


zhmiscellanygsudo.rerun_as_admin()

zhmiscellanygsudo.rerun_as_admin(run_as_SYSTEM=False, run_as_TrustedInstaller=False, keep_same_console=True)

By default, rerun the current script as admin. Optionally run as SYSTEM or even TrustedInstaller.

zhmiscellanygsudo.admin_subprocess.Popen()

zhmiscellanygsudo.admin_subprocess.Popen(command, run_as_SYSTEM=False, run_as_TrustedInstaller=False, **kwargs)

Works the exact same way as the beloved subprocess.Popen, but by default run the command as admin. Returns a subprocess "process" object, again, exactly the same as subprocess. Optionally run as SYSTEM or even TrustedInstaller.

zhmiscellanygsudo.admin_subprocess.run()

zhmiscellanygsudo.admin_subprocess.run(command, run_as_SYSTEM=False, run_as_TrustedInstaller=False, **kwargs)

Works the exact same way as subprocess.run, but by default run the command as admin. Returns a subprocess "process" object, again, exactly the same as subprocess. Optionally run as SYSTEM or even TrustedInstaller.

zhmiscellanygsudo.is_admin()

zhmiscellanygsudo.is_admin(simple=False)

Returns ints 0 if the current process isn't running as admin, returns 1 if the process is running with admin, returns 2 if the process is running as SYSTEM, and returns 3 if running as TrustedInstaller. On some machines it may return an incorrect result when running as SYSTEM or TrustedInstaller, but only inaccurate between those 2 selections.

if simple is set to True, then it will return False if not running with admin privileges or above, and True if it is.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zhmiscellanygsudo-0.6.3-py3-none-any.whl (14.9 MB view details)

Uploaded Python 3

File details

Details for the file zhmiscellanygsudo-0.6.3-py3-none-any.whl.

File metadata

File hashes

Hashes for zhmiscellanygsudo-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cdd0562bfd90447ad8a063f7b3bdaed10ce0324744223e9c84bf812a7d5ecfdf
MD5 aa9baf032696b6c1d1ae88965a74fb5b
BLAKE2b-256 13f4de180c61ad2943902aed0cb31a2c43bb9dcddc3149a12c86e1fdb23d160e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page