#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright (c) 2008, fuller # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the daemogorgon.net nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. """ DITrack multi-project manager :Author: $Author: fuller $ :Contact: fuller@daemogorgon.net (http://www.daemogorgon.net/software/ditrack-manager) :Date: $Date: 2008-10-23 20:50:07 +0200 (Do, 23. Okt 2008) $ :Revision: $Revision: 7 $ """ import os from os.path import isdir, isfile, join from subprocess import Popen import sys __version__ = '$Revision: 7 $' # configuration; change this to your machine config_path = join(os.environ['HOME'], '.mdt') projects = join(config_path, 'projects') current = join(config_path, 'current') dt = 'dt' def help(): print 'mdt help (%s)' % __version__ print print ' mdt -h|--help this help' print ' mdt -l|--list list projects' print ' mdt -c|--current show current active project path' print ' mdt set define current project path for DITrack' print ' mdt act|cat ... use DITrack commands with current project' print print print 'Specify projects and paths in ~/.mdt/projects:' print print ' project1 /home/juser/devel/project1' print ' project2 /home/juser/devel/project2' print def _write_file(name, content): try: f = file(name, 'w') f.write(content) finally: f.close() def _read_file(name): content = [] try: f = file(name, 'r') for line in f: if len(line.strip()): content.append(line) f.close() except IOError: return None return content def set(prj): # look for path in 'projects' file lines = _read_file(projects) for line in lines: parts = line.split() if parts[0] == prj: path = parts[1] _write_file(current, path) return print >>sys.stderr, 'Project %s not found.' % prj def get_current(): c = _read_file(current) if c: return c[0] else: return None def config_ok(): if not isdir(config_path): return False if not isfile(projects): return False return True def no_current_project_found(): print >>sys.stderr, 'No current project found.' print >>sys.stderr, "Define projects in %s and use '%s set ' to set a current project." % (projects, sys.argv[0]) print >>sys.stderr print >>sys.stderr, 'Example:' print >>sys.stderr print >>sys.stderr, ' project1 /home/juser/devel/project1' print >>sys.stderr, ' project2 /home/juser/devel/project2' def execute_dt(cmd): try: retcode = call(cmd, shell=True) except OSError, e: print >>sys.stderr, "Execution of '%s' failed: %s" % (cmd, e) def main(args): if len(args)==0 or args[0] in ['-h', '--help']: help() sys.exit(0) # check for config directory if not config_ok(): print >>sys.stderr, "Please create directory '%s' and file '%s' first." % (config_path, projects) print >>sys.stderr, "Then define project to directory mappings in '%s':" print >>sys.stderr print >>sys.stderr, ' project_name1 /home/to_user/path/to/project' print >>sys.stderr, ' project_name2 /home/to_user/path/to/another_project' sys.exit(1) elif len(args) > 0: if args[0] in ['-c', '--current']: # show current project current = get_current() if current: print current else: no_current_project_found() elif args[0] in ['-l', '--list']: # show all known projects print 'Known projects:' for prj in _read_file(projects): print prj[:-1] elif args[0] == 'set': if len(args) < 2: print >>sys.stderr, 'You must specify a project.' else: set(args[1]) else: current = get_current() if not current: no_current_project_found() sys.exit() cmd = dt + ' -d ' + current + ' ' + ' '.join(args), execute_dt(cmd) # skip script name main(sys.argv[1:]) # vim:tabstop=4:shiftwidth=4:smarttab:expandtab:softtabstop=4:autoindent