From 39e5a7762eaa8d6480c15d80fe380ac4efb96abb Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Thu, 26 Jul 2012 11:47:45 -0400 Subject: [PATCH] Make github_sync use a namedtuple for repo settings --- cms/djangoapps/github_sync/__init__.py | 29 ++++++++++---------- cms/djangoapps/github_sync/exceptions.py | 4 +++ cms/djangoapps/github_sync/tests/__init__.py | 8 ++---- cms/envs/dev.py | 27 ++++-------------- 4 files changed, 27 insertions(+), 41 deletions(-) diff --git a/cms/djangoapps/github_sync/__init__.py b/cms/djangoapps/github_sync/__init__.py index 1089db7efd..bf6e2114b3 100644 --- a/cms/djangoapps/github_sync/__init__.py +++ b/cms/djangoapps/github_sync/__init__.py @@ -7,34 +7,33 @@ from git import Repo, PushInfo from xmodule.modulestore.xml_importer import import_from_xml 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__) +RepoSettings = namedtuple('RepoSettings', 'path branch origin') + def setup_repo(repo_settings): """ Reset the local github repo specified by repo_settings - repo_settings is a dictionary with the following keys: - 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 + repo_settings (RepoSettings): The settings for the repo to reset """ - course_dir = repo_settings['path'] + course_dir = repo_settings.path repo_path = settings.GITHUB_REPO_ROOT / course_dir 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) origin = git_repo.remotes.origin origin.fetch() # 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 @@ -43,19 +42,19 @@ def load_repo_settings(course_dir): """ Returns the repo_settings for the course stored in course_dir """ - for repo_settings in settings.REPOS.values(): - if repo_settings['path'] == course_dir: - return repo_settings - raise InvalidRepo(course_dir) + if course_dir not in settings.REPOS: + raise InvalidRepo(course_dir) + + return RepoSettings(course_dir, **settings.REPOS[course_dir]) def import_from_github(repo_settings): """ 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.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(), settings.GITHUB_REPO_ROOT, course_dirs=[course_dir]) diff --git a/cms/djangoapps/github_sync/exceptions.py b/cms/djangoapps/github_sync/exceptions.py index 9097ffc2a6..1fe8d1d73e 100644 --- a/cms/djangoapps/github_sync/exceptions.py +++ b/cms/djangoapps/github_sync/exceptions.py @@ -1,2 +1,6 @@ class GithubSyncError(Exception): pass + + +class InvalidRepo(Exception): + pass diff --git a/cms/djangoapps/github_sync/tests/__init__.py b/cms/djangoapps/github_sync/tests/__init__.py index 6b374cda2c..ef6a2f349a 100644 --- a/cms/djangoapps/github_sync/tests/__init__.py +++ b/cms/djangoapps/github_sync/tests/__init__.py @@ -1,8 +1,7 @@ from django.test import TestCase from path import path import shutil -import os -from github_sync import import_from_github, export_to_github +from github_sync import import_from_github, export_to_github, load_repo_settings from git import Repo from django.conf import settings from xmodule.modulestore.django import modulestore @@ -16,8 +15,7 @@ REMOTE_DIR = WORKING_DIR / 'remote_repo' @override_settings(REPOS={ - 'local': { - 'path': 'local_repo', + 'local_repo': { 'origin': REMOTE_DIR, 'branch': 'master', } @@ -40,7 +38,7 @@ class GithubSyncTestCase(TestCase): remote.git.commit(m='Initial commit') 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): self.cleanup() diff --git a/cms/envs/dev.py b/cms/envs/dev.py index dd12ce5770..b0729ba885 100644 --- a/cms/envs/dev.py +++ b/cms/envs/dev.py @@ -32,38 +32,23 @@ DATABASES = { REPOS = { 'edx4edx': { - 'path': "edx4edx", - 'org': 'edx', - 'course': 'edx4edx', - 'branch': 'for_cms', + 'branch': 'master', 'origin': 'git@github.com:MITx/edx4edx.git', }, - '6002x-fall-2012': { - 'path': '6002x-fall-2012', - 'org': 'mit.edu', - 'course': '6.002x', - 'branch': 'for_cms', + 'content-mit-6002x': { + 'branch': 'master', 'origin': 'git@github.com:MITx/6002x-fall-2012.git', }, '6.00x': { - 'path': '6.00x', - 'org': 'mit.edu', - 'course': '6.00x', - 'branch': 'for_cms', + 'branch': 'master', 'origin': 'git@github.com:MITx/6.00x.git', }, '7.00x': { - 'path': '7.00x', - 'org': 'mit.edu', - 'course': '7.00x', - 'branch': 'for_cms', + 'branch': 'master', 'origin': 'git@github.com:MITx/7.00x.git', }, '3.091x': { - 'path': '3.091x', - 'org': 'mit.edu', - 'course': '3.091x', - 'branch': 'for_cms', + 'branch': 'master', 'origin': 'git@github.com:MITx/3.091x.git', }, }