forked from gitter-badger/SIERRA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage_submodules.py
74 lines (70 loc) · 3.44 KB
/
manage_submodules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os.path
from os import path
import subprocess
import sys
project_dir = os.getcwd()
project_branch = subprocess.check_output('git branch --show-current', shell=True).decode(sys.stdout.encoding)
default_module_branch = 'dev'
modules_branches = {}
modules = {
"admission": "[email protected]:uclouvain/osis-admission.git",
"assistant": "[email protected]:uclouvain/osis-assistant.git",
"continuing_education": "[email protected]:uclouvain/osis-continuing-education.git",
"dissertation": "[email protected]:uclouvain/osis-dissertation.git",
"internship": "[email protected]:uclouvain/osis-internship.git",
"osis_common": "[email protected]:uclouvain/osis-common.git",
"partnership": "[email protected]:uclouvain/osis-partnership.git",
}
arguments = len(sys.argv) - 1
if arguments > 0:
default_module_branch = sys.argv[1]
if arguments > 1:
if arguments % 2 == 0:
raise Exception("Le nombre d'arguments doit être impair : "
"la branche par défaut en premier et un suite de couple nom_dule/branhc_module")
idx = 2
while idx < arguments:
module_name = sys.argv[idx]
if module_name not in modules:
raise Exception("Le module {} n'est pas dans la liste des modules".format(module_name))
idx = idx + 1
module_branch = sys.argv[idx]
modules_branches[module_name] = module_branch
idx = idx + 1
elif project_branch in ['dev', 'test', 'qa', 'master']:
default_module_branch = project_branch
print('Project Dir : {}'.format(project_dir))
print('Project Branch : {}'.format(project_branch))
print('Module Branch : {}'.format(default_module_branch))
print('')
for module, git_url in modules.items():
module_dir = os.path.join(project_dir, module)
specific_module_branch = modules_branches.get(module, default_module_branch)
if path.exists(module_dir) and path.isdir(module_dir):
print('')
print('Module {} exists in project directory'.format(module))
print('Updating module {} from branch {}'.format(module, default_module_branch))
fetch_command = 'git fetch origin {branch}'.format(branch=specific_module_branch)
switch_branch_command = 'git checkout {branch}'.format(branch=specific_module_branch)
new_branch_command = 'git checkout -b {branch} origin/{branch}'.format(branch=specific_module_branch)
check_branch_exists_locally_command = ' git rev-parse --verify {branch}'.format(branch=specific_module_branch)
pull_command = 'git pull'
os.chdir(module_dir)
os.system(fetch_command)
try:
command_status = subprocess.check_call(check_branch_exists_locally_command.split())
os.system(switch_branch_command)
except subprocess.CalledProcessError:
os.system(new_branch_command)
os.system(pull_command)
os.chdir(project_dir)
else:
print('')
print('Module {} not exists in project directory'.format(module))
print('Cloning module {} from branch {} in directory {}'.format(module, specific_module_branch, module_dir))
command = 'git clone {git_url} -b {branch} {directory}'.format(branch=specific_module_branch,
git_url=git_url,
directory=module_dir)
os.system(command)