Merge "sourcedr: Reorganize directory structure"
diff --git a/vndk/tools/source-deps-reviewer/README.md b/vndk/tools/source-deps-reviewer/README.md
index 57920c9..dd45eb1 100644
--- a/vndk/tools/source-deps-reviewer/README.md
+++ b/vndk/tools/source-deps-reviewer/README.md
@@ -20,11 +20,16 @@
 
 Note that the index file locates at ~/.csearchindex by default.
 
-The functional test of this tool depends on several Python packages,
-please install them with
+This tool depends on several Python packages,
 
 ```
-$ pip install -r requirements.txt
+$ pip install -e .
+```
+
+To run functional test, please do
+
+```
+$ pip install -e .[dev]
 ```
 
 Prism, a code syntax highlighter is used.
@@ -33,7 +38,7 @@
 ## Usage
 
 ```
-$ python3 server.py --android-root DIRECTORY_ROOT_PATH --pattern PATTERN_TO_GREP
+$ sourcedr --android-root DIRECTORY_ROOT_PATH --pattern PATTERN_TO_GREP
 ```
 
 Add flag --is-regex if the pattern given is a regex open browser at
@@ -44,5 +49,5 @@
 ## Testing
 
 ```
-$ python3 functional_tests.py
+$ python3 sourcedr/functional_tests.py
 ```
diff --git a/vndk/tools/source-deps-reviewer/example.sh b/vndk/tools/source-deps-reviewer/example.sh
deleted file mode 100644
index 84ede7b..0000000
--- a/vndk/tools/source-deps-reviewer/example.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/bash
-rm ~/.csearchindex
-python3 server.py --android-root test --pattern dlopen
diff --git a/vndk/tools/source-deps-reviewer/setup.py b/vndk/tools/source-deps-reviewer/setup.py
new file mode 100755
index 0000000..0b6759e
--- /dev/null
+++ b/vndk/tools/source-deps-reviewer/setup.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+from setuptools import setup
+
+setup(
+    name='sourcedr',
+    version='1.0',
+    description='Shared Libs Deps Review Tool',
+    url='https://android.googlesource.com/platform/development/+'
+        '/master/vndk/tools/source-deps-reviewer/',
+    packages=['sourcedr'],
+    package_data={
+        'sourcedr': [
+            'static/prism/js/prism.js',
+            'static/prism/css/prism.css',
+            'static/js/main.js',
+            'static/css/main.css',
+        ],
+    },
+    install_requires=['flask'],
+    extras_require={
+        'dev': [
+            'flask_testing'
+        ],
+    },
+    entry_points={
+        'console_scripts': [
+            'sourcedr = sourcedr.server:main',
+        ],
+    }
+)
diff --git a/vndk/tools/source-deps-reviewer/sourcedr/__init__.py b/vndk/tools/source-deps-reviewer/sourcedr/__init__.py
new file mode 100644
index 0000000..e5a0d9b
--- /dev/null
+++ b/vndk/tools/source-deps-reviewer/sourcedr/__init__.py
@@ -0,0 +1 @@
+#!/usr/bin/env python3
diff --git a/vndk/tools/source-deps-reviewer/config.py b/vndk/tools/source-deps-reviewer/sourcedr/config.py
similarity index 90%
rename from vndk/tools/source-deps-reviewer/config.py
rename to vndk/tools/source-deps-reviewer/sourcedr/config.py
index 503b71d..da981d5 100755
--- a/vndk/tools/source-deps-reviewer/config.py
+++ b/vndk/tools/source-deps-reviewer/sourcedr/config.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python3
+
 # The following file extensions are ignored
 FILE_EXT_BLACK_LIST = {
     b'.1',
@@ -32,4 +34,7 @@
     b'autom4te.cache',
     b'dejagnu',
     b'llvm/Config/Config',
+    b'.git',
+    b'.repo',
+    b'out',
 )
