Skip to content

Commit

Permalink
added git support
Browse files Browse the repository at this point in the history
  • Loading branch information
bugy committed Mar 20, 2017
1 parent dc561c0 commit 61a1db1
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 52 deletions.
5 changes: 2 additions & 3 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

import utils.file_utils as file_utils
import utils.mvn_utils as mvn_utils
import utils.svn_utils as svn_utils
import common

(ROOT_PROJECT_PATH, MVN_OPTS, ROOT_ONLY, TRACK_UNVERSIONED) = common.parse_options()
(ROOT_PROJECT_PATH, MVN_OPTS, ROOT_ONLY, TRACK_UNVERSIONED, vcs_gateway) = common.parse_options()
MAVEN_REPO_PATH = mvn_utils.repo_path()

def is_important(file_path):
Expand All @@ -26,7 +25,7 @@ def get_unique_name(root_project_path):
return result


changed_files = svn_utils.get_local_changed_files(ROOT_PROJECT_PATH, not TRACK_UNVERSIONED)
changed_files = vcs_gateway.get_local_changed_files(ROOT_PROJECT_PATH, not TRACK_UNVERSIONED)
important_files = filter(is_important, changed_files)

pom_paths = set([])
Expand Down
7 changes: 3 additions & 4 deletions ci_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
import utils.collections as collections
import utils.file_utils as file_utils
import utils.mvn_utils as mvn_utils
import utils.svn_utils as svn_utils

(ROOT_PROJECT_PATH, MVN_OPTS, ROOT_ONLY, track_unversioned) = common.parse_options()
(ROOT_PROJECT_PATH, MVN_OPTS, ROOT_ONLY, track_unversioned, vcs_gateway) = common.parse_options()
MAVEN_REPO_PATH = mvn_utils.repo_path()


def incremental_rebuild(last_revision, current_revision):
changed_files = svn_utils.get_revision_changed_files(ROOT_PROJECT_PATH, last_revision, current_revision)
changed_files = vcs_gateway.get_revision_changed_files(ROOT_PROJECT_PATH, last_revision, current_revision)

changed_project_poms = set([])

Expand Down Expand Up @@ -63,7 +62,7 @@ def incremental_rebuild(last_revision, current_revision):
mvn_utils.rebuild(ROOT_PROJECT_PATH, changed_projects, MVN_OPTS, silent=False)


current_revision = svn_utils.get_revision(ROOT_PROJECT_PATH)
current_revision = vcs_gateway.get_revision(ROOT_PROJECT_PATH)

info_file_path = os.path.join(ROOT_PROJECT_PATH, "_ci_rebuild.info")
if os.path.exists(info_file_path):
Expand Down
19 changes: 18 additions & 1 deletion common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import sys

import utils.file_utils as file_utils
import utils.git_utils as git_utils
import utils.mvn_utils as mvn_utils
import utils.svn_utils as svn_utils


def parse_options():
Expand All @@ -14,6 +16,7 @@ def parse_options():
action='store_true')
parser.add_argument("-t", "--track_unversioned", help="also consider local changes in unversioned files",
action='store_true')
parser.add_argument("-c", "--vcs", help="version control system", choices=['svn', 'git'])
args = vars(parser.parse_args())

if args["root_path"]:
Expand All @@ -35,7 +38,21 @@ def parse_options():
print("ERROR! No root pom.xml find in path", os.path.abspath(root_project_path))
sys.exit(1)

return (root_project_path, mvn_opts, root_only, track_unversioned)
if args['vcs']:
if args['vcs'] == 'git':
vcs_gateway = git_utils.GitGateway()
else:
vcs_gateway = svn_utils.SvnGateway()
else:
if svn_utils.is_svn_repo(root_project_path):
vcs_gateway = svn_utils.SvnGateway()
elif git_utils.is_git_repo(root_project_path):
vcs_gateway = git_utils.GitGateway()
else:
print("Couldn't resolve VCS type, please specify it explicitly using -c argument")
sys.exit(-1)

return (root_project_path, mvn_opts, root_only, track_unversioned, vcs_gateway)


def to_mvn_projects(pom_paths, root_path, root_only):
Expand Down
61 changes: 61 additions & 0 deletions utils/git_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
import re

import utils.process_utils as process_utils


class GitGateway():
def get_local_changed_files(self, abs_path, ignore_unversioned=True):
result = set()

status_info = process_utils.invoke(['git', 'status', '-s'], abs_path, ['?'])
if ignore_unversioned:
changed_files = self.parse_changed_files(status_info, abs_path, ['?'])
else:
changed_files = self.parse_changed_files(status_info, abs_path)

result.update(changed_files)

return list(result)

def parse_changed_files(self, diff_info, abs_path, ignored_statuses=None):
result = []

