Skip to content

Commit

Permalink
Cleanup code using "black".
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaCensi committed Sep 20, 2020
1 parent 5133a21 commit 7f2bf53
Show file tree
Hide file tree
Showing 64 changed files with 2,365 additions and 2,553 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ upload:
git push --tags
git push


black:
black -l 110 .

10 changes: 5 additions & 5 deletions challenges/challenges_cmd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
from dt_shell import OtherVersions, UserError


__all__ = ['wrap_server_operations', 'check_duckietown_challenges_version']
__all__ = ["wrap_server_operations", "check_duckietown_challenges_version"]


def check_duckietown_challenges_version():
PKG = 'duckietown-challenges-daffy'
PKG = "duckietown-challenges-daffy"

try:
from duckietown_challenges import __version__
except ImportError:
msg = f'Package {PKG} not installed.'
msg = f"Package {PKG} not installed."
raise UserError(msg)

version = tuple(map(int, __version__.split(".")))
Expand All @@ -35,11 +36,10 @@ def v(x):
raise UserError(msg)




@contextmanager
def wrap_server_operations():
from duckietown_challenges.rest import NotAuthorized, NotFound, ServerIsDown

try:
yield
except ServerIsDown as e:
Expand Down
4 changes: 1 addition & 3 deletions challenges/config/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ class DTCommand(DTCommandAbs):
@staticmethod
def command(shell: DTShell, args):
parser = argparse.ArgumentParser(prog="dts challenges config")
parser.add_argument(
"--docker-username", dest="username", help="Docker username", required=True
)
parser.add_argument("--docker-username", dest="username", help="Docker username", required=True)
parsed = parser.parse_args(args)

username = parsed.username
Expand Down
30 changes: 12 additions & 18 deletions challenges/define/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from dt_shell.env_checks import check_docker_environment



class DTCommand(DTCommandAbs):
@staticmethod
def command(shell: DTShell, args):
Expand All @@ -17,27 +16,18 @@ def command(shell: DTShell, args):
token = shell.get_dt1_token()

parser = argparse.ArgumentParser()
parser.add_argument(
"--config", default="challenge.yaml", help="YAML configuration file"
)
parser.add_argument("--config", default="challenge.yaml", help="YAML configuration file")

parser.add_argument("--no-cache", default=False, action="store_true")
parser.add_argument(
"--steps", default=None, help="Which steps (comma separated)"
)
parser.add_argument(
"--force-invalidate-subs", default=False, action="store_true"
)
parser.add_argument("--steps", default=None, help="Which steps (comma separated)")
parser.add_argument("--force-invalidate-subs", default=False, action="store_true")
parser.add_argument("-C", dest="cwd", default=None, help="Base directory")
parser.add_argument("--impersonate", type=str, default=None)
parser.add_argument(
"--pull", default=False, action="store_true"
)
parser.add_argument("--pull", default=False, action="store_true")

parsed = parser.parse_args(args)
impersonate = parsed.impersonate


client = check_docker_environment()
if client is None: # To remove when done
client = check_docker_environment()
Expand Down Expand Up @@ -67,13 +57,17 @@ def command(shell: DTShell, args):
dtslogger.info(msg)

base = os.path.dirname(fn)
dtslogger.info(f'data {data}')
dtslogger.info(f"data {data}")
challenge = ChallengeDescription.from_yaml(data)
assert challenge.date_close.tzinfo is not None, (
challenge.date_close.tzinfo, challenge.date_open.tzinfo)
challenge.date_close.tzinfo,
challenge.date_open.tzinfo,
)
assert challenge.date_open.tzinfo is not None, (
challenge.date_close.tzinfo, challenge.date_open.tzinfo)
challenge.date_close.tzinfo,
challenge.date_open.tzinfo,
)

dc_logger.info('read challenge', challenge=challenge)
dc_logger.info("read challenge", challenge=challenge)
with wrap_server_operations():
dts_define(token, impersonate, parsed, challenge, base, client, no_cache)
43 changes: 10 additions & 33 deletions challenges/evaluate/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,14 @@ def command(shell: DTShell, args):
group.add_argument("--challenge", help="Specific challenge to evaluate")

group.add_argument(
"--image",
help="Evaluator image to run",
default="duckietown/dt-challenges-evaluator:daffy",
"--image", help="Evaluator image to run", default="duckietown/dt-challenges-evaluator:daffy",
)
group.add_argument(
"--shell",
action="store_true",
default=False,
help="Runs a shell in the container",
"--shell", action="store_true", default=False, help="Runs a shell in the container",
)
group.add_argument("--output", help="", default="output")
group.add_argument(
"--visualize",
help="Visualize the evaluation",
action="store_true",
default=False,
"--visualize", help="Visualize the evaluation", action="store_true", default=False,
)
parser.add_argument("--impersonate", type=str, default=None)
group.add_argument("-C", dest="change", default=None)
Expand Down Expand Up @@ -100,9 +92,7 @@ def command(shell: DTShell, args):
dir_tmpdir_host = "/tmp"
dir_tmpdir_guest = "/tmp"

volumes = {
"/var/run/docker.sock": {"bind": "/var/run/docker.sock", "mode": "rw"}
}
volumes = {"/var/run/docker.sock": {"bind": "/var/run/docker.sock", "mode": "rw"}}
d = os.path.join(os.getcwd(), parsed.output)
if not os.path.exists(d):
os.makedirs(d)
Expand All @@ -126,19 +116,13 @@ def command(shell: DTShell, args):
# command.extend(['-C', fake_dir])
env = {}

extra_environment = dict(
username=USERNAME, uid=UID, USER=USERNAME, HOME=dir_fake_home_guest
)
extra_environment = dict(username=USERNAME, uid=UID, USER=USERNAME, HOME=dir_fake_home_guest)

env.update(extra_environment)

dtslogger.debug(
"Volumes:\n\n%s" % yaml.safe_dump(volumes, default_flow_style=False)
)
dtslogger.debug("Volumes:\n\n%s" % yaml.safe_dump(volumes, default_flow_style=False))

dtslogger.debug(
"Environment:\n\n%s" % yaml.safe_dump(env, default_flow_style=False)
)
dtslogger.debug("Environment:\n\n%s" % yaml.safe_dump(env, default_flow_style=False))

from duckietown_challenges.rest import get_duckietown_server_url

Expand All @@ -148,19 +132,12 @@ def command(shell: DTShell, args):
h = socket.gethostname()
replacement = h + ".local"

dtslogger.warning(
'There is "localhost" inside, so I will try to change it to %r'
% replacement
)
dtslogger.warning(
'This is because Docker cannot see the host as "localhost".'
)
dtslogger.warning('There is "localhost" inside, so I will try to change it to %r' % replacement)
dtslogger.warning('This is because Docker cannot see the host as "localhost".')

url = url.replace("localhost", replacement)
dtslogger.warning("The new url is: %s" % url)
dtslogger.warning(
"This will be passed to the evaluator in the Docker container."
)
dtslogger.warning("This will be passed to the evaluator in the Docker container.")

env["DTSERVER"] = url

Expand Down
59 changes: 16 additions & 43 deletions challenges/evaluator/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def command(shell: DTShell, args):

group = parser.add_argument_group("Basic")

group.add_argument(
"--submission", type=int, default=None, help="Run a specific submission."
)
group.add_argument("--submission", type=int, default=None, help="Run a specific submission.")
group.add_argument(
"--reset",
dest="reset",
Expand Down Expand Up @@ -114,22 +112,14 @@ def command(shell: DTShell, args):
)

group.add_argument(
"--image",
help="Evaluator image to run",
default=EVALUATOR_IMAGE,
"--image", help="Evaluator image to run", default=EVALUATOR_IMAGE,
)

group.add_argument("--name", default=None, help="Name for this evaluator")
group.add_argument(
"--features", default=None, help="Pretend to be what you are not."
)
group.add_argument("--features", default=None, help="Pretend to be what you are not.")

group.add_argument(
"--ipfs", action="store_true", default=False, help="Run with IPFS available"
)
group.add_argument(
"--one", action="store_true", default=False, help="Only run 1 submission"
)
group.add_argument("--ipfs", action="store_true", default=False, help="Run with IPFS available")
group.add_argument("--one", action="store_true", default=False, help="Only run 1 submission")

# dtslogger.debug('args: %s' % args)
parsed = parser.parse_args(args)
Expand Down Expand Up @@ -205,19 +195,12 @@ def command(shell: DTShell, args):
h = socket.gethostname()
replacement = h + ".local"

dtslogger.warning(
'There is "localhost" inside, so I will try to change it to %r'
% replacement
)
dtslogger.warning(
'This is because Docker cannot see the host as "localhost".'
)
dtslogger.warning('There is "localhost" inside, so I will try to change it to %r' % replacement)
dtslogger.warning('This is because Docker cannot see the host as "localhost".')

url = url.replace("localhost", replacement)
dtslogger.warning("The new url is: %s" % url)
dtslogger.warning(
"This will be passed to the evaluator in the Docker container."
)
dtslogger.warning("This will be passed to the evaluator in the Docker container.")

env["DTSERVER"] = url

Expand Down Expand Up @@ -277,9 +260,7 @@ def command(shell: DTShell, args):
if container.status == "exited":