diff --git a/vndk/tools/source-deps-reviewer/data_utils.py b/vndk/tools/source-deps-reviewer/sourcedr/data_utils.py
similarity index 80%
rename from vndk/tools/source-deps-reviewer/data_utils.py
rename to vndk/tools/source-deps-reviewer/sourcedr/data_utils.py
index 7c2d62f..e39ce65 100755
--- a/vndk/tools/source-deps-reviewer/data_utils.py
+++ b/vndk/tools/source-deps-reviewer/sourcedr/data_utils.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python3
+
 import json
 import os
 
@@ -30,3 +32,10 @@
     with open(data_path, 'w') as f:
         json.dump(data, f, sort_keys=True, indent=4)
 
+def remove_data():
+    for path in (data_path, pattern_path):
+        try:
+            os.remove(path)
+        except IOError:
+            pass
+
diff --git a/vndk/tools/source-deps-reviewer/functional_tests.py b/vndk/tools/source-deps-reviewer/sourcedr/functional_tests.py
similarity index 76%
rename from vndk/tools/source-deps-reviewer/functional_tests.py
rename to vndk/tools/source-deps-reviewer/sourcedr/functional_tests.py
index ec68a56..5bbc731 100755
--- a/vndk/tools/source-deps-reviewer/functional_tests.py
+++ b/vndk/tools/source-deps-reviewer/sourcedr/functional_tests.py
@@ -1,6 +1,8 @@
-from data_utils import *
-from preprocess import prepare
-from server import *
+#!/usr/bin/env python3
+
+from sourcedr.data_utils import *
+from sourcedr.preprocess import prepare
+from sourcedr.server import *
 
 from flask import Flask, jsonify, render_template, request
 from flask_testing import LiveServerTestCase, TestCase
@@ -13,16 +15,22 @@
 
 class TestPreprocess(unittest.TestCase):
     def test_prepare(self):
-        os.remove(data_path)
-        prepare(android_root='test', pattern='dlopen', is_regex=False)
+        remove_data()
+        prepare(android_root='sourcedr/test', pattern='dlopen', is_regex=False)
         self.assertTrue( os.path.exists(data_path) )
 
 class TestViews(TestCase):
     def create_app(self):
         return app
 
+    def setUp(self):
+        prepare(android_root='sourcedr/test', pattern='dlopen', is_regex=False)
+
+    def tearDown(self):
+        remove_data()
+
     def test_get_file(self):
-        test_arg = 'test/example.c'
+        test_arg = 'sourcedr/test/example.c'
         response = self.client.get('/get_file',
                                    query_string=dict(path=test_arg))
         ret = response.json['result']
@@ -30,7 +38,7 @@
             self.assertEqual(ret, f.read())
 
     def test_load_file(self):
-        test_arg = os.path.abspath('./test/dlopen/test.c:10')
+        test_arg = os.path.abspath('./sourcedr/test/dlopen/test.c:10')
         response = self.client.get('/load_file',
                                    query_string=dict(path=test_arg))
         deps = json.loads(response.json['deps'])
@@ -53,5 +61,4 @@
         self.assertEqual(['arr_0', 'arr_1'], cdata[test_arg['path']][1])
 
 if __name__ == '__main__':
-    prepare(android_root='test', pattern='dlopen', is_regex=False)
     unittest.main()
diff --git a/vndk/tools/source-deps-reviewer/preprocess.py b/vndk/tools/source-deps-reviewer/sourcedr/preprocess.py
similarity index 98%
rename from vndk/tools/source-deps-reviewer/preprocess.py
rename to vndk/tools/source-deps-reviewer/sourcedr/preprocess.py
index ef6418e..d71d3c3 100755
--- a/vndk/tools/source-deps-reviewer/preprocess.py
+++ b/vndk/tools/source-deps-reviewer/sourcedr/preprocess.py
@@ -1,6 +1,7 @@
 #!/usr/bin/env python3
-from config import *
-from data_utils import *
+
+from sourcedr.config import *
+from sourcedr.data_utils import *
 
 from subprocess import call
 import collections
diff --git a/vndk/tools/source-deps-reviewer/server.py b/vndk/tools/source-deps-reviewer/sourcedr/server.py
similarity index 95%
rename from vndk/tools/source-deps-reviewer/server.py
rename to vndk/tools/source-deps-reviewer/sourcedr/server.py
index 71d6682..2a51fe4 100755
--- a/vndk/tools/source-deps-reviewer/server.py
+++ b/vndk/tools/source-deps-reviewer/sourcedr/server.py
@@ -1,5 +1,7 @@
-from data_utils import *
-from preprocess import add_pattern, prepare
+#!/usr/bin/env python3
+
+from sourcedr.data_utils import *
+from sourcedr.preprocess import add_pattern, prepare
 
 from flask import Flask, jsonify, render_template, request
 import argparse
@@ -111,7 +113,7 @@
         except KeyError:
             continue
 
-if __name__=='__main__':
+def main():
     parser = argparse.ArgumentParser()
     parser.add_argument('--android-root', default='test')
     parser.add_argument('--pattern', default='dlopen')
@@ -129,3 +131,6 @@
 
     assert data_exist()
     app.run()
+
+if __name__=='__main__':
+    main()
diff --git a/vndk/tools/source-deps-reviewer/static/css/main.css b/vndk/tools/source-deps-reviewer/sourcedr/static/css/main.css
similarity index 100%
rename from vndk/tools/source-deps-reviewer/static/css/main.css
rename to vndk/tools/source-deps-reviewer/sourcedr/static/css/main.css
diff --git a/vndk/tools/source-deps-reviewer/static/js/main.js b/vndk/tools/source-deps-reviewer/sourcedr/static/js/main.js
similarity index 100%
rename from vndk/tools/source-deps-reviewer/static/js/main.js
rename to vndk/tools/source-deps-reviewer/sourcedr/static/js/main.js
diff --git a/vndk/tools/source-deps-reviewer/static/prism/MODULE_LICENSE_MIT b/vndk/tools/source-deps-reviewer/sourcedr/static/prism/MODULE_LICENSE_MIT
similarity index 100%
rename from vndk/tools/source-deps-reviewer/static/prism/MODULE_LICENSE_MIT
rename to vndk/tools/source-deps-reviewer/sourcedr/static/prism/MODULE_LICENSE_MIT
diff --git a/vndk/tools/source-deps-reviewer/static/prism/css/prism.css b/vndk/tools/source-deps-reviewer/sourcedr/static/prism/css/prism.css
similarity index 100%
rename from vndk/tools/source-deps-reviewer/static/prism/css/prism.css
rename to vndk/tools/source-deps-reviewer/sourcedr/static/prism/css/prism.css
diff --git a/vndk/tools/source-deps-reviewer/static/prism/js/prism.js b/vndk/tools/source-deps-reviewer/sourcedr/static/prism/js/prism.js
similarity index 100%
rename from vndk/tools/source-deps-reviewer/static/prism/js/prism.js
rename to vndk/tools/source-deps-reviewer/sourcedr/static/prism/js/prism.js
diff --git a/vndk/tools/source-deps-reviewer/templates/index.html b/vndk/tools/source-deps-reviewer/sourcedr/templates/index.html
similarity index 100%
rename from vndk/tools/source-deps-reviewer/templates/index.html
rename to vndk/tools/source-deps-reviewer/sourcedr/templates/index.html
diff --git a/vndk/tools/source-deps-reviewer/test/dlopen/test.c b/vndk/tools/source-deps-reviewer/sourcedr/test/dlopen/test.c
similarity index 100%
rename from vndk/tools/source-deps-reviewer/test/dlopen/test.c
rename to vndk/tools/source-deps-reviewer/sourcedr/test/dlopen/test.c
diff --git a/vndk/tools/source-deps-reviewer/test/example.c b/vndk/tools/source-deps-reviewer/sourcedr/test/example.c
similarity index 100%
rename from vndk/tools/source-deps-reviewer/test/example.c
rename to vndk/tools/source-deps-reviewer/sourcedr/test/example.c