-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_config.py
211 lines (170 loc) · 6.31 KB
/
generator_config.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
Readme generator for ShotGrid configurations
"""
import subprocess
from pathlib import Path
import semver
import yaml
import utils
def _get_include(base_filepath: Path, filename: str, info_key: str) -> str:
"""
Get the information for an env/include file.
Args:
base_filepath: Filepath to info.yml
filename: Filename to look for
info_key: Key to use for info in file
Returns:
Include section with version status table
"""
filepath = base_filepath.parent / "env" / "includes" / f"{filename}.yml"
if not filepath.is_file():
return ""
with open(str(filepath), "r") as file:
info = yaml.safe_load(file)
if info is None:
return ""
info = utils.yaml_to_nested_dict(info)[info_key]
items = []
rows = []
for key, value in info.items():
item = {**value["location"], "key": key}
if item["type"] == "app_store":
item["repo"] = f"ShotGunSoftware/{item['name']}"
elif item["type"] == "github_release":
item["repo"] = f"{item['organization']}/{item['repository']}"
else:
continue
print(
f"[{utils.capitalize(info_key)}] Getting latest version for {item['repo']}..."
)
repo_url = f"https://github.com/{item['repo']}.git"
output_lines = subprocess.check_output(
[
"git",
"ls-remote",
"--tags",
"--refs",
"--sort=version:refname",
repo_url,
],
encoding="utf-8",
).splitlines()
latest_version = output_lines[-1].rpartition("/")[-1]
source_version = None
if item["type"] == "github_release":
try:
source_lines = subprocess.check_output(
[
"git",
"ls-remote",
"--tags",
"--refs",
"--sort=version:refname",
f"https://github.com/ShotGunSoftware/{item['repository']}.git",
],
encoding="utf-8",
).splitlines()
source_version = source_lines[-1].rpartition("/")[-1]
except:
pass
item["latest"] = latest_version
if source_version is not None:
compare_version = source_version
else:
compare_version = latest_version
version = item["version"]
if version.startswith("v"):
version = version[1:]
if compare_version.startswith("v"):
compare_version = compare_version[1:]
version_count = version.count(".")
compare_count = compare_version.count(".")
if version_count != compare_count:
if version_count > 2:
parts = version.split(".")
version = ".".join(parts[0:3])
if compare_count > 2:
parts = compare_version.split(".")
compare_version = ".".join(parts[0:3])
else:
if version_count > 2:
parts = version.split(".")
version = ".".join(parts[-3:])
parts = compare_version.split(".")
compare_version = ".".join(parts[-3:])
try:
item["compared"] = semver.compare(version, compare_version)
except:
item["compared"] = None
items.append(item)
color = "blue"
if item["compared"] == -1:
if source_version is None:
color = "red"
else:
color = "orange"
if item["compared"] == 1:
color = "green"
if source_version is None:
rows.append(
[
item["key"],
f"![{item['version']}](https://img.shields.io/badge/{item['version']}-{color})",
"",
f"[![{item['repo']}](https://img.shields.io/github/v/tag/{item['repo']})](https://github.com/{item['repo']})",
]
)
else:
rows.append(
[
item["key"],
f"![{item['version']}](https://img.shields.io/badge/{item['version']}-{color})",
f"[![{item['repo']}](https://img.shields.io/github/v/tag/{item['repo']})](https://github.com/{item['repo']})",
f"[![ShotGunSoftware/{item['repository']}](https://img.shields.io/github/v/tag/ShotGunSoftware/{item['repository']})](https://github.com/ShotGunSoftware/{item['repository']})",
]
)
title = utils.capitalize(info_key)
return f"## {title}\n\n" + utils.make_table(
["Name", "Current", "Fork", "Latest"], rows
)
def generate_config_readme(
filepath: Path, prepend: str = None, append: str = None
) -> str:
"""
Generate a readme file for a ShotGrid framework/engine/app
Args:
filepath: File path to info.yml
prepend: File path to Markdown file to prepend in Readme
append: File path to Markdown file to append in Readme
Returns:
Markdown readme
"""
with open(str(filepath), "r") as file:
info = yaml.safe_load(file)
readme = ""
readme += utils.get_header(info, filepath)
# Prepend readme file
if prepend is not None and prepend != "":
prepend_filepath = Path(prepend)
if prepend_filepath.is_file():
with open(prepend_filepath, "r") as prepend_file:
readme += prepend_file.read()
readme += "\n\n"
readme += "## Requirements\n\n"
readme += utils.get_general_requirements(info)
print("Getting framework info...")
readme += _get_include(filepath, "frameworks", "frameworks")
readme += "\n\n"
print("Getting engine info...")
readme += _get_include(filepath, "engine_locations", "engines")
readme += "\n\n"
print("Getting app info...")
readme += _get_include(filepath, "app_locations", "apps")
# Append readme file
if append is not None and append != "":
append_filepath = Path(append)
if append_filepath.is_file():
with open(append_filepath, "r") as append_file:
readme += "\n\n"
readme += append_file.read()
return readme