-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg-process.jl
282 lines (261 loc) · 9.4 KB
/
pkg-process.jl
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#######################################################################
# Processing package source code for type annotations
###############################
#
# TODO collection ana analysis of type annotations
#
#######################################################################
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Collecting type annotations
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const TYPE_ANNS_FNAME = "type-annotations.csv"
const TYPE_ANNS_ANALYSIS_FNAME = "analyzed-type-annotations.csv"
const TYPE_ANNS_SUMMARY_FNAME = "summary.csv"
const INTR_TYPE_ANNS_FNAME = "interesting-type-annotations.csv"
const USESITE_TYPE_ANNS_FNAME = "non-use-site-type-annotations.csv"
collectAndSaveTypeAnns2CSV(
pkgsDirPath :: AbstractString, destDirPath :: AbstractString
) = begin
if !isdir(pkgsDirPath)
@error "Packages directory doesn't exist: $pkgsDirPath"
return nothing
end
isdir(destDirPath) || mkdir(destDirPath)
pkgsWithPaths = map(
pkgDir -> (pkgDir, joinpath(pkgsDirPath, pkgDir)),
readdir(pkgsDirPath)
)
pkgsWithPaths = filter(pkg -> isdir(pkg[2]), pkgsWithPaths)
processPkg((pkgDir, pkgPath)) = begin
@status "Processing $pkgDir..."
destPkgInfoPath = joinpath(destDirPath, pkgDir)
isdir(destPkgInfoPath) || mkdir(destPkgInfoPath)
tyAnnFilePath = joinpath(destPkgInfoPath, TYPE_ANNS_FNAME)
pkgLog = collectAndSavePkgTypeAnns2CSV(pkgPath, tyAnnFilePath)
@status "$pkgDir done"
pkgDir => pkgLog
end
mapfunc = nprocs() > 1 ? pmap : map
Dict(mapfunc(processPkg, pkgsWithPaths))
end
collectAndSavePkgTypeAnns2CSV(
pkgPath :: AbstractString, destFilePath :: AbstractString
) = begin
# make sure pkgPath ends with "/" for consistency
endswith(pkgPath, "/") ||
(pkgPath *= "/")
# handy for extracting paths within the package
pkgPathLen1 = length(pkgPath) + 1
filesLog = Dict(:succ => String[], :fail => String[])
destFileIO = open(destFilePath, "w")
# recursively walk all Julia files in the package
try
write(destFileIO, "File,Function,Kind,TypeAnnotation\n")
for (pkgSubDir, _, files) in walkdir(pkgPath)
collectAndWritePkgDirTypeAnns2IO!(
pkgPathLen1, pkgSubDir, files,
destFileIO, filesLog
)
end
catch err
@error "Problem when processing $pkgPath" err
finally
close(destFileIO)
end
filesLog
end
collectAndWritePkgDirTypeAnns2IO!(
pkgPathLen1 :: Int, pkgSubdir :: AbstractString, files :: Vector,
destFileIO :: IOStream, filesLog :: Dict{Symbol, Vector{String}}
) = begin
for fileName in files
filePath = joinpath(pkgSubdir, fileName)
# process only Julia files
isfile(filePath) && isJuliaFile(filePath) || continue
try
collectAndWritePkgFileTypeAnns2IO!(pkgPathLen1, filePath, destFileIO)
push!(filesLog[:succ], filePath)
catch err
@error "Problem when processing $filePath" err
push!(filesLog[:fail], filePath)
end
end
end
collectAndWritePkgFileTypeAnns2IO!(
pkgPathLen1 :: Int, jlFilePath :: AbstractString,
destFileIO :: IOStream
) = begin
reversedTypeAnns = parseAndCollectTypeAnnotations(jlFilePath)
jlFilePathInPkg = jlFilePath[pkgPathLen1:end]
for tyAnn in reverse(reversedTypeAnns)
#=write(
destFileIO,
jlFilePathInPkg * "," * csvLineString(tyAnn)
)=#
for info in [jlFilePathInPkg, tyAnn.funName, tyAnn.kind]
show(destFileIO, string(info))
write(destFileIO, ",")
end
show(destFileIO, string(tyAnn.tyExpr))
write(destFileIO, "\n")
end
end
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Analysing type annotations
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
"Should coincide with the new columns in `addTypeAnnsAnalysis!`"
const ANALYSIS_COLS = [
:Error, :Warning,
:VarCnt, :HasWhere, :VarsUsedOnce, :UseSiteVariance,
:ImprUseSiteVariance, :RestrictedScope, :ClosedLowerBound
]
analyzePkgTypeAnnsAndSave2CSV(
pkgsDirPath :: AbstractString
) = begin
if !isdir(pkgsDirPath)
@error "Packages directory doesn't exist: $pkgsDirPath"
return nothing
end
pkgsWithPaths = map(
pkgDir -> (pkgDir, joinpath(pkgsDirPath, pkgDir)),
readdir(pkgsDirPath)
)
pkgsWithPaths = filter(pkg -> isdir(pkg[2]), pkgsWithPaths)
processPkg((pkgDir, pkgPath)) = begin
@status "Processing $pkgDir..."
analyzePkgTypeAnns(pkgPath)
end
mapfunc = nprocs() > 1 ? pmap : map
pkgResults = mapfunc(processPkg, pkgsWithPaths)
combineResults(d1, d2) = begin
d = Dict{Symbol, Any}()
for key in [:goodPkg, :badPkg, :totalta, :statsums]
d[key] = d1[key] + d2[key]
end
d[:statnames] = d1[:statnames]
for key in [:pkgwarn, :pkgusesite, :pkgintr, :tasintr, :tasusvar]
d[key] = vcat(d1[key], d2[key])
end
d
end
rslt = reduce(combineResults, pkgResults)
try
CSV.write(joinpath(pkgsDirPath, INTR_TYPE_ANNS_FNAME), rslt[:tasintr])
CSV.write(joinpath(pkgsDirPath, USESITE_TYPE_ANNS_FNAME), rslt[:tasusvar])
catch err
@error "Problem when saving interesting type annotations" err
end
rslt
end
analyzePkgTypeAnns(pkgPath :: AbstractString) = begin
failedResult = Dict(
:goodPkg => 0,
:badPkg => 1,
:totalta => 0,
:statnames => ANALYSIS_COLS,
:statsums => fill(0, length(ANALYSIS_COLS)),
:pkgwarn => [],
:pkgusesite => [],
:pkgintr => [],
:tasintr => DataFrame(),
:tasusvar => DataFrame(),
)
if !isdir(pkgPath)
@error "Packages directory doesn't exist: $pkgPath"
return failedResult
end
typeAnnsPath = joinpath(pkgPath, TYPE_ANNS_FNAME)
if !isfile(typeAnnsPath)
@error "Type annotations file doesn't exist: $typeAnnsPath"
return failedResult
end
try
df = CSV.read(typeAnnsPath, DataFrame; escapechar='\\')
df = addTypeAnnsAnalysis!(df)
dfSumm = summarizeTypeAnnsAnalysis(df)
CSV.write(
joinpath(pkgPath, TYPE_ANNS_ANALYSIS_FNAME),
#df[:, [:File, :Function, :Kind, :TypeAnnotation, :Error, :Warning, :VarCnt, :HasWhere, :VarsUsedOnce, :UseSiteVariance, :RestrictedScope]]
df[:, Not("TypeVarsSummary")]
)
CSV.write(joinpath(pkgPath, TYPE_ANNS_SUMMARY_FNAME), dfSumm)
errOrWarn = dfSumm.sum[1] > 0 || dfSumm.sum[2] > 0
totalta = size(df, 1)
strongRestrictionFailed = any(
ind -> dfSumm.sum[ind] < totalta,
[7, 8, 9]
)
dfta = df[.!(ismissing.(df.ImprUseSiteVariance)) .&&
(.!df.ImprUseSiteVariance .|| .!df.RestrictedScope .|| .!df.ClosedLowerBound), :]
dfus = df[.!(ismissing.(df.UseSiteVariance)) .&& .!df.UseSiteVariance, :]
dfta.Package = fill(pkgPath, size(dfta, 1))
dfus.Package = fill(pkgPath, size(dfus, 1))
Dict(
:goodPkg => 1,
:badPkg => 0,
:totalta => totalta,
:statnames => dfSumm.variable,
:statsums => dfSumm.sum,
:pkgwarn => errOrWarn ? [pkgPath] : [],
:pkgusesite => dfSumm.sum[6] < totalta ? [pkgPath] : [],
:pkgintr => strongRestrictionFailed ? [pkgPath] : [],
:tasintr => dfta,
:tasusvar => dfus,
)
catch err
@error "Problem when processing CSVs" err
failedResult
end
end
addTypeAnnsAnalysis!(df :: DataFrame) = begin
df.UnrolledTypeAnnotation = ByRow(tastr ->
try
string(transformShortHand(Meta.parse(tastr)).expr)
catch err
@error "Couldn't transform type annotation" tastr err
missing
end
)(df.TypeAnnotation)
df.TypeVarsSummary = ByRow(tastr -> (ismissing(tastr) ? missing :
try
collectTyVarsSummary(Meta.parse(tastr))
catch err
@error "Couldn't analyze type annotation" tastr err
missing
end)
)(df.UnrolledTypeAnnotation)
df.Error = ByRow(ismissing)(df.TypeVarsSummary)
df.Warning = ByRow(
tasumm -> ismissing(tasumm) ? true : tasumm[2]
)(df.TypeVarsSummary)
df.VarCnt = mkDFAnalysisFunction(length, df.TypeVarsSummary)
df.HasWhere = ByRow(
varcnt -> ismissing(varcnt) ? missing : varcnt > 0
)(df.VarCnt)
for (col, fun) in [
:VarsUsedOnce => tyVarUsedOnce,
:UseSiteVariance => tyVarOccursAsUsedSiteVariance,
:ImprUseSiteVariance => tyVarOccursAsImpredicativeUsedSiteVariance,
:RestrictedScope => tyVarRestrictedScopePreserved,
:ClosedLowerBound => tyVarIsNotInLowerBound,
]
df[!, col] = mkDFAnalysisFunction(fun, df.TypeVarsSummary)
end
df
end
mkDFAnalysisFunction(fun :: Function, dfrow) = ByRow(tasumm ->
ismissing(tasumm) ?
missing :
try
fun(tasumm[1])
catch err
@error "Couldn't analyze $(Symbol(fun)) for type vars summary" tasumm[1] err
missing
end
)(dfrow)
summarizeTypeAnnsAnalysis(df :: DataFrame) = describe(df,
:mean, :min, :median, :max,
:nmissing,
sum => :sum,
cols = ANALYSIS_COLS
)