Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export as Json #23

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ Make sure to install required dependencies by running:
## Arguments (--help)
```
-h, --help show this help message and exit
-t {npm,NuGet,maven}, --type {npm,NuGet,maven}
Package Manager Type, i.e: npm, NuGet, maven
-t {npm,pypi,maven}, --type {npm,pypi,maven}
Package Manager Type, i.e: npm, PyPI, maven
-l LIST_FROM_FILE, --load_list LIST_FROM_FILE
Load list of dependencies from a file
-d FROM_SRC, --directory FROM_SRC
Extract dependencies from local source repository
-p--package SINGLE Name a single package.
-c CSV, --csv CSV Export packages properties onto CSV file
-j JSON, --json JSON Export packages properties onto JSON file
-gh GITHUB_TOKEN, --github GITHUB_TOKEN
GitHub Access Token (Overrides .env file setting)
-a {compare,comp,heuristics,heur}, --analysis {compare,comp,heuristics,heur}
Required analysis level - compare (comp), heuristics
(heur) (default: compare)
Required analysis level - compare (comp), heuristics (heur) (default: compare)

Apiiro <Heart> Community
```
Expand All @@ -58,6 +58,7 @@ Analysis level is customizable as you can build your own preferred analysis prof
Supported output format:
- Screen stdout (default)
- CSV export to designated file -(-CSV)
- JSON export to designated file -(-JSON)

## Usage examples

Expand Down
32 changes: 29 additions & 3 deletions src/combobulator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import os

from dotenv import load_dotenv

# internal module imports
Expand All @@ -12,6 +13,8 @@
# export
import csv
import sys
from json import dump


SUPPORTED_PACKAGES=['npm', 'pypi', 'maven']
LEVELS = ['compare', "comp", 'heuristics', "heur"]
Expand Down Expand Up @@ -54,6 +57,11 @@ def parse_args():
dest="CSV",
help="Export packages properties onto CSV file",
action="store", type=str)
output_group.add_argument("-j", "--json",
dest="JSON",
help="Export packages properties onto JSON file",
action="store", type=str)

# support variables
parser.add_argument("-gh", "--github",
dest="GITHUB_TOKEN",
Expand Down Expand Up @@ -127,9 +135,24 @@ def export_csv(instances, path):
except:
print("[ERROR] CSV file couldn't be written to disk.")
sys.exit(1)





def export_json(instances, path):
headers = ["Package Name", "Package Type", "Exists on External",
"Org/Group ID", "Score", "Version Count", "Timestamp"]
data = [{k: v for k, v in zip(headers, x.listall())} for x in instances]
print(len(instances))
print(data)
try:
with open(path, 'w', newline='') as file:
dump(data, file)

print("[EXPORT] JSON file has been successfuly exported at: " + path)
except:
print("[ERROR] JSON file couldn't be written to disk.")
sys.exit(1)


def main():
# envs to be consumed: GITHUB_TOKEN
init_args()
Expand Down Expand Up @@ -190,6 +213,9 @@ def main():
# OUTPUT
if args.CSV:
export_csv(metapkg.instances, args.CSV)
if args.JSON:
export_json(metapkg.instances, args.JSON)


if __name__ == "__main__":
main()