def main(argv=None, show_help=False): argv = argv or sys.argv[1:] # Set the script name to ``amp`` so that OptionParser error messages don't # display a meaningless ``main.py`` to end users. sys.argv[0] = 'amp' usage = ("""Usage: amp <command> [options] \nCommands: \n%s version show the version number and exit \nSee `amp help <command>` for more info on a specific command.""" % '\n'.join(" %-8s %s" % (cmd, COMMANDS[cmd].help) for cmd in sorted(COMMANDS)) ) autocomplete( OptionParser(add_help_option=False), ListCompleter(AUTOCOMPLETE_COMMANDS.keys()), subcommands=AUTOCOMPLETE_COMMANDS ) if not argv: show_help = True else: command = argv[0] argv = argv[1:] if command in ['-h', '--help']: show_help = True elif command == 'help': if argv: command = argv[0] argv = ['--help'] else: show_help = True if command in ['-v', '--version', 'version']: print('amp version %s' % ampify.__release__) sys.exit() if show_help: print(usage) sys.exit(1) if command in COMMANDS: return COMMANDS[command](argv) # We support git-command like behaviour. That is, if there's an external # binary named ``amp-foo`` available on the ``$PATH``, then running ``amp # foo`` will automatically delegate to it. try: output, retcode = run_command( ['amp-%s' % command] + argv, retcode=True, redirect_stdout=False, redirect_stderr=False ) except CommandNotFound: exit("ERROR: Unknown command %r" % command) if retcode: sys.exit(retcode)
def check(argv=None, completer=None): op = OptionParser(usage="Usage: amp check", add_help_option=False) options, args = parse_options(op, argv, completer) log("Checking the current revision id for your code.", PROGRESS) revision_id = do( 'git', 'show', '--pretty=oneline', '--summary', redirect_stdout=True ).split()[0] log("Checking the latest commits on GitHub.", PROGRESS) commit_info = urlopen( 'http://github.com/api/v2/json/commits/list/tav/ampify/master' ).read() latest_revision_id = decode_json(commit_info)['commits'][0]['id'] if revision_id != latest_revision_id: exit("A new version is available. Please run `git pull`.") log("Your checkout is up-to-date.", SUCCESS)
def init(argv=None, completer=None): op = OptionParser( usage="Usage: amp init <instance-name> [options]", add_help_option=False ) op.add_option('--clobber', dest='clobber', action='store_true', help="clobber any existing files/directories if they exist") op.add_option('--from', dest='git_repo', default='', help="initialise by cloning the given git repository") options, args = parse_options(op, argv, completer) git_repo = options.git_repo if git_repo: if args: instance_name = args[0] else: instance_name = git_repo.split('/')[-1].rsplit('.', 1)[0] if not instance_name: exit(ERRMSG_GIT_NAME_DETECTION) else: if args: instance_name = args[0] else: op.print_help() sys.exit(1) instance_name, instance_root = normalise_instance_name(instance_name) clobber = options.clobber if exists(instance_root): if not clobber: exit( "ERROR: A directory already exists at %s\n " "Use the --clobber parameter to overwrite the directory" % instance_root ) chdir(instance_root) diff = do('git', 'diff', '--cached', '--name-only', redirect_stdout=1) if diff.strip(): error( "ERROR: You have a dirty working tree at %s\n " "Please either commit your changes or move your files.\n" % instance_root ) error(" These are the problematic files:") for filename in diff.strip().splitlines(): log(" %s" % filename, ERROR) print first_run = 0 else: create = query("Create a instance at %s" % instance_root).lower() if not create.startswith('y'): sys.exit(2) makedirs(instance_root) print print("Created %s" % instance_root) print chdir(instance_root) do('git', 'init') print readme = open('README.md', 'wb') readme.close() do('git', 'add', 'README.md') do('git', 'commit', '-m', "Initialised the instance [amp].") first_run = 1 diff = do('git', 'diff', '--cached', '--name-only', redirect_stdout=1) if diff.strip(): do('git', 'commit', '-m', "Updated instance [amp].") print(DEBUG)