-
Notifications
You must be signed in to change notification settings - Fork 7
/
to_xls.py
184 lines (147 loc) · 6.21 KB
/
to_xls.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
import json
import pandas as pd
import os
from datetime import datetime, UTC
# Tabs in the resulting report sheet
REPOS_ALL_TAB = '1 All repositories'
COMPONENT_COUNT_TAB = '2 Component Count'
COMPONENT_DETAILS_TAB = '3 Component Details'
do_collect = False # Set to True to trigger the collection from here
collect_args = "-j " # Add -p to also collect private repos
if do_collect:
print("----------- running the collector code -------")
if '-p' not in collect_args:
print("-- for public repos only --")
os.system('node src/static-analysis/cli.js collect %s' % collect_args)
print("----------- collector done -------------------")
# Find the directory with the csv files
today = datetime.now(UTC)
today_str = "%04d-%02d-%02d" % (today.year, today.month, today.day)
in_dir = "stats-static/" + today_str
# Helper method to clean up cell values by
# turning 'undefined' and 'null' into empty cell
def clean_undefined(x) -> str:
if "undefined" in x:
return ''
if "null" in x:
return ''
return x
def write_repos_all_tab():
bikes = []
for file in os.listdir(in_dir):
if file.endswith('.csv'):
print(file)
df = pd.read_csv('%s/%s' % (in_dir, file))
df = df.map(clean_undefined)
df = df.map(lambda x: str.lstrip(x))
bikes.append(df)
df = pd.concat(bikes, axis=0, ignore_index=True)
df = df.rename(str.strip, axis='columns') # strip blanks from all header names
df = df.sort_values(by=['name'], key=lambda col: col.str.lower())
for _index, row in df.iterrows():
nam_s = row['name']
url_s = row['url']
out = '=HYPERLINK("{}","{}")'.format(url_s, nam_s)
row['name'] = out
# Now we can drop the url column
df = df.drop(columns=['url'])
df.to_excel(writer,
sheet_name=REPOS_ALL_TAB,
freeze_panes=tuple([1, 1]),
index=False)
for column in df:
if column == 'name':
column_len = 4 # 'name'
tmp = df[column].astype(str)
for value in tmp.values.tolist():
val = value.split(',')
val = val[1]
val = val.replace('"', '').replace(')', '')
column_len = max(column_len, len(val))
else:
column_len = max(df[column].astype(str).map(len).max(), len(column))
col_idx = df.columns.get_loc(column)
writer.sheets[REPOS_ALL_TAB].set_column(col_idx, col_idx, column_len + 1) # +1 for padding
def write_components_count_tab():
items = []
with open(in_dir + "/_all_product_uses.json") as input_file:
data_all = json.load(input_file)
imports = data_all['imports']
for imp in imports:
print(imp)
import_dict = imports[imp]
prod_count = import_dict['product_count']
total_usage = import_dict['total_usage']
d = {
'Name': imp,
'Product Count': prod_count,
'Total Usage': total_usage}
df = pd.DataFrame(d, index=[imp])
items.append(df)
df = pd.concat(items, axis=0, ignore_index=False)
df = df.sort_values(by=['Name'], key=lambda col: col.str.lower())
df.to_excel(writer,
sheet_name=COMPONENT_COUNT_TAB,
freeze_panes=tuple([1, 1]),
index=False)
for column in df:
col_idx = df.columns.get_loc(column)
column_len = max(df[column].astype(str).map(len).max(), len(column))
writer.sheets[COMPONENT_COUNT_TAB].set_column(col_idx, col_idx, column_len + 1) # +1 for padding
def write_components_details_tab():
items = []
with open(in_dir + "/_all_product_uses.json") as input_file:
data_all = json.load(input_file)
imports = data_all['imports']
imported_components = {}
for imp in imports: # Imp is the toplevel import like 'Button'
import_dict = imports[imp]
for product in import_dict: # product is the product/project that uses the Button like MigrationToolkit
if product in ['product_count', 'total_usage']:
continue
print(product)
prod_dict = import_dict[product]
for pf_component in prod_dict:
if pf_component in ['unique_import_paths', 'repo_usage']:
continue
if pf_component.endswith('/'):
pf_component = pf_component.rstrip('/')
txt = imp + ":" + pf_component
if txt in imported_components.keys():
imported_components[txt].append(product)
else:
imported_components[txt] = [ product]
for imported_component in imported_components:
component_list = imported_components[imported_component]
component_count = len(component_list)
imp = imported_component.split(':')[0]
pf_component = imported_component.split(':')[1]
d = {
'Name': imp,
'Import Path': pf_component,
'Count' : component_count,
'Products': ", ".join(component_list)
}
df = pd.DataFrame(d, index=[imp])
items.append(df)
df = pd.concat(items, axis=0, ignore_index=False)
df = df.sort_values(by=['Name', 'Import Path'], key=lambda col: col.str.lower())
df.to_excel(writer,
sheet_name=COMPONENT_DETAILS_TAB,
freeze_panes=tuple([1, 1]),
index=False)
for column in df:
col_idx = df.columns.get_loc(column)
column_len = max(df[column].astype(str).map(len).max(), len(column))
writer.sheets[COMPONENT_DETAILS_TAB].set_column(col_idx, col_idx, column_len + 1) # +1 for padding
if __name__ == "__main__":
# Action starts here
today_report_dir = 'reports/%s' % today_str
os.makedirs(today_report_dir, exist_ok=True)
report_name = "%s/%s.xlsx" % (today_report_dir, 'pf_report')
writer = pd.ExcelWriter(report_name, engine='xlsxwriter')
write_repos_all_tab()
write_components_count_tab()
write_components_details_tab()
writer.close()
print("Output is in %s " % report_name)