# list-modules-imported-v1.py by Jerry Fat from __future__ import print_function import sys, os ''' import os, time, rlcompleter, readline, lxml, yaml import logging import logging.config import argparse from PyQt5.QtGui import * from PyQt5.QtWidgets import * import yaml import importlib #---------------- ##from __future__ import print_function import sys sys.path.append("/usr/local/python/cv2/python-3.6/") sys.path.append("../") #from PyQt5 import QtWidgets as qtw #from PyQt5 import QtGui as qtg #from PyQt5 import QtCore as qtc # from PyQt5.QtCore import Qt, QSize, QTimer from PyQt5 import QtCore from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QMenu, QMdiArea, QMdiSubWindow, QMenuBar, QAction, qApp, QStatusBar, QMessageBox, QAbstractItemView from PyQt5.QtGui import QPalette, QColor # from PyQt5 import QtWidgets # for mdi demo # DRONEKIT from dronekit import connect, VehicleMode, LocationGlobal, LocationGlobalRelative, Command # EOF on TCP socket forever loop # exit button pressed from pymavlink import mavutil # Needed for command message definitions # Import ardupilotmega module for MAVLink 1 from pymavlink.dialects.v10 import ardupilotmega as mavlink1 # Import common module for MAVLink 2 from pymavlink.dialects.v20 import common as mavlink2 import time from datetime import datetime, timedelta # #import tkinter as tk # python3 gui widgets# Import mavutil #from tkinter import ttk #from tkinter import scrolledtext # xml import xml.dom.minidom import xml.etree.ElementTree as ET # gui #from tkinter import * #import tkMessageBox #import tkinter #from tkinter import messagebox #from tkinter import filedialog # for file open close dialog box #from tkinter import Menu # start and use SITL internally import dronekit_sitl # import json # to convert strings to python dictionaries import socket #for tcp/udp # import os # import struct # forpacking dict into binary to send dict over socket import pickle # for sending over tcp import cherrypy from jinja2 import Environment, FileSystemLoader # from __future__ import print_function import os import simplejson import time # Set up option parsing to get connection string --connect "connection_string" on cli import argparse ''' import subprocess # non-blocking import requests # for pip dependencies info and msg text # demo imports import argparse # single package deps import pyavtools # twp packages deps from platform import python_version ############################################################################### print("########## START ########## START ########## START ########## START ########## START ##########") # Parse connection argument parser = argparse.ArgumentParser() filename = "" #parser.add_argument("-c", "--connect", help="connection string") parser.add_argument("-f", "--filename", help="filename.py filename to find import(s) pip .whl and formatted [components] for pyqtdeploy3.3.0") args = parser.parse_args() # command line parameters if args.filename: # input from sitl qgcs or usb serial port to autopilot #connection_string = args.px4 filename = args.filename print("filename = ", filename ) print("python version:",python_version()) print("mkdir dist-site-wheels/ for module wheels dir.. && cd wheels/") # cwd =current working dir from import os cwd = os.getcwd() # use thuis apps imports # create wheels dir to store .whl file downloaded using pip download command = "mkdir "+cwd+"/wheels" # && cd dist-site-wheels subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', command], stderr=subprocess.STDOUT, stdout=subprocess.PIPE) print("$$$$$$$$ begin downloading wheels .whl for imported dist-site module(s): $$$$$$$$" ) import ast allmodules = set() if( len(filename) <=3 ): print("using this apps imported modules") modulenames = set(sys.modules) & set(globals()) ALLmodules = [sys.modules[name] for name in modulenames] for i in ALLmodules: # if not a ''built in'' pkg in python, then download pkg module from pip each wheel as .whl name for each name in modulenames name = str(i).split("'") allmodules.add(name[1]) for module in allmodules: print(module) ''' fruit_list = ["Apple", "Banana"] print(f'Current Fruits List {fruit_list}') new_fruit = input("Please enter a fruit name:\n") fruit_list.append(new_fruit) print(f'Updated Fruits List {fruit_list}') ''' #sys.modules keeps a list of all modules that have been loaded, regardless of whether they're bound to any name in your program. else: print("create dict {module:module-wheel.whl} using command line filename.py -f imported modules and find filename.whl from pip download") # get all import statements from filename.py def visit_Import(node): for name in node.names: allmodules.add(name.name.split(".")[0]) def visit_ImportFrom(node): # if node.module is missing it's a "from . import ..." statement # if level > 0 it's a "from .submodule import ..." statement if node.module is not None and node.level == 0: allmodules.add(node.module.split(".")[0]) node_iter = ast.NodeVisitor() node_iter.visit_Import = visit_Import node_iter.visit_ImportFrom = visit_ImportFrom # with open(filename) as f: node_iter.visit(ast.parse(f.read())) print(allmodules) print("------end-------------") for module in allmodules: print(module) ''' from modulefinder import ModuleFinder finder = ModuleFinder() finder.run_script(filename) # this script filename is "list-modules-imported.py") for name, mod in finder.modules.items(): print(name)''' #This will print all modules loaded by myscript.py. # # # make allmodules var with list of mudules from filename.py #modulenames = set(sys.modules) & set(globals()) #allmodules = [sys.modules[name] for name in modulenames] # sys.modules keeps a list of all modules that have been loaded, regardless of whether they're bound to any name in your program. print("allmodules:",allmodules) #exit() ModuleWheels = {} # dict key:val module:wheel.whl for i in allmodules: # if not a ''built in'' pkg in python, then download pkg module from pip each wheel as .whl name for each name in modulenames name = "\'"+ i + "\'" print("i:",name) component = "[" + i + "]" #str(i).split("'") # if ( " (built-in)>" not in name ) or ("ERROR" not in name ): # skip downloading and adding module, if it is ''built-in'' package #name = str(i).split("'") print("name:",name) # get wheel file(s)from pypi command = "python -m pip download --only-binary :all: --dest "+cwd+"/wheels"+" --no-cache "+i # proc = subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', command], stderr=subprocess.STDOUT, stdout=subprocess.PIPE) proc = subprocess.Popen([ 'bash', '-c', command], stderr=subprocess.STDOUT, stdout=subprocess.PIPE) output = str(proc.stdout.read()).replace("\\n" , "\n\b") #.split("\n") output = str(output).replace("b'" , "") output = str(output).replace("\'" , "") print("pip download stdout:",str(output) ) #("\n")) # print("stdout: ",output.splitlines() ) #("\n")) wheels = output.split("\n") if("ERROR" in str(output) ): # no module wheel names to get or get module print("###### ", component, " module IGNORE, no wheel needed from pip download ######" ) else: print("###### ", component, " module ###### \n##### STARTING - getting module .whl(s) for pyqtdeploy" ) for whl in wheels: # check each line in wheels returned back from pip dowload command in subproccess if ("Collecting" in whl): # if string found in line from pip download stdout if("ERROR" not in str(output) ): # if no ERROR in returned stdout from pip download pypi_url = 'https://pypi.python.org/pypi/' + component + '/json' dependencies = requests.get(pypi_url).json() if (("Downloading" in whl) or ("File was already downloaded" in whl)): if("ERROR" not in str(output) ): # if no ERROR in returned stdout from pip download if ("Downloading" in whl): wheel = whl.replace( ("Downloading "+cwd+"/wheels/") , "") print('#### wheel = ', wheel) moduleStr = wheel.split("-") module = moduleStr[0].split(" ")[2] print(moduleStr, module ) ModuleWheels[ module ] = wheel.replace("\x08 ","") if ("File was already downloaded " in whl): wheel = whl.replace( ("File was already downloaded "+cwd+"/wheels/"), "") print('#### wheel = ', wheel) moduleStr = wheel.split("-") module = moduleStr[0].split(" ")[2] print(moduleStr, module ) ModuleWheels[ module ] = wheel.replace("\x08 ","") if ("Saved" in whl): if("ERROR" not in str(output) ): # if no ERROR in returned stdout from pip download print("#### 'Saved' txt in stdout ",component) if ("Saved " in whl): wheel = whl.replace( ("Saved ./wheels/") , "") print('wheel = ', wheel) moduleStr = wheel.split("-") module = moduleStr[0].split(" ")[2] print(moduleStr, module ) ModuleWheels[ module ] = wheel.replace("\x08 ","") print('wheel = ' + whl.replace("Saved ./wheels/" , "") ) if ("Successfully downloaded" in whl): if("ERROR" not in str(output) ): # if no ERROR in returned stdout from pip download print("#### 'Successfully downloaded' txt in stdout ",component) # printout sysroot.toml b.decode('utf-8') print("###### FINISHED getting wheels for: ", whl.replace("Successfully downloaded " , "") ) else: print("###### skipped ", i, " ###### --> IGNORING module "+component + " is a (built-in) ...") plugin = "\"wheel\"" dependencies = "[]" #"[" + "Python:importlib.resources, Python:os" + "]" exclusions = "[" + "__main__.py" + "]" print("\n#### append to sysroot.toml: ####\n") for component in ModuleWheels: whl = ModuleWheels[component] # print dict ke:value seems to add a space when printing, this does not print("["+component+"]") print('plugin = ' + plugin) print('wheel =', whl) #"\"", str(ModuleWheels[component]).replace(" ","\"") , "\"") #space at front of wheel ? print('dependencies = ' + dependencies ) print('exclusions = ' + exclusions ) print("") print("########## END ########## END ########## END ########## END ########## END ##########")