22""" The Menu Bars for faceswap GUI """
33
44import gettext
5- import locale
65import logging
76import os
87import sys
1110from tkinter import ttk
1211import webbrowser
1312
14- from subprocess import Popen , PIPE , STDOUT
15-
1613from lib .multithreading import MultiThread
1714from lib .serializer import get_serializer , Serializer
1815from lib .utils import FaceswapError
19- import update_deps
2016
2117from .popup_configure import open_popup
2218from .custom_widgets import Tooltip
@@ -273,130 +269,9 @@ def _output_sysinfo(self):
273269 print (info )
274270 self .root .config (cursor = "" )
275271
276- @classmethod
277- def _check_for_updates (cls , encoding : str , check : bool = False ) -> bool :
278- """ Check whether an update is required
279-
280- Parameters
281- ----------
282- encoding: str
283- The encoding to use for decoding process returns
284- check: bool
285- ``True`` if we are just checking for updates ``False`` if a check and update is to be
286- performed. Default: ``False``
287-
288- Returns
289- -------
290- bool
291- ``True`` if an update is required
292- """
293- # Do the check
294- logger .info ("Checking for updates..." )
295- update = False
296- msg = ""
297- gitcmd = "git remote update && git status -uno"
298- with Popen (gitcmd , shell = True , stdout = PIPE , stderr = STDOUT , cwd = _WORKING_DIR ) as cmd :
299- stdout , _ = cmd .communicate ()
300- retcode = cmd .poll ()
301- if retcode != 0 :
302- msg = ("Git is not installed or you are not running a cloned repo. "
303- "Unable to check for updates" )
304- else :
305- chk = stdout .decode (encoding , errors = "replace" ).splitlines ()
306- for line in chk :
307- if line .lower ().startswith ("your branch is ahead" ):
308- msg = "Your branch is ahead of the remote repo. Not updating"
309- break
310- if line .lower ().startswith ("your branch is up to date" ):
311- msg = "Faceswap is up to date."
312- break
313- if line .lower ().startswith ("your branch is behind" ):
314- msg = "There are updates available"
315- update = True
316- break
317- if "have diverged" in line .lower ():
318- msg = "Your branch has diverged from the remote repo. Not updating"
319- break
320- if not update or check :
321- logger .info (msg )
322- logger .debug ("Checked for update. Update required: %s" , update )
323- return update
324-
325- def _check (self ) -> None :
326- """ Check for updates and clone repository """
327- logger .debug ("Checking for updates..." )
328- self .root .config (cursor = "watch" )
329- encoding = locale .getpreferredencoding ()
330- logger .debug ("Encoding: %s" , encoding )
331- self ._check_for_updates (encoding , check = True )
332- self .root .config (cursor = "" )
333-
334- @classmethod
335- def _do_update (cls , encoding : str ) -> bool :
336- """ Update Faceswap
337-
338- Parameters
339- ----------
340- encoding: str
341- The encoding to use for decoding process returns
342-
343- Returns
344- -------
345- bool
346- ``True`` if update was successful
347- """
348- logger .info ("A new version is available. Updating..." )
349- gitcmd = "git pull"
350- with Popen (gitcmd ,
351- shell = True ,
352- stdout = PIPE ,
353- stderr = STDOUT ,
354- bufsize = 1 ,
355- cwd = _WORKING_DIR ) as cmd :
356- while True :
357- out = cmd .stdout
358- output = "" if out is None else out .readline ().decode (encoding , errors = "replace" )
359- if output == "" and cmd .poll () is not None :
360- break
361- if output :
362- logger .debug ("'%s' output: '%s'" , gitcmd , output .strip ())
363- print (output .strip ())
364- retcode = cmd .poll ()
365- logger .debug ("'%s' returncode: %s" , gitcmd , retcode )
366- if retcode != 0 :
367- logger .info ("An error occurred during update. return code: %s" , retcode )
368- retval = False
369- else :
370- retval = True
371- return retval
372-
373- def _update (self ) -> None :
374- """ Check for updates and clone repository """
375- logger .debug ("Updating Faceswap..." )
376- self .root .config (cursor = "watch" )
377- encoding = locale .getpreferredencoding ()
378- logger .debug ("Encoding: %s" , encoding )
379- success = False
380- if self ._check_for_updates (encoding ):
381- success = self ._do_update (encoding )
382- update_deps .main (is_gui = True )
383- if success :
384- logger .info ("Please restart Faceswap to complete the update." )
385- self .root .config (cursor = "" )
386-
387272 def _build (self ) -> None :
388273 """ Build the help menu """
389274 logger .debug ("Building Help menu" )
390-
391- self .add_command (label = _ ("Check for updates..." ),
392- underline = 0 ,
393- command = lambda action = "_check" : self ._in_thread (action )) # type:ignore
394- self .add_command (label = _ ("Update Faceswap..." ),
395- underline = 0 ,
396- command = lambda action = "_update" : self ._in_thread (action )) # type:ignore
397- if self ._build_branches_menu ():
398- self .add_cascade (label = _ ("Switch Branch" ), underline = 7 , menu = self ._branches_menu )
399- self .add_separator ()
400275 self ._build_recources_menu ()
401276 self .add_cascade (label = _ ("Resources" ), underline = 0 , menu = self .recources_menu )
402277 self .add_separator ()
@@ -406,109 +281,6 @@ def _build(self) -> None:
406281 command = lambda action = "_output_sysinfo" : self ._in_thread (action )) # type:ignore
407282 logger .debug ("Built help menu" )
408283
409- def _build_branches_menu (self ) -> bool :
410- """ Build branch selection menu.
411-
412- Queries git for available branches and builds a menu based on output.
413-
414- Returns
415- -------
416- bool
417- ``True`` if menu was successfully built otherwise ``False``
418- """
419- stdout = self ._get_branches ()
420- if stdout is None :
421- return False
422-
423- branches = self ._filter_branches (stdout )
424- if not branches :
425- return False
426-
427- for branch in branches :
428- self ._branches_menu .add_command (
429- label = branch ,
430- command = lambda b = branch : self ._switch_branch (b )) # type:ignore
431- return True
432-
433- @classmethod
434- def _get_branches (cls ) -> T .Optional [str ]:
435- """ Get the available github branches
436-
437- Returns
438- -------
439- str or ``None``
440- The list of branches available. If no branches were found or there was an
441- error then `None` is returned
442- """
443- gitcmd = "git branch -a"
444- with Popen (gitcmd , shell = True , stdout = PIPE , stderr = STDOUT , cwd = _WORKING_DIR ) as cmd :
445- stdout , _ = cmd .communicate ()
446- retcode = cmd .poll ()
447- if retcode != 0 :
448- logger .debug ("Unable to list git branches. return code: %s, message: %s" ,
449- retcode ,
450- stdout .decode (locale .getpreferredencoding (),
451- errors = "replace" ).strip ().replace ("\n " , " - " ))
452- return None
453- return stdout .decode (locale .getpreferredencoding (), errors = "replace" )
454-
455- @classmethod
456- def _filter_branches (cls , stdout : str ) -> T .List [str ]:
457- """ Filter the branches, remove duplicates and the current branch and return a sorted
458- list.
459-
460- Parameters
461- ----------
462- stdout: str
463- The output from the git branch query converted to a string
464-
465- Returns
466- -------
467- list[str]
468- Unique list of available branches sorted in alphabetical order
469- """
470- current = None
471- branches = set ()
472- for line in stdout .splitlines ():
473- branch = line [line .rfind ("/" ) + 1 :] if "/" in line else line .strip ()
474- if branch .startswith ("*" ):
475- branch = branch .replace ("*" , "" ).strip ()
476- current = branch
477- continue
478- branches .add (branch )
479- logger .debug ("Found branches: %s" , branches )
480- if current in branches :
481- logger .debug ("Removing current branch from output: %s" , current )
482- branches .remove (current )
483-
484- retval = sorted (list (branches ), key = str .casefold )
485- logger .debug ("Final branches: %s" , retval )
486- return retval
487-
488- @classmethod
489- def _switch_branch (cls , branch : str ) -> None :
490- """ Change the currently checked out branch, and return a notification.
491-
492- Parameters
493- ----------
494- str
495- The branch to switch to
496- """
497- logger .info ("Switching branch to '%s'..." , branch )
498- gitcmd = f"git checkout { branch } "
499- with Popen (gitcmd , shell = True , stdout = PIPE , stderr = STDOUT , cwd = _WORKING_DIR ) as cmd :
500- stdout , _ = cmd .communicate ()
501- retcode = cmd .poll ()
502- if retcode != 0 :
503- logger .error ("Unable to switch branch. return code: %s, message: %s" ,
504- retcode ,
505- stdout .decode (T .cast (str , locale .getdefaultlocale ()),
506- errors = "replace" ).strip ().replace ("\n " , " - " ))
507- return
508- logger .info ("Succesfully switched to '%s'. You may want to check for updates to make sure "
509- "that you have the latest code." , branch )
510- logger .info ("Please restart Faceswap to complete the switch." )
511-
512284 def _build_recources_menu (self ) -> None :
513285 """ Build resources menu """
514286 # pylint: disable=cell-var-from-loop
0 commit comments