Spaces:
Runtime error
Runtime error
update
Browse files- app.py +10 -2
- py_code_analyzer/code_imports_analyzer.py +15 -12
app.py
CHANGED
|
@@ -23,12 +23,19 @@ st.markdown(
|
|
| 23 |
owner = st.text_input("Fill in the GitHub username", value="cyyeh")
|
| 24 |
repo = st.text_input("Fill in the GitHib repository", value="py-code-analyzer")
|
| 25 |
tree_sha = st.text_input(
|
| 26 |
-
"Fill in
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
clicked_ok_button = st.button("OK", disabled=not owner or not repo or not tree_sha)
|
| 29 |
st.markdown("---")
|
| 30 |
|
| 31 |
|
|
|
|
| 32 |
@conditonal_decorator(time_function, DEV)
|
| 33 |
def get_python_files(owner, repo, tree_sha):
|
| 34 |
return CodeFetcher().get_python_files(owner, repo, tree_sha)
|
|
@@ -69,4 +76,5 @@ if clicked_ok_button and owner and repo:
|
|
| 69 |
file_name="result.html",
|
| 70 |
mime="text/html",
|
| 71 |
)
|
| 72 |
-
|
|
|
|
|
|
| 23 |
owner = st.text_input("Fill in the GitHub username", value="cyyeh")
|
| 24 |
repo = st.text_input("Fill in the GitHib repository", value="py-code-analyzer")
|
| 25 |
tree_sha = st.text_input(
|
| 26 |
+
"Fill in SHA", value="2f387d0adea72a7b4c99a5e8fc3e4fd83b5469b8"
|
| 27 |
+
)
|
| 28 |
+
show_graph_visualization = st.checkbox(
|
| 29 |
+
"Show graph visualization",
|
| 30 |
+
value=True,
|
| 31 |
+
help="If the graph is large, then consider uncheck the checkbox. "
|
| 32 |
+
"For example, the result graph of fetching TensorFlow repo would be large.",
|
| 33 |
)
|
| 34 |
clicked_ok_button = st.button("OK", disabled=not owner or not repo or not tree_sha)
|
| 35 |
st.markdown("---")
|
| 36 |
|
| 37 |
|
| 38 |
+
@st.cache
|
| 39 |
@conditonal_decorator(time_function, DEV)
|
| 40 |
def get_python_files(owner, repo, tree_sha):
|
| 41 |
return CodeFetcher().get_python_files(owner, repo, tree_sha)
|
|
|
|
| 76 |
file_name="result.html",
|
| 77 |
mime="text/html",
|
| 78 |
)
|
| 79 |
+
if show_graph_visualization:
|
| 80 |
+
components.html(graph_visualization_file, height=600, scrolling=True)
|
py_code_analyzer/code_imports_analyzer.py
CHANGED
|
@@ -27,18 +27,20 @@ def construct_fetch_program_text_api_url(api_url):
|
|
| 27 |
|
| 28 |
|
| 29 |
async def get_program_text(session, python_file):
|
|
|
|
|
|
|
| 30 |
async with session.get(
|
| 31 |
construct_fetch_program_text_api_url(python_file["url"]),
|
| 32 |
-
headers={"Accept": "application/vnd.github.v3+json"},
|
| 33 |
) as response:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
|
| 44 |
class CodeImportsAnalyzer:
|
|
@@ -83,9 +85,10 @@ class CodeImportsAnalyzer:
|
|
| 83 |
|
| 84 |
base64_program_texts = await asyncio.gather(*tasks)
|
| 85 |
for base64_program_text in base64_program_texts:
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
def generate_imports_graph(self):
|
| 91 |
# TODO: thought on how to improve the graph generation logic
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
async def get_program_text(session, python_file):
|
| 30 |
+
# about Retry-After
|
| 31 |
+
# https://docs.github.com/en/rest/guides/best-practices-for-integrators#dealing-with-secondary-rate-limits
|
| 32 |
async with session.get(
|
| 33 |
construct_fetch_program_text_api_url(python_file["url"]),
|
| 34 |
+
headers={"Accept": "application/vnd.github.v3+json", "Retry-After": "5"},
|
| 35 |
) as response:
|
| 36 |
+
if response.status == 200:
|
| 37 |
+
data = await response.json()
|
| 38 |
+
if data["encoding"] == "base64":
|
| 39 |
+
return data["content"]
|
| 40 |
+
else:
|
| 41 |
+
print(
|
| 42 |
+
f"WARNING: {python_file['path']}'s encoding is {data['encoding']}, not base64"
|
| 43 |
+
)
|
| 44 |
|
| 45 |
|
| 46 |
class CodeImportsAnalyzer:
|
|
|
|
| 85 |
|
| 86 |
base64_program_texts = await asyncio.gather(*tasks)
|
| 87 |
for base64_program_text in base64_program_texts:
|
| 88 |
+
if base64_program_text:
|
| 89 |
+
program = pybase64.b64decode(base64_program_text)
|
| 90 |
+
tree = ast.parse(program)
|
| 91 |
+
self._node_visitor.visit(tree)
|
| 92 |
|
| 93 |
def generate_imports_graph(self):
|
| 94 |
# TODO: thought on how to improve the graph generation logic
|