예제 #1
0
    def test_dirty_repo(self):
        """
        Add additional items not in the repo and make sure they aren't
        there after the export. This allows old content to removed
        in the repo.
        """
        repo_name = 'dirty_repo1'
        self.make_bare_repo_with_course(repo_name)
        git_export_utils.export_to_git(self.course.id,
                                       self.course_module.giturl, self.user)

        # Make arbitrary change to course to make diff
        self.course_module.matlab_api_key = 'something'
        modulestore().update_item(self.course_module, self.user.id)
        # Touch a file in the directory, export again, and make sure
        # the test file is gone
        repo_dir = os.path.join(
            os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR),
            repo_name
        )
        test_file = os.path.join(repo_dir, 'test.txt')
        open(test_file, 'a').close()
        self.assertTrue(os.path.isfile(test_file))
        git_export_utils.export_to_git(self.course.id,
                                       self.course_module.giturl, self.user)
        self.assertFalse(os.path.isfile(test_file))
예제 #2
0
 def test_bad_course_id(self):
     """
     Test valid git url, but bad course.
     """
     with self.assertRaisesRegexp(GitExportError, str(GitExportError.BAD_COURSE)):
         git_export_utils.export_to_git(
             '', 'file://{0}'.format(self.bare_repo_dir), '', '/blah')
예제 #3
0
    def handle(self, *args, **options):
        """
        Checks arguments and runs export function if they are good
        """

        if len(args) != 2:
            raise CommandError('This script requires exactly two arguments: '
                               'course_loc and git_url')

        # Rethrow GitExportError as CommandError for SystemExit
        try:
            course_key = CourseKey.from_string(args[0])
        except InvalidKeyError:
            try:
                course_key = SlashSeparatedCourseKey.from_deprecated_string(args[0])
            except InvalidKeyError:
                raise CommandError(unicode(GitExportError.BAD_COURSE))

        try:
            git_export_utils.export_to_git(
                course_key,
                args[1],
                options.get('user', ''),
                options.get('rdir', None)
            )
        except git_export_utils.GitExportError as ex:
            raise CommandError(unicode(ex.message))
예제 #4
0
def export_git(request, org, course, name):
    """
    This method serves up the 'Export to Git' page
    """
    location = Location('i4x', org, course, 'course', name)
    if not has_course_access(request.user, location):
        raise PermissionDenied()
    course_module = modulestore().get_item(location)
    failed = False

    log.debug('export_git course_module=%s', course_module)

    msg = ""
    if 'action' in request.GET and course_module.giturl:
        if request.GET['action'] == 'push':
            try:
                git_export_utils.export_to_git(
                    course_module.id,
                    course_module.giturl,
                    request.user,
                )
                msg = _('Course successfully exported to git repository')
            except git_export_utils.GitExportError as ex:
                failed = True
                msg = str(ex)

    return render_to_response('export_git.html', {
        'context_course': course_module,
        'msg': msg,
        'failed': failed,
    })
예제 #5
0
def export_git(request, course_key_string):
    """
    This method serves up the 'Export to Git' page
    """
    course_key = CourseKey.from_string(course_key_string)
    if not has_course_author_access(request.user, course_key):
        raise PermissionDenied()

    course_module = modulestore().get_course(course_key)
    failed = False

    log.debug('export_git course_module=%s', course_module)

    msg = ""
    if 'action' in request.GET and course_module.giturl:
        if request.GET['action'] == 'push':
            try:
                git_export_utils.export_to_git(
                    course_module.id,
                    course_module.giturl,
                    request.user,
                )
                msg = _('Course successfully exported to git repository')
            except git_export_utils.GitExportError as ex:
                failed = True
                msg = unicode(ex)

    return render_to_response('export_git.html', {
        'context_course': course_module,
        'msg': msg,
        'failed': failed,
    })
예제 #6
0
    def test_dirty_repo(self):
        """
        Add additional items not in the repo and make sure they aren't
        there after the export. This allows old content to removed
        in the repo.
        """
        repo_name = 'dirty_repo1'
        self.make_bare_repo_with_course(repo_name)
        git_export_utils.export_to_git(self.course.id,
                                       self.course_module.giturl, self.user)

        # Make arbitrary change to course to make diff
        self.course_module.matlab_api_key = 'something'
        modulestore().update_item(self.course_module, self.user.id)
        # Touch a file in the directory, export again, and make sure
        # the test file is gone
        repo_dir = os.path.join(
            os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR),
            repo_name
        )
        test_file = os.path.join(repo_dir, 'test.txt')
        open(test_file, 'a').close()
        self.assertTrue(os.path.isfile(test_file))
        git_export_utils.export_to_git(self.course.id,
                                       self.course_module.giturl, self.user)
        self.assertFalse(os.path.isfile(test_file))
