Skip to content

Commit 1ca9205

Browse files
authored
Merge pull request #51 from bsync/exclusions
Support command line exclusions from the dependency scan
2 parents bf4c41d + dfea2ea commit 1ca9205

5 files changed

Lines changed: 40 additions & 4 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>cn.emergentdesign.se</groupId>
88
<artifactId>depends</artifactId>
99
<!-- <version>1.0-SNAPSHOT</version> -->
10-
<version>0.9.7</version>
10+
<version>0.9.8</version>
1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
<antlr4.visitor>true</antlr4.visitor>

src/main/java/depends/DependsCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public static class SupportedTypes extends ArrayList<String> {
6868
private String namePathPattern="";
6969
@Option(names = {"-i","--includes"},split=",", description = "The files of searching path")
7070
private String[] includes = new String[] {};
71+
@Option(names = {"--exclude"},split=",", description = "Exclude files/directories matching path patterns")
72+
private String[] excludes = new String[] {};
7173
@Option(names = {"--auto-include"},split=",", description = "auto include all paths under the source path (please notice the potential side effect)")
7274
private boolean autoInclude = false;
7375
@Option(names = {"--detail"},split=",", description = "add detail dependency information to output (only applicable for JSON output format)")
@@ -119,6 +121,9 @@ public boolean isDv8map() {
119121
public String[] getIncludes() {
120122
return includes;
121123
}
124+
public String[] getExcludes() {
125+
return excludes;
126+
}
122127
public boolean isHelp() {
123128
return help;
124129
}

src/main/java/depends/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private static void executeCommand(DependsCommand args) throws ParameterExceptio
9999
String lang = args.getLang();
100100
String inputDir = args.getSrc();
101101
String[] includeDir = args.getIncludes();
102+
String[] excludes = args.getExcludes();
102103
String outputName = args.getOutputName();
103104
String outputDir = args.getOutputDir();
104105
String[] outputFormat = args.getFormat();
@@ -119,7 +120,7 @@ private static void executeCommand(DependsCommand args) throws ParameterExceptio
119120

120121
long startTime = System.currentTimeMillis();
121122
//step1: build data
122-
EntityRepo entityRepo = langProcessor.buildDependencies(inputDir, includeDir, bindingResolver);
123+
EntityRepo entityRepo = langProcessor.buildDependencies(inputDir, includeDir, excludes, bindingResolver);
123124

124125
new RelationCounter(entityRepo,langProcessor, bindingResolver).computeRelations();
125126
System.out.println("Dependency done....");

src/main/java/depends/extractor/AbstractLangProcessor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ abstract public class AbstractLangProcessor {
8787
protected EntityRepo entityRepo;
8888
protected String inputSrcPath;
8989
public String[] includeDirs;
90+
private String[] excludes;
9091
private Set<UnsolvedBindings> potentialExternalDependencies;
9192
private List<String> includePaths;
9293
private static Logger logger = LoggerFactory.getLogger(AbstractLangProcessor.class);
@@ -106,8 +107,13 @@ public AbstractLangProcessor() {
106107
* @return
107108
*/
108109
public EntityRepo buildDependencies(String inputDir, String[] includeDir, IBindingResolver bindingResolver) {
110+
return buildDependencies(inputDir, includeDir, new String[]{}, bindingResolver);
111+
}
112+
113+
public EntityRepo buildDependencies(String inputDir, String[] includeDir, String[] excludes, IBindingResolver bindingResolver) {
109114
this.inputSrcPath = inputDir;
110115
this.includeDirs = includeDir;
116+
this.excludes = excludes;
111117
this.bindingResolver = bindingResolver;
112118
logger.info("Start parsing files...");
113119
parseAllFiles();
@@ -166,6 +172,9 @@ public void visit(File file) {
166172

167173
});
168174
fileTransversal.extensionFilter(this.fileSuffixes());
175+
if (this.excludes != null && this.excludes.length > 0) {
176+
fileTransversal.excludeFilter(this.excludes);
177+
}
169178
fileTransversal.travers(this.inputSrcPath);
170179
for (String f : phase2Files) {
171180
parseFile(f, phase2Files);

src/main/java/depends/extractor/python/union/PythonCodeListener.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,30 @@ public void enterImport_stmt(Import_stmtContext ctx) {
6868
}
6969
List<String> fullNames = sureImportedModulesParsed(0,moduleName,null);
7070

71+
// Find the main entity for this import (the package or main module)
72+
Entity mainEntity = null;
73+
String mainFullName = null;
7174
for (String fullName:fullNames) {
7275
if (FileUtil.existFile(fullName) && !(FileUtil.isDirectory(fullName))) {
7376
context.foundNewImport(new FileImport(fullName));
7477
}
75-
context.foundNewImport(new NameAliasImport(fullName, entityRepo.getEntity(fullName), aliasName));
78+
Entity importedEntity = entityRepo.getEntity(fullName);
79+
// If this is the __init__.py file, use the package entity instead
80+
if (fullName.endsWith(PythonBuiltInType.PACKAGE_PY_NAME) && importedEntity != null) {
81+
Entity parent = importedEntity.getParent();
82+
if (parent != null) {
83+
mainEntity = parent;
84+
mainFullName = parent.getRawName().uniqName();
85+
}
86+
} else if (mainEntity == null && importedEntity != null) {
87+
// For non-package imports, use the first file as main entity
88+
mainEntity = importedEntity;
89+
mainFullName = fullName;
90+
}
91+
}
92+
// Create only ONE NameAliasImport for the main entity
93+
if (mainEntity != null) {
94+
context.foundNewImport(new NameAliasImport(mainFullName, mainEntity, aliasName));
7695
}
7796
}
7897
super.enterImport_stmt(ctx);
@@ -220,7 +239,9 @@ private List<String> visitIncludedFile(String fullName) {
220239
}
221240
}
222241
if (FileUtil.existFile(fullName+File.separator + PythonBuiltInType.PACKAGE_PY_NAME)) {
223-
visitedFiles.add( fullName+File.separator +PythonBuiltInType.PACKAGE_PY_NAME);
242+
String initFile = fullName+File.separator +PythonBuiltInType.PACKAGE_PY_NAME;
243+
invokeParser(initFile);
244+
visitedFiles.add(initFile);
224245
}
225246
}else{
226247
invokeParser(fullName);

0 commit comments

Comments
 (0)