示例#1
0
def __load_config_and_client(require_local):
    ctx = get_current_context()

    try:
        ctx.obj["config"] = Config.get_config(ctx.obj["config_dir"],
                                              ctx.obj["work_dir"],
                                              ctx.obj["config_overrides"])
    except ConfigDirectoryNotFoundException:
        if not require_local:
            ctx.obj["config"] = Config.get_global_config(
                ctx.obj["config_overrides"])
        else:
            raise ChisubmitException(
                "This command must be run in a directory configured to use chisubmit."
            )

    api_url = ctx.obj["config"].get_api_url()
    api_key = ctx.obj["config"].get_api_key()
    ssl_verify = ctx.obj["config"].get_ssl_verify()

    if api_url is None:
        raise ChisubmitException("Configuration value 'api-url' not found")

    if api_key is None:
        raise ChisubmitException("No chisubmit credentials were found!")

    ctx.obj["client"] = Chisubmit(api_key,
                                  base_url=api_url,
                                  ssl_verify=ssl_verify)
示例#2
0
def chisubmit_cmd(ctx, conf, dir, course, verbose, debug, testing):
    global VERBOSE, DEBUG

    VERBOSE = verbose
    DEBUG = debug

    ctx.obj = {}

    config = Config(dir, conf)
    log.init_logging(verbose, debug)

    if not config['api-key']:
        raise click.BadParameter("Sorry, can't find your chisubmit api token")

    if testing:
        from chisubmit.backend.webapp.api import app
        session.connect_test(app, access_token=config['api-key'])
    else:
        session.connect(config['api-url'], config['api-key'])

    if course:
        course_specified = True
        course_id = course
    else:
        course_specified = False
        course_id = config['default-course']

    ctx.obj["course_specified"] = course_specified
    ctx.obj["course_id"] = course_id
    ctx.obj["config"] = config
    ctx.obj["verbose"] = verbose
    ctx.obj["debug"] = debug

    return CHISUBMIT_SUCCESS
示例#3
0
def chisubmit_get_credentials_cmd(ctx, conf, dir, verbose, debug, user,
                                  password, url, no_save, reset, testing):
    global VERBOSE, DEBUG

    VERBOSE = verbose
    DEBUG = debug

    config = Config(dir, conf)

    server_url = None
    if testing:
        from chisubmit.backend.webapp.api import app
        session.connect_test(app)
    else:
        if config['api-url'] is None and url is None:
            print "No server URL specified. Please add it to your chisubmit.conf file"
            print "or use the --url option"
            ctx.exit(CHISUBMIT_FAIL)

        if url is not None:
            server_url = url
        else:
            server_url = config['api-url']

        session.connect(server_url)

    try:
        token, exists_prior, is_new = User.get_token(user, password, reset)
    except HTTPError, he:
        if he.response.status_code == 401:
            print "ERROR: Incorrect username/password"
            ctx.exit(CHISUBMIT_FAIL)
        else:
            raise
示例#4
0
def chisubmit_server_cmd(ctx, conf, dir, verbose, debug):
    ctx.obj = {}

    config = Config(dir, conf)
    log.init_logging(verbose, debug)

    ctx.obj["config"] = config

    return CHISUBMIT_SUCCESS
示例#5
0
def __load_config_and_client(require_local):
    ctx = get_current_context()
    
    try:
        ctx.obj["config"] = Config.get_config(ctx.obj["config_dir"], ctx.obj["work_dir"], ctx.obj["config_overrides"])
    except ConfigDirectoryNotFoundException:
        if not require_local:
            ctx.obj["config"] = Config.get_global_config(ctx.obj["config_overrides"])
        else:
            raise ChisubmitException("This command must be run in a directory configured to use chisubmit.")

    api_url = ctx.obj["config"].get_api_url()
    api_key = ctx.obj["config"].get_api_key()
    
    if api_url is None:
        raise ChisubmitException("Configuration value 'api-url' not found")

    if api_key is None:
        raise ChisubmitException("No chisubmit credentials were found!")

    ctx.obj["client"] = Chisubmit(api_key, base_url=api_url)    
示例#6
0
def chisubmit_init(ctx, course_id, username, password, git_username, git_password, force):
                
    if ctx.obj["config_dir"] is None and ctx.obj["work_dir"] is None:
        try:
            config = Config.get_config()
            if not force:
                in_root = os.path.normpath(config.work_dir) == os.path.normpath(os.getcwd())
                
                if in_root:
                    print("The current directory is already configured to use chisubmit.")
                else:
                    print("You are already inside a directory configured to use chisubmit.")
                    print("Root directory: {}".format(config.work_dir))
                print()
                if in_root:
                    print("If you're sure you want to reset the configuration of this directory,")
                    print("use the --force option")
                else:
                    print("If you're sure you want to create another chisubmit directory")
                    print("in the current directory, use the --force option")
                ctx.exit(CHISUBMIT_FAIL)
        except ConfigDirectoryNotFoundException:
            pass
    else:
        if not os.path.exists(ctx.obj["work_dir"]):
            print("The specified work directory does not exist: {}".format(ctx.obj["work_dir"]))
            ctx.exit(CHISUBMIT_FAIL)      

        if os.path.exists(ctx.obj["config_dir"]) and not force:
            print("The specified configuration directory already exists: {}".format(ctx.obj["config_dir"]))
            print("If you're sure you want to create a configuration directory there,")
            print("use the --force option.")
            ctx.exit(CHISUBMIT_FAIL)      

    
    global_config = Config.get_global_config(config_overrides = ctx.obj["config_overrides"])
    
    api_url = global_config.get_api_url()
    api_key = global_config.get_api_key()
    ssl_verify = global_config.get_ssl_verify()
    
    if api_url is None:
        print("The 'api-url' configuration option is not set. I need this to connect to")
        print("the chisubmit server. If your instructor did not set this option")
        print("globally, you need to specify it like this:")
        print()
        print("    chisubmit -c api-url=CHISUBMIT_API_URL init")
        print()
        print("Where CHISUBMIT_API_URL should be replaced with the URL of the chisubmit server.")
        print("Your instructor can provide you with the correct URL.")
        ctx.exit(CHISUBMIT_FAIL)
        
    if api_key is None:
        guess_user = getpass.getuser()
        
        user_prompt = "Enter your chisubmit username [{}]: ".format(guess_user)
        password_prompt = "Enter your chisubmit password: "******"ERROR: Incorrect username/password")
            ctx.exit(CHISUBMIT_FAIL)
    
    client = Chisubmit(api_key, base_url=api_url, ssl_verify=ssl_verify)
    
    if course_id is not None:
        try:
            course = client.get_course(course_id = course_id)
        except UnknownObjectException:
            print("Cannot access course '{}'".format(course_id))
            print("This could mean the course does not exist, or you have not been added to this course.")            
            ctx.exit(CHISUBMIT_FAIL)
    else:
        courses = client.get_courses()
        if len(courses) == 0:
            print("You do not have access to any courses on chisubmit.")
            print("This could you have not been added to any course, or no courses have been created.")
            ctx.exit(CHISUBMIT_FAIL)
            
        print()
        print("You are a member of the following course(s).")
        print("Please select the one you want to use:")
        print()
        n = 1
        for course in courses:
            print("[{}] {}: {}".format(n, course.course_id, course.name))
            n+=1
        print()
        print("[X] Exit")
        print()
        valid_options = [repr(x) for x in range(1, len(courses)+1)] + ['X', 'x']
        option = None
        while option not in valid_options:
            option = input("Choose one: ")
            if option not in valid_options:
                print("'{}' is not a valid option!".format(option))
                print()
        
        if option in ['X', 'x']:
            ctx.exit(CHISUBMIT_FAIL)
        else:
            course = courses[int(option)-1]
    
    connstr = course.git_server_connstr
    
    if connstr is None or connstr == "":
        print("Error: Course '{}' doesn't seem to be configured to use a Git server." % course.id)
        ctx.exit(CHISUBMIT_FAIL)
        
    conn = RemoteRepositoryConnectionFactory.create_connection(connstr, staging = False, ssl_verify=ssl_verify)
    server_type = conn.get_server_type_name()

    # If there are global credentials defined, see if they work
    git_credentials = global_config.get_git_credentials(server_type)
    if git_credentials is not None:
        try:
            conn.connect(git_credentials)
        except ChisubmitException:
            git_credentials = None

    # If the existing credentials didn't work, get new ones
    if git_credentials is None:
        if username is not None and password is not None:
            # Try the chisubmit username/password
            git_credentials, _ = conn.get_credentials(username, password)
        
        if git_credentials is None:
            user_prompt = "Enter your {} username: "******"Enter your {} password: "******"Unable to obtain {} credentials. Incorrect username/password.".format(server_type))

    config = Config.create_config_dir(ctx.obj["config_dir"], ctx.obj["work_dir"])
    
    # Always save the API URL locally
    config.set_api_url(api_url, where = Config.LOCAL)
    
    # Save the API key only if one was not found globally
    if config.get_api_key() is None:
        config.set_api_key(api_key, where = Config.LOCAL)
        
    # Always save the course locally
    config.set_course(course.course_id, where = Config.LOCAL)
    
    # Save the git credentials only if they're not the same as the global ones
    if config.get_git_credentials(server_type) != git_credentials:
        config.set_git_credentials(server_type, git_credentials, where = Config.LOCAL)
    
    print()
    print("The following directory has been set up to use chisubmit")
    print("for course '{}' ({})".format(course.course_id, course.name))
    print()
    print("   " + config.work_dir)
    print()
    print("Remember to run chisubmit only inside this directory")
    print("(or any of its subdirectories)")
    print()
示例#7
0
def chisubmit_init(ctx, course_id, username, password, git_username,
                   git_password, force):

    if ctx.obj["config_dir"] is None and ctx.obj["work_dir"] is None:
        try:
            config = Config.get_config()
            if not force:
                in_root = os.path.normpath(
                    config.work_dir) == os.path.normpath(os.getcwd())

                if in_root:
                    print "The current directory is already configured to use chisubmit."
                else:
                    print "You are already inside a directory configured to use chisubmit."
                    print "Root directory: {}".format(config.work_dir)
                print
                if in_root:
                    print "If you're sure you want to reset the configuration of this directory,"
                    print "use the --force option"
                else:
                    print "If you're sure you want to create another chisubmit directory"
                    print "in the current directory, use the --force option"
                ctx.exit(CHISUBMIT_FAIL)
        except ConfigDirectoryNotFoundException:
            pass
    else:
        if not os.path.exists(ctx.obj["work_dir"]):
            print "The specified work directory does not exist: {}".format(
                ctx.obj["work_dir"])
            ctx.exit(CHISUBMIT_FAIL)

        if os.path.exists(ctx.obj["config_dir"]) and not force:
            print "The specified configuration directory already exists: {}".format(
                ctx.obj["config_dir"])
            print "If you're sure you want to create a configuration directory there,"
            print "use the --force option."
            ctx.exit(CHISUBMIT_FAIL)

    global_config = Config.get_global_config(
        config_overrides=ctx.obj["config_overrides"])

    api_url = global_config.get_api_url()
    api_key = global_config.get_api_key()

    if api_url is None:
        print "The 'api-url' configuration option is not set. I need this to connect to"
        print "the chisubmit server. If your instructor did not set this option"
        print "globally, you need to specify it like this:"
        print
        print "    chisubmit -c api-url=CHISUBMIT_API_URL init"
        print
        print "Where CHISUBMIT_API_URL should be replaced with the URL of the chisubmit server."
        print "Your instructor can provide you with the correct URL."
        ctx.exit(CHISUBMIT_FAIL)

    if api_key is None:
        guess_user = getpass.getuser()

        user_prompt = "Enter your chisubmit username [{}]: ".format(guess_user)
        password_prompt = "Enter your chisubmit password: "******"ERROR: Incorrect username/password"
            ctx.exit(CHISUBMIT_FAIL)

    client = Chisubmit(api_key, base_url=api_url)

    if course_id is not None:
        try:
            course = client.get_course(course_id=course_id)
        except UnknownObjectException:
            print "Cannot access course '{}'".format(course_id)
            print "This could mean the course does not exist, or you have not been added to this course."
            ctx.exit(CHISUBMIT_FAIL)
    else:
        courses = client.get_courses()
        if len(courses) == 0:
            print "You do not have access to any courses on chisubmit."
            print "This could you have not been added to any course, or no courses have been created."
            ctx.exit(CHISUBMIT_FAIL)

        print
        print "You are a member of the following course(s)."
        print "Please select the one you want to use:"
        print
        n = 1
        for course in courses:
            print "[{}] {}: {}".format(n, course.course_id, course.name)
            n += 1
        print
        print "[X] Exit"
        print
        valid_options = [ ` x ` for x in range(1,
                                               len(courses) + 1)] + ['X', 'x']
        option = None
        while option not in valid_options:
            option = raw_input("Choose one: ")
            if option not in valid_options:
                print "'{}' is not a valid option!".format(option)
                print

        if option in ['X', 'x']:
            ctx.exit(CHISUBMIT_FAIL)
        else:
            course = courses[int(option) - 1]

    connstr = course.git_server_connstr

    if connstr is None or connstr == "":
        print "Error: Course '{}' doesn't seem to be configured to use a Git server." % course.id
        ctx.exit(CHISUBMIT_FAIL)

    conn = RemoteRepositoryConnectionFactory.create_connection(connstr,
                                                               staging=False)
    server_type = conn.get_server_type_name()

    # If there are global credentials defined, see if they work
    git_credentials = global_config.get_git_credentials(server_type)
    if git_credentials is not None:
        try:
            conn.connect(git_credentials)
        except ChisubmitException:
            git_credentials = None

    # If the existing credentials didn't work, get new ones
    if git_credentials is None:
        # Try the chisubmit username/password
        git_credentials, _ = conn.get_credentials(username, password)

        if git_credentials is None:
            user_prompt = "Enter your {} username: "******"Enter your {} password: "******"Unable to obtain {} credentials. Incorrect username/password.".format(
                    server_type)

    config = Config.create_config_dir(ctx.obj["config_dir"],
                                      ctx.obj["work_dir"])

    # Always save the API URL locally
    config.set_api_url(api_url, where=Config.LOCAL)

    # Save the API key only if one was not found globally
    if config.get_api_key() is None:
        config.set_api_key(api_key, where=Config.LOCAL)

    # Always save the course locally
    config.set_course(course.course_id, where=Config.LOCAL)

    # Save the git credentials only if they're not the same as the global ones
    if config.get_git_credentials(server_type) != git_credentials:
        config.set_git_credentials(server_type,
                                   git_credentials,
                                   where=Config.LOCAL)

    print
    print "The following directory has been set up to use chisubmit"
    print "for course '{}' ({})".format(course.course_id, course.name)
    print
    print "   " + config.work_dir
    print
    print "Remember to run chisubmit only inside this directory"
    print "(or any of its subdirectories)"
    print