Skip to content

Commit

Permalink
Merge pull request #4 from ongres/ndjson_implementation
Browse files Browse the repository at this point in the history
feat: implement ndjson artifact
  • Loading branch information
3manuek authored Jun 13, 2023
2 parents 58b8118 + 33ec8b0 commit 299284a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ jobs:
pip install -r requirements.txt
- name: execute artifact_builder
run: python artifact_builder -D sql -O index.json
run: python artifact_builder -D sql -O index.json -E postgres

12 changes: 11 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
pip install -r requirements.txt
- name: execute artifact_builder
run: python artifact_builder -D sql -O index.json
run: python artifact_builder -D sql -O index.json -E postgres

- name: Extract tag name
id: tag
Expand Down Expand Up @@ -66,4 +66,14 @@ jobs:
upload_url: ${{ steps.latest_release_info.outputs.upload_url }}
asset_path: index.json
asset_name: index.json
asset_content_type: application/json

- name: upload ndjson artifact
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ github.token }}
with:
upload_url: ${{ steps.latest_release_info.outputs.upload_url }}
asset_path: ndindex.json
asset_name: ndindex.json
asset_content_type: application/json
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ pip-selfcheck.json
# End of https://www.gitignore.io/api/venv

.artifacts/
index.json
index.json
ndindex.json
5 changes: 3 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ image: python:latest

default:
script:
- python3 artifact_builder -D sql -O index.json
- python3 artifact_builder -D sql -O index.json -E postgres
only:
- master

release:
script:
- python3 artifact_builder -D sql -O index.json
- python3 artifact_builder -D sql -O index.json -E postgres
artifacts:
paths:
- index.json
- ndindex.json
# rules:
# - if: $CI_COMMIT_BRANCH == "master"
only:
Expand Down
22 changes: 16 additions & 6 deletions artifact_builder/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import indexer
from filemap import fileMap
import json
import ndjson
import argparse


Expand All @@ -9,21 +10,30 @@
_engine: str

def main():
_engine = 'postgres'

parser = argparse.ArgumentParser()
parser.add_argument("-D", "--directory", help="Directory to be indexed", type=str, required=False, default=sqlDirectory)
parser.add_argument("-O", "--output", help="Output artifact", required=False, type=str, default="./index.json")
parser.add_argument("-O", "--output", help="Output json artifact", required=False, type=str, default="./index.json")
parser.add_argument("-o", "--output-ndjson", help="Output ndjson artifact", required=False, type=str, default="./ndindex.json")
parser.add_argument("-v", "--verbose", help="Kind of debug", default=False, action=argparse.BooleanOptionalAction)
parser.add_argument("-E", "--engine", help="Engine name", default="postgres", type=str, required=True)
args = parser.parse_args()

fileMap = indexer.indexDir(args.directory, _engine)
fileMap = indexer.indexDir(args.directory, args.engine)

if args.verbose:
print(json.dumps(fileMap, indent=2))
print(json.dumps(fileMap, indent=1))

with open(args.output, 'w+', encoding="utf-8") as f:
json.dump(fileMap, f)
with open(args.output, 'w+', encoding='utf-8-sig') as f:
json.dump(fileMap, f, indent=1)

with open(args.output, 'rb') as f:
data = json.load(f)

with open(args.output_ndjson, 'w+', encoding='utf-8-sig') as f:
writer = ndjson.writer(f)
for key in data:
writer.writerow(data[key])

if __name__ == "__main__":
main()
Expand Down
9 changes: 6 additions & 3 deletions artifact_builder/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ def indexDir(sqlDirectory: str, _engine: str) -> fileMap:
key = sub('.sql|.md', '', filename)
fpath = join(root.removeprefix('../'), filename)

if key not in _fileMap:
# For now, we ignore READMEs. But, we might furtherly include some documentation
# artifact.
if key not in _fileMap and filename.removesuffix(".md").lower() != 'readme':
_fileMap[key]={'engine': _engine}
_fileMap[key]={'title': sub('[_-]'," ", str(key)).capitalize()}

if filename.endswith(".sql"):
with open(fpath, encoding="utf-8") as f:
_fileMap[key].update({'fpath': fpath,
'category': root.removeprefix(sqlDirectory),
'category': root.removeprefix(sqlDirectory + '/'),
'query': f.read()})
elif filename.endswith(".md"):
elif filename.endswith(".md") and filename.removesuffix(".md").lower() != 'readme':
with open(fpath, encoding="utf-8") as f:
_fileMap[key].update({'docFPath': fpath,
'doc': f.read()})
Expand Down
1 change: 1 addition & 0 deletions artifact_builder/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ndjson==0.3.1

0 comments on commit 299284a

Please sign in to comment.