Skip to content

Commit

Permalink
Add the simple flatten() back in as it's useful. Add some color codin…
Browse files Browse the repository at this point in the history
…g. Don't chdir() into task_dir twice (doh).
  • Loading branch information
aappleby committed Mar 14, 2024
1 parent 98a67ae commit b8d4812
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions hancho.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ def maybe_as_number(text):
return text


def flatten(elements):
"""Converts an arbitrarily-nested list 'elements' into a flat list, or wraps it in [] if it's
not a list."""
if not isinstance(elements, list):
return [elements]
result = []
for element in elements:
result.extend(flatten(element))
return result


# The next three functions require some explanation.
#
# We do not necessarily know in advance how the users will nest strings,
Expand Down Expand Up @@ -487,14 +498,16 @@ async def run_commands(self):

# Print the "[1/N] Foo foo.foo foo.o" status line and debug information
log(
f"[{self.task_index}/{app.tasks_total}] {self.desc}",
f"{color(128,255,196)}[{self.task_index}/{app.tasks_total}]{color()} {self.desc}",
sameline=not self.verbose,
)

if self.verbose or self.debug:
log(f"Reason: {self.reason}")
log(f"{color(128,128,128)}Reason: {self.reason}{color()}")
for command in self.command:
log(f">>>{' (DRY RUN)' if self.dryrun else ''} {command}")
task_dir = Path("root") / relpath(self.task_dir, self.root_dir)
dry_run = "(DRY RUN) " if self.dryrun else ""
log(f"{color(128,128,255)}{task_dir}$ {color()}{dry_run}{command}")
if self.debug:
log(self)

Expand All @@ -517,8 +530,7 @@ async def run_command(self, command):

# Custom commands just get called and then early-out'ed.
if callable(command):
with Chdir(self.task_dir):
result = command(self)
result = command(self)
if result is None:
raise ValueError(f"Command {command} returned None")
return result
Expand All @@ -528,12 +540,11 @@ async def run_command(self, command):
raise ValueError(f"Don't know what to do with {command}")

# Create the subprocess via asyncio and then await the result.
with Chdir(self.task_dir):
proc = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
proc = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
(stdout_data, stderr_data) = await proc.communicate()

self.stdout = stdout_data.decode()
Expand Down Expand Up @@ -681,7 +692,9 @@ def main(self):

# Change directory if needed and kick off the build.
with Chdir(config.chdir):
result = asyncio.run(self.async_main())
# For some reason "result = asyncio.run(self.async_main())" might be breaking actions
# in Github, so I'm gonna try this.
result = asyncio.get_event_loop().run_until_complete(self.async_main())

return result

Expand Down

0 comments on commit b8d4812

Please sign in to comment.