From version < 27.2 >
edited by Jean-Yves Dupertuis
on 27-03-2018, 17:05
To version < 30.5 >
edited by Jean-Yves Dupertuis
on 27-03-2018, 17:59
< >
Change comment: python

Summary

Details

Page properties
Content
... ... @@ -27,6 +27,436 @@
27 27  Le wiki me demande de lancer un script écrit en python qui se trouve :
28 28  https://raw.githubusercontent.com/pothosware/gnuradio-companion-exe/master/GNURadioHelper.py
29 29  
30 +
31 +{{code language="python" title="fichier de test"}}
32 +# Copyright (c) 2015-2016 Josh Blum
33 +# SPDX-License-Identifier: BSL-1.0
34 +
35 +########################################################################
36 +## Do checks and prepare dependencies for GRC
37 +########################################################################
38 +import os
39 +import sys
40 +import inspect
41 +import tempfile
42 +import subprocess
43 +from ctypes.util import find_library
44 +
45 +########################################################################
46 +## Registry/Environment helpers
47 +########################################################################
48 +import ctypes
49 +from ctypes.wintypes import HWND, UINT, WPARAM, LPARAM, LPVOID
50 +LRESULT = LPARAM
51 +import os
52 +import sys
53 +try:
54 + import winreg
55 + unicode = str
56 +except ImportError:
57 + import _winreg as winreg # Python 2.x
58 +
59 +class Environment(object):
60 + #path = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
61 + #hklm = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
62 + path = r'Environment'
63 + hklm = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)
64 + key = winreg.OpenKey(hklm, path, 0, winreg.KEY_READ | winreg.KEY_WRITE)
65 + SendMessage = ctypes.windll.user32.SendMessageW
66 + SendMessage.argtypes = HWND, UINT, WPARAM, LPVOID
67 + SendMessage.restype = LRESULT
68 + HWND_BROADCAST = 0xFFFF
69 + WM_SETTINGCHANGE = 0x1A
70 + NO_DEFAULT = type('NO_DEFAULT', (object,), {})()
71 +
72 + def get(self, name, default=NO_DEFAULT):
73 + try:
74 + value = winreg.QueryValueEx(self.key, name)[0]
75 + except WindowsError:
76 + if default is self.NO_DEFAULT:
77 + raise ValueError("No such registry key", name)
78 + value = default
79 + return value
80 +
81 + def set(self, name, value):
82 + if value:
83 + winreg.SetValueEx(self.key, name, 0, winreg.REG_EXPAND_SZ, value)
84 + else:
85 + winreg.DeleteValue(self.key, name)
86 + self.notify()
87 +
88 + def notify(self):
89 + self.SendMessage(
90 + self.HWND_BROADCAST, self.WM_SETTINGCHANGE, 0, u'Environment')
91 +
92 +########################################################################
93 +## determine-if-an-executable-or-library-is-32-or-64-bits-on-windows
94 +## https://stackoverflow.com/questions/1345632
95 +########################################################################
96 +import struct
97 +
98 +IMAGE_FILE_MACHINE_I386=332
99 +IMAGE_FILE_MACHINE_IA64=512
100 +IMAGE_FILE_MACHINE_AMD64=34404
101 +
102 +def getDllMachineType(path):
103 + f=open(path, "rb")
104 + s=f.read(2)
105 + if s!="MZ": raise Exception("%s is not a DLL"%path)
106 + f.seek(60)
107 + s=f.read(4)
108 + header_offset=struct.unpack("<L", s)[0]
109 + f.seek(header_offset+4)
110 + s=f.read(2)
111 + machine=struct.unpack("<H", s)[0]
112 + f.close()
113 + return machine
114 +
115 +########################################################################
116 +## Pip helpers
117 +########################################################################
118 +PIP_EXE = os.path.join(os.path.dirname(sys.executable), 'Scripts', 'pip.exe')
119 +
120 +def pip_install(arg):
121 + ret = subprocess.call([PIP_EXE, 'install', arg], shell=True)
122 + if ret != 0:
123 + print("Error: pip failed to install %s"%arg)
124 + return -1
125 +
126 +########################################################################
127 +## Python checks
128 +########################################################################
129 +def check_python_version():
130 + is_64bits = sys.maxsize > 2**32
131 + if not is_64bits:
132 + raise Exception("requires 64-bit Python")
133 +
134 + if sys.version_info.major != 2 or sys.version_info.minor != 7:
135 + raise Exception("requires Python version 2.7")
136 +
137 + if not os.path.exists(PIP_EXE):
138 + raise Exception("can't find pip executable %s"%PIP_EXE)
139 +
140 + return sys.version
141 +
142 +def handle_python_version():
143 + print("Error: Invoke/Reinstall Python2.7 for amd64")
144 + return -1
145 +
146 +########################################################################
147 +## GTK checks
148 +########################################################################
149 +def check_gtk_runtime():
150 +
151 + gtk_dll_name = "libgtk-win32-2.0-0.dll"
152 +
153 + #first check that the installer default is found
154 + installer_default = os.path.join("C:\\Program Files\\GTK2-Runtime Win64\\bin", gtk_dll_name)
155 + if os.path.exists(installer_default): return installer_default
156 +
157 + #regular dll search within the path
158 + libgtk = find_library(gtk_dll_name)
159 + if libgtk is None:
160 + raise Exception("failed to locate the GTK+ runtime DLL")
161 +
162 + #reject 32-bit versions of this dll
163 + if getDllMachineType(libgtk) != IMAGE_FILE_MACHINE_AMD64:
164 + raise Exception("%s is not AMD64"%libgtk)
165 +
166 + return libgtk
167 +
168 +def handle_gtk_runtime():
169 +
170 + GTK_URL = 'http://downloads.myriadrf.org/binaries/python27_amd64/gtk2-runtime-2.22.1-2014-02-01-ts-win64.exe'
171 + GTK_EXE = os.path.join(tempfile.gettempdir(), 'gtk2-runtime-2.22.1-2014-02-01-ts-win64.exe')
172 +
173 + if not os.path.exists(GTK_EXE):
174 +
175 + #need requests to download the exe
176 + try: import requests
177 + except: pip_install("requests")
178 + import requests
179 +
180 + #download from the url to the destination
181 + r = requests.get(GTK_URL)
182 + with open(GTK_EXE, 'wb') as fd:
183 + for chunk in r.iter_content(1024*1024):
184 + fd.write(chunk)
185 +
186 + if not os.path.exists(GTK_EXE):
187 + print("Cant find installer: %s"%GTK_EXE)
188 + print("Failed to download: %s"%GTK_URL)
189 + return -1
190 +
191 + print("Running installer: %s"%GTK_EXE)
192 + ret = subprocess.call([GTK_EXE, '/S'], shell=True) #silent install
193 + if ret != 0:
194 + print("The GTK installer failed with exit code %d"%ret)
195 + exit(ret)
196 +
197 + print("The GTK installer should have modified the system path")
198 + print("Open a new command window and re-run this script...")
199 +
200 +def check_import_gtk():
201 + import gtk
202 + return inspect.getfile(gtk)
203 +
204 +def handle_import_gtk():
205 + pip_install('http://downloads.myriadrf.org/binaries/python27_amd64/pygtk-2.22.0-cp27-none-win_amd64.whl')
206 + pip_install('http://downloads.myriadrf.org/binaries/python27_amd64/pygobject-2.28.6-cp27-none-win_amd64.whl')
207 + pip_install('http://downloads.myriadrf.org/binaries/python27_amd64/pycairo_gtk-1.10.0-cp27-none-win_amd64.whl')
208 +
209 +########################################################################
210 +## GNU Radio checks
211 +########################################################################
212 +def guess_bin_path():
213 +
214 + #was it run from the proper install directory?
215 + path = os.path.abspath(os.path.dirname(__file__))
216 + if os.path.exists(os.path.join(path, "gnuradio-runtime.dll")): return path
217 +
218 + #otherwise search the path to find the root
219 + gnuradio_runtime = find_library("gnuradio-runtime.dll")
220 + if gnuradio_runtime: return gnuradio_runtime
221 +
222 +def check_gr_runtime():
223 + gnuradio_runtime = find_library("gnuradio-runtime.dll")
224 +
225 + if gnuradio_runtime is None:
226 + raise Exception("failed to locate the GNURadio runtime DLL")
227 +
228 + return gnuradio_runtime
229 +
230 +def handle_gr_runtime():
231 +
232 + path = guess_bin_path()
233 +
234 + #we dont know where the bin path is, this is probably an installer issue
235 + #print this message and return error so other handlers are not invoked
236 + if path is None:
237 + print("Error: PothosSDR DLLs missing from the system path")
238 + print(" See instructions to 'Add PothosSDR to the system PATH'")
239 + print(" https://github.com/pothosware/PothosSDR/wiki/Tutorial")
240 + return -1
241 +
242 + e = Environment()
243 + PATH = e.get('PATH', '')
244 + print("Current PATH: '%s'"%PATH)
245 + if not PATH: PATH = list()
246 + else: PATH = PATH.split(';')
247 +
248 + if path not in PATH:
249 + print("Adding %s to the PATH"%path)
250 + PATH.append(path)
251 + e.set('PATH', ';'.join(PATH))
252 +
253 + print("")
254 + print("The PATH for the current user has been modified")
255 + print("Open a new command window and re-run this script...")
256 +
257 +def check_import_gr():
258 + import gnuradio
259 + from gnuradio import gr
260 + return inspect.getfile(gnuradio)
261 +
262 +def handle_import_gr():
263 + binDir = guess_bin_path()
264 + path = os.path.join(os.path.dirname(binDir), 'lib', 'python2.7', 'site-packages')
265 + if not os.path.exists(path): #or use old-style path without python version
266 + path = os.path.join(os.path.dirname(binDir), 'lib', 'site-packages')
267 + path = os.path.normpath(path)
268 + print("Error: GNURadio modules missing from PYTHONPATH")
269 +
270 + print("")
271 + print("Current search path:")
272 + for searchPath in sys.path: print(" * %s"%searchPath)
273 + print("")
274 +
275 + e = Environment()
276 + PYTHONPATH = e.get('PYTHONPATH', '')
277 + print("Current PYTHONPATH: '%s'"%PYTHONPATH)
278 + if not PYTHONPATH: PYTHONPATH = list()
279 + else: PYTHONPATH = PYTHONPATH.split(';')
280 +
281 + if path not in PYTHONPATH:
282 + print("Adding %s to the PYTHONPATH"%path)
283 + PYTHONPATH.append(path)
284 + e.set('PYTHONPATH', ';'.join(PYTHONPATH))
285 +
286 + print("")
287 + print("The PYTHONPATH for the current user has been modified")
288 + print("Open a new command window and re-run this script...")
289 +
290 +def check_grc_blocks_path():
291 + GRC_BLOCKS_PATH = os.environ.get('GRC_BLOCKS_PATH', '').split(';')[0] #take the first entry
292 + if not GRC_BLOCKS_PATH:
293 + raise Exception("GRC_BLOCKS_PATH is not set")
294 + if not os.path.exists(GRC_BLOCKS_PATH):
295 + raise Exception("GRC_BLOCKS_PATH '%s' does not exist"%GRC_BLOCKS_PATH)
296 + if not os.path.exists(os.path.join(GRC_BLOCKS_PATH, 'options.xml')):
297 + raise Exception("GRC_BLOCKS_PATH '%s' does not contain options.xml"%GRC_BLOCKS_PATH)
298 + return GRC_BLOCKS_PATH
299 +
300 +def handle_grc_blocks_path():
301 + binDir = guess_bin_path()
302 + path = os.path.join(os.path.dirname(binDir), 'share', 'gnuradio', 'grc', 'blocks')
303 + path = os.path.normpath(path)
304 +
305 + print("Setting the GRC_BLOCKS_PATH to %s"%path)
306 + e = Environment()
307 + e.set('GRC_BLOCKS_PATH', path)
308 +
309 + print("")
310 + print("The GRC_BLOCKS_PATH for the current user has been modified")
311 + print("Open a new command window and re-run this script...")
312 +
313 +########################################################################
314 +## Other module checks
315 +########################################################################
316 +def check_import_numpy():
317 + import numpy
318 + return inspect.getfile(numpy)
319 +
320 +def handle_import_numpy():
321 + pip_install('http://downloads.myriadrf.org/binaries/python27_amd64/numpy-1.12.0+mkl-cp27-cp27m-win_amd64.whl')
322 +
323 +def check_import_lxml():
324 + import lxml
325 + return inspect.getfile(lxml)
326 +
327 +def handle_import_lxml():
328 + pip_install('http://downloads.myriadrf.org/binaries/python27_amd64/lxml-3.7.2-cp27-cp27m-win_amd64.whl')
329 +
330 +def check_import_cheetah():
331 + import Cheetah
332 + return inspect.getfile(Cheetah)
333 +
334 +def handle_import_cheetah():
335 + pip_install('http://downloads.myriadrf.org/binaries/python27_amd64/Cheetah-2.4.4-cp27-none-win_amd64.whl')
336 +
337 +def check_import_wxpython():
338 + import wx
339 + import wx.glcanvas
340 + return inspect.getfile(wx)
341 +
342 +def handle_import_wxpython():
343 + pip_install('http://downloads.myriadrf.org/binaries/python27_amd64/wxPython-3.0.2.0-cp27-none-win_amd64.whl')
344 + pip_install('http://downloads.myriadrf.org/binaries/python27_amd64/wxPython_common-3.0.2.0-py2-none-any.whl')
345 +
346 +def check_import_pyopengl():
347 + import OpenGL
348 + import OpenGL.GL
349 + return inspect.getfile(OpenGL)
350 +
351 +def handle_import_pyopengl():
352 + print("Installing PyOpenGL with pip:")
353 + pip_install('http://downloads.myriadrf.org/binaries/python27_amd64/PyOpenGL-3.1.1-cp27-cp27m-win_amd64.whl')
354 + pip_install('http://downloads.myriadrf.org/binaries/python27_amd64/PyOpenGL_accelerate-3.1.1-cp27-cp27m-win_amd64.whl')
355 + print(" Done!")
356 +
357 +CHECKS = [
358 + #first check gr runtime so we can locate the install based on runtime dll in PATH
359 + ("GR_RUNTIME", 'locate GNURadio runtime', check_gr_runtime, handle_gr_runtime),
360 +
361 + #gtk runtime is similar check for dlls in the seatch PATH (no python required)
362 + ("GTK_RUNTIME", 'locate GTK+ runtime', check_gtk_runtime, handle_gtk_runtime),
363 +
364 + #basic python environment and import checks and using pip to install from a URL
365 + ("PYVERSION", 'Python version is 2.7', check_python_version, handle_python_version),
366 + ("IMPORT_GTK", 'import gtk module', check_import_gtk, handle_import_gtk),
367 + ("IMPORT_NUMPY", 'import numpy module', check_import_numpy, handle_import_numpy),
368 + ("IMPORT_LXML", 'import lxml module', check_import_lxml, handle_import_lxml),
369 + ("IMPORT_CHEETAH", 'import Cheetah module', check_import_cheetah, handle_import_cheetah),
370 + ("IMPORT_WX", 'import wx module', check_import_wxpython, handle_import_wxpython),
371 + ("IMPORT_OPENGL", 'import OpenGL module', check_import_pyopengl, handle_import_pyopengl),
372 +
373 + #final checks for GNU Radio and GRC that set local environment variables
374 + ("GRC_BLOCKS", 'GRC blocks path set', check_grc_blocks_path, handle_grc_blocks_path),
375 + ("IMPORT_GR", 'import GNURadio module', check_import_gr, handle_import_gr),
376 +]
377 +
378 +def main():
379 + print("")
380 + print("="*40)
381 + print("== Runtime and import checks")
382 + print("="*40)
383 +
384 + maxLen = max([len(c[1]) for c in CHECKS])
385 + msgs = dict()
386 + statuses = dict()
387 + numFails = 0
388 + numPasses = 0
389 + for key, what, check, handle in CHECKS:
390 + whatStr = "%s...%s"%(what, ' '*(maxLen-len(what)))
391 + try:
392 + msg = check()
393 + statStr = "PASS"
394 + checkPassed = True
395 + numPasses += 1
396 + except Exception as ex:
397 + statStr = "FAIL"
398 + checkPassed = False
399 + msg = str(ex)
400 + numFails += 1
401 +
402 + print(" * Check %s %s"%(whatStr, statStr))
403 + msgs[key] = msg
404 + statuses[key] = checkPassed
405 +
406 + if numPasses:
407 + print("")
408 + print("="*40)
409 + print("== Checks passed summary")
410 + print("="*40)
411 + for key, what, check, handle in CHECKS:
412 + if statuses[key]: print("%s:\t%s"%(key, msgs[key]))
413 +
414 + if numFails == 0:
415 + print("")
416 + print("All checked passed! gnuradio-companion is ready to use.")
417 + return 0
418 +
419 + if numFails:
420 + print("")
421 + print("="*40)
422 + print("== Checks failed summary")
423 + print("="*40)
424 + for key, what, check, handle in CHECKS:
425 + if not statuses[key]: print("%s:\t%s"%(key, msgs[key]))
426 +
427 + if numFails:
428 + print("")
429 + print("="*40)
430 + print("== Fixing problems")
431 + print("="*40)
432 + for key, what, check, handle in CHECKS:
433 + if not statuses[key]:
434 + print("Handling issues for %s..."%key)
435 + ret = handle()
436 + #exit asap when return code provided
437 + if ret is not None: return ret
438 +
439 + print("")
440 + print("Changes made! Please re-run this script in a new terminal.")
441 +
442 +if __name__ == '__main__':
443 +
444 + #run main with exception handling
445 + ret = None
446 + try: ret = main()
447 + except Exception as ex:
448 + print("Error: %s"%str(ex))
449 +
450 + #give time to read message if opened from explorer
451 + #wait for user to press a key
452 + print("")
453 + os.system('pause')
454 +
455 + #return the error code from main
456 + if ret is None: ret = 0
457 + exit(ret)
458 +{{/code}}
459 +
30 30  Avec copier - coller, je l'ai sauvé sur mon PC.
31 31  
32 32  J'ouvre le fichier avec "Edit with IDLE", ensuite via "run modul F5" , après avoir installé GTK+ runtime, il m'annonce que je dois réinstallé python 2.7, car j'avais chargé un autre 2.7.14 !
... ... @@ -186,6 +186,19 @@
186 186  
187 187  [[testSDR.grc>>attach:testSDR.grc]]
188 188  
619 +[[image:DJtestDaarioSDR.png||height="545" width="562"]]
620 +
621 +
189 189  Normalement on doit pouvoir lancer directement avec le fichier python :
190 190  
191 191  [[DJTestRadioSDR.py>>attach:DJTestRadioSDR.py||title="fonctionne avec python 2.7"]]
625 +
626 +
627 +[[image:testRadioSDR.png||height="358" width="599"]]
628 +
629 +
630 +C'est confirmé ! depuis un terminal :
631 +
632 +~~$//python DJTestRadioSDR.py//
633 +
634 +[[image:terminalDirectPython.png||height="196" width="610"]]
DJtestDaarioSDR.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Dupertuis
Size
... ... @@ -1,0 +1,1 @@
1 +107.0 KB
Content
terminalDirectPython.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Dupertuis
Size
... ... @@ -1,0 +1,1 @@
1 +46.8 KB
Content
testRadioSDR.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Dupertuis
Size
... ... @@ -1,0 +1,1 @@
1 +279.5 KB
Content