Last active
December 19, 2015 09:29
-
-
Save mhl/5933301 to your computer and use it in GitHub Desktop.
A simple script to switch between Mzalendo setups for different countries
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 python | |
import os | |
import sys | |
mzalendo_directory = os.path.join(os.environ['HOME'], 'mzalendo') | |
available_mzalendi = ('nigeria', 'kenya', 'south-africa') | |
def usage_and_exit(): | |
print >> sys.stderr, "Usage: %s <COUNTRY>" % (sys.argv[0],) | |
print >> sys.stderr, "... where country is one of:" | |
for country in available_mzalendi: | |
print >> sys.stderr, " ", country | |
sys.exit(1) | |
if len(sys.argv) != 2: | |
usage_and_exit() | |
requested = sys.argv[1] | |
if requested not in available_mzalendi: | |
usage_and_exit() | |
media_root_symlink = os.path.join(mzalendo_directory, 'media_root') | |
general_yml_symlink = os.path.join(mzalendo_directory, 'mzalendo', 'conf', 'general.yml') | |
media_root_target = 'media_root.' + requested | |
general_yml_target = 'general-' + requested + '.yml' | |
def switch_link(symlink_filename, target_filename): | |
if not os.path.islink(symlink_filename): | |
print >> sys.stderr, "%s was not a symlink, and should be" % (symlink_filename,) | |
sys.exit(1) | |
full_target_filename = os.path.join(os.path.dirname(symlink_filename), | |
target_filename) | |
if not os.path.exists(full_target_filename): | |
print >> sys.stderr, "The intended target of the symlink (%s) didn't exist" % (target_filename,) | |
sys.exit(1) | |
os.unlink(symlink_filename) | |
os.symlink(target_filename, symlink) | |
for target, symlink in ((media_root_target, media_root_symlink), | |
(general_yml_target, general_yml_symlink)): | |
switch_link(symlink, target) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment