Skip to content

Commit

Permalink
Release 0.1.13 (#48)
Browse files Browse the repository at this point in the history
- Add flag to `emu device settings -c` to close settings app
- Use most similar remote branch (using difflib) if user types unknown
close branch
- Use existing function in CommandBase for getting flags, exits if fails
so no need to catch errors in each command that has flags
- Alias `fork` to `emu fork switch`. Ex.: `fork stock -b devel`
  • Loading branch information
sshane committed Feb 22, 2021
1 parent 4e2fa49 commit 1ad3842
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 50 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Release 0.1.13 (2021-02-22)
=====

* Add flag to `emu device settings -c` to close settings app
* Use most similar remote branch (using difflib) if user types unknown close branch
* Use existing function in CommandBase for getting flags, exits if fails so no need to catch errors in each command that has flags
* Alias `fork` to `emu fork switch`. Ex.: `fork stock -b devel`


Release 0.1.12 (2020-10-22)
=====

Expand Down
3 changes: 2 additions & 1 deletion aliases.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
alias ll="ls -lhA"
alias ll="ls -lAh"
alias pf="emu panda flash"
alias controlsd="emu debug controlsd"
alias battery="emu device battery"
alias shutdown="emu device shutdown"
alias settings="emu device settings"
alias fork="emu fork switch"
8 changes: 5 additions & 3 deletions commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ def start_function_from_str(self, cmd):
return
getattr(self, cmd)() # call command's function

def parse_flags(self, parser):
def get_flags(self, cmd_name):
try:
return parser.parse_args(self.args), None
return self.commands[cmd_name].parser.parse_args(self.args)
except Exception as e:
return None, e
error(e)
self._help(cmd_name)
exit(1)

def _help(self, cmd, show_description=True, leading=''):
has_extra_info = False
Expand Down
7 changes: 2 additions & 5 deletions commands/debug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from py_utils.emu_utils import run, kill, warning, error
from py_utils.emu_utils import OPENPILOT_PATH


class Debug(CommandBase):
def __init__(self):
super().__init__()
Expand All @@ -15,11 +16,7 @@ def __init__(self):
self.default_path = '/data/output.log'

def _controlsd(self):
flags, e = self.parse_flags(self.commands['controlsd'].parser)
if e is not None:
error(e)
return

flags = self.get_flags('controlsd')
out_file = self.default_path
if flags.output is not None:
out_file = flags.output
Expand Down
32 changes: 16 additions & 16 deletions commands/device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ def __init__(self):
'reboot': Command(description='⚡ safely reboot your device'),
'shutdown': Command(description='🔌 safely shutdown your device',
flags=[Flag(['-r', '--reboot'], 'An alternate way to reboot your device', dtype='bool')]),
'settings': Command(description='⚙️ open the Settings app')}
'settings': Command(description='⚙️ open the Settings app',
flags=[Flag(['-c', '--close'], 'Closes the settings application', dtype='bool')])}

@staticmethod
def _settings():
check_output('am start -a android.settings.SETTINGS')
success('⚙️ Opened settings!')

@staticmethod
def __reboot():
check_output('am start -a android.intent.action.REBOOT')
success('👋 See you in a bit!')
def _settings(self):
flags = self.get_flags('settings')
if flags.close:
check_output('kill $(pgrep com.android.settings)', shell=True)
success('⚙️ Closed settings!')
else:
check_output('am start -a android.settings.SETTINGS')
success('⚙️ Opened settings!')

def _shutdown(self):
flags, e = self.parse_flags(self.commands['shutdown'].parser)
if e is not None:
error(e)
self._help('shutdown')
return

flags = self.get_flags('shutdown')
if flags.reboot:
self.__reboot()
return
check_output('am start -n android/com.android.internal.app.ShutdownActivity')
success('🌙 Goodnight!')

@staticmethod
def __reboot():
check_output('am start -a android.intent.action.REBOOT')
success('👋 See you in a bit!')

@staticmethod
def _battery():
r = check_output('dumpsys batterymanager')
Expand Down
36 changes: 16 additions & 20 deletions commands/fork/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,8 @@ def __init__(self):
def _list(self):
if not self._init():
return
flags, e = self.parse_flags(self.commands['list'].parser)
flags = self.get_flags('list')
specified_fork = flags.fork
if e is not None:
error(e)
self._help('list')
return

installed_forks = self.fork_params.get('installed_forks')
if specified_fork is None:
Expand Down Expand Up @@ -164,16 +160,11 @@ def _list(self):
def _switch(self):
if not self._init():
return
flags, e = self.parse_flags(self.commands['switch'].parser)
if e is not None:
error(e)
flags = self.get_flags('switch')
if flags.username is flags.branch is None: # since both are non-required we need custom logic to check user supplied sufficient args/flags
error('You must supply either username or branch or both')
self._help('switch')
return
else: # since both are non-required we need custom logic to check user supplied sufficient args/flags
if flags.username is flags.branch is None:
error('You must supply either username or branch or both')
self._help('switch')
return

username = flags.username
branch = flags.branch
Expand Down Expand Up @@ -243,15 +234,20 @@ def _switch(self):
remote_branch = remote_info.default_branch
local_branch = '{}_{}'.format(remote_info.username, remote_branch)
else:
local_branch = '{}_{}'.format(username, default_remote_branch)
remote_branch = default_remote_branch # for command to checkout correct branch from remote, branch is previously None since user didn't specify
local_branch = '{}_{}'.format(username, default_remote_branch)
else:
if branch not in remote_branches:
close_branches = most_similar(branch, remote_branches) # remote_branches is gauranteed to have at least 1 branch
if close_branches[0][1] > 0.5:
branch = close_branches[0][0]
info('Unknown branch, checking out most similar: {}'.format(COLORS.SUCCESS + branch + COLORS.WARNING))
else:
error('The branch you specified does not exist!')
self.__show_similar_branches(branch, remote_branches) # if possible
return
remote_branch = branch # branch is now gauranteed to be in remote_branches
local_branch = f'{username}_{branch}'
remote_branch = branch
if remote_branch not in remote_branches:
error('The branch you specified does not exist!')
self.__show_similar_branches(remote_branch, remote_branches) # if possible
return

# checkout remote branch and prepend username so we can have multiple forks with same branch names locally
if remote_branch not in installed_forks[username]['installed_branches']:
Expand Down Expand Up @@ -301,7 +297,7 @@ def __show_similar_branches(branch, branches):
info('Did you mean:')
close_branches = most_similar(branch, branches)[:5]
for idx in range(len(close_branches)):
cb = close_branches[idx]
cb = close_branches[idx][0]
if idx == 0:
cb = COLORS.OKGREEN + cb
else:
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ COMMUNITY_BASHRC_PATH=/data/community/.bashrc
OH_MY_COMMA_PATH=/data/community/.oh-my-comma
GIT_BRANCH_NAME=master
GIT_REMOTE_URL=https://github.com/emu-sh/.oh-my-comma.git
OMC_VERSION=0.1.12
OMC_VERSION=0.1.13

install_echo() { # only prints if not updating
if [ "$update" != true ]; then
Expand Down
8 changes: 4 additions & 4 deletions py_utils/emu_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ def input_with_options(options, default=None):
def most_similar(find, options):
sims = [[str_sim(i.lower().strip(), find.lower().strip()), i] for i in options]
sims = sorted(sims, reverse=True)
return [o[1] for o in sims]
return [[o[1], o[0]] for o in sims]


def check_output(cmd, cwd=None):
def check_output(cmd, cwd=None, shell=False):
class Output:
def __init__(self, output='', s=True):
self.output = output
self.success = s

if isinstance(cmd, str):
if isinstance(cmd, str) and not shell:
cmd = cmd.split()
try:
return Output(subprocess.check_output(cmd, cwd=cwd, stderr=subprocess.STDOUT, encoding='utf8'))
return Output(subprocess.check_output(cmd, cwd=cwd, stderr=subprocess.STDOUT, encoding='utf8', shell=shell))
except subprocess.CalledProcessError as e:
if e.output is None:
return Output(e, s=False) # command failed to execute
Expand Down

0 comments on commit 1ad3842

Please sign in to comment.