Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/kotlin/app/extractors/CExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ class CExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()
val imports = mutableSetOf<String>()

val regex = Regex("""#include\s+["<](\w+)[/\w+]*\.\w+[">]""")
fileContent.forEach {
val res = regex.find(it)
if (res != null) {
val lineLib = res.groupValues.last()
libraries.add(lineLib)
imports.add(lineLib)
}
}

return libraries.toList()
return imports.toList()
}
}
15 changes: 5 additions & 10 deletions src/main/kotlin/app/extractors/CSharpExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CSharpExtractor : ExtractorInterface {
companion object {
val LANGUAGE_NAME = "cs"
val FILE_EXTS = listOf("cs")
val LIBRARIES = ExtractorInterface.getLibraries("cs")
}

override fun extract(files: List<DiffFile>): List<CommitStats> {
Expand All @@ -20,27 +21,21 @@ class CSharpExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()

// TODO(anatoly): Load file statically.
val csLibraries = File("data/libraries/cs_libraries.txt")
.inputStream().bufferedReader()
.readLines()
.toSet()
val imports = mutableSetOf<String>()

val regex = Regex("""using\s+(\w+[.\w+]*)""")
fileContent.forEach {
val res = regex.find(it)
if (res != null) {
val importedName = res.groupValues[1]
csLibraries.forEach { library ->
LIBRARIES.forEach { library ->
if (importedName.startsWith(library)) {
libraries.add(library)
imports.add(library)
}
}
}
}

return libraries.toList()
return imports.toList()
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/app/extractors/CppExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ class CppExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()
val imports = mutableSetOf<String>()

val regex = Regex("""#include\s+["<](\w+)[/\w+]*\.\w+[">]""")
fileContent.forEach {
val res = regex.find(it)
if (res != null) {
val lineLib = res.groupValues.last()
libraries.add(lineLib)
imports.add(lineLib)
}
}

return libraries.toList()
return imports.toList()
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/app/extractors/ExtractorInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import app.model.DiffFile
import app.model.CommitStats

interface ExtractorInterface {
companion object {
fun getLibraries(name: String): Set<String> {
return ExtractorInterface::class.java.classLoader
.getResourceAsStream("data/libraries/${name}_libraries.txt")
.bufferedReader().readLines().toSet()
}
}

fun extract(files: List<DiffFile>): List<CommitStats> {
files.map { file ->
file.old.imports = extractImports(file.old.content)
Expand All @@ -29,4 +37,6 @@ interface ExtractorInterface {
fun extractImports(fileContent: List<String>): List<String> {
return listOf()
}


}
8 changes: 4 additions & 4 deletions src/main/kotlin/app/extractors/GoExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ class GoExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()
val imports = mutableSetOf<String>()

val singleImportRegex = Regex("""import\s+"(\w+)"""")
fileContent.forEach {
val res = singleImportRegex.find(it)
if (res != null) {
val lineLib = res.groupValues.last()
libraries.add(lineLib)
imports.add(lineLib)
}
}
val multipleImportRegex = Regex("""import[\s\t\n]+\((.+?)\)""",
RegexOption.DOT_MATCHES_ALL)
val contentJoined = fileContent.joinToString(separator = "")
multipleImportRegex.findAll(contentJoined).forEach { matchResult ->
libraries.addAll(matchResult.groupValues.last()
imports.addAll(matchResult.groupValues.last()
.split(Regex("""(\t+|\n+|\s+)"""))
.filter { it.isNotEmpty() }
.map { it -> it.replace("\"", "") })
}

return libraries.toList()
return imports.toList()
}
}
15 changes: 5 additions & 10 deletions src/main/kotlin/app/extractors/JavaExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class JavaExtractor : ExtractorInterface {
companion object {
val LANGUAGE_NAME = "java"
val FILE_EXTS = listOf("java")
val LIBRARIES = ExtractorInterface.getLibraries("java")
}

val KEYWORDS = listOf("abstract", "continue", "for", "new", "switch",
Expand Down Expand Up @@ -56,27 +57,21 @@ class JavaExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()

// TODO(anatoly): Load file statically.
val javaLibraries = File("data/libraries/java_libraries.txt")
.inputStream().bufferedReader()
.readLines()
.toSet()
val imports = mutableSetOf<String>()

val regex = Regex("""import\s+(\w+[.\w+]*)""")
fileContent.forEach {
val res = regex.find(it)
if (res != null) {
val importedName = res.groupValues[1]
javaLibraries.forEach { library ->
LIBRARIES.forEach { library ->
if (importedName.startsWith(library)) {
libraries.add(library)
imports.add(library)
}
}
}
}

return libraries.toList()
return imports.toList()
}
}
14 changes: 4 additions & 10 deletions src/main/kotlin/app/extractors/JavascriptExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package app.extractors

import app.model.CommitStats
import app.model.DiffFile
import java.io.File

class JavascriptExtractor : ExtractorInterface {
companion object {
val LANGUAGE_NAME = "js"
val FILE_EXTS = listOf("js")
val LIBRARIES = ExtractorInterface.getLibraries("js")
}

override fun extract(files: List<DiffFile>): List<CommitStats> {
Expand All @@ -20,20 +20,14 @@ class JavascriptExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()

// TODO(anatoly): Load file statically.
val jsLibraries = File("data/libraries/js_libraries.txt")
.inputStream().bufferedReader()
.readLines()
.toSet()
val imports = mutableSetOf<String>()

val splitRegex =
Regex("""\s+|,|;|:|\\*|\n|\(|\)|\\[|]|\{|}|\+|=|\.|>|<|#|@|\$""")
val fileTokens = fileContent.joinToString(separator = " ")
.split(splitRegex)
libraries.addAll(fileTokens.filter { token -> token in jsLibraries })
imports.addAll(fileTokens.filter { token -> token in LIBRARIES })

return libraries.toList()
return imports.toList()
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/app/extractors/ObjectiveCExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ObjectiveCExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()
val imports = mutableSetOf<String>()

val sharpImportIncludeRegex =
Regex("""#(import|include)\s+[">](\w+)[/\w+]*\.\w+[">]""")
Expand All @@ -30,10 +30,10 @@ class ObjectiveCExtractor : ExtractorInterface {
atImportRegex.findAll(it)
if (res.toList().isNotEmpty()) {
val lineLib = res.toList().map { it.groupValues }.last().last()
libraries.add(lineLib)
imports.add(lineLib)
}
}

return libraries.toList()
return imports.toList()
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/app/extractors/PhpExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PhpExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()
val imports = mutableSetOf<String>()

val useRegex = Regex("""use\s+(\w+)[\\\w+]*""")
val requireIncludeRegex = Regex("""(require|require_once|include|""" +
Expand All @@ -28,10 +28,10 @@ class PhpExtractor : ExtractorInterface {
val res = useRegex.findAll(it) + requireIncludeRegex.findAll(it)
if (res.toList().isNotEmpty()) {
val lineLib = res.toList().map { it.groupValues }.last().last()
libraries.add(lineLib)
imports.add(lineLib)
}
}

return libraries.toList()
return imports.toList()
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/app/extractors/PythonExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PythonExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()
val imports = mutableSetOf<String>()

val regex =
Regex("""(from\s+(\w+)[.\w+]*\s+import|import\s+(\w+[,\s*\w+]*))""")
Expand All @@ -28,10 +28,10 @@ class PythonExtractor : ExtractorInterface {
if (res != null) {
val lineLibs = res.groupValues.last { it != "" }
.split(Regex(""",\s*"""))
libraries.addAll(lineLibs)
imports.addAll(lineLibs)
}
}

return libraries.toList()
return imports.toList()
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/app/extractors/RubyExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ class RubyExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()
val imports = mutableSetOf<String>()

val regex = Regex("""(require\s+'(\w+)'|load\s+'(\w+)\.\w+')""")
fileContent.forEach {
val res = regex.find(it)
if (res != null) {
val lineLib = res.groupValues.last { it -> it != "" }
libraries.add(lineLib)
imports.add(lineLib)
}
}

return libraries.toList()
return imports.toList()
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/app/extractors/SwiftExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ class SwiftExtractor : ExtractorInterface {
}

override fun extractImports(fileContent: List<String>): List<String> {
val libraries = mutableSetOf<String>()
val imports = mutableSetOf<String>()

val regex = Regex("""import\s+(\w+)""")
fileContent.forEach {
val res = regex.find(it)
if (res != null) {
val lineLib = res.groupValues[1]
libraries.add(lineLib)
imports.add(lineLib)
}
}

return libraries.toList()
return imports.toList()
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/app/hashers/CommitHasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class CommitHasher(private val localRepo: LocalRepo,
.subscribe({ commitsBundle -> // OnNext.
postCommitsToServer(commitsBundle) // Send ready commits.
}, { e -> // OnError.
Logger.error("Hashing error: " + e.message)
throwables.add(e) // TODO(anatoly): Top-class handling errors.
})
}
Expand Down