Commit | Line | Data |
---|---|---|
bcc7759d JR |
1 | import pytest |
2 | import os | |
3 | import yaml | |
4 | import logging | |
5 | import urllib.parse | |
6 | ||
7 | from git import Repo | |
8 | ||
9 | default_git_remote_dir = "./git_remote" | |
10 | ||
11 | ||
12 | def is_ref_branch(repo, ref): | |
13 | try: | |
14 | repo.remote().refs[ref] | |
15 | is_branch = True | |
16 | except: | |
17 | is_branch = False | |
18 | ||
19 | return is_branch | |
20 | ||
21 | ||
22 | def is_ref_tag(repo, ref): | |
23 | try: | |
24 | repo.tags[ref] | |
25 | is_tag = True | |
26 | except: | |
27 | is_tag = False | |
28 | ||
29 | return is_tag | |
30 | ||
31 | ||
32 | def is_ref_commit(repo, ref): | |
33 | try: | |
34 | Repo.rev_parse(repo, ref) | |
35 | is_commit = True | |
36 | except: | |
37 | is_commit = False | |
38 | ||
39 | return is_commit | |
40 | ||
41 | ||
42 | def logging_setup(): | |
43 | logger_format = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s' | |
44 | logging.basicConfig(level=logging.DEBUG, | |
45 | format=logger_format, | |
46 | datefmt='%m-%d %H:%M', | |
47 | filename='./debug.log', | |
48 | filemode='w') | |
49 | # define a Handler which writes INFO messages or higher to the sys.stderr | |
50 | console = logging.StreamHandler() | |
51 | console.setLevel(logging.DEBUG) | |
52 | # set a format which is simpler for console use | |
53 | formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') | |
54 | # tell the handler to use this format | |
55 | console.setFormatter(formatter) | |
56 | # add the handler to the root logger | |
57 | logging.getLogger('').addHandler(console) | |
58 | ||
59 | ||
60 | logging_setup() | |
61 | ||
62 | # Remote setup | |
63 | logger_git = logging.getLogger('setup.git') | |
64 | ||
65 | # Fetch local base repository | |
66 | with open("config.yaml", 'r') as stream: | |
67 | config = yaml.load(stream) | |
68 | ||
69 | # Retrieve all possibles remotes and clean url for path | |
70 | remotes = {} | |
71 | for project, markers in config.items(): | |
72 | if markers is None: | |
73 | continue | |
74 | for marker in markers: | |
75 | url = marker['url'] | |
76 | url2path = urllib.parse.quote_plus(url) | |
77 | path = os.path.abspath(default_git_remote_dir + '/' + url2path) | |
78 | remotes[url] = path | |
79 | ||
80 | logger_git.info('Remotes to be fetched {}'.format(remotes)) | |
81 | ||
82 | if not os.path.isdir(default_git_remote_dir): | |
83 | os.mkdir(default_git_remote_dir) | |
84 | ||
85 | # Fetch the remote | |
86 | for url, path in remotes.items(): | |
87 | if os.path.exists(path): | |
88 | if not os.path.isdir(path): | |
89 | logger_git.error('Remote path {} exists and is not a folder'.format(path)) | |
90 | exit() | |
91 | repo = Repo(path) | |
92 | else: | |
93 | repo = Repo.clone_from(url, path) | |
94 | ||
95 | # TODO: might be necessary to actually update the base branch, to validate | |
96 | repo.remote().fetch() | |
97 | ||
98 | # Create marker definition for test runners | |
99 | runnable_markers = {} | |
100 | for project, markers in config.items(): | |
101 | if markers is None: | |
102 | continue | |
103 | for marker in markers: | |
104 | name = marker['marker'] | |
105 | ref = marker['ref'] | |
106 | url = marker['url'] | |
107 | path = remotes[url] | |
108 | repo = Repo(path) | |
109 | ||
110 | git_object = None | |
111 | if is_ref_branch(repo, ref): | |
112 | git_object = Repo.rev_parse(repo, repo.remote().refs[ref].name) | |
113 | elif is_ref_tag(repo, ref): | |
114 | git_object = repo.tags[ref].commit | |
115 | elif is_ref_commit(repo, ref): | |
116 | git_object = repo.commit(ref) | |
117 | ||
118 | if git_object is None: | |
119 | logger_git.error('Invalid git reference for marker "{}"'.format(name)) | |
120 | exit(1) | |
121 | ||
122 | logger_git.info('Marker:{: <30} Sha1 {: <20}'.format(name, git_object.hexsha)) | |
123 | ||
124 | if name in runnable_markers: | |
125 | logger_git.error('Duplicate for entry for marker "{}"'.format(name)) | |
126 | exit(1) | |
127 | ||
128 | runnable_markers[name] = { | |
129 | 'project': project, | |
130 | 'sha1': git_object.hexsha, | |
131 | 'url': url, | |
132 | 'path': path | |
133 | } | |
134 | ||
135 | with open('run_configuration.yaml', 'w') as run_configuration: | |
136 | yaml.dump(runnable_markers, run_configuration, default_flow_style=False) |