Skip to content

Commit

Permalink
Add -b flag to fork switch (#37)
Browse files Browse the repository at this point in the history
* Add -b flag to fork switch (#36)

* Hmm, I wonder if this will work straight away with no further changes... unless?

* ha nope 😢, i think having the user specify -b might be nicer since most of the time the user will be switching through a single fork's branches (at least that's my use-case)

* add flags to usage!

* test two flags to see if syntax is right

* what if we switch them now?

* better formatting! might need to make it into a list?

* debug

* debug

* fixxxx

* now format with brackets surrounding

* now format with brackets surrounding

* see if a str() will do the same

* how bou dis

* this removes '' for each flag

* update comment

* fix

* test non-required positional. hoping my previous changes to flags will make this work fine

* test different logic

* this is the cleanest 🧼

* add logic to use current fork/username if only branch is specified. should be done!

* make it info

* bump version and update docs

* add another example

* update description of username in the code

* highlight fork username for readability

* okay done! need to test on first install still

* update example after initing

Co-authored-by: Shane Smiskol <[email protected]>
  • Loading branch information
sshane authored Jul 23, 2020
2 parents 835e246 + 4d5fce1 commit 67836a4
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 8 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
Release 0.1.5 (2020-07-22)
=====
* Make the username argument under `emu fork switch` optional. If not specified, it will use the current fork switched to
* Add `--branch` (`-b`) flag for specifying the branch
* This means you must supply `-b` or `--branch` when switching branches, even when supplying the username:

Old syntax: emu fork switch another_fork branch
New syntax: emu fork switch another_fork -b branch

Old syntax: emu fork switch same_fork new_branch
New syntax: emu fork switch -b new_branch

Release 0.1.4 (2020-07-12)
=====
* Add `emu device settings` command to open the settings app

Release 0.1.3 (2020-07-06)
=====
* Make flags/arguments more robust. Optional non-positional arguments are now supported, as long as they are the last arguments.
Expand Down
5 changes: 3 additions & 2 deletions commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
🍴 Manage installed forks, or install a new one
- `emu fork switch`: 🍴 Switch between any openpilot fork
- Arguments 💢:
- username: 👤 The username of the fork's owner to install
- branch (optional): 🌿 Branch to switch to, will use default branch if not provided
- username (optional): 👤 The username of the fork's owner to switch to, will use current fork if not provided
- -b, --branch (optional): 🌿 Branch to switch to, will use fork's default branch if not provided
- *New Behavior:* If a branch is provided with `-b` and username is not supplied, it will use the current fork switched to
- Example 📚:
- `emu fork switch stock devel`
- `emu fork list`: 📜 See a list of installed forks and branches
Expand Down
2 changes: 2 additions & 0 deletions commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import importlib
from py_utils.emu_utils import error
Expand Down
10 changes: 9 additions & 1 deletion commands/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from py_utils.colors import COLORS
from py_utils.emu_utils import ArgumentParser, BaseFunctions, success, error

Expand Down Expand Up @@ -44,10 +46,16 @@ def _help(self, cmd, show_description=True, leading=''):
if flags is not None and len(flags) > 0:
usage_req = [f.aliases[0] for f in flags if f.required and len(f.aliases) == 1] # if required
usage_non_req = [f.aliases[0] for f in flags if not f.required and len(f.aliases) == 1] # if non-required non-positional
usage_flags = [f.aliases for f in flags if not f.required and len(f.aliases) > 1 or f.aliases[0].startswith('-')] # if flag
if len(usage_req) > 0 or len(usage_non_req) > 0: # print usage with proper braces
usage_req = ['[{}]'.format(u) for u in usage_req]
usage_non_req = ['({})'.format(u) for u in usage_non_req]
usage = ['emu', self.name, cmd] + usage_req + usage_non_req
if len(usage_flags):
# formats flags to: "[-b BRANCH, -o OUTPUT]"
usage_flags = ['{} {}'.format(min(u, key=len), max(u, key=len).upper()[2:]) for u in usage_flags]
usage_flags = ['[{}]'.format(', '.join(usage_flags))]

usage = ['emu', self.name, cmd] + usage_req + usage_non_req + usage_flags
print(leading + COLORS.WARNING + '>> Usage:{} {}'.format(COLORS.OKGREEN, ' '.join(usage)) + COLORS.ENDC)

print(leading + COLORS.WARNING + '>> Arguments 💢:' + COLORS.ENDC)
Expand Down
2 changes: 2 additions & 0 deletions commands/debug/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from commands.base import CommandBase, Command, Flag
from py_utils.emu_utils import run, kill, warning, error
from py_utils.emu_utils import OPENPILOT_PATH
Expand Down
2 changes: 2 additions & 0 deletions commands/device/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from commands.base import CommandBase, Command, Flag
from py_utils.emu_utils import run, warning, error, check_output, COLORS, success

Expand Down
24 changes: 20 additions & 4 deletions commands/fork/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import shutil
import os
import json
Expand Down Expand Up @@ -71,8 +73,8 @@ def __init__(self):
self.stock_aliases = ['stock', COMMA_ORIGIN_NAME, 'origin']

self.commands = {'switch': Command(description='🍴 Switch between any openpilot fork',
flags=[Flag('username', '👤 The username of the fork\'s owner to install', required=True, dtype='str'),
Flag('branch', '🌿 Branch to switch to, will use default branch if not provided', dtype='str')]),
flags=[Flag('username', '👤 The username of the fork\'s owner to switch to, will use current fork if not provided', required=False, dtype='str'),
Flag(['-b', '--branch'], '🌿 Branch to switch to, will use default branch if not provided', required=False, dtype='str')]),
'list': Command(description='📜 See a list of installed forks and branches',
flags=[Flag('fork', '🌿 See branches of specified fork', dtype='str')])}

