#!/usr/bin/env python3 # # Determine dependencies of python scripts or available python modules in a search path. # # Given the -d argument and a filename/filenames, returns the modules imported by those files. # Given the -d argument and a directory/directories, recurses to find all # python packages and modules, returns the modules imported by these. # Given the -p argument and a path or paths, scans that path for available python modules/packages. import argparse import ast import importlib from importlib import machinery import logging import os.path import sys logger = logging.getLogger('pythondeps') suffixes = importlib.machinery.all_suffixes() class PythonDepError(Exception): pass class DependError(PythonDepError): def __init__(self, path, error): self.path = path self.error = error PythonDepError.__init__(self, error) def __str__(self): return "Failure determining dependencies of {}: {}".format(self.path, self.error) class ImportVisitor(ast.NodeVisitor): def __init__(self): self.imports = set() self.importsfrom = [] def visit_Import(self, node): for alias in node.names: self.imports.add(alias.name) def visit_ImportFrom(self, node): self.importsfrom.append((node.module, [a.name for a in node.names], node.level)) def walk_up(path): while path: yield path path, _, _ = path.rpartition(os.sep) def get_provides(path): path = os.path.realpath(path) def get_fn_name(fn): for suffix in suffixes: if fn.endswith(suffix): return fn[:-len(suffix)] isdir = os.path.isdir(path) if isdir: pkg_path = path walk_path = path else: pkg_path = get_fn_name(path) if pkg_path is None: return walk_path = os.path.dirname(path) for curpath in walk_up(walk_path): if not os.path.exists(os.path.join(curpath, '__init__.py')): libdir = curpath break else: libdir = '' package_relpath = pkg_path[len(libdir)+1:] package = '.'.join(package_relpath.split(os.sep)) if not isdir: yield package, path else: if os.path.exists(os.path.join(path, '__init__.py')): yield package, path for dirpath, dirnames, filenames in os.walk(path): relpath = dirpath[len(path)+1:] if relpath: if '__init__.py' not in filenames: dirnames[:] = [] continue else: context = '.'.join(relpath.split(os.sep)) if package: context = package + '.' + context yield context, dirpath else: context = package for fn in filenames: adjusted_fn = get_fn_name(fn) if not adjusted_fn or adjusted_fn == '__init__': continue fullfn = os.path.join(dirpath, fn) if context: yield context + '.' + adjusted_fn, fullfn else: yield adjusted_fn, fullfn def get_code_depends(code_string, path=None, provide=None, ispkg=False): try: code = ast.parse(code_string, path) except TypeError as exc: raise DependError(path, exc) except SyntaxError as exc: raise DependError(path, exc) visitor = ImportVisitor() visitor.visit(code) for builtin_module in sys.builtin_module_names: if builtin_module in visitor.imports: visitor.imports.remove(builtin_module) if provide: provide_elements = provide.split('.') if ispkg: provide_elements.append("__self__") context = '.'.join(provide_elements[:-1]) package_path = os.path.dirname(path) else: context = None package_path = None levelzero_importsfrom = (module for module, names, level in visitor.importsfrom if level == 0) for module in visitor.imports | set(levelzero_importsfrom): if context and path: module_basepath = os.path.join(package_path, module.replace('.', '/')) if os.path.exists(module_basepath): # Implicit relative import yield context + '.' + module, path continue for suffix in suffixes: if os.path.exists(module_basepath + suffix): # Implicit relative import yield context + '.' + module, path break else: yield module, path else: yield module, path for module, names, level in visitor.importsfrom: if level == 0: continue elif not provide: raise DependError("Error: ImportFrom non-zero level outside of a package: {0}".format((module, names, level)), path) elif level > len(provide_elements): raise DependError("Error: ImportFrom level exceeds package depth: {0}".format((module, names, level)), path) else: context = '.'.join(provide_elements[:-level]) if module: if context: yield context + '.' + module, path else: yield module, path def get_file_depends(path): try: code_string = open(path, 'r').read() except (OSError, IOError) as exc: raise DependError(path, exc) return get_code_depends(code_string, path) def get_depends_recursive(directory): directory = os.path.realpath(directory) provides = dict((v, k) for k, v in get_provides(directory)) for filename, provide in provides.items(): if os.path.isdir(filename): filename = os.path.join(filename, '__init__.py') ispkg = True elif not filename.endswith('.py'): continue elseSUMMARY = "Utilities for Advanced Power Management" DESCRIPTION = "The Advanced Power Management (APM) support provides \ access to battery status information and a set of tools for managing \ notebook power consumption." HOMEPAGE = "http://apenwarr.ca/apmd/" SECTION = "base" LICENSE = "GPLv2+" LIC_FILES_CHKSUM = "file://COPYING;md5=94d55d512a9ba36caa9b7df079bae19f \ file://apm.h;beginline=6;endline=18;md5=7d4acc1250910a89f84ce3cc6557c4c2" DEPENDS = "libtool-cross" PR = "r2" SRC_URI = "${DEBIAN_MIRROR}/main/a/apmd/apmd_3.2.2.orig.tar.gz;name=tarball \ ${DEBIAN_MIRROR}/main/a/apmd/apmd_${PV}.diff.gz;name=patch \ file://libtool.patch \ file://unlinux.patch \ file://init \ file://default \ file://apmd_proxy \ file://apmd_proxy.conf \ file://apmd.service" SRC_URI[tarball.md5sum] = "b1e6309e8331e0f4e6efd311c2d97fa8" SRC_URI[tarball.sha256sum] = "7f7d9f60b7766b852881d40b8ff91d8e39fccb0d1d913102a5c75a2dbb52332d" SRC_URI[patch.md5sum] = "57e1b689264ea80f78353519eece0c92" SRC_URI[patch.sha256sum] = "7905ff96be93d725544d0040e425c42f9c05580db3c272f11cff75b9aa89d430" S = "${WORKDIR}/apmd-3.2.2.orig" inherit update-rc.d systemd INITSCRIPT_NAME = "apmd" INITSCRIPT_PARAMS = "defaults" SYSTEMD_SERVICE_${PN} = "apmd.service" SYSTEMD_AUTO_ENABLE = "disable" do_compile() { # apmd doesn't use whole autotools. Just libtool for installation oe_runmake "LIBTOOL=${STAGING_BINDIR_CROSS}/${HOST_SYS}-libtool" apm apmd } do_install() { install -d ${D}${sysconfdir} install -d ${D}${sysconfdir}/apm install -d ${D}${sysconfdir}/apm/event.d install -d ${D}${sysconfdir}/apm/other.d install -d ${D}${sysconfdir}/apm/suspend.d install -d ${D}${sysconfdir}/apm/resume.d install -d ${D}${sysconfdir}/apm/scripts.d install -d ${D}${sysconfdir}/default install -d ${D}${sysconfdir}/init.d install -d ${D}${sbindir} install -d ${D}${bindir} install -d ${D}${libdir} install -d ${D}${datadir}/apmd install -d ${D}${includedir} install -m 4755 ${S}/.libs/apm ${D}${bindir}/apm install -m 0755 ${S}/.libs/apmd ${D}${sbindir}/apmd install -m 0755 ${WORKDIR}/apmd_proxy ${D}${sysconfdir}/apm/ install -m 0644 ${WORKDIR}/apmd_proxy.conf ${D}${datadir}/apmd/ install -m 0644 ${WORKDIR}/default ${D}${sysconfdir}/default/apmd oe_libinstall -so libapm ${D}${libdir} install -m 0644 apm.h ${D}${includedir} sed -e 's,/usr/sbin,${sbindir},g; s,/etc,${sysconfdir},g;' ${WORKDIR}/init > ${D}${sysconfdir}/init.d/apmd chmod 755 ${D}${sysconfdir}/init.d/apmd install -d ${D}${systemd_unitdir}/system install -m 0644 ${WORKDIR}/apmd.service ${D}${systemd_unitdir}/system/ sed -i -e 's,@SYSCONFDIR@,${sysconfdir},g' \ -e 's,@SBINDIR@,${sbindir},g' ${D}${systemd_unitdir}/system/apmd.service } PACKAGES =+ "libapm libapm-dev libapm-staticdev apm" FILES_libapm = "${libdir}/libapm${SOLIBS}" FILES_libapm-dev = "${libdir}/libapm${SOLIBSDEV} ${includedir} ${libdir}/libapm.la" FILES_libapm-staticdev = "${libdir}/libapm.a" FILES_apm = "${bindir}/apm*"
SUMMARY = "Utilities for Advanced Power Management" DESCRIPTION = "The Advanced Power Management (APM) support provides \ access to battery status information and a set of tools for managing \ notebook power consumption." HOMEPAGE = "http://apenwarr.ca/apmd/" SECTION = "base" LICENSE = "GPLv2+" LIC_FILES_CHKSUM = "file://COPYING;md5=94d55d512a9ba36caa9b7df079bae19f \ file://apm.h;beginline=6;endline=18;md5=7d4acc1250910a89f84ce3cc6557c4c2" DEPENDS = "libtool-cross" PR = "r2" SRC_URI = "${DEBIAN_MIRROR}/main/a/apmd/apmd_3.2.2.orig.tar.gz;name=tarball \ ${DEBIAN_MIRROR}/main/a/apmd/apmd_${PV}.diff.gz;name=patch \ file://libtool.patch \ file://unlinux.patch \ file://init \ file://default \ file://apmd_proxy \ file://apmd_proxy.conf \ file://apmd.service" SRC_URI[tarball.md5sum] = "b1e6309e8331e0f4e6efd311c2d97fa8" SRC_URI[tarball.sha256sum] = "7f7d9f60b7766b852881d40b8ff91d8e39fccb0d1d913102a5c75a2dbb52332d" SRC_URI[patch.md5sum] = "57e1b689264ea80f78353519eece0c92" SRC_URI[patch.sha256sum] = "7905ff96be93d725544d0040e425c42f9c05580db3c272f11cff75b9aa89d430" S = "${WORKDIR}/apmd-3.2.2.orig" inherit update-rc.d systemd INITSCRIPT_NAME = "apmd" INITSCRIPT_PARAMS = "defaults" SYSTEMD_SERVICE_${PN} = "apmd.service" SYSTEMD_AUTO_ENABLE = "disable" do_compile() { # apmd doesn't use whole autotools. Just libtool for installation oe_runmake "LIBTOOL=${STAGING_BINDIR_CROSS}/${HOST_SYS}-libtool" apm apmd } do_install() { install -d ${D}${sysconfdir} install -d ${D}${sysconfdir}/apm install -d ${D}${sysconfdir}/apm/event.d install -d ${D}${sysconfdir}/apm/other.d install -d ${D}${sysconfdir}/apm/suspend.d install -d ${D}${sysconfdir}/apm/resume.d install -d ${D}${sysconfdir}/apm/scripts.d install -d ${D}${sysconfdir}/default install -d ${D}${sysconfdir}/init.d install -d ${D}${sbindir} install -d ${D}${bindir} install -d ${D}${libdir} install -d ${D}${datadir}/apmd install -d ${D}${includedir} install -m 4755 ${S}/.libs/apm ${D}${bindir}/apm install -m 0755 ${S}/.libs/apmd ${D}${sbindir}/apmd install -m 0755 ${WORKDIR}/apmd_proxy ${D}${sysconfdir}/apm/ install -m 0644 ${WORKDIR}/apmd_proxy.conf ${D}${datadir}/apmd/ install -m 0644 ${WORKDIR}/default ${D}${sysconfdir}/default/apmd oe_libinstall -so libapm ${D}${libdir} install -m 0644 apm.h ${D}${includedir} sed -e 's,/usr/sbin,${sbindir},g; s,/etc,${sysconfdir},g;' ${WORKDIR}/init > ${D}${sysconfdir}/init.d/apmd chmod 755 ${D}${sysconfdir}/init.d/apmd install -d ${D}${systemd_unitdir}/system install -m 0644 ${WORKDIR}/apmd.service ${D}${systemd_unitdir}/system/ sed -i -e 's,@SYSCONFDIR@,${sysconfdir},g' \ -e 's,@SBINDIR@,${sbindir},g' ${D}${systemd_unitdir}/system/apmd.service } PACKAGES =+ "libapm libapm-dev libapm-staticdev apm" FILES_libapm = "${libdir}/libapm${SOLIBS}" FILES_libapm-dev = "${libdir}/libapm${SOLIBSDEV} ${includedir} ${libdir}/libapm.la" FILES_libapm-staticdev = "${libdir}/libapm.a" FILES_apm = "${bindir}/apm*"