lines = diff_info.split('\n')
for line in lines:
file_info = re.split(r'\s+', line.strip(), 1)

if len(file_info) != 2:
continue

if ignored_statuses:
status = file_info[0]
suitable = False

for singe_status in status:
if singe_status not in ignored_statuses:
suitable = True
break

if not suitable:
continue

file_path = file_info[1]

result.append(os.path.join(abs_path, file_path))

return result

def get_revision_changed_files(self, abs_path, from_revision, to_revision):
diff_info = process_utils.invoke(
['git', 'diff', '--name-status', from_revision, to_revision], abs_path)

return self.parse_changed_files(diff_info, abs_path)

def get_revision(self, project_path):
return process_utils.invoke(['git', 'rev-parse', 'HEAD'], project_path).strip()


def is_git_repo(project_path):
git_files_path = os.path.join(project_path, '.git')
return os.path.isdir(git_files_path) or process_utils.check_call('git rev-parse --git-dir', project_path)
16 changes: 16 additions & 0 deletions utils/process_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ def invoke(command, work_dir=".", exit_on_failure=False):
return output


def check_call(command, work_dir="."):
command = prepare_command(command)

shell = requires_shell()
p = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=work_dir,
shell=shell)

p.communicate()
result_code = p.returncode

return result_code == 0


def invoke_attached(command, work_dir="."):
command = prepare_command(command)

Expand Down
89 changes: 45 additions & 44 deletions utils/svn_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,68 @@
import utils.xml_utils as xml_utils


def get_local_changed_files(abs_path, ignore_unversioned=True):
status_info = process_utils.invoke(['svn', 'status', '--xml', abs_path])
class SvnGateway():
def get_local_changed_files(self, abs_path, ignore_unversioned=True):
status_info = process_utils.invoke(['svn', 'status', '--xml', abs_path])

entries_xpath = '*/entry'
found_results = xml_utils.find_in_string(status_info, [entries_xpath])
entries = found_results[entries_xpath]
entries_xpath = '*/entry'
found_results = xml_utils.find_in_string(status_info, [entries_xpath])
entries = found_results[entries_xpath]

return svn_xml_status_to_files(entries, ignore_unversioned)
return self.svn_xml_status_to_files(entries, ignore_unversioned)

def svn_status_to_files(self, all_lines):
result = []

def svn_status_to_files(all_lines):
result = []
for line in all_lines:
if os.name != 'nt':
if not ('/' in line):
continue
file_path = line[line.index("/"):]

for line in all_lines:
if os.name != 'nt':
if not ('/' in line):
continue
file_path = line[line.index("/"):]

else:
if not ('\\' in line):
continue
file_path = line[line.index(':\\') - 1:]
else:
if not ('\\' in line):
continue
file_path = line[line.index(':\\') - 1:]

result.append(file_path)
result.append(file_path)

return result
return result

def svn_xml_status_to_files(self, found_entries, ignore_unversioned=True):
result = []

def svn_xml_status_to_files(found_entries, ignore_unversioned=True):
result = []
for entry in found_entries:
status_info = entry['wc-status']
status = status_info['item']

for entry in found_entries:
status_info = entry['wc-status']
status = status_info['item']

if ignore_unversioned and (status == 'unversioned'):
continue
if ignore_unversioned and (status == 'unversioned'):
continue

path = entry['path']
result.append(path)
path = entry['path']
result.append(path)

return result
return result

def get_revision_changed_files(self, abs_path, from_revision, to_revision):
changed_files = process_utils.invoke(
['svn', 'diff', '--summarize', '-r' + from_revision + ':' + to_revision, abs_path])
lines = changed_files.split('\n')

def get_revision_changed_files(abs_path, from_revision, to_revision):
changed_files = process_utils.invoke(
['svn', 'diff', '--summarize', '-r' + from_revision + ':' + to_revision, abs_path])
lines = changed_files.split('\n')
return self.svn_status_to_files(lines)

return svn_status_to_files(lines)
def get_revision(self, project_path):
svn_info = process_utils.invoke(['svn', 'info', project_path])
info_lines = svn_info.split('\n')

revision_prefix = 'Revision: '

def get_revision(project_path):
svn_info = process_utils.invoke(['svn', 'info', project_path])
info_lines = svn_info.split('\n')
for line in info_lines:
if line.startswith(revision_prefix):
return line[len(revision_prefix):]

revision_prefix = 'Revision: '
raise Exception("Couldn't get svn revision in " + project_path)

for line in info_lines:
if line.startswith(revision_prefix):
return line[len(revision_prefix):]

raise Exception("Couldn't get svn revision in " + project_path)
def is_svn_repo(path):
return process_utils.check_call('svn info', path)

0 comments on commit 61a1db1

Please sign in to comment.