From de37686a7bd11b92dd3e9341542221ea94266fee Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 1 Jan 2017 19:17:29 +0100 Subject: [PATCH] apt-get top-level --- install | 55 ++++++++++++++++++++++++++++++++++++++ main | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ manifest | 1 + purge | 55 ++++++++++++++++++++++++++++++++++++++ remove | 55 ++++++++++++++++++++++++++++++++++++++ update | 28 ++++++++++++++++++++ 6 files changed, 275 insertions(+) create mode 100644 install create mode 100644 main create mode 100644 manifest create mode 100644 purge create mode 100644 remove create mode 100644 update diff --git a/install b/install new file mode 100644 index 0000000..6de3ae1 --- /dev/null +++ b/install @@ -0,0 +1,55 @@ +#!/bin/py + +import re; +from os import system as ossystem; +from os import path as ospath; +from sys import argv as sysargv; + +# [1] get absolute path +path = ospath.dirname(ospath.realpath(__file__)); + +# [2] get packages +packages = sysargv[1:]; + +# [3] search for manifest file +manifest=""; +with open( "%s/manifest" % path, 'r' ) as f: + manifest=f.readline(); + +# [4] search for non-installed entries in manifest +alrdy = []; +with open(manifest, 'r+') as f: + for line in f: + m = re.search(r"\s*\[[\. ]\] ([a-zA-Z0-9_-]+)", line); + if ( m != None ): + alrdy.append( m.group(1) ) ; + +# [5] Install packages +installed = []; + +for pkg in packages: + print "\033[38;5;208m[install](%d/%d) %s\033[0m" % (packages.index(pkg)+1, len(packages), pkg); + exitcode = ossystem("sudo apt-get install %s" % pkg); + + # if all ran successfully + if ( exitcode == 0 ): + print "\033[38;5;78msuccess\033[0m"; + installed.append( pkg ); + else: + print "\033[38;5;161merror\033[0m"; + + +# [6] update manifest +for pkg in installed: + print "\033[38;5;39m[notifying](%d/%d) %s\033[0m" % (installed.index(pkg)+1, len(installed), pkg); + + # if new package + if( not pkg in alrdy ): + with open(manifest, "a") as f: + f.write(" [x] %s\n" % pkg); + else: + with open(manifest, 'r') as reader: + replaced = reader.read().replace("[ ] %s" % pkg, "[x] %s" % pkg).replace("[.] %s" % pkg, "[x] %s" % pkg); + + with open(manifest, 'w') as writer: + writer.write( replaced ); diff --git a/main b/main new file mode 100644 index 0000000..5e20c8a --- /dev/null +++ b/main @@ -0,0 +1,81 @@ +#!/bin/py + + +########### +# IMPORTS # +########### +from os import path as ospath; +from os import system as ossystem; +import sys; + + +# get absolute path +path = ospath.dirname(ospath.realpath(__file__)); + +# help function +def showhelp(): + print "apt-plus 1.0.0 (amd64)"; + print "Usage: apt-plus command arguments"; + print "Usage: apt-plus install|remove|purge pkg1 [pkg2 ...]"; + print "Usage: apt-plus manifest /path/to/manifest.txt"; + print; + print "apt-plus is a set of shell aliases using apt-get"; + print "in order to automate basic routines"; + print "installed packages are notices in the manifest file"; + print "(default is ~/.x-migration)"; + print; + print "List of commands:"; + print " update - Upgrades packages and kernel and cleans after"; + print " install - Installs packages and notice it in the manifest"; + print " remove - Removes packages and notice it in the manifest"; + print " purge - Purges packages and notice it in the manifest"; + print " manifest - To set the manifest file"; + print; + + +# If at least 1 arg given +if ( len(sys.argv) > 1 ): + + command=sys.argv[1]; + + # if "updated" + if ( command == "update" ): + ossystem("sh %s/update;" % path); + elif ( command == "install" ): + # if package/s given + if ( len(sys.argv) < 3 ): + sys.exit("Missing argument : package names"); + + packages = " ".join(sys.argv[2:]); + + ossystem( "python %s/install %s" % (path, packages) ); + elif ( command == "remove" ): + # if package/s given + if ( len(sys.argv) < 3 ): + sys.exit("Missing argument : package names"); + + packages = " ".join(sys.argv[2:]); + ossystem( "python %s/remove %s" % (path, packages) ); + elif ( command == "purge" ): + # if package/s given + if ( len(sys.argv) < 3 ): + sys.exit("Missing argument : package names"); + + packages = " ".join(sys.argv[2:]); + ossystem( "python %s/purge %s" % (path, packages) ); + + elif ( command == "manifest" ): + # if package/s given + if ( len(sys.argv) < 3 ): + sys.exit("Missing argument : manifest file"); + elif ( not ospath.isfile(sys.argv[2]) ): + sys.exit("Given path isn't a file"); + + file = ospath.realpath(sys.argv[2]); + with open( ("%s/manifest" % path), "w") as f: + f.write(file); + else: + showhelp(); +# if missing 1 arg +else: + showhelp(); diff --git a/manifest b/manifest new file mode 100644 index 0000000..73efadb --- /dev/null +++ b/manifest @@ -0,0 +1 @@ +/home/xdrm-brackets/.x-migration \ No newline at end of file diff --git a/purge b/purge new file mode 100644 index 0000000..b941b0d --- /dev/null +++ b/purge @@ -0,0 +1,55 @@ +#!/bin/py + +import re; +from os import system as ossystem; +from os import path as ospath; +from sys import argv as sysargv; + +# [1] get absolute path +path = ospath.dirname(ospath.realpath(__file__)); + +# [2] get packages +packages = sysargv[1:]; + +# [3] search for manifest file +manifest=""; +with open( "%s/manifest" % path, 'r' ) as f: + manifest=f.readline(); + +# [4] search for installed entries in manifest +alrdy = []; +with open(manifest, 'r+') as f: + for line in f: + m = re.search(r"\s*\[x\] ([a-zA-Z0-9_-]+)", line); + if ( m != None ): + alrdy.append( m.group(1) ) ; + +# [5] Purge packages +purged = []; + +for pkg in packages: + print "\033[38;5;208m[purge](%d/%d) %s\033[0m" % (packages.index(pkg)+1, len(packages), pkg); + exitcode = ossystem("sudo apt-get purge %s" % pkg); + + # if all ran successfully + if ( exitcode == 0 ): + print "\033[38;5;78msuccess\033[0m"; + purged.append( pkg ); + else: + print "\033[38;5;161merror\033[0m"; + + +# [6] update manifest +for pkg in purged: + print "\033[38;5;39m[notifying](%d/%d) %s\033[0m" % (purged.index(pkg)+1, len(purged), pkg); + + # if new package + if( not pkg in alrdy ): + with open(manifest, "a") as f: + f.write(" [ ] %s\n" % pkg); + else: + with open(manifest, 'r') as reader: + replaced = reader.read().replace("[x] %s" % pkg, "[ ] %s" % pkg); + + with open(manifest, 'w') as writer: + writer.write( replaced ); diff --git a/remove b/remove new file mode 100644 index 0000000..dc84e34 --- /dev/null +++ b/remove @@ -0,0 +1,55 @@ +#!/bin/py + +import re; +from os import system as ossystem; +from os import path as ospath; +from sys import argv as sysargv; + +# [1] get absolute path +path = ospath.dirname(ospath.realpath(__file__)); + +# [2] get packages +packages = sysargv[1:]; + +# [3] search for manifest file +manifest=""; +with open( "%s/manifest" % path, 'r' ) as f: + manifest=f.readline(); + +# [4] search for installed entries in manifest +alrdy = []; +with open(manifest, 'r+') as f: + for line in f: + m = re.search(r"\s*\[x\] ([a-zA-Z0-9_-]+)", line); + if ( m != None ): + alrdy.append( m.group(1) ) ; + +# [5] Remove packages +removed = []; + +for pkg in packages: + print "\033[38;5;208m[remove](%d/%d) %s\033[0m" % (packages.index(pkg)+1, len(packages), pkg); + exitcode = ossystem("sudo apt-get remove %s" % pkg); + + # if all ran successfully + if ( exitcode == 0 ): + print "\033[38;5;78msuccess\033[0m"; + removed.append( pkg ); + else: + print "\033[38;5;161merror\033[0m"; + + +# [6] update manifest +for pkg in removed: + print "\033[38;5;39m[notifying](%d/%d) %s\033[0m" % (removed.index(pkg)+1, len(removed), pkg); + + # if new package + if( not pkg in alrdy ): + with open(manifest, "a") as f: + f.write(" [.] %s\n" % pkg); + else: + with open(manifest, 'r') as reader: + replaced = reader.read().replace("[x] %s" % pkg, "[.] %s" % pkg); + + with open(manifest, 'w') as writer: + writer.write( replaced ); diff --git a/update b/update new file mode 100644 index 0000000..fbbc5e0 --- /dev/null +++ b/update @@ -0,0 +1,28 @@ +#!/bin/sh + +success_f(){ + echo "\033[38;5;78mSUCCESS\033[31;0m"; +} + +abort_f(){ + echo "\033[38;5;161mABORTING\033[31;0m"; + exit; +} + +# Update sources list +echo "\033[38;5;208m[1/4] UPDATING THE LIST OF PACKAGES\033[31;0m"; +sudo apt-get update && success_f || abort_f; + +# Upgrade found packages +echo "\033[38;5;208m[2/4] UPGRADING PACKAGES\033[31;0m"; +sudo apt-get upgrade && success_f || abort_f; + +# Upgrade kernel +echo "\033[38;5;208m[3/4] UPGRADING KERNEL\033[31;0m"; +sudo apt-get dist-upgrade && success_f || abort_f; + +# Remove unused dependencies +echo "\033[38;5;208m[4/4] CLEANING\033[31;0m"; +sudo apt-get autoremove; +sudo apt-get autoclean; +success_f;