예제 #7
0
    def handle(self, *args, **options):
        """
        Checks arguments and runs export function if they are good
        """

        if len(args) != 2:
            raise CommandError('This script requires exactly two arguments: '
                               'course_loc and git_url')

        # Rethrow GitExportError as CommandError for SystemExit
        try:
            course_key = CourseKey.from_string(args[0])
        except InvalidKeyError:
            try:
                course_key = SlashSeparatedCourseKey.from_deprecated_string(
                    args[0])
            except InvalidKeyError:
                raise CommandError(unicode(GitExportError.BAD_COURSE))

        try:
            git_export_utils.export_to_git(course_key, args[1],
                                           options.get('user', ''),
                                           options.get('rdir', None))
        except git_export_utils.GitExportError as ex:
            raise CommandError(unicode(ex.message))
예제 #8
0
    def test_git_ident(self):
        """
        Test valid course with and without user specified.

        Test skipped if git global config override environment variable GIT_CONFIG
        is set.
        """
        git_export_utils.export_to_git(self.course.id,
                                       'file://{0}'.format(self.bare_repo_dir),
                                       'enigma')
        expect_string = '{0}|{1}\n'.format(
            git_export_utils.GIT_EXPORT_DEFAULT_IDENT['name'],
            git_export_utils.GIT_EXPORT_DEFAULT_IDENT['email'])
        cwd = os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR /
                              'test_bare')
        git_log = subprocess.check_output(
            ['git', 'log', '-1', '--format=%an|%ae'], cwd=cwd)
        self.assertEqual(expect_string, git_log)

        # Make changes to course so there is something to commit
        self.populate_course()
        git_export_utils.export_to_git(self.course.id,
                                       'file://{0}'.format(self.bare_repo_dir),
                                       self.user.username)
        expect_string = '{0}|{1}\n'.format(
            self.user.username,
            self.user.email,
        )
        git_log = subprocess.check_output(
            ['git', 'log', '-1', '--format=%an|%ae'], cwd=cwd)
        self.assertEqual(expect_string, git_log)
예제 #9
0
 def test_bad_course_id(self):
     """
     Test valid git url, but bad course.
     """
     with self.assertRaisesRegexp(GitExportError, str(GitExportError.BAD_COURSE)):
         git_export_utils.export_to_git(
             '', 'file://{0}'.format(self.bare_repo_dir), '', '/blah')
예제 #10
0
    def test_git_ident(self):
        """
        Test valid course with and without user specified.

        Test skipped if git global config override environment variable GIT_CONFIG
        is set.
        """
        git_export_utils.export_to_git(
            self.course.id,
            'file://{0}'.format(self.bare_repo_dir),
            'enigma'
        )
        expect_string = '{0}|{1}\n'.format(
            git_export_utils.GIT_EXPORT_DEFAULT_IDENT['name'],
            git_export_utils.GIT_EXPORT_DEFAULT_IDENT['email']
        )
        cwd = os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR / 'test_bare')
        git_log = subprocess.check_output(['git', 'log', '-1',
                                           '--format=%an|%ae'], cwd=cwd)
        self.assertEqual(expect_string, git_log)

        # Make changes to course so there is something to commit
        self.populate_course()
        git_export_utils.export_to_git(
            self.course.id,
            'file://{0}'.format(self.bare_repo_dir),
            self.user.username
        )
        expect_string = '{0}|{1}\n'.format(
            self.user.username,
            self.user.email,
        )
        git_log = subprocess.check_output(
            ['git', 'log', '-1', '--format=%an|%ae'], cwd=cwd)
        self.assertEqual(expect_string, git_log)
예제 #11
0
    def test_no_change(self):
        """
        Test response if there are no changes
        """
        git_export_utils.export_to_git(self.course.id, "file://{0}".format(self.bare_repo_dir))

        with self.assertRaisesRegexp(GitExportError, unicode(GitExportError.CANNOT_COMMIT)):
            git_export_utils.export_to_git(self.course.id, "file://{0}".format(self.bare_repo_dir))
예제 #12
0
    def test_no_change(self):
        """
        Test response if there are no changes
        """
        git_export_utils.export_to_git(self.course.id,
                                       'file://{0}'.format(self.bare_repo_dir))

        with self.assertRaisesRegexp(GitExportError,
                                     str(GitExportError.CANNOT_COMMIT)):
            git_export_utils.export_to_git(
                self.course.id, 'file://{0}'.format(self.bare_repo_dir))
예제 #13
0
    def handle(self, *args, **options):
        """
        Checks arguments and runs export function if they are good
        """

        if len(args) != 2:
            raise CommandError('This script requires exactly two arguments: '
                               'course_loc and git_url')

        # Rethrow GitExportError as CommandError for SystemExit
        try:
            git_export_utils.export_to_git(args[0], args[1],
                                           options.get('user', ''),
                                           options.get('rdir', None))
        except git_export_utils.GitExportError as ex:
            raise CommandError(str(ex))
예제 #14
0
    def handle(self, *args, **options):
        """
        Checks arguments and runs export function if they are good
        """
        # Rethrow GitExportError as CommandError for SystemExit
        try:
            course_key = CourseKey.from_string(options['course_loc'])
        except InvalidKeyError:
            raise CommandError(text_type(GitExportError.BAD_COURSE))

        try:
            git_export_utils.export_to_git(course_key, options['git_url'],
                                           options.get('user', ''),
                                           options.get('rdir', None))
        except git_export_utils.GitExportError as ex:
            raise CommandError(text_type(ex))
예제 #15
0
    def handle(self, *args, **options):
        """
        Checks arguments and runs export function if they are good
        """
        # Rethrow GitExportError as CommandError for SystemExit
        try:
            course_key = CourseKey.from_string(options['course_loc'])
        except InvalidKeyError:
            raise CommandError(text_type(GitExportError.BAD_COURSE))

        try:
            git_export_utils.export_to_git(
                course_key,
                options['git_url'],
                options.get('user', ''),
                options.get('rdir', None)
            )
        except git_export_utils.GitExportError as ex:
            raise CommandError(text_type(ex))
예제 #16
0
    def handle(self, *args, **options):
        """
        Checks arguments and runs export function if they are good
        """

        if len(args) != 2:
            raise CommandError('This script requires exactly two arguments: '
                               'course_loc and git_url')

        # Rethrow GitExportError as CommandError for SystemExit
        try:
            git_export_utils.export_to_git(
                args[0],
                args[1],
                options.get('user', ''),
                options.get('rdir', None)
            )
        except git_export_utils.GitExportError as ex:
            raise CommandError(str(ex))
예제 #17
0
    def test_git_ident(self):
        """
        Test valid course with and without user specified.

        Test skipped if git global config override environment variable GIT_CONFIG
        is set.
        """
        git_export_utils.export_to_git(self.course.id, "file://{0}".format(self.bare_repo_dir), "enigma")
        expect_string = "{0}|{1}\n".format(
            git_export_utils.GIT_EXPORT_DEFAULT_IDENT["name"], git_export_utils.GIT_EXPORT_DEFAULT_IDENT["email"]
        )
        cwd = os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR / "test_bare")
        git_log = subprocess.check_output(["git", "log", "-1", "--format=%an|%ae"], cwd=cwd)
        self.assertEqual(expect_string, git_log)

        # Make changes to course so there is something to commit
        self.populate_course()
        git_export_utils.export_to_git(self.course.id, "file://{0}".format(self.bare_repo_dir), self.user.username)
        expect_string = "{0}|{1}\n".format(self.user.username, self.user.email)
        git_log = subprocess.check_output(["git", "log", "-1", "--format=%an|%ae"], cwd=cwd)
        self.assertEqual(expect_string, git_log)
예제 #18
0
    def test_bad_git_repos(self):
        """
        Test invalid git repos
        """
        test_repo_path = '{}/test_repo'.format(
            git_export_utils.GIT_REPO_EXPORT_DIR)
        self.assertFalse(os.path.isdir(test_repo_path))
        course_key = SlashSeparatedCourseKey('foo', 'blah', '100-')
        # Test bad clones
        with self.assertRaisesRegexp(GitExportError,
                                     str(GitExportError.CANNOT_PULL)):
            git_export_utils.export_to_git(
                course_key, 'https://*****:*****@example.com/test_repo.git')
        self.assertFalse(os.path.isdir(test_repo_path))

        # Setup good repo with bad course to test xml export
        with self.assertRaisesRegexp(GitExportError,
                                     str(GitExportError.XML_EXPORT_FAIL)):
            git_export_utils.export_to_git(
                course_key, 'file://{0}'.format(self.bare_repo_dir))

        # Test bad git remote after successful clone
        with self.assertRaisesRegexp(GitExportError,
                                     str(GitExportError.CANNOT_PULL)):
            git_export_utils.export_to_git(
                course_key, 'https://*****:*****@example.com/r.git')
