Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
"""
Just a simple script to install the latest version of the Atom Editor
"""
import os
import sys
import requests
import subprocess
from bs4 import BeautifulSoup
DOWNLOAD_FILE = 'atom-amd64.deb'
DOWNLOAD_PATH = '{home}/Downloads'.format(home=os.path.expanduser('~'))
DONWLOAD_URL = 'https://atom.io/download/deb'
LATEST_RELEASE_URL = 'https://github.com/atom/atom/releases/latest'
# silence SSL certificate warnings
requests.packages.urllib3.disable_warnings()
def get_latest_version_number():
"""
Get the version number for the latest Atom release
return: str
"""
response = requests.get(url=LATEST_RELEASE_URL, verify=False)
soup = BeautifulSoup(response.content, 'html.parser')
release_header = soup.find('h1', {'class': 'release-title'})
return str(release_header.text)
def get_installed_version_number():
"""
Get the version number of currently installed Atom
return: str
"""
try:
versions = subprocess.check_output(['atom', '--version'])
except FileNotFoundError:
# Atom is not installed.
return '0.0.0'
versions_parts = versions.split('\n')
atom_info = versions_parts[0]
atom_version = atom_info.split(': ')[-1]
return str(atom_version)
def should_update():
"""
Compare latest and installed version numbers and advice update
return: bool
"""
latest = [int(n) for n in get_latest_version_number().split('.')]
installed = [int(n) for n in get_installed_version_number().split('.')]
return latest > installed
def update_atom():
"""
Download and install the latest Atom release
"""
os.chdir(DOWNLOAD_PATH)
subprocess.check_call(['wget', DONWLOAD_URL, '-O', DOWNLOAD_FILE])
subprocess.check_call(['sudo', 'dpkg', '--install', DOWNLOAD_FILE])
subprocess.check_call(['rm', '-rf', DOWNLOAD_FILE])
if __name__ == '__main__':
if os.geteuid() != 0:
sys.exit('Need to be root, exiting ...')
if should_update():
update_atom()
else:
sys.exit('No need to update, exiting ...')
@DusanMadar

This comment has been minimized.

Copy link
Owner Author

@DusanMadar DusanMadar commented Feb 16, 2016

You need to execute this with sudo. If you are using virtualenv, specify the full path to the Python executable, e.g.:

sudo /home/dm/workspace/github/venv/bin/python update_atom.py

or point the shebang to the virtualenv Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.