Skip to content

Commit 95be6ab

Browse files
committed
Initial release
0 parents  commit 95be6ab

35 files changed

+5433
-0
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length = 160

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# Test coverage
7+
.coverage
8+
tests/coverage/
9+
10+
# Example files
11+
examples/*.sqlite3
12+
13+
# Generated docs
14+
site/

LICENSE

Lines changed: 691 additions & 0 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.PHONY: deps example test mypy lint check docs-serve docs-build docs-github
2+
3+
deps:
4+
poetry install
5+
6+
example:
7+
poetry run python -m examples.basic
8+
9+
lint:
10+
poetry run flake8
11+
mypy:
12+
poetry run mypy --show-error-codes codesurvey
13+
poetry run mypy --show-error-codes examples
14+
test:
15+
poetry run pytest \
16+
--cov="codesurvey" \
17+
--cov-report="html:tests/coverage" \
18+
--cov-report=term
19+
check: lint mypy test
20+
21+
docs-serve:
22+
poetry run mkdocs serve
23+
docs-build:
24+
poetry run mkdocs build
25+
docs-github:
26+
poetry run mkdocs gh-deploy

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<div align="center">
2+
3+
<h1>CodeSurvey</h1>
4+
5+
<a href="https://pypi.org/project/codesurvey">
6+
<img alt="PyPI" src="https://img.shields.io/pypi/v/codesurvey">
7+
</a>
8+
9+
<p>
10+
<a href="https://github.com/when-of-python/codesurvey">GitHub</a> - <a href="https://when-of-python.github.io/codesurvey">Documentation</a>
11+
</p>
12+
13+
</div>
14+
15+
CodeSurvey is a framework and tool to survey code repositories for
16+
language feature usage, library usage, and more:
17+
18+
* Survey a specific set of repositories, or randomly sample
19+
repositories from services like GitHub
20+
* Built-in support for analyzing Python code; extensible to support
21+
any language
22+
* Write simple Python functions to define the code features you want
23+
to survey; record arbitrary details of feature occurrences
24+
* Supports parallelizization of repository downloading and analysis
25+
across multiple processes
26+
* Logging and progress tracking to monitor your survey as it runs
27+
* Inspect the results as Python objects, or in an sqlite database
28+
29+
30+
## Installation
31+
32+
```
33+
pip install codesurvey
34+
```
35+
36+
37+
## Usage
38+
39+
The `CodeSurvey` class can easily be configured to run a survey, such
40+
as to measure how often the `math` module is used in a random set of
41+
recently updated Python repositories from GitHub:
42+
43+
```python
44+
from codesurvey import CodeSurvey
45+
from codesurvey.sources import GithubSampleSource
46+
from codesurvey.analyzers.python import PythonAstAnalyzer
47+
from codesurvey.analyzers.python.features import py_module_feature_finder
48+
49+
# Define a FeatureFinder to look for the `math` module in Python code
50+
has_math = py_module_feature_finder('math', modules=['math'])
51+
52+
# Configure the survey
53+
survey = CodeSurvey(
54+
db_filepath='math_survey.sqlite3',
55+
sources=[
56+
GithubSampleSource(language='python'),
57+
],
58+
analyzers=[
59+
PythonAstAnalyzer(
60+
feature_finders=[
61+
has_math,
62+
],
63+
),
64+
],
65+
max_workers=5,
66+
)
67+
68+
# Run the survey on 10 repositories
69+
survey.run(max_repos=10)
70+
71+
# Report on the results
72+
repo_features = survey.get_repo_features(feature_names=['math'])
73+
repo_count_with_math = sum([
74+
1 for repo_feature in repo_features if
75+
repo_feature.occurrence_count > 0
76+
])
77+
print(f'{repo_count_with_math} out of {len(repo_features)} repos use math')
78+
```
79+
80+
![Animated GIF of CodeSurvey demo on the command-line](https://when-of-python.github.io/codesurvey/images/codesurvey-demo.gif)
81+
82+
* For more Sources of repositories, see [Source
83+
docs](https://when-of-python.github.io/codesurvey/sources/core)
84+
* For more Analyzers and FeatureFinders, see [Analyzer
85+
docs](https://when-of-python.github.io/codesurvey/analyzers/core)
86+
* For more options and methods for inspecting results, see
87+
[`CodeSurvey` docs](https://when-of-python.github.io/codesurvey/core)
88+
* For details on directly inspecting the sqlite database of survey
89+
results see [Database docs](https://when-of-python.github.io/codesurvey/database)
90+
* More examples can be found in
91+
[examples](https://github.com/when-of-python/codesurvey/tree/main/examples)
92+
93+
94+
## Contributing
95+
96+
* Install Poetry dependencies with `make deps`
97+
* Documentation:
98+
* Run local server: `make docs-serve`
99+
* Build docs: `make docs-build`
100+
* Deploy docs to GitHub Pages: `make docs-github`
101+
* Docstring style follows the [Google style guide](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings)
102+
103+
104+
## TODO
105+
106+
* Add unit tests

codesurvey/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Library for surveying source code for the frequency of various features.
2+
3+
Typical usage:
4+
5+
```python
6+
from codesurvey import CodeSurvey, LocalSource
7+
from codesurvey.sources import LocalSource
8+
from codesurvey.analyzers.python import PythonAstAnalyzer
9+
from codesurvey.analyzers.python.features import has_set
10+
11+
survey = CodeSurvey(
12+
sources=[LocalSource(['/path/to/some_python_code'])],
13+
analyzers=[PythonAstAnalyzer(feature_finders=[has_set])],
14+
)
15+
survey.run(max_repos=1)
16+
print(survey.get_repo_features())
17+
print(survey.get_code_features())
18+
print(survey.get_survey_tree())
19+
```
20+
21+
"""
22+
23+
__version__ = '0.1.0'
24+
25+
from .core import CodeSurvey
26+
from .database import RepoFeature, CodeFeature
27+
from .utils import logger
28+
from . import sources
29+
from . import analyzers
30+
31+
__all__ = [
32+
'CodeSurvey',
33+
'RepoFeature',
34+
'CodeFeature',
35+
'logger',
36+
'sources',
37+
'analyzers',
38+
]

codesurvey/analyzers/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .core import Analyzer, Code, CodeThunk, FileAnalyzer, FileInfo
2+
from .features import (
3+
Feature, FeatureDict, FeatureFinder,
4+
feature_finder, partial_feature_finder, union_feature_finder,
5+
)
6+
7+
__all__ = [
8+
'Analyzer',
9+
'Code',
10+
'CodeThunk',
11+
'FileAnalyzer',
12+
'FileInfo',
13+
'Feature',
14+
'FeatureDict',
15+
'FeatureFinder',
16+
'feature_finder',
17+
'partial_feature_finder',
18+
'union_feature_finder',
19+
]

0 commit comments

Comments
 (0)