def test_branch_exceptions(self): """ This wil create conditions to exercise bad paths in the switch_branch function. """ # create bare repo that we can mess with and attempt an import bare_repo = os.path.abspath('{0}/{1}'.format(settings.TEST_ROOT, 'bare.git')) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output([ 'git', '--bare', 'init', ], stderr=subprocess.STDOUT, cwd=bare_repo) # Build repo dir repo_dir = self.GIT_REPO_DIR if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) rdir = '{0}/bare'.format(repo_dir) with self.assertRaisesRegexp(GitImportError, GitImportError.BAD_REPO): git_import.add_repo('file://{0}'.format(bare_repo), None, None) # Get logger for checking strings in logs output = StringIO.StringIO() test_log_handler = logging.StreamHandler(output) test_log_handler.setLevel(logging.DEBUG) glog = git_import.log glog.addHandler(test_log_handler) # Move remote so fetch fails shutil.move(bare_repo, '{0}/not_bare.git'.format(settings.TEST_ROOT)) try: git_import.switch_branch('master', rdir) except GitImportError: self.assertIn('Unable to fetch remote', output.getvalue()) shutil.move('{0}/not_bare.git'.format(settings.TEST_ROOT), bare_repo) output.truncate(0) # Replace origin with a different remote subprocess.check_output([ 'git', 'remote', 'rename', 'origin', 'blah', ], stderr=subprocess.STDOUT, cwd=rdir) with self.assertRaises(GitImportError): git_import.switch_branch('master', rdir) self.assertIn('Getting a list of remote branches failed', output.getvalue())
def test_branch_exceptions(self): """ This wil create conditions to exercise bad paths in the switch_branch function. """ # create bare repo that we can mess with and attempt an import bare_repo = os.path.abspath('{0}/{1}'.format(settings.TEST_ROOT, 'bare.git')) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output(['git', '--bare', 'init', ], stderr=subprocess.STDOUT, cwd=bare_repo) # Build repo dir repo_dir = self.git_repo_dir if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) rdir = '{0}/bare'.format(repo_dir) with self.assertRaises(GitImportErrorBadRepo): git_import.add_repo('file://{0}'.format(bare_repo), None, None) # Get logger for checking strings in logs output = StringIO.StringIO() test_log_handler = logging.StreamHandler(output) test_log_handler.setLevel(logging.DEBUG) glog = git_import.log glog.addHandler(test_log_handler) # Move remote so fetch fails shutil.move(bare_repo, '{0}/not_bare.git'.format(settings.TEST_ROOT)) try: git_import.switch_branch('master', rdir) except GitImportError: self.assertIn('Unable to fetch remote', output.getvalue()) shutil.move('{0}/not_bare.git'.format(settings.TEST_ROOT), bare_repo) output.truncate(0) # Replace origin with a different remote subprocess.check_output( ['git', 'remote', 'rename', 'origin', 'blah', ], stderr=subprocess.STDOUT, cwd=rdir ) with self.assertRaises(GitImportError): git_import.switch_branch('master', rdir) self.assertIn('Getting a list of remote branches failed', output.getvalue())
def test_branch_exceptions(self): """ This wil create conditions to exercise bad paths in the switch_branch function. """ # create bare repo that we can mess with and attempt an import bare_repo = os.path.abspath("{0}/{1}".format(settings.TEST_ROOT, "bare.git")) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output(["git", "--bare", "init"], stderr=subprocess.STDOUT, cwd=bare_repo) # Build repo dir repo_dir = self.GIT_REPO_DIR if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) rdir = "{0}/bare".format(repo_dir) with self.assertRaisesRegexp(GitImportError, GitImportError.BAD_REPO): git_import.add_repo("file://{0}".format(bare_repo), None, None) # Get logger for checking strings in logs output = StringIO.StringIO() test_log_handler = logging.StreamHandler(output) test_log_handler.setLevel(logging.DEBUG) glog = git_import.log glog.addHandler(test_log_handler) # Move remote so fetch fails shutil.move(bare_repo, "{0}/not_bare.git".format(settings.TEST_ROOT)) try: git_import.switch_branch("master", rdir) except GitImportError: self.assertIn("Unable to fetch remote", output.getvalue()) shutil.move("{0}/not_bare.git".format(settings.TEST_ROOT), bare_repo) output.truncate(0) # Replace origin with a different remote subprocess.check_output(["git", "remote", "rename", "origin", "blah"], stderr=subprocess.STDOUT, cwd=rdir) with self.assertRaises(GitImportError): git_import.switch_branch("master", rdir) self.assertIn("Getting a list of remote branches failed", output.getvalue())
def import_xml_course(self, gitloc, branch): """Imports a git course into the XMLModuleStore""" msg = u'' if not getattr(settings, 'GIT_IMPORT_WITH_XMLMODULESTORE', False): # Translators: "GIT_IMPORT_WITH_XMLMODULESTORE" is a variable name. # "XMLModuleStore" and "MongoDB" are database systems. You should not # translate these names. return _('Refusing to import. GIT_IMPORT_WITH_XMLMODULESTORE is ' 'not turned on, and it is generally not safe to import ' 'into an XMLModuleStore with multithreaded. We ' 'recommend you enable the MongoDB based module store ' 'instead, unless this is a development environment.') cdir = (gitloc.rsplit('/', 1)[1])[:-4] gdir = settings.DATA_DIR / cdir if os.path.exists(gdir): msg += _("The course {0} already exists in the data directory! " "(reloading anyway)").format(cdir) cmd = [ 'git', 'pull', ] cwd = gdir else: cmd = [ 'git', 'clone', gitloc, ] cwd = settings.DATA_DIR cwd = os.path.abspath(cwd) try: cmd_output = escape( subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=cwd)) except subprocess.CalledProcessError as ex: log.exception('Git pull or clone output was: %r', ex.output) # Translators: unable to download the course content from # the source git repository. Clone occurs if this is brand # new, and pull is when it is being updated from the # source. return _('Unable to clone or pull repository. Please check ' 'your url. Output was: {0!r}').format(ex.output) msg += u'<pre>{0}</pre>'.format(cmd_output) if not os.path.exists(gdir): msg += _('Failed to clone repository to {directory_name}').format( directory_name=gdir) return msg # Change branch if specified if branch: try: git_import.switch_branch(branch, gdir) except GitImportError as ex: return str(ex) # Translators: This is a git repository branch, which is a # specific version of a courses content msg += u'<p>{0}</p>'.format( _('Successfully switched to branch: ' '{branch_name}').format(branch_name=branch)) self.def_ms.try_load_course(os.path.abspath(gdir)) errlog = self.def_ms.errored_courses.get(cdir, '') if errlog: msg += u'<hr width="50%"><pre>{0}</pre>'.format(escape(errlog)) else: course = self.def_ms.courses[os.path.abspath(gdir)] msg += _('Loaded course {course_name}<br/>Errors:').format( course_name="{} {}".format(cdir, course.display_name)) errors = self.def_ms.get_course_errors(course.id) if not errors: msg += u'None' else: msg += u'<ul>' for (summary, err) in errors: msg += u'<li><pre>{0}: {1}</pre></li>'.format( escape(summary), escape(err)) msg += u'</ul>' return msg
def import_xml_course(self, gitloc, branch): """Imports a git course into the XMLModuleStore""" msg = u'' if not getattr(settings, 'GIT_IMPORT_WITH_XMLMODULESTORE', False): # Translators: "GIT_IMPORT_WITH_XMLMODULESTORE" is a variable name. # "XMLModuleStore" and "MongoDB" are database systems. You should not # translate these names. return _('Refusing to import. GIT_IMPORT_WITH_XMLMODULESTORE is ' 'not turned on, and it is generally not safe to import ' 'into an XMLModuleStore with multithreaded. We ' 'recommend you enable the MongoDB based module store ' 'instead, unless this is a development environment.') cdir = (gitloc.rsplit('/', 1)[1])[:-4] gdir = settings.DATA_DIR / cdir if os.path.exists(gdir): msg += _("The course {0} already exists in the data directory! " "(reloading anyway)").format(cdir) cmd = ['git', 'pull', ] cwd = gdir else: cmd = ['git', 'clone', gitloc, ] cwd = settings.DATA_DIR cwd = os.path.abspath(cwd) try: cmd_output = escape( subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=cwd) ) except subprocess.CalledProcessError as ex: log.exception('Git pull or clone output was: %r', ex.output) # Translators: unable to download the course content from # the source git repository. Clone occurs if this is brand # new, and pull is when it is being updated from the # source. return _('Unable to clone or pull repository. Please check ' 'your url. Output was: {0!r}').format(ex.output) msg += u'<pre>{0}</pre>'.format(cmd_output) if not os.path.exists(gdir): msg += _('Failed to clone repository to {directory_name}').format(directory_name=gdir) return msg # Change branch if specified if branch: try: git_import.switch_branch(branch, gdir) except GitImportError as ex: return str(ex) # Translators: This is a git repository branch, which is a # specific version of a courses content msg += u'<p>{0}</p>'.format( _('Successfully switched to branch: ' '{branch_name}').format(branch_name=branch)) self.def_ms.try_load_course(os.path.abspath(gdir)) errlog = self.def_ms.errored_courses.get(cdir, '') if errlog: msg += u'<hr width="50%"><pre>{0}</pre>'.format(escape(errlog)) else: course = self.def_ms.courses[os.path.abspath(gdir)] msg += _('Loaded course {course_name}<br/>Errors:').format( course_name="{} {}".format(cdir, course.display_name) ) errors = self.def_ms.get_course_errors(course.id) if not errors: msg += u'None' else: msg += u'<ul>' for (summary, err) in errors: msg += u'<li><pre>{0}: {1}</pre></li>'.format(escape(summary), escape(err)) msg += u'</ul>' return msg
def import_xml_course(self, gitloc, branch): """Imports a git course into the XMLModuleStore""" msg = u"" if not getattr(settings, "GIT_IMPORT_WITH_XMLMODULESTORE", False): return _( "Refusing to import. GIT_IMPORT_WITH_XMLMODULESTORE is " "not turned on, and it is generally not safe to import " "into an XMLModuleStore with multithreaded. We " "recommend you enable the MongoDB based module store " "instead, unless this is a development environment." ) cdir = (gitloc.rsplit("/", 1)[1])[:-4] gdir = settings.DATA_DIR / cdir if os.path.exists(gdir): msg += _("The course {0} already exists in the data directory! " "(reloading anyway)").format(cdir) cmd = ["git", "pull"] cwd = gdir else: cmd = ["git", "clone", gitloc] cwd = settings.DATA_DIR cwd = os.path.abspath(cwd) try: cmd_output = escape(subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=cwd)) except subprocess.CalledProcessError as ex: log.exception("Git pull or clone output was: %r", ex.output) # Translators: unable to download the course content from # the source git repository. Clone occurs if this is brand # new, and pull is when it is being updated from the # source. return _( "Unable to clone or pull repository. Please check " "your url. Output was: {0!r}".format(ex.output) ) msg += u"<pre>{0}</pre>".format(cmd_output) if not os.path.exists(gdir): msg += _("Failed to clone repository to {0}").format(gdir) return msg # Change branch if specified if branch: try: git_import.switch_branch(branch, gdir) except GitImportError as ex: return str(ex) # Translators: This is a git repository branch, which is a # specific version of a courses content msg += u"<p>{0}</p>".format( _("Successfully switched to branch: " "{branch_name}".format(branch_name=branch)) ) self.def_ms.try_load_course(os.path.abspath(gdir)) errlog = self.def_ms.errored_courses.get(cdir, "") if errlog: msg += u'<hr width="50%"><pre>{0}</pre>'.format(escape(errlog)) else: course = self.def_ms.courses[os.path.abspath(gdir)] msg += _("Loaded course {0} {1}<br/>Errors:").format(cdir, course.display_name) errors = self.def_ms.get_item_errors(course.location) if not errors: msg += u"None" else: msg += u"<ul>" for (summary, err) in errors: msg += u"<li><pre>{0}: {1}</pre></li>".format(escape(summary), escape(err)) msg += u"</ul>" return msg