Skip to content

Instantly share code, notes, and snippets.

@tylermenezes
Created June 27, 2013 18:49
Show Gist options
  • Save tylermenezes/5879238 to your computer and use it in GitHub Desktop.
Save tylermenezes/5879238 to your computer and use it in GitHub Desktop.
Creates apache sites for local Github repos
import os
from subprocess import call
def get_projects(github_directory):
top_level_directories = [d for d in os.listdir(github_directory)]
for directory in top_level_directories:
project_directories = [[directory, p] for p in os.listdir(os.path.join(github_directory, directory))]
for project in project_directories:
yield project
def get_sites(sites_directory):
return [f for f in os.listdir(sites_directory)]
def site_exists(site):
return (site in get_sites('/etc/apache2/sites-available'))
def create_site(name, directory):
vhost_template = """<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName {0}
DocumentRoot {1}
<Directory {1}>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>"""
vhost = vhost_template.format(name, directory)
with open(os.path.join('/etc/apache2/sites-available', name), "w") as f:
f.write(vhost)
call(['a2ensite', name])
call(['service', 'apache2', 'reload'])
def add_to_hosts(site):
with open('/etc/hosts', "a") as f:
f.write("127.0.0.1 " + site + "\n")
for project in get_projects('/home/tylermenezes/Github'):
site_name = '.'.join(project[::-1] + ['localhost']).lower()
if not site_exists(site_name):
site_dir = os.path.join('/home/tylermenezes/Github', os.sep.join(project))
create_site(site_name, site_dir)
add_to_hosts(site_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment