#!/usr/bin/env python import logging import datetime import subprocess import xml.etree.ElementTree # config repo = 'https://xp-dev.com/svn/ns-stats' component_dir = '/website' export_dir = '/home/slooman/public_html' tag_id_file = '/home/slooman/public_html/.tag' deploy_string = '~~DEPLOY~~' # logging logging.basicConfig(filename='website_deployer.log', format='%(asctime)s %(name)s %(levelname)s %(message)s', level=logging.DEBUG) def get_last_deployment_tag(repo_path): # execute svn log repo/trunk tags_url = repo_path + '/tags' out = subprocess.Popen(['svn', 'log', '-v', '--xml', tags_url], stdout=subprocess.PIPE).communicate()[0] # parse output tree = xml.etree.ElementTree.fromstring(out) for log_entry in tree.findall('logentry'): if deploy_string in log_entry.find('msg').text: for paths in log_entry.findall('paths'): for path in paths.findall('path'): return path.text return (None, None) def export(tag_path, target_dir): subprocess.call(['svn', 'export', '--force', tag_path, target_dir]) def current_tag_name(tag_file): f = open(tag_file, 'r') tag_name = f.read() f.close() return tag_name def write_tag_name(tag_file, tag_name): f = open(tag_file, 'w') f.write(tag_name) f.close() def main(): logging.debug('starting deployer at {0}'.format(datetime.datetime.now())) logging.debug('fetching svn tags') tag_name = get_last_deployment_tag(repo) logging.debug('got tag: {0}'.format(tag_name)) if tag_name and tag_name != current_tag_name(tag_id_file): logging.info('deploying new version, path: {0}'.format(tag_name)) tag_path = repo + tag_name export(tag_path + component_dir, export_dir) write_tag_name(tag_id_file, tag_name) logging.debug('ending fetcher at {0}'.format(datetime.datetime.now())) if __name__ == '__main__': main()