-
Notifications
You must be signed in to change notification settings - Fork 0
/
requirements.py
executable file
·154 lines (117 loc) · 4.67 KB
/
requirements.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging,os,sys, git
import argparse
'''
This script requires the next python requirements:
- gitpython
Also a git command line installed in the servers
This script was tested with gitpython == 2.1.11 and git 2.24.2 using MacOS 10.15.04
'''
def prepareLoggin():
# TO-DO prepare loggin from config file/ cmd line args
FORMAT_FILE = '%(asctime)-15s %(levelname)s %(message)s'
FORMAT_CONSOLE = '%(levelname)s %(message)s'
rootLogger = logging.getLogger()
# file handler
# fileHandler = logging.FileHandler('requirements_download.log')
# fileHandler.setFormatter(logging.Formatter(FORMAT_FILE))
# rootLogger.addHandler(fileHandler)
#console handler
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logging.Formatter(FORMAT_CONSOLE))
rootLogger.addHandler(consoleHandler)
rootLogger.setLevel(logging.INFO)
def process_args():
parser = argparse.ArgumentParser(description='Download roles from an ansible-galaxy-style requirements file')
parser.add_argument('-r',
dest='reqs_file',
action='store',
type=str,
required=False,
default='requirements.yml',
help='file paths with the requiremets to download')
return parser.parse_args()
class DownloadRequirements():
def __init__(self, requirements_file):
'''Initializes class. '''
self.working_directory = os.path.dirname(os.path.realpath(__file__))
self.roles_directory = self.working_directory + "/roles"
self.script_name = os.path.basename(__file__)
self.requirements_file = requirements_file
logging.log(logging.DEBUG, self.working_directory)
self.read_config_file_yml()
def read_config_file_yml(self):
import yaml
fName = self.working_directory + str("/") + self.requirements_file
if os.path.exists(fName):
with open(fName, 'r') as stream:
try:
self.role_requirements = yaml.load(stream, Loader=yaml.FullLoader)
except yaml.YAMLError as exc:
logging.log(logging.ERROR, exc)
logging.log(logging.ERROR, "Aborting execution.")
exit(1)
else:
logging.log(logging.ERROR, "No file exists: " + fName + " aborting execution.")
# logging.log(logging.ERROR, "A file named requirements.yml must exists along with " + self.script_name + ".")
exit(1)
def create_directory(self,directory):
if not self.exists_directory(directory):
try:
os.makedirs(directory)
except OSError as exc:
logging.log(logging.ERROR, exc)
return False
return True
def exists_directory(self,directory):
if os.path.exists(directory):
return True
return False
def run(self):
'''iterate over configuration and download the repositories'''
if not self.create_directory(self.roles_directory):
logging.log(logging.ERROR, "Aborting execution. ")
exit(1)
for role in self.role_requirements:
if (not "name" in role) or (not "src" in role):
logging.log(logging.INFO, "role config line not understand: " + str(role) + " must provide 'name' and 'src' keys. Ignoring it.")
continue
method,git_url = role['src'].split('+')
role_dir = self.roles_directory + "/" + role['name']
if not "version" in role:
logging.log(logging.DEBUG, "No version in requirements.yml for " + role['name'] + " using 'master' ")
version="master"
else:
version=role['version']
if not self.exists_directory(role_dir):
logging.log(logging.INFO, "cloning: " + git_url + " into: " + role_dir)
try:
git.Git(self.roles_directory).clone(git_url, role_dir)
role_repo = git.Repo(role_dir)
role_repo.git.checkout(version)
except git.exc.GitError as exc:
logging.log(logging.ERROR, exc)
logging.log(logging.ERROR, "Error cloning o doing checkout: " + role['name'])
continue
else:
# Existing directory. Find out if it has a git repo and it is in the right branch
try:
role_repo = git.Repo(role_dir)
branch_name = role_repo.git.rev_parse("--abbrev-ref","HEAD")
status = role_repo.git.status("--porcelain")
except git.exc.GitError as exc:
logging.log(logging.ERROR, exc)
logging.log(logging.ERROR, "Not a valid Git repository? " + role['name'])
continue
if branch_name != version:
logging.log(logging.WARNING, "Current branch: " + branch_name + " and version: " + version + " differs for role " + role['name'])
if status != "":
logging.log(logging.WARNING, "Current role: " + role['name'] + " has unstagged/uncommited changes")
if __name__ == "__main__":
pargs = process_args()
prepareLoggin()
logging.log(logging.INFO, 'Starting download requirements...')
download = DownloadRequirements(pargs.reqs_file)
download.run()
exit(0)