Skip to content

Instantly share code, notes, and snippets.

@vitorbrandao
Created December 11, 2014 14:10
Show Gist options
  • Save vitorbrandao/8881da80f748822a04d9 to your computer and use it in GitHub Desktop.
Save vitorbrandao/8881da80f748822a04d9 to your computer and use it in GitHub Desktop.
Ghost upgrade script (Python)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 Vítor Brandão <[email protected]>
import subprocess
from contextlib import closing
import glob
import os
import requests
import shutil
import sys
import tempfile
import zipfile
# Configuration
config = {
'zip_url': "http://ghost.org/zip/ghost-latest.zip"
}
sys.stdout.write('Welcome to the Ghost upgrade script. Hang on tight!\n')
# Donwload ghost-latest.zip
sys.stdout.write('Downloading the latest Ghost zip file... ')
sys.stdout.flush()
tmp_dir = tempfile.mkdtemp(prefix='ghost-')
zip_file = os.path.join(tmp_dir, 'ghost.zip')
with closing(requests.get(config['zip_url'], stream=True)) as r:
with open(zip_file, 'wb') as fd:
for chunk in r.iter_content(8192):
fd.write(chunk)
# If failed to download the zip file, bail out.
if not os.path.exists(zip_file):
sys.stdout.write('No good.\nFailed to download the zip file. Aborting.\n')
shutil.rmtree(tmp_dir)
sys.exit(1)
# Unzip the archive
sys.stdout.write('Done.\nExtracting archive contents... ')
sys.stdout.flush()
with zipfile.ZipFile(zip_file, "r") as z:
z.extractall(tmp_dir)
sys.stdout.write('Done.\n')
# Copy *.js, *.json and *.md files
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.stdout.write('Updating your Ghost installation... ')
sys.stdout.flush()
files = [tmp_dir + '/LICENSE']
files.extend(glob.glob(tmp_dir + '/*.js'))
files.extend(glob.glob(tmp_dir + '/*.json'))
files.extend(glob.glob(tmp_dir + '/*.md'))
for f in files:
shutil.copy2(f, root_dir)
# Copy the core/ directory
core_dir = os.path.join(root_dir, 'core')
if os.path.isdir(core_dir):
shutil.rmtree(core_dir)
shutil.copytree(os.path.join(tmp_dir, 'core'), core_dir)
shutil.rmtree(tmp_dir)
sys.stdout.write('Done.\n')
# Run npm install
sys.stdout.write('Updating node modules...\n')
sys.stdout.flush()
os.chdir(root_dir)
subprocess.call(['npm', 'install', '--production'])
sys.stdout.write('Congratulations. Your Ghost installation is now up-to-date!\n')
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment