Exemple #1
0
def shared_course_get_git_credentials(ctx, course, username, password, no_save, delete_permissions, staging):
    if not staging:
        connstr = course.git_server_connstr
    else:
        connstr = course.git_staging_connstr
    
    if connstr is None or connstr == "":
        print "Course '%s' doesn't seem to be configured to use a Git server." % course.id
        ctx.exit(CHISUBMIT_FAIL)
        
    conn = RemoteRepositoryConnectionFactory.create_connection(connstr, staging = staging)
    server_type = conn.get_server_type_name()

    token, existing = conn.get_credentials(username, password, delete_repo = delete_permissions)

    if token is None:
        print "Unable to create token. Incorrect username/password."
    else:
        if not no_save:
            if ctx.obj['config']['git-credentials'] is None:
                ctx.obj['config']['git-credentials'] = {}
            ctx.obj['config']['git-credentials'][server_type] = token
            ctx.obj['config'].save()
        
        if existing:
            print "Your existing %s access token is: %s" % (server_type, token)
        else:
            print "The following %s access token has been created: %s" % (server_type, token)

        if not no_save:
            print "chisubmit has been configured to use this token from now on."

    return CHISUBMIT_SUCCESS
Exemple #2
0
def shared_course_get_git_credentials(ctx, course, username, password, no_save,
                                      delete_permissions, staging):
    if not staging:
        connstr = course.git_server_connstr
    else:
        connstr = course.git_staging_connstr

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

    conn = RemoteRepositoryConnectionFactory.create_connection(
        connstr,
        staging=staging,
        ssl_verify=ctx.obj['config'].get_ssl_verify())
    server_type = conn.get_server_type_name()

    token, existing = conn.get_credentials(username,
                                           password,
                                           delete_repo=delete_permissions)

    if token is None:
        print("Unable to create token. Incorrect username/password.")
    else:
        if not no_save:
            if ctx.obj['config']['git-credentials'] is None:
                ctx.obj['config']['git-credentials'] = {}
            ctx.obj['config']['git-credentials'][server_type] = token
            ctx.obj['config'].save()

        if existing:
            print("Your existing %s access token is: %s" %
                  (server_type, token))
        else:
            print("The following %s access token has been created: %s" %
                  (server_type, token))

        if not no_save:
            print(
                "chisubmit has been configured to use this token from now on.")

    return CHISUBMIT_SUCCESS
Exemple #3
0
def create_connection(course, config, staging = False):
    if not staging:
        connstr = course.options["git-server-connstr"]
    else:
        connstr = course.options["git-staging-connstr"]

    conn = RemoteRepositoryConnectionFactory.create_connection(connstr, staging)
    server_type = conn.get_server_type_name()
    
    git_credentials = None
    if config['git-credentials'] is not None:
        git_credentials = config['git-credentials'].get(server_type, None)

    if git_credentials is None:
        print "You do not have %s credentials." % server_type
        return None
    else:
        conn.connect(git_credentials)
        return conn
Exemple #4
0
def create_connection(course, config, staging=False):
    if not staging:
        connstr = course.options["git-server-connstr"]
    else:
        connstr = course.options["git-staging-connstr"]

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

    git_credentials = None
    if config['git-credentials'] is not None:
        git_credentials = config['git-credentials'].get(server_type, None)

    if git_credentials is None:
        print "You do not have %s credentials." % server_type
        return None
    else:
        conn.connect(git_credentials)
        return conn
Exemple #5
0
def shared_course_get_git_credentials(ctx, course, username, password, no_save,
                                      delete_permissions, staging):
    if not staging:
        connstr_field = "git-server-connstr"
    else:
        connstr_field = "git-staging-connstr"

    if not course.options.has_key(connstr_field):
        print "Course '%s' doesn't seem to be configured to use a Git server." % course.id
        ctx.exit(CHISUBMIT_FAIL)

    connstr = course.options[connstr_field]

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

    token, existing = conn.get_credentials(username,
                                           password,
                                           delete_repo=delete_permissions)

    if token is None:
        print "Unable to create token. Incorrect username/password."
    else:
        if not no_save:
            if ctx.obj['config']['git-credentials'] is None:
                ctx.obj['config']['git-credentials'] = {}
            ctx.obj['config']['git-credentials'][server_type] = token
            ctx.obj['config'].save()

        if existing:
            print "Your existing %s access token is: %s" % (server_type, token)
        else:
            print "The following %s access token has been created: %s" % (
                server_type, token)

        if not no_save:
            print "chisubmit has been configured to use this token from now on."

    return CHISUBMIT_SUCCESS
Exemple #6
0
def create_connection(course, config, staging = False):
    if not staging:
        connstr = course.git_server_connstr
    else:
        connstr = course.git_staging_connstr

    if connstr is None:
        print "The course's git server has not been configured."
        print "Please contact the course instructor or the chisubmit administrator."
        return None        

    conn = RemoteRepositoryConnectionFactory.create_connection(connstr, staging)
    server_type = conn.get_server_type_name()
    
    git_credentials = config.get_git_credentials(server_type)

    if git_credentials is None:
        print "You do not have %s credentials." % server_type
        return None
    else:
        conn.connect(git_credentials)
        return conn
Exemple #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()
    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()
Exemple #8
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