Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Add GitDependency as extdep support to git repositories. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
spbrogan authored Jun 11, 2019
1 parent 294573e commit 03833b1
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 52 deletions.
3 changes: 3 additions & 0 deletions MuEnvironment/ExternalDependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,12 @@ def ExtDepFactory(descriptor):
# Add all supported external dependencies here to avoid import errors.
from MuEnvironment.WebDependency import WebDependency
from MuEnvironment.NugetDependency import NugetDependency
from MuEnvironment.GitDependency import GitDependency
if descriptor['type'] == NugetDependency.TypeString:
return NugetDependency(descriptor)
elif descriptor['type'] == WebDependency.TypeString:
return WebDependency(descriptor)
elif descriptor['type'] == GitDependency.TypeString:
return GitDependency(descriptor)

raise ValueError("Unknown extdep type '%s' requested!" % descriptor['type'])
104 changes: 104 additions & 0 deletions MuEnvironment/GitDependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# @file GitDependency.py
# This module implements ExternalDependency for a git repository
# This should only be used for read-only repositories. Any changes in
# these extdeps will be removed.
#
##
# Copyright (c) 2019, Microsoft Corporation
#
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##
import os
import logging
from MuEnvironment.ExternalDependency import ExternalDependency
from MuEnvironment import RepoResolver
from MuEnvironment.MuGit import Repo
from MuEnvironment import VersionAggregator


class GitDependency(ExternalDependency):
'''
ext_dep fields:
- source: url for git clone
- version: commit from git repo
'''

TypeString = "git"

def __init__(self, descriptor):
super().__init__(descriptor)
self.repo_url = self.source
self.commit = self.version
self._local_repo_root_path = os.path.join(os.path.abspath(self.contents_dir), self.name)
self.logger = logging.getLogger("git-dependency")

# valid_attributes = ["Path", "Url", "Branch", "Commit", "ReferencePath", "Full"]
self._repo_resolver_dep_obj = {"Path": self.name, "Url": self.repo_url, "Commit": self.commit}

def fetch(self):

# def resolve(file_system_path, dependency, force=False, ignore=False, update_ok=False):
RepoResolver.resolve(self._local_repo_root_path, self._repo_resolver_dep_obj, update_ok=True)

# Add a file to track the state of the dependency.
self.update_state_file()

def clean(self):
self.logger.debug("Cleaning git dependency directory for '%s'..." % self.name)

if os.path.isdir(self._local_repo_root_path):
# Clean up git dependency specific stuff
RepoResolver.clear_folder(self.contents_dir)

# Let super class clean up common dependency stuff
super().clean()

# override verify due to different scheme with git
def verify(self, logversion=True):
result = True

if not os.path.isdir(self._local_repo_root_path):
self.logger.error("no dir for Git Dependency")
result = False

if result and len(os.listdir(self._local_repo_root_path)) == 0:
self.logger.error("no files in Git Dependency")
result = False

if result:
# valid repo folder
r = Repo(self._local_repo_root_path)
if(not r.initalized):
self.logger.error("Git Dependency: Not Initialized")
result = False
elif(r.dirty):
self.logger.error("Git Dependency: dirty")
result = False

if(r.head.commit != self.version):
self.logger.error(f"Git Dependency: head is {r.head.commit} and version is {self.version}")
result = False

self.logger.debug("Verify '%s' returning '%s'." % (self.name, result))
if(logversion):
VersionAggregator.GetVersionAggregator().ReportVersion(self.name, self.version,
VersionAggregator.VersionTypes.INFO)
return result
28 changes: 15 additions & 13 deletions MuEnvironment/RepoResolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ def resolve(file_system_path, dependency, force=False, ignore=False, update_ok=F
##
if not os.path.isdir(git_path):
clone_repo(git_path, dependency)
checkout(git_path, dependency, Repo(git_path), True, False)
return
r = Repo(git_path)
checkout(git_path, dependency, r, True, False)
return r

folder_empty = len(os.listdir(git_path)) == 0
if folder_empty: # if the folder is empty, we can clone into it
clone_repo(git_path, dependency)
checkout(git_path, dependency, Repo(git_path), True, False)
return
r = Repo(git_path)
checkout(git_path, dependency, r, True, False)
return r

repo = Repo(git_path)
if not repo.initalized: # if there isn't a .git folder in there
Expand All @@ -68,14 +70,14 @@ def resolve(file_system_path, dependency, force=False, ignore=False, update_ok=F
logger.warning(
"Folder {0} is not a git repo and is being overwritten!".format(git_path))
clone_repo(git_path, dependency)
checkout(git_path, dependency, Repo(git_path), True, False)
return
checkout(git_path, dependency, repo, True, False)
return repo
else:
if(ignore):
logger.warning(
"Folder {0} is not a git repo but Force parameter not used. "
"Ignore State Allowed.".format(git_path))
return
return repo
else:
logger.critical(
"Folder {0} is not a git repo and it is not empty.".format(git_path))
Expand All @@ -88,14 +90,14 @@ def resolve(file_system_path, dependency, force=False, ignore=False, update_ok=F
logger.warning(
"Folder {0} is a git repo but is dirty and is being overwritten as requested!".format(git_path))
clone_repo(git_path, dependency)
checkout(git_path, dependency, Repo(git_path), True, False)
return
checkout(git_path, dependency, repo, True, False)
return repo
else:
if(ignore):
logger.warning(
"Folder {0} is a git repo but is dirty and Force parameter not used. "
"Ignore State Allowed.".format(git_path))
return
return repo
else:
logger.critical(
"Folder {0} is a git repo and is dirty.".format(git_path))
Expand All @@ -109,7 +111,7 @@ def resolve(file_system_path, dependency, force=False, ignore=False, update_ok=F
"Folder {0} is a git repo but it is at a different repo and is "
"being overwritten as requested!".format(git_path))
clone_repo(git_path, dependency)
checkout(git_path, dependency, Repo(git_path), True, False)
checkout(git_path, dependency, repo, True, False)
else:
if ignore:
logger.warning(
Expand All @@ -123,6 +125,7 @@ def resolve(file_system_path, dependency, force=False, ignore=False, update_ok=F
git_path, dependency["Url"], repo.remotes.origin.url))

checkout(git_path, dependency, repo, update_ok, ignore, force)
return repo

##
# dependencies is a list of objects - it has Path, Commit, Branch,
Expand Down Expand Up @@ -154,9 +157,8 @@ def resolve_all(WORKSPACE_PATH, dependencies, force=False, ignore=False, update_

return packages

# Gets the details of a particular repo


# Gets the details of a particular repo
def get_details(abs_file_system_path):
repo = Repo(abs_file_system_path)
url = repo.remotes.origin.url
Expand Down
Loading

0 comments on commit 03833b1

Please sign in to comment.