logs = ""
for c in container.logs(
stdout=True, stderr=True, stream=True, since=last_log_timestamp
):
for c in container.logs(stdout=True, stderr=True, stream=True, since=last_log_timestamp):
logs += c.decode("utf-8")
last_log_timestamp = datetime.datetime.now()

Expand All @@ -297,11 +278,7 @@ def command(shell: DTShell, args):
if last_log_timestamp is not None:
print("since: %s" % last_log_timestamp.isoformat())
for c0 in container.logs(
stdout=True,
stderr=True,
stream=True, # follow=True,
since=last_log_timestamp,
tail=0,
stdout=True, stderr=True, stream=True, since=last_log_timestamp, tail=0, # follow=True,
):
c: bytes = c0
try:
Expand Down Expand Up @@ -357,15 +334,15 @@ def make_sure_image_pulled(client: DockerClient, repository: str, tag: str = Non
outs2 = outs.decode().strip()
# print(outs2.__repr__())
out = json.loads(outs2)
s = out.get('status', '')
s = '%d: %s' % (i, s)
s = out.get("status", "")
s = "%d: %s" % (i, s)
i += 1
s = s.ljust(cols)
sys.stderr.write(s + '\r')
sys.stderr.write(s + "\r")
except:
pass
sys.stderr.write('\n')
dtslogger.info('pull complete')
sys.stderr.write("\n")
dtslogger.info("pull complete")


def ensure_watchtower_active(client: DockerClient):
Expand All @@ -392,11 +369,7 @@ def ensure_watchtower_active(client: DockerClient):
}

container = client.containers.run(
watchtower_tag,
volumes=volumes,
environment=env,
network_mode="host",
detach=True,
watchtower_tag, volumes=volumes, environment=env, network_mode="host", detach=True,
)
dtslogger.info("Detached: %s" % container)

Expand Down
7 changes: 2 additions & 5 deletions challenges/info/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from challenges import check_duckietown_challenges_version, wrap_server_operations
from dt_shell import DTCommandAbs, DTShell


class DTCommand(DTCommandAbs):
@staticmethod
def command(shell: DTShell, args):
Expand All @@ -13,7 +14,6 @@ def command(shell: DTShell, args):
from duckietown_challenges import get_duckietown_server_url
from duckietown_challenges.rest_methods import get_dtserver_user_info


parser = argparse.ArgumentParser()
parser.add_argument("--impersonate", type=str, default=None)

Expand Down Expand Up @@ -45,10 +45,7 @@ def command(shell: DTShell, args):
profile: {profile}
""".format(
uid=bold(uid),
user_login=bold(user_login),
display_name=bold(display_name),
profile=profile,
uid=bold(uid), user_login=bold(user_login), display_name=bold(display_name), profile=profile,
).strip()

server = get_duckietown_server_url()
Expand Down
5 changes: 1 addition & 4 deletions challenges/list/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def command(shell: DTShell, args):
from duckietown_challenges.rest_methods import dtserver_get_user_submissions
from duckietown_challenges.utils import pad_to_screen_length


with wrap_server_operations():
submissions = dtserver_get_user_submissions(token)

Expand Down Expand Up @@ -52,9 +51,7 @@ def d(dt):

url = server + "/humans/submissions/%s" % submission_id

user_label = submission.get("user_label", None) or dark(
"(no user label)"
)
user_label = submission.get("user_label", None) or dark("(no user label)")

M = 30
if len(user_label) > M:
Expand Down
17 changes: 4 additions & 13 deletions challenges/reset/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@ def command(shell: DTShell, args):

token = shell.get_dt1_token()
parser = argparse.ArgumentParser(prog="dts challenges reset")
parser.add_argument("--job", default=None, help="Only reset this particular job", type=int)
parser.add_argument(
"--job", default=None, help="Only reset this particular job", type=int
)
parser.add_argument(
"--submission",
default=None,
type=int,
help="Reset this particular submission",
)
parser.add_argument(
"--step", default=None, help="Only reset this particular step"
"--submission", default=None, type=int, help="Reset this particular submission",
)
parser.add_argument("--step", default=None, help="Only reset this particular step")
parser.add_argument("--impersonate", default=None)
parsed = parser.parse_args(args)

Expand All @@ -44,9 +37,7 @@ def command(shell: DTShell, args):
elif parsed.job is not None:
from duckietown_challenges.rest_methods import dtserver_reset_job

job_id = dtserver_reset_job(
token, job_id=parsed.job, impersonate=parsed.impersonate
)
job_id = dtserver_reset_job(token, job_id=parsed.job, impersonate=parsed.impersonate)
shell.sprint("Successfully reset %s" % job_id)
else:
assert False
Loading

0 comments on commit 7f2bf53

Please sign in to comment.