Permalink
Browse files

Mercurial support

  • Loading branch information...
1 parent 7190598 commit 65c58cd4fab2bbb8d60603f15db4fdc1be42b739 @vmalloc committed Jul 25, 2012
Showing with 82 additions and 16 deletions.
  1. +60 −13 dwight_chroot/resources.py
  2. +1 −0 example_config.py
  3. +3 −0 tests/test__resources.py
  4. +1 −1 vagrant_setup_client.sh
  5. +17 −2 vagrant_setup_server.sh
View
@@ -17,6 +17,8 @@ class Resource(object):
def get_resource_type_from_string(cls, s):
if s.startswith("git://") or s.startswith("ssh+git://"):
return GitResource
+ if s.startswith("http+hg://") or s.startswith("https+hg://"):
+ return MercurialResource
if s.startswith("http://") or s.startswith("https://"):
return HTTPResource
return LocalResource
@@ -56,14 +58,12 @@ def fetch(self, path):
def refresh(self, path):
raise NotImplementedError() # pragma: no cover
-class GitResource(CacheableResource):
+class DVCSResource(CacheableResource):
def __init__(self, repo_url, commit=None, branch=None):
- super(GitResource, self).__init__()
+ super(DVCSResource, self).__init__()
self.repo_url = repo_url
if commit is not None and branch is not None:
raise UsageException("Cannot specify both branch and commit for git resources")
- if commit is None and branch is None:
- branch = "master"
self.commit = commit
self.branch = branch
def get_cache_key(self):
@@ -73,18 +73,65 @@ def fetch(self, path):
def refresh(self, path):
self._refresh(path, initialize=False)
def _refresh(self, path, initialize):
- execute = functools.partial(execute_command_assert_success, cwd=path)
if initialize:
shutil.rmtree(path)
- execute_command_assert_success("git clone {0} {1}".format(self.repo_url, path))
- else:
- execute("git fetch origin")
+ self._clone_into(path)
+ if self.commit:
+ # a commit never changes, so we only do this upon
+ # initialization
+ self._checkout_commit(path)
+ if self.branch:
+ self._checkout_branch(path)
+ elif not self.commit:
+ self._pull_changes(path)
+
+class MercurialResource(DVCSResource):
+ def __init__(self, *args, **kwargs):
+ super(MercurialResource, self).__init__(*args, **kwargs)
+ self._fix_url_scheme()
+ def _fix_url_scheme(self):
+ for prefix, fix in [
+ ("http+hg://", "http://"),
+ ("https+hg://", "https://")
+ ]:
+ if self.repo_url.startswith(prefix):
+ self.repo_url = fix + self.repo_url[len(prefix):]
+ break
+ def _clone_into(self, path):
+ cmd = "hg clone"
+ if self.commit:
+ cmd += " -r {0}".format(self.commit)
if self.branch:
- execute("git fetch origin && git checkout origin/{0} && git reset --hard".format(self.branch))
- else:
- assert self.commit
- execute("git checkout {0} && git reset --hard".format(self.commit))
-
+ cmd += " -b {0}".format(self.branch)
+ cmd += " {0} {1}".format(self.repo_url, path)
+ execute_command_assert_success(cmd)
+ def _checkout_branch(self, path):
+ pass
+ def _checkout_commit(self, path):
+ pass
+ def _pull_changes(self, path):
+ if not self.commit:
+ execute_command_assert_success("hg pull", cwd=path)
+
+class GitResource(DVCSResource):
+ def _clone_into(self, path):
+ execute_command_assert_success("git clone {0} {1}".format(self.repo_url, path))
+ def _checkout_branch(self, path):
+ assert self.branch
+ execute_command_assert_success(
+ "git fetch origin && git checkout -b {0} origin/{0}".format(self.branch),
+ cwd=path)
+ def _checkout_commit(self, path):
+ assert self.commit
+ execute_command_assert_success(
+ "git fetch origin && git checkout {0} && git reset --hard".format(self.commit),
+ cwd=path
+ )
+ def _pull_changes(self, path):
+ execute_command_assert_success(
+ "git pull",
+ cwd=path
+ )
class HTTPResource(CacheableResource):
def __init__(self, url):
View
@@ -4,6 +4,7 @@
"/mounts/fetched_from_local_path" : Include("/local/path"),
"/mounts/fetched_from_git" : Include("git://server/git/git_repository"),
"/mounts/fetched_from_http" : Include("http://server/fetched_from_http.squashfs"),
+ "/mounts/fetched_from_hg" : Include("http+hg://server:8000/repository"),
}
ENVIRON = {
View
@@ -8,6 +8,9 @@ def test__local_resource(self):
def test__git_resource(self):
self.assertDetectedAs("git://git_server/repo", resources.GitResource)
self.assertDetectedAs("ssh+git://git_server/repo", resources.GitResource)
+ def test__hg_resource(self):
+ self.assertDetectedAs("http+hg://server/repo", resources.MercurialResource)
+ self.assertDetectedAs("https+hg://server/repo", resources.MercurialResource)
def test__web_resource(self):
self.assertDetectedAs("http://server/file.tar.gz", resources.HTTPResource)
self.assertDetectedAs("https://secure_server/file.tar.gz", resources.HTTPResource)
View
@@ -1,7 +1,7 @@
echo "192.168.1.11 server" >> /etc/hosts
# setup packages
-sudo apt-get install -y python-setuptools build-essential python-dev squashfs-tools libnss-mdns git
+sudo apt-get install -y python-setuptools build-essential python-dev squashfs-tools libnss-mdns git mercurial
sudo easy_install nose ipdb ipdbplugin ipython
pushd ~vagrant/src
sudo python setup.py develop
View
@@ -1,5 +1,5 @@
echo "192.168.1.10 client" >> /etc/hosts
-sudo apt-get install -y python-setuptools build-essential python-dev debootstrap squashfs-tools libnss-mdns
+sudo apt-get install -y python-setuptools build-essential python-dev debootstrap squashfs-tools libnss-mdns tmux
# setup git server
sudo apt-get -y install git-daemon-run
@@ -14,6 +14,21 @@ exec chpst -ugitdaemon \
EOF
sudo sv restart git-daemon
+# setup mercurial server
+sudo apt-get -y install mercurial
+
+echo <<EOF > $HOME/.hgrc
+[ui]
+username = John Doe <john@example.com>
+EOF
+
+hg init $HOME/hgrepo
+pushd $HOME/hgrepo
+touch fetched_from_hg_file
+hg commit -m some_commit
+hg serve -d --prefix repository
+popd
+
# setup web server
sudo apt-get -y install nginx
sudo service nginx start
@@ -22,7 +37,7 @@ sudo service nginx start
mkdir -p /tmp/base_image
sudo debootstrap --variant=buildd --arch amd64 precise /tmp/base_image http://archive.ubuntu.com/ubuntu/
sudo touch /tmp/base_image/dwight_base_image_file
-sudo mkdir -p /tmp/base_image/mounts/{fetched_from_git,fetched_from_http,fetched_from_ssh,fetched_from_local_path}
+sudo mkdir -p /tmp/base_image/mounts/{fetched_from_git,fetched_from_hg,fetched_from_http,fetched_from_ssh,fetched_from_local_path}
mksquashfs /tmp/base_image /usr/share/nginx/www/ubuntu_precise64.squashfs
# setup external squashfs (to be exported over http)

0 comments on commit 65c58cd

Please sign in to comment.