-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon_functions.py
194 lines (148 loc) · 4.77 KB
/
common_functions.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
# Copyright 2022 VMware, Inc.
# SPDX-License-Identifier: BSD-2-Clause
"""Common Functions
This file contains some common functions that are used within
the other scripts in this repo.
This file can also be imported as a module.
"""
def read_key(file_name):
"""Retrieves a GitHub API key from a file.
Parameters
----------
file_name : str
Returns
-------
key : str
"""
from os.path import dirname, join
# Reads the first line of a file containing the GitHub API key
# Usage: key = read_key('gh_key')
current_dir = dirname(__file__)
file2 = "./" + file_name
file_path = join(current_dir, file2)
with open(file_path, 'r') as kf:
key = kf.readline().rstrip() # remove newline & trailing whitespace
return key
def read_orgs(file_name):
"""Retrieves a list of orgs from a file.
Parameters
----------
file_name : str
Returns
-------
org_list : list
"""
import csv
org_list = []
with open(file_name) as orgfile:
orgs = csv.reader(orgfile)
for row in orgs:
org_list.append(row[0])
return org_list
def read_file(file_name):
"""Retrieves a list from a file.
Parameters
----------
file_name : str
Returns
-------
a_list : list
"""
import csv
content_list = []
with open(file_name) as in_file:
content = csv.reader(in_file)
for row in content:
content_list.append(row[0])
return content_list
def expand_name_df(df,old_col,new_col):
"""Takes a dataframe df with an API JSON object with nested elements in old_col,
extracts the name, and saves it in a new dataframe column called new_col
Parameters
----------
df : dataframe
old_col : str
new_col : str
Returns
-------
df : dataframe
"""
import pandas as pd
def expand_name(nested_name):
"""Takes an API JSON object with nested elements and extracts the name
Parameters
----------
nested_name : JSON API object
Returns
-------
object_name : str
"""
if pd.isnull(nested_name):
object_name = 'Not Found'
else:
object_name = nested_name['name']
return object_name
df[new_col] = df[old_col].apply(expand_name)
return df
def get_criticality(org_name, repo_name, api_token):
"""See https://github.com/ossf/criticality_score for more details
This function requires that you have version 1.0.7 of this tool
installed (the older Python version but not the final Python version,
which doesn't work for some reason within the script - possibly because
of how they've implemented the deprecation warnings). You can install
the correct version using:
pip install criticality-score==1.0.7
Parameters
----------
org_name : str
repo_name : str
api_token : str
Returns
-------
dependents_count : str
Numeric integer that is returned as a string
criticality_score : str
This value ranges from 0 to 1 (like a float) with lower scores indicating less critical projects.
"""
import subprocess
import os
os.environ['GITHUB_AUTH_TOKEN'] = api_token
cmd_str = 'criticality_score --repo github.com/' + org_name + '/' + repo_name + ' --format csv'
try:
proc = subprocess.Popen(cmd_str, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
out, err = proc.communicate()
if not err:
csv_str = out.decode("utf-8")
items = csv_str.split(',')
dependents_count = items[25]
criticality_score = items[26].rstrip()
else:
dependents_count = None
criticality_score = None
except:
dependents_count = None
criticality_score = None
return dependents_count, criticality_score
def create_file(pre_string):
"""Creates an output file in an "output" directory with today's date
as part of the filename and prints the file_path to the terminal to
make it easier to open the output file.
Parameters
----------
pre_string : str
This is the string that will preface today's date in the filename
Returns
-------
file : file object
file_path : str
This is the full path to the file name for the output.
"""
from datetime import datetime
from os.path import dirname, join
today = datetime.today().strftime('%Y-%m-%d')
output_filename = "./output/" + pre_string + "_" + today + ".csv"
current_dir = dirname(__file__)
file_path = join(current_dir, output_filename)
file = open(file_path, 'w', newline ='')
print("Output file:\n", file_path, sep="")
return file, file_path