Make github_sync use a namedtuple for repo settings
This commit is contained in:
@@ -7,34 +7,33 @@ from git import Repo, PushInfo
|
|||||||
|
|
||||||
from xmodule.modulestore.xml_importer import import_from_xml
|
from xmodule.modulestore.xml_importer import import_from_xml
|
||||||
from xmodule.modulestore.django import modulestore
|
from xmodule.modulestore.django import modulestore
|
||||||
from xmodule.modulestore import Location
|
from collections import namedtuple
|
||||||
|
|
||||||
from .exceptions import GithubSyncError
|
from .exceptions import GithubSyncError, InvalidRepo
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
RepoSettings = namedtuple('RepoSettings', 'path branch origin')
|
||||||
|
|
||||||
|
|
||||||
def setup_repo(repo_settings):
|
def setup_repo(repo_settings):
|
||||||
"""
|
"""
|
||||||
Reset the local github repo specified by repo_settings
|
Reset the local github repo specified by repo_settings
|
||||||
|
|
||||||
repo_settings is a dictionary with the following keys:
|
repo_settings (RepoSettings): The settings for the repo to reset
|
||||||
path: file system path to the local git repo
|
|
||||||
branch: name of the branch to track on github
|
|
||||||
origin: git url for the repository to track
|
|
||||||
"""
|
"""
|
||||||
course_dir = repo_settings['path']
|
course_dir = repo_settings.path
|
||||||
repo_path = settings.GITHUB_REPO_ROOT / course_dir
|
repo_path = settings.GITHUB_REPO_ROOT / course_dir
|
||||||
|
|
||||||
if not os.path.isdir(repo_path):
|
if not os.path.isdir(repo_path):
|
||||||
Repo.clone_from(repo_settings['origin'], repo_path)
|
Repo.clone_from(repo_settings.origin, repo_path)
|
||||||
|
|
||||||
git_repo = Repo(repo_path)
|
git_repo = Repo(repo_path)
|
||||||
origin = git_repo.remotes.origin
|
origin = git_repo.remotes.origin
|
||||||
origin.fetch()
|
origin.fetch()
|
||||||
|
|
||||||
# Do a hard reset to the remote branch so that we have a clean import
|
# Do a hard reset to the remote branch so that we have a clean import
|
||||||
git_repo.git.checkout(repo_settings['branch'])
|
git_repo.git.checkout(repo_settings.branch)
|
||||||
|
|
||||||
return git_repo
|
return git_repo
|
||||||
|
|
||||||
@@ -43,19 +42,19 @@ def load_repo_settings(course_dir):
|
|||||||
"""
|
"""
|
||||||
Returns the repo_settings for the course stored in course_dir
|
Returns the repo_settings for the course stored in course_dir
|
||||||
"""
|
"""
|
||||||
for repo_settings in settings.REPOS.values():
|
if course_dir not in settings.REPOS:
|
||||||
if repo_settings['path'] == course_dir:
|
raise InvalidRepo(course_dir)
|
||||||
return repo_settings
|
|
||||||
raise InvalidRepo(course_dir)
|
return RepoSettings(course_dir, **settings.REPOS[course_dir])
|
||||||
|
|
||||||
|
|
||||||
def import_from_github(repo_settings):
|
def import_from_github(repo_settings):
|
||||||
"""
|
"""
|
||||||
Imports data into the modulestore based on the XML stored on github
|
Imports data into the modulestore based on the XML stored on github
|
||||||
"""
|
"""
|
||||||
course_dir = repo_settings['path']
|
course_dir = repo_settings.path
|
||||||
git_repo = setup_repo(repo_settings)
|
git_repo = setup_repo(repo_settings)
|
||||||
git_repo.head.reset('origin/%s' % repo_settings['branch'], index=True, working_tree=True)
|
git_repo.head.reset('origin/%s' % repo_settings.branch, index=True, working_tree=True)
|
||||||
|
|
||||||
module_store = import_from_xml(modulestore(),
|
module_store = import_from_xml(modulestore(),
|
||||||
settings.GITHUB_REPO_ROOT, course_dirs=[course_dir])
|
settings.GITHUB_REPO_ROOT, course_dirs=[course_dir])
|
||||||
|
|||||||
@@ -1,2 +1,6 @@
|
|||||||
class GithubSyncError(Exception):
|
class GithubSyncError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidRepo(Exception):
|
||||||
|
pass
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from path import path
|
from path import path
|
||||||
import shutil
|
import shutil
|
||||||
import os
|
from github_sync import import_from_github, export_to_github, load_repo_settings
|
||||||
from github_sync import import_from_github, export_to_github
|
|
||||||
from git import Repo
|
from git import Repo
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from xmodule.modulestore.django import modulestore
|
from xmodule.modulestore.django import modulestore
|
||||||
@@ -16,8 +15,7 @@ REMOTE_DIR = WORKING_DIR / 'remote_repo'
|
|||||||
|
|
||||||
|
|
||||||
@override_settings(REPOS={
|
@override_settings(REPOS={
|
||||||
'local': {
|
'local_repo': {
|
||||||
'path': 'local_repo',
|
|
||||||
'origin': REMOTE_DIR,
|
'origin': REMOTE_DIR,
|
||||||
'branch': 'master',
|
'branch': 'master',
|
||||||
}
|
}
|
||||||
@@ -40,7 +38,7 @@ class GithubSyncTestCase(TestCase):
|
|||||||
remote.git.commit(m='Initial commit')
|
remote.git.commit(m='Initial commit')
|
||||||
remote.git.config("receive.denyCurrentBranch", "ignore")
|
remote.git.config("receive.denyCurrentBranch", "ignore")
|
||||||
|
|
||||||
self.import_revision, self.import_course = import_from_github(settings.REPOS['local'])
|
self.import_revision, self.import_course = import_from_github(load_repo_settings('local_repo'))
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.cleanup()
|
self.cleanup()
|
||||||
|
|||||||
@@ -32,38 +32,23 @@ DATABASES = {
|
|||||||
|
|
||||||
REPOS = {
|
REPOS = {
|
||||||
'edx4edx': {
|
'edx4edx': {
|
||||||
'path': "edx4edx",
|
'branch': 'master',
|
||||||
'org': 'edx',
|
|
||||||
'course': 'edx4edx',
|
|
||||||
'branch': 'for_cms',
|
|
||||||
'origin': 'git@github.com:MITx/edx4edx.git',
|
'origin': 'git@github.com:MITx/edx4edx.git',
|
||||||
},
|
},
|
||||||
'6002x-fall-2012': {
|
'content-mit-6002x': {
|
||||||
'path': '6002x-fall-2012',
|
'branch': 'master',
|
||||||
'org': 'mit.edu',
|
|
||||||
'course': '6.002x',
|
|
||||||
'branch': 'for_cms',
|
|
||||||
'origin': 'git@github.com:MITx/6002x-fall-2012.git',
|
'origin': 'git@github.com:MITx/6002x-fall-2012.git',
|
||||||
},
|
},
|
||||||
'6.00x': {
|
'6.00x': {
|
||||||
'path': '6.00x',
|
'branch': 'master',
|
||||||
'org': 'mit.edu',
|
|
||||||
'course': '6.00x',
|
|
||||||
'branch': 'for_cms',
|
|
||||||
'origin': 'git@github.com:MITx/6.00x.git',
|
'origin': 'git@github.com:MITx/6.00x.git',
|
||||||
},
|
},
|
||||||
'7.00x': {
|
'7.00x': {
|
||||||
'path': '7.00x',
|
'branch': 'master',
|
||||||
'org': 'mit.edu',
|
|
||||||
'course': '7.00x',
|
|
||||||
'branch': 'for_cms',
|
|
||||||
'origin': 'git@github.com:MITx/7.00x.git',
|
'origin': 'git@github.com:MITx/7.00x.git',
|
||||||
},
|
},
|
||||||
'3.091x': {
|
'3.091x': {
|
||||||
'path': '3.091x',
|
'branch': 'master',
|
||||||
'org': 'mit.edu',
|
|
||||||
'course': '3.091x',
|
|
||||||
'branch': 'for_cms',
|
|
||||||
'origin': 'git@github.com:MITx/3.091x.git',
|
'origin': 'git@github.com:MITx/3.091x.git',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user