diff --git a/README.md b/README.md index 85e0321..a548172 100644 --- a/README.md +++ b/README.md @@ -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 Community ``` @@ -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 diff --git a/src/combobulator.py b/src/combobulator.py index 273d2ea..6a7aa7c 100644 --- a/src/combobulator.py +++ b/src/combobulator.py @@ -1,5 +1,6 @@ import argparse import os + from dotenv import load_dotenv # internal module imports @@ -12,6 +13,8 @@ # export import csv import sys +from json import dump + SUPPORTED_PACKAGES=['npm', 'pypi', 'maven'] LEVELS = ['compare', "comp", 'heuristics', "heur"] @@ -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", @@ -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() @@ -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() \ No newline at end of file