96

GitHub - src-d/engine: source{d} engine

 5 years ago
source link: https://github.com/src-d/engine
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md


source{d}

source{d} Engine

Powerful language-agnostic analysis of your source code and git history.

GitHub version Build Status Go Report Card source{d} design document

WebsiteDocumentationBlogSlackTwitter

Introduction

source{d} Engine exposes powerful Universal AST's to analyze your code and a SQL engine to analyze your git history:

  • Code Retrieval: retrieve and store git repositories as a dataset.
  • Language Agnostic Code Analysis: automatically identify languages, parse source code, and extract the pieces that matter in a completely language-agnostic way.
  • Git Analysis powerful SQL based analysis on top of your git repositories.
  • Querying With Familiar APIs analyze your code through powerful friendly APIs, such as SQL, gRPC, REST, and various client libraries.

Contents

Quickstart

Follow the steps below to get started with source{d} Engine.

1. Install Docker

Follow these instructions:

sudo apt-get update
sudo apt-get install docker-ce
sudo pacman -S docker

2. Install source{d} Engine

Download the latest release for MacOS (Darwin) or Linux.

MacOS / Linux:

# For MacOS you can click to unarchive, for Linux use the tar command:
tar -xvf engine_REPLACEVERSION_linux_amd64.tar.gz

# Move it into your local bin folder to be executable from anywhere
sudo mv srcd /usr/local/bin/

Windows support is coming soon!

3. Start source{d} Engine with your local repositories

Now it's time to initialize the source{d} engine and provide it some repositories to analyze:

# Without a path it operates on the local folder,
# it works with nested folders.
srcd init

# You can also provide a path
srcd init /home/user/replace/path/

4. Explore the source{d} Engine

To launch the web client for the SQL interface, run the following command and start executing queries:

srcd web sql

The first time you run some of these commands, the source{d} Engine will download and install the Docker containers that are needed. Be aware that this might take a bit of time, it is only on your first use.

If you prefer to stay with the command line, you can execute:

srcd sql

This will open a SQL REPL that allows you to execute queries against your repositories.

If you want to run a query directly, you can also execute it as such:

srcd sql "SHOW tables;"

You might have noticed that some queries below use the UAST function. This is to transform code to a Universal Abstract Syntax Tree. If you want a playground to see examples of the UAST, or run your own, you can launch the parse web client.

To see which languages are available, check the table of supported languages.

# See the drivers already automatically installed based on previous parse requests:
srcd parse drivers list

# Install the drivers you are interested in
srcd parse drivers install python
srcd parse drivers install go
srcd parse drivers install java
srcd parse drivers install javascript
srcd parse drivers install php
srcd parse drivers install ruby
srcd parse drivers install bash
# Launch the web client
srcd web parse

Alternatively you can also start parsing files on the command line:

srcd parse uast /path/to/file.java

5. Start executing queries

Understand which tables are available to you to query:

gitbase> show tables;
+--------------+
|    TABLE     |
+--------------+
| blobs        |
| commit_blobs |
| commit_files |
| commit_trees |
| commits      |
| files        |
| ref_commits  |
| refs         |
| remotes      |
| repositories |
| tree_entries |
+--------------+
gitbase> DESCRIBE TABLE commits;
+---------------------+-----------+
|        NAME         |   TYPE    |
+---------------------+-----------+
| repository_id       | TEXT      |
| commit_hash         | TEXT      |
| commit_author_name  | TEXT      |
| commit_author_email | TEXT      |
| commit_author_when  | TIMESTAMP |
| committer_name      | TEXT      |
| committer_email     | TEXT      |
| committer_when      | TIMESTAMP |
| commit_message      | TEXT      |
| tree_hash           | TEXT      |
| commit_parents      | JSON      |
+---------------------+-----------+

Show me the repositories I am analyzing:

SELECT * FROM repositories;

Top 10 repositories by commit count in HEAD:

SELECT repository_id,commit_count 
FROM (
    SELECT r.repository_id, COUNT(*) AS commit_count
    FROM ref_commits r
    WHERE r.ref_name = 'HEAD'
    GROUP BY r.repository_id
) AS q
ORDER BY commit_count
DESC
LIMIT 10

Query all files from HEAD:

SELECT cf.file_path, f.blob_content 
FROM ref_commits r 
NATURAL JOIN commit_files cf 
NATURAL JOIN files f 
WHERE r.ref_name = 'HEAD' 
AND r.history_index = 0

Retrieve the UAST for all files from HEAD:

SELECT * FROM (
    SELECT cf.file_path,
           UAST(f.blob_content, LANGUAGE(f.file_path,  f.blob_content)) as uast
    FROM ref_commits r 
    NATURAL JOIN commit_files cf 
    NATURAL JOIN files f 
    WHERE r.ref_name = 'HEAD' 
    AND r.history_index = 0
) t WHERE ARRAY_LENGTH(uast) > 0

Query for all LICENSE & README files across history:

SELECT repository_id, blob_content 
FROM files 
WHERE file_path = 'LICENSE' 
OR file_path = 'README.md'

You can find further sample queries in the examples folder.

Guides and Examples

For the full list of the commands supported by srcd and those that have been planned, please read commands.md.

Collection of guide & examples using the source{d} Engine:

Architecture

source{d} Engine functions as a CLI tool that provides easy access to components of the source{d} stack for Code As Data. It consists of a daemon managing all of the services (Babelfish, Enry, Gitbase etc.) which are packaged as docker containers.

architecture.png

For more details on the architecture of this project, read docs/architecture.md.

Babelfish UAST

One of the most important components of the source{d} engine is the UAST.

UAST stands for Universal Abstract Syntax Tree, it is a normalized form of a programming language's AST, annotated with language agnostic roles and transformed with language agnostic concepts (e.g. Functions, Imports etc.). It enables advanced static analysis of code and easy feature extraction for statistics or Machine Learning on Code.

To parse a file for a UAST, it is as easy as:

srcd parse uast --lang=LANGUAGE /path/to/file

To launch the web client, run the following command and start executing queries*:

srcd web parse

Be sure to have already installed some language drivers, you can check this by running:

srcd parse drivers list

and install them by:

srcd parse drivers install python

To see which languages are available, check the table of supported languages.

Clients and Connectors

For connecting to the language parsing server (Babelfish) and analyzing the UAST, there are several language clients currently supported and maintained:

The Gitbase Spark connector is under development, which aims to allow for an easy integration with Spark & PySpark:

Community

source{d} has an amazing community of developers & contributors who are interested in Code As Data and/or Machine Learning on Code. Please join us! ?

Contributing

Contributions are welcome and very much appreciated ? Please refer to our contribution guide for more details.

Credits

This software uses code from several open source packages. We'd like to thank the contributors for all their efforts:

License

Apache 2.0 License


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK