2009-10-26 10:54:37 +03:00
|
|
|
#! /usr/bin/env python
|
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
# hm.py: 'Help Me', is a simple wrapper for all build tools.
|
2009-10-26 10:54:37 +03:00
|
|
|
#
|
|
|
|
# This script was created for the Synergy+ project.
|
|
|
|
# http://code.google.com/p/synergy-plus
|
|
|
|
#
|
2010-04-10 20:04:07 +04:00
|
|
|
# The idea behind this is to simplify the build system,
|
|
|
|
# however, it's not a dependancy of building Synergy+.
|
|
|
|
# In other words, you don't need to use this script!
|
2009-10-26 10:54:37 +03:00
|
|
|
#
|
|
|
|
# If you don't wish to run this script, simply run:
|
|
|
|
# cmake .
|
|
|
|
# make
|
|
|
|
# This will create an in-source UNIX Makefile.
|
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
import sys, os
|
|
|
|
from build import commands
|
2009-10-26 10:54:37 +03:00
|
|
|
|
2010-04-11 06:22:15 +04:00
|
|
|
# list of valid commands as keys. the values are optarg strings, but most
|
|
|
|
# are None for now (this is mainly for extensibility)
|
|
|
|
cmd_dict = {
|
|
|
|
'about' : [None, []],
|
|
|
|
'setup' : [None, []],
|
|
|
|
'configure' : [None, []],
|
|
|
|
'build' : ['dr', []],
|
|
|
|
'clean' : ['dr', []],
|
|
|
|
'update' : [None, []],
|
|
|
|
'install' : [None, []],
|
|
|
|
'package' : [None, []],
|
|
|
|
'destroy' : [None, []],
|
|
|
|
'kill' : [None, []],
|
|
|
|
'usage' : [None, []],
|
|
|
|
'revision' : [None, []],
|
|
|
|
'hammer' : [None, []],
|
|
|
|
'reformat' : [None, []],
|
|
|
|
'open' : [None, []],
|
|
|
|
}
|
2009-10-26 10:54:37 +03:00
|
|
|
|
2010-04-11 06:22:15 +04:00
|
|
|
# aliases to valid commands
|
2010-04-10 20:04:07 +04:00
|
|
|
cmd_alias_dict = {
|
2010-04-11 06:22:15 +04:00
|
|
|
'info' : 'about',
|
2010-04-10 20:04:07 +04:00
|
|
|
'help' : 'usage',
|
|
|
|
'dist' : 'package',
|
|
|
|
'make' : 'build',
|
|
|
|
'cmake' : 'configure',
|
2009-10-26 10:54:37 +03:00
|
|
|
}
|
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
def complete_command(arg):
|
|
|
|
completions = []
|
2009-10-26 10:54:37 +03:00
|
|
|
|
2010-04-11 06:22:15 +04:00
|
|
|
for cmd, optarg in cmd_dict.iteritems():
|
2010-04-10 20:04:07 +04:00
|
|
|
if cmd.startswith(arg):
|
|
|
|
completions.append(cmd)
|
2009-10-26 10:54:37 +03:00
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
for alias, cmd in cmd_alias_dict.iteritems():
|
|
|
|
if alias.startswith(arg):
|
|
|
|
completions.append(alias)
|
2009-10-26 10:54:37 +03:00
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
return completions
|
2009-10-26 10:54:37 +03:00
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
def start_cmd(argv):
|
2010-04-11 06:22:15 +04:00
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
cmd_arg = ''
|
2009-10-26 10:54:37 +03:00
|
|
|
if len(argv) > 1:
|
2010-04-10 20:04:07 +04:00
|
|
|
cmd_arg = argv[1]
|
|
|
|
|
|
|
|
# change common help args to help command
|
|
|
|
if cmd_arg in ('--help', '-h', '--usage', '-u', '/?'):
|
|
|
|
cmd_arg = 'usage'
|
2009-10-26 10:54:37 +03:00
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
completions = complete_command(cmd_arg)
|
|
|
|
|
2010-04-11 06:22:15 +04:00
|
|
|
if cmd_arg and len(completions) > 0:
|
2009-10-26 10:54:37 +03:00
|
|
|
|
|
|
|
if len(completions) == 1:
|
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
# get the only completion (since in this case we have 1)
|
2009-10-26 10:54:37 +03:00
|
|
|
cmd = completions[0]
|
|
|
|
|
2010-04-11 06:22:15 +04:00
|
|
|
# build up the first part of the map (for illustrative purposes)
|
2010-04-10 20:04:07 +04:00
|
|
|
cmd_map = list()
|
|
|
|
if cmd_arg != cmd:
|
|
|
|
cmd_map.append(cmd_arg)
|
|
|
|
cmd_map.append(cmd)
|
|
|
|
|
2010-04-11 06:22:15 +04:00
|
|
|
# map an alias to the command, and build up the map
|
2010-04-10 20:04:07 +04:00
|
|
|
if cmd in cmd_alias_dict.keys():
|
|
|
|
alias = cmd
|
|
|
|
if cmd_arg == cmd:
|
|
|
|
cmd_map.append(alias)
|
|
|
|
cmd = cmd_alias_dict[cmd]
|
|
|
|
cmd_map.append(cmd)
|
|
|
|
|
2010-04-11 06:22:15 +04:00
|
|
|
# show command map to avoid confusion
|
2010-04-10 20:04:07 +04:00
|
|
|
if len(cmd_map) != 0:
|
|
|
|
print 'Mapping command: %s' % ' -> '.join(cmd_map)
|
|
|
|
|
2010-04-11 06:22:15 +04:00
|
|
|
# pass args and optarg data to command handler, which figures out
|
|
|
|
# how to handle the arguments
|
|
|
|
optarg_data = cmd_dict[cmd]
|
|
|
|
handler = commands.CommandHandler(argv[2:], optarg_data)
|
2010-04-10 20:04:07 +04:00
|
|
|
|
2010-04-11 06:22:15 +04:00
|
|
|
# use reflection to get the function pointer
|
|
|
|
cmd_func = getattr(handler, cmd)
|
|
|
|
cmd_func()
|
|
|
|
return 0
|
2010-04-10 20:04:07 +04:00
|
|
|
|
2009-10-26 10:54:37 +03:00
|
|
|
else:
|
2010-04-11 06:22:15 +04:00
|
|
|
print (
|
|
|
|
'Command `%s` too ambiguous, '
|
2009-10-26 10:54:37 +03:00
|
|
|
'could mean any of: %s'
|
2010-04-10 20:04:07 +04:00
|
|
|
) % (cmd_arg, ', '.join(completions))
|
2009-10-26 10:54:37 +03:00
|
|
|
else:
|
|
|
|
|
|
|
|
if len(argv) == 1:
|
|
|
|
print 'No command specified, showing usage.\n'
|
|
|
|
else:
|
2010-04-10 20:04:07 +04:00
|
|
|
print 'Command not recognised: %s\n' % cmd_arg
|
2009-10-26 10:54:37 +03:00
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
commands.usage(argv[2:])
|
2009-11-03 00:17:45 +03:00
|
|
|
|
2010-04-11 06:22:15 +04:00
|
|
|
# generic error code if not returned sooner
|
|
|
|
return 1
|
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
def main(argv):
|
2009-11-03 00:17:45 +03:00
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
if sys.version_info < (2, 4):
|
|
|
|
print 'Python version must be at least: 2.4'
|
|
|
|
sys.exit(1)
|
2009-11-03 00:17:45 +03:00
|
|
|
|
2010-04-10 20:04:07 +04:00
|
|
|
try:
|
2010-04-11 06:22:15 +04:00
|
|
|
sys.exit(start_cmd(argv))
|
2010-04-10 20:04:07 +04:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print '\n\nUser aborted, exiting.'
|
2009-12-27 23:12:41 +03:00
|
|
|
|
2009-10-26 10:54:37 +03:00
|
|
|
# Start the program.
|
|
|
|
main(sys.argv)
|