aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/llvm.py86
1 files changed, 77 insertions, 9 deletions
diff --git a/modules/llvm.py b/modules/llvm.py
index 34cd02c..2e7c44f 100644
--- a/modules/llvm.py
+++ b/modules/llvm.py
@@ -1,4 +1,5 @@
import os
+import re
from linaropy.git.worktree import Worktree
@@ -146,6 +147,24 @@ class LLVMSourceConfig(object):
for subproj in remove:
self.__remove_subproject(subproj)
+ def for_each_enabled(self, action):
+ """Perform the given action for each enabled subproject including LLVM.
+
+ The action must be a callable object receiving the path to the
+ subproject's directory as its only parameter.
+
+ If the action throws an exception, it will be rethrown as a
+ RuntimeError. Note that this does not have transactional behaviour.
+ """
+ for subproj in self.get_enabled_subprojects():
+ try:
+ action(self.__get_subproj_cmake_path(subproj))
+ except Exception as exc:
+ raise RuntimeError("Error while processing {}".format(subproj)) from exc
+
+ # Visit LLVM last, in case getting the enabled subprojects errors out.
+ action(self.llvmSourceTree.repodir)
+
def __get_subproj_object(self, subprojName):
"""Get the LLVMSubproject object corresponding to subprojName."""
if not subprojName in list(self.subprojs.keys()):
@@ -160,18 +179,22 @@ class LLVMSourceConfig(object):
def __is_enabled(self, subprojName):
"""
Check if subproj is enabled in this configuration. A subproj is
- considered to be enabled if it is a worktree on the correct branch.
+ considered to be enabled if it is a worktree on the correct branch. If
+ a directory for the subproject exists but does not satisfy those
+ conditions, an EnvironmentError is thrown.
"""
- try:
- # If this succeeds, the subproject has already been added.
- existing = Worktree(self.proj,
- self.__get_subproj_cmake_path(subprojName))
- except EnvironmentError:
- # If it's not a worktree (for whatever reason), it's not enabled.
+ subprojPath = self.__get_subproj_cmake_path(subprojName)
+
+ if not os.path.isdir(subprojPath):
return False
- # If it is a worktree, but on the wrong branch, it is not enabled.
- return existing.getbranch() == self.llvmSourceTree.getbranch()
+ existing = Worktree(self.proj, subprojPath)
+
+ if existing.getbranch() != self.llvmSourceTree.getbranch():
+ raise EnvironmentError("{} is on branch {}, but should be on {}".format(
+ subprojName, existing.getbranch(), self.llvmSourceTree.getbranch()))
+
+ return True
# TODO: add_subproject, remove_subproject and is_enabled should live in
# another object (AddRemoveStrategy?) that would know what we want to add
@@ -222,3 +245,48 @@ class LLVMSourceConfig(object):
worktree = Worktree(self.proj, path)
worktree.clean(False)
+
+
+# FIXME: repo.pushToBranch doesn't work, because it doesn't handle remote
+# branches properly. Furthermore, there's no support for getting the remote URL,
+# so we need to resort to raw git commands. We may also consider moving the
+# functionality for parsing info out of the remote URL into the repo object.
+from sh import git
+from linaropy.cd import cd
+
+
+def get_user_from_remote(url):
+ """Get the username used as part of the remote URL, or None.
+
+ The remote URLs that we expect to see look like $protocol://$user@$location.
+ If they look any different, we won't be able to parse them.
+ """
+ pattern = re.compile("(.*://)?(?P<user>.*)@(.*)\n?")
+ match = pattern.match(str(url))
+ if match is None:
+ return None
+ return match.group('user')
+
+
+def push_current_branch(proj, pathToRepo):
+ """Push the current branch to origin.
+
+ It will be pushed into linaro-local/$user/$branch, where $branch is the
+ current branch and $user is the username used as part of the remote URL.
+ """
+ repo = Worktree(proj, pathToRepo)
+
+ with cd(repo.repodir):
+ remote = git("remote", "get-url", "--push", "origin").strip()
+ user = get_user_from_remote(remote)
+ if not user:
+ raise EnvironmentError("Couldn't parse user from {}.".format(remote))
+
+ local_branch = repo.getbranch()
+ remote_branch = "linaro-local/{}/{}".format(user, local_branch)
+ if not repo.is_valid_branch_name(remote_branch):
+ raise EnvironmentError(
+ "{} is not a valid branch name.".format(remote_branch))
+
+ with cd(repo.repodir):
+ git("push", "-u", "origin", "+{}:{}".format(local_branch, remote_branch))