Expand Down Expand Up @@ -137,6 +139,20 @@ def _switch(self):
error(e)
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

if flags.username is None: # branch is specified, so use current checked out fork/username
_current_fork = self.fork_params.get('current_fork')
if _current_fork is not None: # ...if available
info('No username specified, using current fork: {}'.format(COLORS.SUCCESS + _current_fork + COLORS.ENDC))
flags.username = _current_fork
else:
error('Current fork is unknown, please switch to a fork first before switching between branches!')
return

username = flags.username.lower()
if username in self.stock_aliases:
Expand Down Expand Up @@ -185,7 +201,7 @@ def _switch(self):
error('Error: Cannot find default branch from fork!')
return

if flags.branch is None: # user hasn't specified a branch, use remote's branch
if flags.branch is None: # user hasn't specified a branch, use remote's default branch
if username == COMMA_ORIGIN_NAME: # todo: use a dict for default branches if we end up needing default branches for multiple forks
branch = COMMA_DEFAULT_BRANCH # use release2 and default branch for stock
fork_branch = 'commaai_{}'.format(branch)
Expand Down Expand Up @@ -331,7 +347,7 @@ def _init(self):
return

success('Fork management set up successfully! You\'re on {}/{}'.format(COMMA_ORIGIN_NAME, COMMA_DEFAULT_BRANCH))
success('To get started, try running: {}emu fork switch [fork_username] (branch){}'.format(COLORS.RED, COLORS.ENDC))
success('To get started, try running: {}emu fork switch (username) [-b BRANCH]{}'.format(COLORS.RED, COLORS.ENDC))
self.fork_params.put('setup_complete', True)
self.fork_params.put('current_fork', COMMA_ORIGIN_NAME)
self.fork_params.put('current_branch', COMMA_DEFAULT_BRANCH)
Expand Down
2 changes: 2 additions & 0 deletions commands/panda/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import importlib
from commands.base import CommandBase, Command
from py_utils.emu_utils import run, error
Expand Down
2 changes: 2 additions & 0 deletions commands/uninstall/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from commands.base import CommandBase, Command, Flag
from py_utils.emu_utils import run, warning, error, check_output, COLORS, success, input_with_options, UNINSTALL_PATH

Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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.4
OMC_VERSION=0.1.5

update=false
if [ $# -ge 1 ] && [ $1 = "update" ]; then
Expand Down

0 comments on commit 67836a4

Please sign in to comment.