Skip to content

Instantly share code, notes, and snippets.

@krisanalfa
Last active June 12, 2017 08:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisanalfa/153f8322c6f3562538b0a4a4c160dcc4 to your computer and use it in GitHub Desktop.
Save krisanalfa/153f8322c6f3562538b0a4a4c160dcc4 to your computer and use it in GitHub Desktop.
System Updater
#!/usr/bin/python
from os import devnull
from distutils import spawn
from pip import get_installed_distributions
from cement.core.foundation import CementApp
from subprocess import call, check_output, PIPE, Popen
from cement.core.controller import CementBaseController, expose
class BaseController(CementBaseController):
class Meta:
label = 'base'
description = """
__ __ __ __ ___ ____
/ / / /___ ____/ /___ _/ /____ / | / / /
/ / / / __ \/ __ / __ `/ __/ _ \ / /| | / / /
/ /_/ / /_/ / /_/ / /_/ / /_/ __/ / ___ |/ / /
\____/ .___/\__,_/\__,_/\__/\___/ /_/ |_/_/_/
/_/
Version : v0.0.1
Author : Krisan Alfa Timur <krisan47@gmail.com>
"""
arguments = [
( ['--no-system'], dict(action='store_true', help='Don\'t update system package.') ),
( ['--no-node'], dict(action='store_true', help='Don\'t update nodejs and it\'s packages.') ),
( ['--no-node-modules'], dict(action='store_true', help='Don\'t update nodejs packages.') ),
( ['--no-php'], dict(action='store_true', help='Don\'t update PHP Module and it\'s Composer Packages.') ),
( ['--no-php-packages'], dict(action='store_true', help='Don\'t update PHP Composer Packages.') ),
( ['--no-python'], dict(action='store_true', help='Don\'t update Python PIP Module.') ),
( ['--no-ruby'], dict(action='store_true', help='Don\'t update Ruby Gem Module.') ),
( ['--version'], dict(action='store_true', help='Print script version and exit.') ),
]
@expose(hide=True)
def default(self):
if (self.app.pargs.version):
print 'Update All version v0.0.1 (Made by Krisan Alfa Timur <krisan47@gmail.com>)'
else:
self.app.log.info('Invalid command, use --help for more information.')
@expose(help='Run all updater (use --no-* to override what to skip).')
def run(self):
self.update_system()
self.update_node()
self.update_php()
self.update_pip()
self.update_gem()
@expose(hide=True)
def command_exists (self = False, executable = ''):
return spawn.find_executable(executable) is not None
@expose(help='Update system package (via apt).')
def update_system (self = False):
apt = 'apt' if self.command_exists('apt') else 'apt-get'
if (self.app.pargs.no_system == False):
self.app.log.info('Updating cache...')
call(['sudo', apt, 'update'])
self.app.log.info('Upgrading system...')
call(['sudo', apt, 'upgrade', '--yes'])
self.app.log.info('Cleaning cache...')
call(['sudo', apt, 'clean'])
self.app.log.info('Remove unused package...')
call(['sudo', apt, 'autoremove', '--yes', '--purge'])
@expose(help='Update only nodejs (via n) and all it\'s package (via npm).')
def update_node (self = False):
if (self.app.pargs.no_node == False and self.command_exists('n')):
self.app.log.info('Upgrading nodejs...')
call(['sudo', 'n', 'latest'])
if(self.app.pargs.no_node_modules == False and self.command_exists('npm-check')):
self.app.log.info('Upgrading global node-modules...')
call(['sudo', 'npm-check', '--global', '--update'])
@expose(help='Update only PHP Module and all it\'s Global Composer Packages.')
def update_php (self = False):
if (self.app.pargs.no_php == False and self.command_exists('pear')):
self.app.log.info('Upgrading PHP modules...')
call(['sudo', 'pear', 'upgrade-all'])
if(self.app.pargs.no_php_packages == False and self.command_exists('composer')):
self.app.log.info('Upgrading composer global dependency...')
call(['composer', 'global', 'update', '-vvv'])
@expose(help='Update only Python module (via PIP).')
def update_python (self = False):
if (self.app.pargs.no_python == False and self.command_exists('pip')):
self.app.log.info('Upgrading pip...')
for dist in get_installed_distributions():
self.app.log.info("{0} {1}...".format('Updating', dist.project_name))
call(['sudo', '-H', 'pip', 'install', '--upgrade', dist.project_name], stdout=open(devnull, 'wb'))
@expose(help='Update only Ruby module (via RubyGems).')
def update_ruby (self = False):
if (self.app.pargs.no_ruby == False and self.command_exists('gem')):
self.app.log.info('Upgrading gem...')
ps = Popen(('gem', 'list'), stdout=PIPE)
output = check_output(('cut', '-d', ' ', '-f', '1'), stdin=ps.stdout)
for dist in output.splitlines():
self.app.log.info("{0} {1}...".format('Updating', dist))
call(['sudo', 'gem', 'update', dist, '--no-ri', '--no-rdoc'], stdout=open(devnull, 'wb'))
class MyApp(CementApp):
class Meta:
label = 'update-all'
base_controller = 'base'
handlers = [BaseController]
def main():
with MyApp() as app:
app.run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment