pawamoy / pawamoy.github.io

http://pawamoy.github.io/
2 stars 0 forks source link

Write post on getting info about your project's dependencies #3

Closed pawamoy closed 3 years ago

pawamoy commented 7 years ago
#!/bin/bash

print_line() {
  echo -n ${name}\;
  echo -n ${manager}\;
  echo -n ${version}\;
  echo -n ${license}\;
  echo -n ${kind}\;
  echo -n ${description}\;
  echo -n ${status}\;
  echo -n ${risk}\;
  echo -n ${verify}\;
  echo -n ${dependencies}\;
  echo -n ${homepage}\;
  echo -n ${sources}\;
  echo -n ${documentation}\;
  unset name manager version license kind description status risk verify
  unset dependencies homepage sources documentation
}

print_line_pip() {
  echo -n ${pypi}\;
  echo -n ${django}\;
  unset pypi django
}

# license from python code
# def get_pkg_license(pkgname):
#     """
#     Given a package reference (as from requirements.txt),
#     return license listed in package metadata.
#     Note: This function does no error checking and is for
#     demonstration purposes only.
#     """
#     pkgs = pkg_resources.require(pkgname)
#     pkg = pkgs[0]
#     for line in pkg.get_metadata_lines('PKG-INFO'):
#         (k, v) = line.split(': ', 1)
#         if k == "License":
#             return v
#     return None

list_apt() {
  local a
  for a; do
    name="$a"
    info="$(apt-cache show "$a")"
    manager=${MANAGER:-apt}
    version=$(echo "${info}" | grep Version: | head -n1 | cut -d' ' -f2-)
    kind=$(echo "${info}" | grep Section: | head -n1 | cut -d' ' -f2-)
    dependencies=$(echo "${info}" | grep Depends: | head -n1 | cut -d' ' -f2-)
    description=$(echo "${info}" | grep 'Description[^:]*:' | head -n1 | cut -d' ' -f2-)

    print_line
    echo
  done
}

list_pip() {
  local p
  for p; do
    info="$(pip show -v "$p")"
    name="$p"
    manager=${MANAGER:-pip}
    description=$(echo "${info}" | grep '^Summary' | cut -d' ' -f2-)
    license=$(echo "${info}" | grep 'License ::' | sed 's/License :: //;s/OSI Approved :: //;s/ *$/,/' | xargs)
    license=${license%,}
    version=$(echo "${info}" | grep '^Version' | cut -d' ' -f2-)
    homepage=$(echo "${info}" | grep '^Home-page' | cut -d' ' -f2-)
    dependencies=$(echo "${info}" | grep '^Requires' | cut -d' ' -f2-)

    print_line
    print_line_pip
    echo
  done
}

list_npm() {
  local p
  for p; do
    p=${p//#/@}
    name="$p"
    manager=${MANAGER:-npm}
    info=$(npm view "$p" --json description version homepage repository.url license | sed 's/,$//')
    description=$(echo "${info}" | grep '"description":' | xargs | cut -d' ' -f2-)
    version=$(echo "${info}" | grep '"version":' | xargs | cut -d' ' -f2-)
    homepage=$(echo "${info}" | grep '"homepage":' | xargs | cut -d' ' -f2-)
    sources=$(echo "${info}" | grep '"repository.url":' | xargs | cut -d' ' -f2-)
    license=$(echo "${info}" | grep '"license":' | xargs | cut -d' ' -f2-)

    kind=$(npm view "$p" --json keywords | grep '^  ' | sed 's/\"//g')
    dependencies=$(npm view "$p" --json dependencies | grep '^  ' | sed 's/\"//g')

    print_line
    echo
  done  # package-info, npm view PACKAGE dependencies, license-checker
}

list_bower() {
  local p
  for p; do
    p=${p//@/#}
    name="$p"
    manager=${MANAGER:-bower}
    info=$(bower info "$p" --json --offline 2>/dev/null | sed 's/,$//')
    version=$(echo "${info}" | grep '"version":' | xargs | cut -d' ' -f2-)
    license=$(echo "${info}" | grep '"license":' | xargs | cut -d' ' -f2-)
    kind=$(bower info "$p" keywords --json --offline 2>/dev/null | grep '^  ' | sed 's/\"//g')
    description=$(echo "${info}" | grep '"description":' | xargs | cut -d' ' -f2-)
    dependencies=$(bower info "$p" dependencies --json --offline 2>/dev/null | grep '^  ' | sed 's/\"//g')
    homepage=$(echo "${info}" | grep '"homepage":' | xargs | cut -d' ' -f2-)

    print_line
    echo
  done
}

list_apt_auto() {
  MANAGER=apt
  list_apt $(cat "${PROJECT_DIR}/scripts/data/dependencies/apt"*)
}

list_pip_auto() {
  MANAGER=pip
  list_pip $(cat "${PROJECT_DIR}/scripts/data/dependencies/pip"* | sed 's/==.*//' | sort -u) # $(pipdeptree -a -f -w silence | grep -P '^\w+' | sed 's/==.*//')
}

list_npm_auto() {
  MANAGER=npm
  list_npm $(cat "${PROJECT_DIR}/scripts/data/dependencies/npm"*)
}

list_bower_auto() {
  MANAGER=bower
  list_npm bootstrap jquery jquery-ui highcharts
}

list_all_auto() {
  echo "APT packages"; line -; echo
  list_apt_auto
  echo; echo "PIP packages"; line -; echo
  . "${VENV_DIR}/bin/activate"
  list_pip_auto
  echo; echo "NPM packages"; line -; echo
  list_npm_auto
  echo; echo "Bower packages"; line -; echo
  list_bower_auto
}

main() {
  case "$1" in
    apt) shift; list_apt "$@" ;;
    pip) shift; list_pip "$@" ;;
    npm) shift; list_npm "$@" ;;
    bower) shift; MANAGER=bower; list_npm "$@" ;;
    all|"") list_all_auto ;;
    auto)
      case "$2" in
        apt) shift; list_apt_auto ;;
        pip) shift; list_pip_auto ;;
        npm) shift; list_npm_auto ;;
        bower) shift; list_bower_auto ;;
        all|"") list_all_auto ;;
      esac
    ;;
    *)
      echo "usage: list_dependencies [apt|pip|npm|bower <PACKAGE_LIST>]" >&2
      echo "       list_dependencies auto [apt|pip|npm|bower|all]" >&2
      echo "       list_dependencies all" >&2
      exit 1
    ;;
  esac
}
pawamoy commented 3 years ago

Still interesting but I don't think I'll spend time working on this.