Package: pkglic
PkgLicThe purpose of this helper app is to make it fast and easy to get an understanding of which open source licenses you are using in your application.This is done by querying the respective package manager for the meta data rather than what is installed locally, as your build machines may not want/need all the packages installed and you'll also save time by not having to download the bulk of the packages.The intention is to run it in your build pipeline, and at least you get a list of the components you use (as per your requirements files) and their licenses. With a small bit of trickery you can update some wiki page to keep an up to date list of packages.The script handlesrequirements.txt(python),package.json(javascript),*.csproj(C#/nuget) andpackages.config(C#/nuget).Basic operation is to invoke the script with some filepkglic -f requirements.txtand get an output with package names and their license. There are some more switches supported.usage: pkglic [-h] -f file [-t {cs,py,js,cs}] [--uniq] [-x file|package]
              [-u package] [-w file|package] [-o {0,1,2,3}] [--json file]
              [--credits file] [--creditstemplate file] [-v]

optional arguments:
  -h, --help            show this help message and exit
  -f file, --files file
                        input files to scan.
  -t {cs,py,js,cs}, --type {cs,py,js,cs}
                        Assume <type> for all --files if not guessable by
                        filename.
  --uniq                Remove duplicate packages.
  -x file|package, --exclude file|package
                        Do not check (or list) excluded packages.
  -u package, --unwanted package
                        Exit with errorlevel on these license types.
  -w file|package, --whitelist file|package
                        Read whitelisted packages form <file>.
  -o {0,1,2,3}, --order {0,1,2,3}
                        Which fields to use to sort output; 0 - type, name, 1:
                        license, name, 2: type, license, 3: group by license.
  --json file           Output as json-string to <file>.
  --credits file        Generate a credits file.
  --creditstemplate file
                        Template used to generate credits file.
  -v, --verbose         Increase verbosity.ParametersAdd parameters using a parameter file by prefixing it with @. If there is a file namedargs.txtin the current directory it will automatically be added. Place one parameter per line.InstallationEither install it from pypipip install pkglicor download the sourcesgit clone https://github.com/jhogstrom/pkglic.gitand make sure your environment has the required packages installed (pip install -r requirements.txt) and invoke the script from wherever you store it. You may for instance want to check it in with the rest of your build tools.Supported formatsPythonAny file path containing "requirements.txt" will be analyzed as a requirements file as supported bypip.http://pypi.orgis used to fetch the meta data.JavascriptAny file path containing "package.json" will be analyzed as a package.json file. Only the "dependencies" block will be checked!https://npmjs.orgis used to fetch the meta data.NugetAny file containing ".csproj" will be analyzed as a C# project and the nuget packages extracted for analysis.Any file containing "packages.config" will be analyzed as a nuget packages file (an older way to specify nuget dependencies).https://nuget.orgis used to fetch the meta data.WhitelistingIn some cases you'll find a package that lists as NOT_SPECIFIED or 404_NOT_FOUND, but you know from some other source the license it is actually used as. In that case you can add a--whitelistfile. The whitelist file can be written as a text file or as a json formatted structure.The textfile has the following format:[# comment lines are allowed]
<package_name>[: <expected_license>[ -> <map_to_license>]]
...Valid examples arefoo
foobar: NOT_SPECIFIED
barbaz: 404_NOT_FOUND -> MITThe above file willwhitelistfoono matter what license it presents.whitelistfoobarif it presents as NOT_SPECIFIED.whitelistbarbazif it presents as 404_NOT_FOUND and remap it to MIT.The remapping is used when listing the output as well as in the--jsonoutput.If written as json, the following is eqiuvalent:{
    "foo": {},
    "foobar": {"expected": "NOT_SPECIFIED"},
    "barbaz": {"expected": ""404_NOT_FOUND", "mapto": "MIT"}
}It is possible to add several--whitelistarguments. If they reference a file, the file will be read. If they do not, the argument will be treated as a line in the textfile as described above.Exclude packagesSome packages you may want to exclude. Maybe because they are your own. Maybe because some other reason. Fear not. Simply list them in a text file and add--exclude <file>as an argument. The packages listed in the file (case sensitive) will be excluded from any output - their meta data will not even be fetched!The file format is simple. One package per line. Lines starting with "#" are considered comments. You cannot add comments after the package name.Note that you can add a bunch of packages straight on the command line using the same switch:-x pack1 -x pack2 -x pack3. It is even possible to merge it into one argument--exclude pack1,pack2,pack3(or even--exclude "pack1, pack2, pack3"). Anything that matches a filename will be treated as a file and read. Other values will be trated as package names. Note that if you merge items togetherHard check on licensesSome projects prefer to avoid certain OSS licenses. This was actually the main reason for writing the tool. There are many ways to accomplish such a verification, including using the switch-uor--unwanted- for instance-u GPLor-unwanted "MIT License". You can add as manu-u/--unwantedarguments as you wish.Adding the-uswitch will first print all packages and their licenses, then print out all packages that match any unwanted license and finallyterminate with an error code, breaking the build.License types are checked case-insensitive.Updating wiki pagesAll output is written tostdout, so something likepkglic -f requirements.txt | tee /tmp/licenses_in_use
wikiupdater --host wiki.intranet --target-page licenses --upload /tmp/licenses_in_usewill do the trick (assuming you have a tool called wikiupdater etc etc).Scanning multiple filesIf you have scattered your requirements.txt throughout your source tree, and even separated the development packages into dev-requirements.txt, you can use the existing tools tofindall files and then add them to the command lline usingxargs.find -iname '*requirements.txt' | xargs pkglic -fThis can of course be combined with tee.find -iname '*requirements.txt' | xargs python pkglic -f | tee /tmp/licenses_in_useIf you know your file locations in advance, you can specify them directly.pkglic -f module1/requirements.txt -f module2/requirements.txt -f frontend/package.jsonNote that adding multiple files may result in duplication of packages, if they appear in multiple files. This may add some value, as you will see in how many places (but not which places) you require each package. If that is more information than you need, then remove the duplicate packages with--uniq. Duplicates are eliminated prior to fetching meta data, thus saving some execution time. Worth noting is that the dupes will not be part of any output!Modifying the outputIf you are reasonably satisfied with the standard output format, but want to tweak it just a little, you can for instance remove the type indicator easily usingsed.find -iname '*requirements.txt' | xargs pkglic -f | | sed -e "s/\[.*\] //"For heavy duty modification, add--json <file>to the argument list. That will yield standard output on stdout and a list of dictionaries in<file>. This should be easy enough to import into some other tool to generate a nifty report or a credits page.pkglic -f requirements.txt --json licenses.json
generate_credits -i licenses.lic > credits-html(assuming you have a toolgenerate_creditsetc etc)The output file has the following format:{
    "generator": "pkglic (c) Jesper Hogstrom 2021",
    "generated": "date of execution in iso-format",
    "packages":
    [
        {
            "name": <packageid>,
            "version": <version>,
            "license": <license type or filename with license (prefixed by 'file: ')>,
            "licenseurl": <url with license text or null>,
            "author": <author or null>,
            "author_email": <author's email or null>,
            "home_page": <url to project's home page or null>,
            "summary": <summary or null>
        },
        ...
    ]
}It is also possible to generate an output file straight from pkglic, by means of--creditsand--creditstemplate. The credits template will be expanded byjinja2, with a list of packages as described above passed in namedpackages.There is a simple template included as default if you omit the--creditstemplateargument.License typesThe license type will be set to whatever the package specifies. However, in some cases the license cannot be determined. If so, the following applies:The meta data could not be downloaded: 404_NOT_FOUNDThe license node not present: NOT_SPECIFIED