예제 #19
0
    def test_bad_git_repos(self):
        """
        Test invalid git repos
        """
        test_repo_path = '{}/test_repo'.format(git_export_utils.GIT_REPO_EXPORT_DIR)
        self.assertFalse(os.path.isdir(test_repo_path))
        course_key = SlashSeparatedCourseKey('foo', 'blah', '100-')
        # Test bad clones
        with self.assertRaisesRegexp(GitExportError,
                                     str(GitExportError.CANNOT_PULL)):
            git_export_utils.export_to_git(
                course_key,
                'https://*****:*****@example.com/test_repo.git')
        self.assertFalse(os.path.isdir(test_repo_path))

        # Setup good repo with bad course to test xml export
        with self.assertRaisesRegexp(GitExportError,
                                     str(GitExportError.XML_EXPORT_FAIL)):
            git_export_utils.export_to_git(
                course_key,
                'file://{0}'.format(self.bare_repo_dir))

        # Test bad git remote after successful clone
        with self.assertRaisesRegexp(GitExportError,
                                     str(GitExportError.CANNOT_PULL)):
            git_export_utils.export_to_git(
                course_key,
                'https://*****:*****@example.com/r.git')
예제 #20
0
    def test_bad_git_url(self):
        """
        Test several bad URLs for validation
        """
        with self.assertRaisesRegexp(GitExportError, str(GitExportError.URL_BAD)):
            git_export_utils.export_to_git('', 'Sillyness')

        with self.assertRaisesRegexp(GitExportError, str(GitExportError.URL_BAD)):
            git_export_utils.export_to_git('', 'example.com:edx/notreal')

        with self.assertRaisesRegexp(GitExportError,
                                     str(GitExportError.URL_NO_AUTH)):
            git_export_utils.export_to_git('', 'http://blah')
예제 #21
0
    def test_bad_git_url(self):
        """
        Test several bad URLs for validation
        """
        course_key = SlashSeparatedCourseKey("org", "course", "run")
        with self.assertRaisesRegexp(GitExportError, unicode(GitExportError.URL_BAD)):
            git_export_utils.export_to_git(course_key, "Sillyness")

        with self.assertRaisesRegexp(GitExportError, unicode(GitExportError.URL_BAD)):
            git_export_utils.export_to_git(course_key, "example.com:edx/notreal")

        with self.assertRaisesRegexp(GitExportError, unicode(GitExportError.URL_NO_AUTH)):
            git_export_utils.export_to_git(course_key, "http://blah")
예제 #22
0
    def test_bad_git_url(self):
        """
        Test several bad URLs for validation
        """
        with self.assertRaisesRegexp(GitExportError, str(GitExportError.URL_BAD)):
            git_export_utils.export_to_git('', 'Sillyness')

        with self.assertRaisesRegexp(GitExportError, str(GitExportError.URL_BAD)):
            git_export_utils.export_to_git('', 'example.com:edx/notreal')

        with self.assertRaisesRegexp(GitExportError,
                                     str(GitExportError.URL_NO_AUTH)):
            git_export_utils.export_to_git('', 'http://blah')
예제 #23
0
    def test_bad_git_url(self):
        """
        Test several bad URLs for validation
        """
        course_key = CourseLocator('org', 'course', 'run')
        with self.assertRaisesRegex(GitExportError, six.text_type(GitExportError.URL_BAD)):
            git_export_utils.export_to_git(course_key, 'Sillyness')

        with self.assertRaisesRegex(GitExportError, six.text_type(GitExportError.URL_BAD)):
            git_export_utils.export_to_git(course_key, 'example.com:edx/notreal')

        with self.assertRaisesRegex(GitExportError, six.text_type(GitExportError.URL_NO_AUTH)):
            git_export_utils.export_to_git(course_key, 'http://blah')
예제 #24
0
    def test_bad_git_url(self):
        """
        Test several bad URLs for validation
        """
        course_key = SlashSeparatedCourseKey('org', 'course', 'run')
        with self.assertRaisesRegexp(GitExportError, str(GitExportError.URL_BAD)):
            git_export_utils.export_to_git(course_key, 'Sillyness')

        with self.assertRaisesRegexp(GitExportError, str(GitExportError.URL_BAD)):
            git_export_utils.export_to_git(course_key, 'example.com:edx/notreal')

        with self.assertRaisesRegexp(GitExportError,
                                     str(GitExportError.URL_NO_AUTH)):
            git_export_utils.export_to_git(course_key, 'http://blah')
예제 #25
0
    def test_bad_git_url(self):
        """
        Test several bad URLs for validation
        """
        course_key = SlashSeparatedCourseKey('org', 'course', 'run')
        with self.assertRaisesRegexp(GitExportError,
                                     unicode(GitExportError.URL_BAD)):
            git_export_utils.export_to_git(course_key, 'Sillyness')

        with self.assertRaisesRegexp(GitExportError,
                                     unicode(GitExportError.URL_BAD)):
            git_export_utils.export_to_git(course_key,
                                           'example.com:edx/notreal')

        with self.assertRaisesRegexp(GitExportError,
                                     unicode(GitExportError.URL_NO_AUTH)):
            git_export_utils.export_to_git(course_key, 'http://blah')