Skip to content

Commit

Permalink
fix issue 60. add async nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
yinwang0 committed Mar 29, 2016
1 parent efbd4d3 commit 6009cb7
Show file tree
Hide file tree
Showing 28 changed files with 313 additions and 894 deletions.
661 changes: 0 additions & 661 deletions LICENSE

This file was deleted.

38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,32 @@ tags into the memory.



#### License (GNU AGPLv3)
#### License (BSD Style)

PySonar - a type inferencer and indexer for Python

Copyright (c) 2013-2014 Yin Wang
Copyright (c) 2013-2016 Yin Wang

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
1. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

1. The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.

### Donations

If you want to support the development of PySonar, click here to donate:

<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=F7KQH572KG8US&lc=US&item_number=blog&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif"></a>
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* unsorted utility class
*/
public class _ {
public class $ {

public static final Charset UTF_8 = Charset.forName("UTF-8");

Expand Down Expand Up @@ -131,7 +131,7 @@ public static void writeFile(String path, String contents) {
out.print(contents);
out.flush();
} catch (Exception e) {
_.die("Failed to write: " + path);
$.die("Failed to write: " + path);
} finally {
if (out != null) {
out.close();
Expand Down Expand Up @@ -199,7 +199,7 @@ public static void copyJarResourcesRecursively(File destination, JarURLConnectio
try {
jarFile = jarConnection.getJarFile();
} catch (Exception e) {
_.die("Failed to get jar file)");
$.die("Failed to get jar file)");
return;
}

Expand Down Expand Up @@ -241,7 +241,7 @@ public static String readResource(String resource) {
@NotNull
public static String getFileHash(@NotNull String path) {
byte[] bytes = getBytesFromFile(path);
return _.getContentHash(path.getBytes()) + "." + getContentHash(bytes);
return $.getContentHash(path.getBytes()) + "." + getContentHash(bytes);
}


Expand All @@ -252,7 +252,7 @@ public static String getContentHash(byte[] fileContents) {
try {
algorithm = MessageDigest.getInstance("SHA-1");
} catch (Exception e) {
_.die("Failed to get SHA, shouldn't happen");
$.die("Failed to get SHA, shouldn't happen");
return "";
}

Expand Down Expand Up @@ -575,7 +575,7 @@ public static String getGCStats() {
sb.append("\n- total collection time: " + formatTime(gcTime));

Runtime runtime = Runtime.getRuntime();
sb.append("\n- total memory: " + _.printMem(runtime.totalMemory()));
sb.append("\n- total memory: " + $.printMem(runtime.totalMemory()));

return sb.toString();
}
Expand Down
44 changes: 22 additions & 22 deletions src/main/java/org/yinwang/pysonar/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Analyzer {

// global static instance of the analyzer itself
public static Analyzer self;
public String sid = _.newSessionId();
public String sid = $.newSessionId();
public State moduleTable = new State(null, State.StateType.GLOBAL);
public List<String> loadedFiles = new ArrayList<>();
public State globaltable = new State(null, State.StateType.GLOBAL);
Expand Down Expand Up @@ -91,7 +91,7 @@ public void setOption(String option) {

// main entry to the analyzer
public void analyze(String path) {
String upath = _.unifyPath(path);
String upath = $.unifyPath(path);
File f = new File(upath);
projectDir = f.isDirectory() ? f.getPath() : f.getParent();
loadFileRecursive(upath);
Expand All @@ -100,7 +100,7 @@ public void analyze(String path) {

public void setCWD(String cd) {
if (cd != null) {
cwd = _.unifyPath(cd);
cwd = $.unifyPath(cd);
}
}

Expand All @@ -113,7 +113,7 @@ public void addPaths(@NotNull List<String> p) {


public void addPath(String p) {
path.add(_.unifyPath(p));
path.add($.unifyPath(p));
}


Expand All @@ -136,14 +136,14 @@ private void addPythonPath() {

private void copyModels() {
URL resource = Thread.currentThread().getContextClassLoader().getResource(MODEL_LOCATION);
String dest = _.locateTmp("models");
String dest = $.locateTmp("models");
this.modelDir = dest;

try {
_.copyResourcesRecursively(resource, new File(dest));
_.msg("copied models to: " + modelDir);
$.copyResourcesRecursively(resource, new File(dest));
$.msg("copied models to: " + modelDir);
} catch (Exception e) {
_.die("Failed to copy models. Please check permissions of writing to: " + dest);
$.die("Failed to copy models. Please check permissions of writing to: " + dest);
}
addPath(dest);
}
Expand Down Expand Up @@ -201,7 +201,7 @@ public List<Binding> getAllBindings() {

@Nullable
ModuleType getCachedModule(String file) {
Type t = moduleTable.lookupType(_.moduleQname(file));
Type t = moduleTable.lookupType($.moduleQname(file));
if (t == null) {
return null;
} else if (t instanceof UnionType) {
Expand Down Expand Up @@ -292,7 +292,7 @@ List<Diagnostic> getFileErrs(String file, @NotNull Map<String, List<Diagnostic>>

@Nullable
public Type loadFile(String path) {
path = _.unifyPath(path);
path = $.unifyPath(path);
File f = new File(path);

if (!f.canRead()) {
Expand Down Expand Up @@ -349,13 +349,13 @@ private Type parseAndResolve(String file) {


private void createCacheDir() {
cacheDir = _.makePathString(_.getSystemTempDir(), "pysonar2", "ast_cache");
cacheDir = $.makePathString($.getSystemTempDir(), "pysonar2", "ast_cache");
File f = new File(cacheDir);
_.msg("AST cache is at: " + cacheDir);
$.msg("AST cache is at: " + cacheDir);

if (!f.exists()) {
if (!f.mkdirs()) {
_.die("Failed to create tmp directory: " + cacheDir +
$.die("Failed to create tmp directory: " + cacheDir +
".Please check permissions");
}
}
Expand Down Expand Up @@ -412,7 +412,7 @@ public String locateModule(String headName) {

for (String p : loadPath) {
File startDir = new File(p, headName);
File initFile = new File(_.joinPath(startDir, "__init__.py").getPath());
File initFile = new File($.joinPath(startDir, "__init__.py").getPath());

if (initFile.exists()) {
return p;
Expand Down Expand Up @@ -457,7 +457,7 @@ public Type loadModule(@NotNull List<Name> name, @NotNull State state) {

for (int i = 0; i < name.size(); i++) {
path = new File(path, name.get(i).id);
File initFile = new File(_.joinPath(path, "__init__.py").getPath());
File initFile = new File($.joinPath(path, "__init__.py").getPath());

if (initFile.exists()) {
Type mod = loadFile(initFile.getPath());
Expand Down Expand Up @@ -539,8 +539,8 @@ public int countFileRecursive(String fullname) {


public void finish() {
_.msg("\nFinished loading files. " + nCalled + " functions were called.");
_.msg("Analyzing uncalled functions");
$.msg("\nFinished loading files. " + nCalled + " functions were called.");
$.msg("Analyzing uncalled functions");
applyUncalled();

// mark unused variables
Expand All @@ -554,7 +554,7 @@ public void finish() {
}
}

_.msg(getAnalysisSummary());
$.msg(getAnalysisSummary());
}


Expand Down Expand Up @@ -592,9 +592,9 @@ public void applyUncalled() {
@NotNull
public String getAnalysisSummary() {
StringBuilder sb = new StringBuilder();
sb.append("\n" + _.banner("analysis summary"));
sb.append("\n" + $.banner("analysis summary"));

String duration = _.formatTime(System.currentTimeMillis() - stats.getInt("startTime"));
String duration = $.formatTime(System.currentTimeMillis() - stats.getInt("startTime"));
sb.append("\n- total time: " + duration);
sb.append("\n- modules loaded: " + loadedFiles.size());
sb.append("\n- semantic problems: " + semanticErrors.size());
Expand All @@ -615,8 +615,8 @@ public String getAnalysisSummary() {
long unresolved = Analyzer.self.unresolved.size();
sb.append("\n- resolved names: " + resolved);
sb.append("\n- unresolved names: " + unresolved);
sb.append("\n- name resolve rate: " + _.percent(resolved, resolved + unresolved));
sb.append("\n" + _.getGCStats());
sb.append("\n- name resolve rate: " + $.percent(resolved, resolved + unresolved));
sb.append("\n" + $.getGCStats());

return sb.toString();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/yinwang/pysonar/AstCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void clear() {
*/
public boolean clearDiskCache() {
try {
_.deleteDirectory(new File(Analyzer.self.cacheDir));
$.deleteDirectory(new File(Analyzer.self.cacheDir));
return true;
} catch (Exception x) {
LOG.log(Level.SEVERE, "Failed to clear disk cache: " + x);
Expand Down Expand Up @@ -116,7 +116,7 @@ public Node getAST(@NotNull String path) {
*/
@NotNull
public String getCachePath(@NotNull String sourcePath) {
return _.makePathString(Analyzer.self.cacheDir, _.getFileHash(sourcePath));
return $.makePathString(Analyzer.self.cacheDir, $.getFileHash(sourcePath));
}


Expand All @@ -130,7 +130,7 @@ void serialize(@NotNull Node ast) {
oos = new ObjectOutputStream(fos);
oos.writeObject(ast);
} catch (Exception e) {
_.msg("Failed to serialize: " + path);
$.msg("Failed to serialize: " + path);
} finally {
try {
if (oos != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/yinwang/pysonar/Binding.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public boolean equals(Object obj) {
Binding b = (Binding) obj;
return (start == b.start &&
end == b.end &&
_.same(fileOrUrl, b.fileOrUrl));
$.same(fileOrUrl, b.fileOrUrl));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/yinwang/pysonar/JSONDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public static void main(String[] args) throws Exception {
docOut = new BufferedOutputStream(new FileOutputStream(docFilename));
symOut = new BufferedOutputStream(new FileOutputStream(symFilename));
refOut = new BufferedOutputStream(new FileOutputStream(refFilename));
_.msg("graphing: " + srcpath);
$.msg("graphing: " + srcpath);
graph(srcpath, inclpaths, symOut, refOut, docOut);
docOut.flush();
symOut.flush();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/yinwang/pysonar/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Options(String[] args) {
String key = args[i];
if (key.startsWith("--")) {
if (i + 1 >= args.length) {
_.die("option needs a value: " + key);
$.die("option needs a value: " + key);
} else {
key = key.substring(2);
String value = args[i + 1];
Expand Down
Loading

0 comments on commit 6009cb7

Please sign in to comment.