11from __future__ import absolute_import , division , print_function
22
3- import argparse
43import json
5- import logging
64import os
75import re
86import shutil
97import subprocess
108import sys
9+ from io import BytesIO
10+
1111import zipfile
1212import requests
13- from importlib import import_module
14- from io import BytesIO
13+ import argparse
1514
1615from octoprint .plugins .softwareupdate import exceptions
17- from octoprint .settings import _default_basedir
1816
19- import update_script_config
17+ from octoprint .settings import _default_basedir
18+ from octoprint_mrbeam .mrb_logger import mrb_logger
2019
20+ from octoprint_mrbeam .util .pip_util import get_version_of_pip_module , get_pip_caller
2121from requests .adapters import HTTPAdapter
2222from urllib3 import Retry
2323from urllib3 .exceptions import MaxRetryError , ConnectionError
2424
25- pip_util_mod = import_module (
26- "{}.util.pip_util" .format (update_script_config .IMPORT_TOP_DIR )
27- )
25+ _logger = mrb_logger ("octoprint.plugins.mrbeam.softwareupdate.updatescript" )
2826
29- get_version_of_pip_module = getattr (pip_util_mod , "get_version_of_pip_module" )
30- get_pip_caller = getattr (pip_util_mod , "get_pip_caller" )
3127
32- _logger = logging .getLogger (
33- "octoprint.plugins.{}.softwareupdate.updatescript" .format (
34- update_script_config .PLUGIN_NAME
35- )
36- )
28+ UPDATE_CONFIG_NAME = "mrbeam"
29+ REPO_NAME = "MrBeamPlugin"
30+ MAIN_SRC_FOLDER_NAME = "octoprint_mrbeam"
31+ PLUGIN_NAME = "Mr_Beam"
32+ DEFAULT_OPRINT_VENV = "/home/pi/oprint/bin/pip"
33+ PIP_WHEEL_TEMP_FOLDER = "/tmp/wheelhouse"
3734
3835
3936def _parse_arguments ():
@@ -129,7 +126,7 @@ def get_dependencies(path):
129126 mrbeam-ledstrips==0.2.2-alpha.2
130127 output: [[iobeam][==][0.7.15]]
131128 [[mrb-hw-info][==][0.0.25]]
132- [[mrbeam-ledstrips][==][0.2.2-alpha.2]]
129+ [[mrbeam-ledstrips][==][0.2.2-alpha.2]]
133130 """
134131 try :
135132 with open (dependencies_path , "r" ) as f :
@@ -168,33 +165,23 @@ def build_wheels(build_queue):
168165
169166 """
170167 try :
171- if not os .path .isdir (update_script_config . PIP_WHEEL_TEMP_FOLDER ):
172- os .mkdir (update_script_config . PIP_WHEEL_TEMP_FOLDER )
168+ if not os .path .isdir (PIP_WHEEL_TEMP_FOLDER ):
169+ os .mkdir (PIP_WHEEL_TEMP_FOLDER )
173170 except OSError as e :
174- raise RuntimeError (
175- "can't create wheel tmp folder {} - {}" .format (
176- update_script_config .PIP_WHEEL_TEMP_FOLDER , e
177- )
178- )
171+ raise RuntimeError ("can't create wheel tmp folder {} - {}" .format (PIP_WHEEL_TEMP_FOLDER , e ))
179172
180173 for venv , packages in build_queue .items ():
181- tmp_folder = os .path .join (
182- update_script_config .PIP_WHEEL_TEMP_FOLDER ,
183- re .search (r"\w+((?=\/venv)|(?=\/bin))" , venv ).group (0 ),
184- )
174+ tmp_folder = os .path .join (PIP_WHEEL_TEMP_FOLDER , re .search (r"\w+((?=\/venv)|(?=\/bin))" , venv ).group (0 ))
185175 if os .path .isdir (tmp_folder ):
186176 try :
187177 os .system ("sudo rm -r {}" .format (tmp_folder ))
188178 except Exception as e :
189- raise RuntimeError (
190- "can't delete pip wheel temp folder {} - {}" .format (tmp_folder , e )
191- )
179+ raise RuntimeError ("can't delete pip wheel temp folder {} - {}" .format (tmp_folder , e ))
192180
193181 pip_args = [
194182 "wheel" ,
195183 "--disable-pip-version-check" ,
196- "--wheel-dir={}" .format (tmp_folder ),
197- # Build wheels into <dir>, where the default is the current working directory.
184+ "--wheel-dir={}" .format (tmp_folder ), # Build wheels into <dir>, where the default is the current working directory.
198185 "--no-dependencies" , # Don't install package dependencies.
199186 ]
200187 for package in packages :
@@ -226,23 +213,22 @@ def install_wheels(install_queue):
226213 raise RuntimeError ("install queue is not a dict" )
227214
228215 for venv , packages in install_queue .items ():
229- tmp_folder = os .path .join (
230- update_script_config .PIP_WHEEL_TEMP_FOLDER ,
231- re .search (r"\w+((?=\/venv)|(?=\/bin))" , venv ).group (0 ),
232- )
216+ tmp_folder = os .path .join (PIP_WHEEL_TEMP_FOLDER , re .search (r"\w+((?=\/venv)|(?=\/bin))" , venv ).group (0 ))
233217 pip_args = [
234218 "install" ,
235219 "--disable-pip-version-check" ,
236- "--upgrade" ,
237- # Upgrade all specified packages to the newest available version. The handling of dependencies depends on the upgrade-strategy used.
220+ "--upgrade" , # Upgrade all specified packages to the newest available version. The handling of dependencies depends on the upgrade-strategy used.
238221 "--force-reinstall" , # Reinstall all packages even if they are already up-to-date.
239222 "--no-index" , # Ignore package index (only looking at --find-links URLs instead).
240- "--find-links={}" .format (tmp_folder ),
241- # If a URL or path to an html file, then parse for links to archives such as sdist (.tar.gz) or wheel (.whl) files. If a local path or file:// URL that's a directory, then look for archives in the directory listing. Links to VCS project URLs are not supported.
223+ "--find-links={}" .format (tmp_folder ), # If a URL or path to an html file, then parse for links to archives such as sdist (.tar.gz) or wheel (.whl) files. If a local path or file:// URL that's a directory, then look for archives in the directory listing. Links to VCS project URLs are not supported.
242224 "--no-dependencies" , # Don't install package dependencies.
243225 ]
244226 for package in packages :
245- pip_args .append ("{package}" .format (package = package ["name" ]))
227+ pip_args .append (
228+ "{package}" .format (
229+ package = package ["name" ]
230+ )
231+ )
246232
247233 returncode , exec_stdout , exec_stderr = get_pip_caller (venv , _logger ).execute (
248234 * pip_args
@@ -268,21 +254,18 @@ def build_queue(update_info, dependencies, plugin_archive):
268254 install_queue = {}
269255
270256 install_queue .setdefault (
271- update_info .get (update_script_config .UPDATE_CONFIG_NAME ).get (
272- "pip_command" , update_script_config .DEFAULT_OPRINT_VENV
273- ),
274- [],
257+ update_info .get (UPDATE_CONFIG_NAME ).get ("pip_command" , DEFAULT_OPRINT_VENV ), []
275258 ).append (
276259 {
277- "name" : update_script_config . PLUGIN_NAME ,
260+ "name" : PLUGIN_NAME ,
278261 "archive" : plugin_archive ,
279- "target" : "" ,
262+ "target" : '' ,
280263 }
281264 )
282265 print ("dependencies - {}" .format (dependencies ))
283266 if dependencies :
284267 for dependency in dependencies :
285- plugin_config = update_info .get (update_script_config . UPDATE_CONFIG_NAME )
268+ plugin_config = update_info .get (UPDATE_CONFIG_NAME )
286269 plugin_dependencies_config = plugin_config .get ("dependencies" )
287270 dependency_config = plugin_dependencies_config .get (dependency ["name" ])
288271
@@ -309,17 +292,12 @@ def build_queue(update_info, dependencies, plugin_archive):
309292
310293 installed_version = get_version_of_pip_module (
311294 dependency ["name" ],
312- dependency_config .get (
313- "pip_command" , update_script_config .DEFAULT_OPRINT_VENV
314- ),
295+ dependency_config .get ("pip_command" , DEFAULT_OPRINT_VENV ),
315296 )
316297
317298 if installed_version != version_needed :
318299 install_queue .setdefault (
319- dependency_config .get (
320- "pip_command" , update_script_config .DEFAULT_OPRINT_VENV
321- ),
322- [],
300+ dependency_config .get ("pip_command" , DEFAULT_OPRINT_VENV ), []
323301 ).append (
324302 {
325303 "name" : dependency ["name" ],
@@ -349,7 +327,9 @@ def run_update():
349327 # get update config of dependencies
350328 update_info = get_update_info ()
351329
352- install_queue = build_queue (update_info , dependencies , args .archive )
330+ install_queue = build_queue (
331+ update_info , dependencies , args .archive
332+ )
353333
354334 print ("install_queue" , install_queue )
355335 if install_queue is not None :
@@ -409,14 +389,11 @@ def loadPluginTarget(archive, folder):
409389 )
410390
411391 # unzip repo
412- plugin_extracted_path = os .path .join (
413- folder , update_script_config .UPDATE_CONFIG_NAME
414- )
392+ plugin_extracted_path = os .path .join (folder , UPDATE_CONFIG_NAME )
415393 plugin_extracted_path_folder = os .path .join (
416394 plugin_extracted_path ,
417395 "{repo_name}-{target}" .format (
418- repo_name = update_script_config .REPO_NAME ,
419- target = re .sub (r"^v" , "" , filename .split (".zip" )[0 ]),
396+ repo_name = REPO_NAME , target = re .sub (r"^v" , "" , filename .split (".zip" )[0 ])
420397 ),
421398 )
422399 try :
@@ -427,57 +404,30 @@ def loadPluginTarget(archive, folder):
427404 raise RuntimeError ("Could not unzip plugin repo - error: {}" .format (e ))
428405
429406 # copy new dependencies to working directory
430- _copy_file_to_working_dir (
431- folder = folder ,
432- plugin_extracted_path_folder = plugin_extracted_path_folder ,
433- origin_file = "dependencies.txt" ,
434- destination_file = "dependencies.txt" ,
435- )
407+ try :
408+ shutil .copy2 (
409+ os .path .join (
410+ plugin_extracted_path_folder , MAIN_SRC_FOLDER_NAME , "dependencies.txt"
411+ ),
412+ os .path .join (folder , "dependencies.txt" ),
413+ )
414+ except IOError :
415+ raise RuntimeError ("Could not copy dependencies to working directory" )
436416
437417 # copy new update script to working directory
438- _copy_file_to_working_dir (
439- folder = folder ,
440- plugin_extracted_path_folder = plugin_extracted_path_folder ,
441- origin_file = "scripts/update_script.py" ,
442- destination_file = "update_script.py" ,
443- )
444-
445- # copy new update script config to working directory
446- _copy_file_to_working_dir (
447- folder = folder ,
448- plugin_extracted_path_folder = plugin_extracted_path_folder ,
449- origin_file = "scripts/update_script_config.py" ,
450- destination_file = "update_script_config.py" ,
451- )
452-
453- return zip_file_path
454-
455-
456- def _copy_file_to_working_dir (
457- folder , plugin_extracted_path_folder , origin_file , destination_file
458- ):
459- """
460- copy a file from the downloaded copy of the plugin to the working directory
461-
462- Args:
463- folder: the destination directory
464- plugin_extracted_path_folder: the path to the extracted plugin code
465- origin_file: the path of the original file inside the plugin
466- destination_file: the destination path
467- """
468418 try :
469419 shutil .copy2 (
470420 os .path .join (
471421 plugin_extracted_path_folder ,
472- update_script_config . MAIN_SRC_FOLDER_NAME ,
473- origin_file ,
422+ MAIN_SRC_FOLDER_NAME ,
423+ "scripts/update_script.py" ,
474424 ),
475- os .path .join (folder , destination_file ),
476- )
477- except IOError as ioe :
478- raise RuntimeError (
479- "Could not copy {} to working directory: {}" .format (origin_file , ioe )
425+ os .path .join (folder , "update_script.py" ),
480426 )
427+ except IOError :
428+ raise RuntimeError ("Could not copy update_script to working directory" )
429+
430+ return zip_file_path
481431
482432
483433def main ():
@@ -492,7 +442,9 @@ def main():
492442 args = _parse_arguments ()
493443 if args .call :
494444 if args .archive is None :
495- raise RuntimeError ("Could not run update archive is missing" )
445+ raise RuntimeError (
446+ "Could not run update archive is missing"
447+ )
496448 run_update ()
497449 else :
498450
@@ -505,14 +457,17 @@ def main():
505457
506458 update_info = get_update_info ()
507459 archive = loadPluginTarget (
508- update_info .get (update_script_config . UPDATE_CONFIG_NAME )
460+ update_info .get (UPDATE_CONFIG_NAME )
509461 .get ("pip" )
510462 .format (target_version = args .target ),
511463 folder ,
512464 )
513465
514466 # call new update script with args
515- sys .argv = ["--call=true" , "--archive={}" .format (archive )] + sys .argv [1 :]
467+ sys .argv = [
468+ "--call=true" ,
469+ "--archive={}" .format (archive )
470+ ] + sys .argv [1 :]
516471 try :
517472 result = subprocess .call (
518473 [sys .executable , os .path .join (folder , "update_script.py" )] + sys .argv ,
0 commit comments