Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revise part 06 of the programmers' "workflows" tutorial.
Provide solutions to the exercises thereof.
- Loading branch information
1 parent
a2a4d3c
commit e37e150
Showing
9 changed files
with
462 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#! /usr/bin/env python | ||
|
||
""" | ||
Exercise 6.A: In the ``colorize.py`` script from Exercise 4.A, | ||
modify the ColorizeApp application to move the output picture file | ||
into directory ``/home/ubuntu/pictures``. You might need to store the | ||
output file name to have it available when the application has | ||
terminated running. | ||
""" | ||
|
||
import os | ||
from os.path import abspath, basename, exists, join | ||
import sys | ||
|
||
from gc3libs import Application, log | ||
from gc3libs.cmdline import SessionBasedScript | ||
|
||
|
||
if __name__ == '__main__': | ||
from ex6a import ColorizeScript | ||
ColorizeScript().run() | ||
|
||
|
||
class ColorizeScript(SessionBasedScript): | ||
""" | ||
Colorize multiple images and collect results | ||
into directory ``./pictures`` | ||
""" | ||
def __init__(self): | ||
super(ColorizeScript, self).__init__(version='1.0') | ||
def setup_args(self): | ||
self.add_param('colors', nargs=3, help="Three colors") | ||
self.add_param('images', nargs='+', help="Images to colorize") | ||
def new_tasks(self, extra): | ||
col1, col2, col3 = self.params.colors | ||
apps_to_run = [] | ||
for input_file in self.params.images: | ||
input_file = abspath(input_file) | ||
apps_to_run.append(ColorizeApp(input_file, col1, col2, col3)) | ||
return apps_to_run | ||
|
||
|
||
from shutil import move | ||
|
||
from gc3libs.quantity import GB | ||
|
||
class ColorizeApp(Application): | ||
"""Add colors to a grayscale image.""" | ||
def __init__(self, img, col1, col2, col3): | ||
inp = basename(img) | ||
# need to save this for later reference in ``terminated()`` | ||
self.output_file_name = "color-" + inp | ||
Application.__init__( | ||
self, | ||
arguments=[ | ||
"convert", inp, | ||
"(", "xc:"+col1, "xc:"+col2, "xc:"+col3, "+append", ")", "-clut", | ||
self.output_file_name], | ||
inputs=[img], | ||
outputs=[self.output_file_name], | ||
output_dir="colorized-" + inp + ".d", | ||
stdout="stdout.txt", | ||
stderr="stderr.txt", | ||
# required for running on the cloud, see GC3Pie issue #559 | ||
requested_memory=1*GB) | ||
def terminated(self): | ||
# full path to output file on local filesystem | ||
output_file = join(self.output_dir, self.output_file_name) | ||
# if the output file is not there, log an error and exit | ||
if not exists(output_file): | ||
log.error("Expected output file `%s` from %s does not exists!", | ||
output_file, self) | ||
return | ||
# ensure destination directory exists | ||
if not exists('pictures'): | ||
os.mkdir('pictures') | ||
# the trailing slash ensures `shutil.move` raises an error if | ||
# the destination exists but is not a directory | ||
move(output_file, 'pictures/') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#! /usr/bin/env python | ||
|
||
""" | ||
Exercise 6.B: Modify the grayscaling script ex2c (or the code it | ||
depends upon) so that, when a ``GrayscaleApp`` task has terminated | ||
execution, it prints: | ||
* whether the program has been killed by a signal, and the signal number; | ||
* whether the program has terminated by exiting, and the exit code. | ||
""" | ||
|
||
import os | ||
from os.path import abspath, basename | ||
import sys | ||
|
||
from gc3libs import Application, log | ||
from gc3libs.cmdline import SessionBasedScript | ||
from gc3libs.quantity import GB | ||
|
||
|
||
if __name__ == '__main__': | ||
from ex6b import GrayscaleScript | ||
GrayscaleScript().run() | ||
|
||
|
||
class GrayscaleScript(SessionBasedScript): | ||
""" | ||
Convert images to grayscale. | ||
""" | ||
def __init__(self): | ||
super(GrayscaleScript, self).__init__(version='1.0') | ||
def new_tasks(self, extra): | ||
# since `self.params.args` is already a list of file names, | ||
# just iterate over it to build the list of apps to run... | ||
apps_to_run = [] | ||
for input_file in self.params.args: | ||
input_file = abspath(input_file) | ||
apps_to_run.append(VerboseGrayscaleApp(input_file)) | ||
return apps_to_run | ||
|
||
|
||
# alternately, one could just copy code from `grayscale_app.py` here, | ||
# and append the `terminated()` method to the definition | ||
|
||
from grayscale_app import GrayscaleApp | ||
|
||
class VerboseGrayscaleApp(GrayscaleApp): | ||
"""Convert a single image file to grayscale and log termination status.""" | ||
def terminated(self): | ||
if self.execution.signal != 0: | ||
log.info("Task %s killed by signal %d", self, self.execution.signal) | ||
else: | ||
# self.execution.signal == 0, hence normal termination | ||
if self.execution.exitcode == 0: | ||
log.info("Task %s exited successfully!", self) | ||
else: | ||
log.info("Task %s exited with error code %d", self, self.execution.exitcode) |
Oops, something went wrong.