Last active
February 26, 2021 01:02
-
-
Save jshwi/185e51faae26b0d0efefb50e4b38ddd5 to your computer and use it in GitHub Desktop.
Full apt-get routine for Debian derivatives
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
fullapt | |
======= | |
Full apt-get routine for Debian derivatives | |
""" | |
# Copyright 2020 Stephen Whitlock | |
# | |
# 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. | |
__author__ = "Stephen Whitlock" | |
__maintainer__ = "Stephen Whitlock" | |
__email__ = "[email protected]" | |
__version__ = "1.0.0" | |
__copyright__ = "2019, Stephen Whitlock" | |
__license__ = "MIT" | |
import argparse | |
import subprocess | |
def argument_parser(): | |
parser = argparse.ArgumentParser(prog="fullapt") | |
parser.add_argument( | |
"-l", | |
"--level", | |
choices=["1", "2", "3", "4"], | |
nargs="+", | |
action="store", | |
default=["1"], | |
help="see readme for a description of the various levels", | |
) | |
parser.add_argument( | |
"-c", | |
"--cronjob", | |
action="store_true", | |
help="cron-friendly output mode", | |
) | |
return parser.parse_args() | |
def shell(cmd, **kwargs): | |
devnull = kwargs.get("devnull", False) | |
stdout = subprocess.DEVNULL if devnull else None | |
return subprocess.call(cmd, stdout=stdout, stderr=stdout, shell=True) | |
def apt_shell(cmd, **kwargs): | |
cmd = f"sudo apt-get {cmd}" | |
return shell(cmd, **kwargs) | |
def cronprint(cron, string): | |
if not cron: | |
print(f"\u001b[0;33;40m{string}\u001b[0;0m") | |
def resolve_deborphan(): | |
deborphan_not_installed = shell("dpkg-query -l deborphan", devnull=True) | |
if deborphan_not_installed: | |
print("\u001b[0;33;40m`deborphan' is used in this script\u001b[0;0m") | |
print("It is not installed on your system") | |
print("Would you like to install `deborphan'?") | |
try: | |
input( | |
"\u001b[0;33;40mPress enter to install or <CTRL-C> to " | |
"continue without using `deborphan'\u001b[0;0m" | |
) | |
print("\u001b[0;33;40mUpdating apt...\u001b[0;0m") | |
apt_shell("update") | |
print("\u001b[0;33;40mInstalling `deborphan'\u001b[0;0m") | |
exit_code = apt_shell("install deborphan -y") | |
if not exit_code: | |
print( | |
"\u001b[0;32;40m`deborphan' installed " | |
"successfully\u001b[0;0m" | |
) | |
except KeyboardInterrupt: | |
print("\u001b[1;33;40mContinuing without `deborphan'\u001b[0;0m") | |
return False | |
return True | |
def levelbar(level): | |
space = " " * (4 - level) | |
bar = "#" * level | |
return f"[{bar}{space}]" | |
def main() -> None: | |
deborphan = False | |
args = argument_parser() | |
level = int(args.level[0]) | |
cron = args.cronjob | |
print(f"\u001b[0;32;40m{levelbar(level)}Fullapt: Level {level}\u001b[0;0m") | |
if level >= 3 and not cron: | |
deborphan = resolve_deborphan() | |
cronprint(cron, "Refreshing apt cache...") | |
apt_shell("clean") | |
apt_shell("autoclean") | |
apt_shell("update") | |
cronprint(cron, "Running upgrade...") | |
apt_shell("upgrade -y") | |
if level >= 2: | |
cronprint(cron, "Running dist-upgrade...") | |
apt_shell("dist-upgrade -y") | |
cronprint(cron, "Purging unneeded packages...") | |
apt_shell("-y autoremove --purge") | |
if level == 4: | |
cronprint(cron, "Running full purge...") | |
apt_shell("-y purge $(dpkg -l | grep '^rc' | awk '{print $2}')") | |
if deborphan: | |
cronprint(cron, "Removing all orphans...") | |
shell("deborphan | sudo apt-get -y remove --purge") | |
apt_shell("clean") | |
apt_shell("autoclean") | |
apt_shell("update") | |
cronprint(cron, "Done") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment