-
Notifications
You must be signed in to change notification settings - Fork 11
/
cop_bazaar.py
executable file
·175 lines (145 loc) · 6.55 KB
/
cop_bazaar.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
#!/usr/bin/env python3
import logging
import os
import pprint
import requests
import sys
import urllib
import yaml
from datetime import datetime
from model.repository import Repository
from model.category import Category
CONFIG = "config.yaml"
class MarkDownOutputGenerator(object):
__OUTPUT_DIR = "output"
__SORT_BY_STARS = "Stars"
__SORT_BY_LAST_UPDATED = "Last Updated"
def __category_basename(self, category, sort_by):
return ("%s.%s.md" % (category.title, sort_by)).replace('/', '_')
def __category_link(self, category, sort_by):
return urllib.parse.quote(self.__category_basename(category, sort_by))
def __category_filename(self, category, sort_by):
basename = self.__category_basename(category, sort_by)
filename = ("%s/%s" % (self.__OUTPUT_DIR, basename))
return filename
def __add_sort_by_link(self, out_file, category, sort_by):
if sort_by == self.__SORT_BY_STARS:
out_file.write("[Sort by Last Updated](%s)" % (
self.__category_link(category, self.__SORT_BY_LAST_UPDATED)))
else:
out_file.write("[Sort by Stars](%s)" % (
self.__category_link(category, self.__SORT_BY_STARS)))
def __write_category(self, category, sort_by):
fname = self.__category_filename(category, sort_by)
logging.info("Writing %s", fname)
try:
out_file = open(fname, 'w')
try:
out_file.write("# %s by %s\n" % (category.title, sort_by))
out_file.write("\n")
if sort_by == self.__SORT_BY_STARS:
sorted_repositories = sorted(
category.repositories,
key=lambda repo: repo.data['stargazers_count'],
reverse=True)
else:
sorted_repositories = sorted(
category.repositories,
key=lambda repo: repo.data['pushed_at'],
reverse=True)
self.__add_sort_by_link(out_file, category, sort_by)
out_file.write("\n")
out_file.write("\n")
out_file.write("Name | Description | Last Updated | Stars \n")
out_file.write("--- | --- | --- | --- \n")
for repo in sorted_repositories:
description = repo.data['description']
if description is not None:
description = description.replace("|", "\|")
out_file.write("[%s](%s) | %s | %s | %s \n" % (
repo.data['repo_path'],
repo.data['html_url'],
description,
repo.data['pushed_at'][0:len('2020-01-01')],
repo.data['stargazers_count']
))
out_file.write("\n")
self.__add_sort_by_link(out_file, category, sort_by)
finally:
out_file.close()
except:
logging.exception("Failed to write file '" + fname + "'")
def __generate_category(self, category):
self.__write_category(category, self.__SORT_BY_STARS)
self.__write_category(category, self.__SORT_BY_LAST_UPDATED)
def __generate_categories(self):
for category in Category.all:
logging.info("Generating category %s", category.title)
self.__generate_category(category)
def __generate_front_page(self):
fname = ("%s/%s" % (self.__OUTPUT_DIR, "README.md"))
logging.info("Writing %s", fname)
try:
out_file = open(fname, 'w')
try:
out_file.write(
"# Welcome to OpenShift Bazaar Source Code Index\n")
out_file.write("\n")
out_file.write(
"This is a catalog of OpenShift related projects created by Red Hatters.\n")
out_file.write("\n")
out_file.write("## Choose a category\n")
for category in Category.all:
out_file.write("* [%s](%s) - %s\n" % (category.title, self.__category_link(category, self.__SORT_BY_STARS),
category.desc))
out_file.write("\n")
out_file.write("## Contributing\n")
out_file.write("If you would like to add your project to the catalog, clone the https://github.com/noseka1/cop-bazaar"
" git repository, include your project in the *config.yaml* file and submit your change as a pull request.\n")
out_file.write("\n")
out_file.write("Last updated %s" %
(datetime.now().strftime("%d/%m/%Y %H:%M:%S")))
finally:
out_file.close()
except:
logging.exception("Failed to write file '" + fname + "'")
def generate_output(self):
os.mkdir(self.__OUTPUT_DIR)
self.__generate_front_page()
self.__generate_categories()
class Main(object):
def __init_logging(self):
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
def __load_config(self):
with open(CONFIG) as file:
config = yaml.load(file, Loader=yaml.FullLoader)
return config
def __prepare_data(self, config):
categories = {"all": Category("All", "All projects.")}
for category_yaml in config['categories']:
categories[category_yaml['name']] = Category(
category_yaml['title'], category_yaml['desc'])
for repo_yaml in config['repositories']:
repo = Repository(repo_yaml['url'])
for category_yaml in repo_yaml['categories']:
repo.categories.append(categories[category_yaml])
# grab info from the web
repo.fetchRepoData()
# add repo to the referred categories
for category in repo.categories:
category.repositories.append(repo)
# add repo to the category All
categories['all'].repositories.append(repo)
def main(self):
self.__init_logging()
config = self.__load_config()
self.__prepare_data(config)
MarkDownOutputGenerator().generate_output()
Main().main()