Skip to content

Commit

Permalink
Use PATH variable to find where frotz binary exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Saumya-Mishra9129 committed Aug 21, 2020
1 parent ca59b68 commit 65c66e6
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions frotz.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
from sugar3.graphics import style
from sugar3.graphics.alert import ConfirmationAlert

# gets the PATH environment variable
PATH = os.getenv('PATH').split(os.pathsep)


class FrotzActivity(activity.Activity):

def __init__(self, handle):
Expand Down Expand Up @@ -129,7 +133,27 @@ def __init__(self, handle):
def _quit_cb(self, vte, foo=None):
logging.debug("Quitting...")
self.close()


def get_executable_path(self, executable, raise_error=True):
"""
Returns the absolute path of the executable
if it is found on the PATH,
if it is not found raises a FileNotFoundError
:param executable:
:return:
"""

for i in PATH:
if os.path.exists(os.path.join(i, executable)):
return os.path.join(i, executable)
if raise_error:
raise FileNotFoundError(
"Could not find {p} on PATH. "
"Make sure {p} is added to path and try again".format(
p=executable))
else:
return False

def start_game(self, game_file):
if not self.game_started:
# cd to a persistent directory
Expand All @@ -138,25 +162,23 @@ def start_game(self, game_file):
# print a welcome banner and pause for a moment
# that way the end user will have a chance to see which version of frotz we are using
# and which file we are loading
if platform.version().find("Ubuntu") > -1 or platform.version().find("Debian") > -1:
cmd = '/usr/games/frotz'
else:
cmd = '/usr/bin/frotz'
if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
logging.debug('Using frotz installed in the system')
self._vte.feed_child(
(
"cd '%s'; "
"clear; "
"frotz|head -3 ; "
"echo '\nLoading %s...'; "
"sleep 2; frotz '%s'; "
"exit\n" % (
save_dir,
os.path.basename(game_file),
game_file)
).encode('utf-8')
)
if self.get_executable_path("frotz", raise_error=False):
cmd = self.get_executable_path("frotz", raise_error=False)
if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
logging.debug('Using frotz installed in the system')
self._vte.feed_child(
(
"cd '%s'; "
"clear; "
"frotz|head -3 ; "
"echo '\nLoading %s...'; "
"sleep 2; frotz '%s'; "
"exit\n" % (
save_dir,
os.path.basename(game_file),
game_file)
).encode('utf-8')
)
else:
alert = ConfirmationAlert()
alert.props.title = "No module named frotz"
Expand All @@ -178,7 +200,7 @@ def _alert_response_cb(self, alert, response_id):
logging.debug("Installing frotz")
if platform.version().find("Ubuntu") > -1 or platform.version().find("Debian") > -1:
self._vte.feed_child("sudo apt install frotz\n".encode('utf-8'))
if platform.platform().find("fedora") > -1:
else:
self._vte.feed_child("sudo dnf install frotz\n".encode('utf-8'))

def _game_start_cb(self, alert, response_id):
Expand Down

0 comments on commit 65c66e6

Please sign in to comment.