bug 1001720 - mozharness examples. r=rail
authorAki Sasaki <asasaki@mozilla.com>
Sun, 27 Apr 2014 21:33:16 -0700
changeset 2696 4ee9c10f73db
parent 2694 42357cb899c3
child 2697 cc57d9be8176
push id1995
push userasasaki@mozilla.com
push date2014-04-28 04:33 +0000
reviewersrail
bugs1001720
bug 1001720 - mozharness examples. r=rail
configs/test/example_config1.json
configs/test/example_config2.py
examples/action_config_script.py
examples/silent_script.py
examples/verbose_script.py
new file mode 100644
--- /dev/null
+++ b/configs/test/example_config1.json
@@ -0,0 +1,5 @@
+{
+    "beverage": "fizzy drink",
+    "long_sleep_time": 1800,
+    "random_config_key1": "spectacular"
+}
new file mode 100644
--- /dev/null
+++ b/configs/test/example_config2.py
@@ -0,0 +1,5 @@
+config = {
+    "beverage": "cider",
+    "long_sleep_time": 300,
+    "random_config_key2": "wunderbar",
+}
new file mode 100755
--- /dev/null
+++ b/examples/action_config_script.py
@@ -0,0 +1,130 @@
+#!/usr/bin/env python -u
+"""action_config_script.py
+
+Demonstrate actions and config.
+"""
+
+import os
+import sys
+import time
+
+sys.path.insert(1, os.path.dirname(sys.path[0]))
+
+from mozharness.base.script import BaseScript
+
+
+# ActionsConfigExample {{{1
+class ActionsConfigExample(BaseScript):
+    config_options = [[
+        ['--beverage', ],
+        {"action": "store",
+         "dest": "beverage",
+         "type": "string",
+         "help": "Specify your beverage of choice",
+         }
+    ], [
+        ['--ship-style', ],
+        {"action": "store",
+         "dest": "ship_style",
+         "type": "choice",
+         "choices": ["1", "2", "3"],
+         "help": "Specify the type of ship",
+         }
+    ], [
+        ['--long-sleep-time', ],
+        {"action": "store",
+         "dest": "long_sleep_time",
+         "type": "int",
+         "help": "Specify how long to sleep",
+         }
+    ]]
+
+    def __init__(self, require_config_file=False):
+        super(ActionsConfigExample, self).__init__(
+            config_options=self.config_options,
+            all_actions=[
+                'clobber',
+                'nap',
+                'ship-it',
+            ],
+            default_actions=[
+                'clobber',
+                'nap',
+                'ship-it',
+            ],
+            require_config_file=require_config_file,
+            config={
+                'beverage': "kool-aid",
+                'long_sleep_time': 3600,
+                'ship_style': "1",
+            }
+        )
+
+    def _sleep(self, sleep_length, interval=5):
+        self.info("Sleeping %d seconds..." % sleep_length)
+        counter = 0
+        while counter + interval <= sleep_length:
+            sys.stdout.write(".")
+            try:
+                time.sleep(interval)
+            except:
+                print
+                self.error("Impatient, are we?")
+                sys.exit(1)
+            counter += interval
+        print
+        self.info("Ok, done.")
+
+    def _ship1(self):
+        self.info("""
+     _~
+  _~ )_)_~
+  )_))_))_)
+  _!__!__!_
+  \______t/
+~~~~~~~~~~~~~
+""")
+
+    def _ship2(self):
+        self.info("""
+    _4 _4
+   _)_))_)
+  _)_)_)_)
+ _)_))_))_)_
+ \_=__=__=_/
+~~~~~~~~~~~~~
+""")
+
+    def _ship3(self):
+        self.info("""
+ ,;;:;,
+   ;;;;;
+  ,:;;:;    ,'=.
+  ;:;:;' .=" ,'_\\
+  ':;:;,/  ,__:=@
+   ';;:;  =./)_
+     `"=\\_  )_"`
+          ``'"`
+""")
+
+    def nap(self):
+        for var_name in self.config.keys():
+            if var_name.startswith("random_config_key"):
+                self.info("This is going to be %s!" % self.config[var_name])
+        sleep_time = self.config['long_sleep_time']
+        if sleep_time > 60:
+            self.info("Ok, grab a %s. This is going to take a while." % self.config['beverage'])
+        else:
+            self.info("This will be quick, but grab a %s anyway." % self.config['beverage'])
+        self._sleep(self.config['long_sleep_time'])
+
+    def ship_it(self):
+        name = "_ship%s" % self.config['ship_style']
+        if hasattr(self, name):
+            getattr(self, name)()
+
+
+# __main__ {{{1
+if __name__ == '__main__':
+    actions_config_example = ActionsConfigExample()
+    actions_config_example.run_and_exit()
new file mode 100755
--- /dev/null
+++ b/examples/silent_script.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+""" This script is an example of why I care so much about Mozharness' 2nd core
+concept, logging.  http://escapewindow.dreamwidth.org/230853.html
+"""
+
+import os
+import shutil
+
+#print "downloading foo.tar.bz2..."
+os.system("curl -s -o foo.tar.bz2 http://people.mozilla.org/~asasaki/foo.tar.bz2")
+#os.system("curl -v -o foo.tar.bz2 http://people.mozilla.org/~asasaki/foo.tar.bz2")
+
+#os.rename("foo.tar.bz2", "foo3.tar.bz2")
+os.system("tar xjf foo.tar.bz2")
+
+#os.chdir("x")
+os.remove("x/ship2")
+os.remove("foo.tar.bz2")
+os.system("tar cjf foo.tar.bz2 x")
+shutil.rmtree("x")
+#os.system("scp -q foo.tar.bz2 people.mozilla.org:public_html/foo2.tar.bz2")
+os.remove("foo.tar.bz2")
new file mode 100755
--- /dev/null
+++ b/examples/verbose_script.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+"""verbose_script.py
+
+Contrast to silent_script.py.
+"""
+
+import os
+import sys
+
+sys.path.insert(1, os.path.dirname(sys.path[0]))
+
+#from mozharness.base.errors import TarErrorList, SSHErrorList
+from mozharness.base.script import BaseScript
+
+
+# VerboseExample {{{1
+class VerboseExample(BaseScript):
+    def __init__(self, require_config_file=False):
+        super(VerboseExample, self).__init__(
+            all_actions=['verbosity', ],
+            require_config_file=require_config_file,
+            config={"tarball_name": "bar.tar.bz2"}
+        )
+
+    def verbosity(self):
+        tarball_name = self.config["tarball_name"]
+        self.download_file(
+            "http://people.mozilla.org/~asasaki/foo.tar.bz2",
+            file_name=tarball_name
+        )
+        # the error_list adds more error checking.
+        # the halt_on_failure will kill the script at this point if
+        # unsuccessful.  Be aware if you need to do any cleanup before you
+        # actually fatal(), though.  If so, you may want to either use an
+        # |if self.run_command(...):| construct, or define a self._post_fatal()
+        # for a generic end-of-fatal-run method.
+        self.run_command(
+            ["tar", "xjvf", tarball_name],
+#            error_list=TarErrorList,
+#            halt_on_failure=True,
+#            fatal_exit_code=3,
+        )
+        self.rmtree("x/ship2")
+        self.rmtree(tarball_name)
+        self.run_command(
+            ["tar", "cjvf", tarball_name, "x"],
+#            error_list=TarErrorList,
+#            halt_on_failure=True,
+#            fatal_exit_code=3,
+        )
+        self.rmtree("x")
+        if self.run_command(
+            ["scp", tarball_name, "people.mozilla.org:public_html/foo2.tar.bz2"],
+#            error_list=SSHErrorList,
+        ):
+            self.error("There's been a problem with the scp.  We're going to proceed anyway.")
+        self.rmtree(tarball_name)
+
+
+# __main__ {{{1
+if __name__ == '__main__':
+    verbose_example = VerboseExample()
+    verbose_example.run_and_exit()