annotate python/example/INSTALL.py @ 798:720e51cb0edb

i dont use mozilla pastebin anymore
author Jeff Hammel <k0scist@gmail.com>
date Fri, 28 Oct 2016 10:45:46 -0700
parents 05fef8e5b8a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
753
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 install and setup stuff
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 ### imports
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import argparse
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import inspect
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import os
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import subprocess
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import sys
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 ### globals
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 string = (str, unicode)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 BASEDIR = os.environ.get('VIRTUAL_ENV', os.getcwd())
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 def comment(text, comment_character='#', join=False):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 """
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 comment some text
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 text -- text to comment
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 join -- whether to ``str.join`` the lines of text
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 """
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 text = text.strip()
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 if text:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 retval = ['{} {}'.format(comment_character, line.rstrip())
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 for line in text.splitlines()]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 else:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 retval = []
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 if join:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 retval = '\n'.join(retval)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 return retval
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 class Command(object):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 """an individual command"""
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 def __init__(self, command):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 self.command = command
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 def shell(self):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 return isinstance(self.command, string)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 def interpolate(self, **variables):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 if self.shell():
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 return self.command.format(**variables)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 return [i.format(**variables) for i in self.command]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 def commandline(self, **variables):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 command = self.interpolate(**variables)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 if self.shell():
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 return command
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 else:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 return subprocess.list2cmdline(command)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 __str__ = commandline
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 def __call__(self, variables=None, **kwargs):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 variables = variables or {}
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 commandline = self.commandline(**variables)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 kwargs['shell'] = self.shell()
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 print (commandline)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 code = subprocess.call(self.interpolate(**variables), **kwargs)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 if code:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 raise subprocess.CalledProcessError(code, commandline)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 ### setup and installation steps
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 class Step(object):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66 commands = []
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 env = {}
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 comment_character = '#'
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 @classmethod
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 def name(cls):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 return cls.__name__
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 @classmethod
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 def description(cls):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 return (getattr(cls, '__doc__', '') or '').strip()
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 @classmethod
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 def name_and_description(cls):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 description = cls.description()
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 return '{}{}'.format(cls.name(), ': {}'.format(description if description else ''))
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 def __init__(self, **variables):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81 self.commands = self.commands[:]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 self.variables = variables
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 for key in self.env:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 os.environ[key] = self.env[key].format(**variables)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85 def command_objs(self):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 return [Command(command) for command in self.commands]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87 def interpolate(self):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88 return [command.interpolate(**self.variables)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
89 for command in self.command_objs()]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
90 def write(self, output=sys.stdout):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
91 for command in self.command_objs():
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
92 output.write(str(command) + '\n')
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
93 def __call__(self):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
94 for command in self.command_objs():
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
95 command(variables=self.variables)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
96 def script(self):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
97 retval = comment(self.name(), self.comment_character)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
98 description = self.description()
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
99 if description:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
100 retval.extend(comment(description, self.comment_character))
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
101 for command in self.command_objs():
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
102 retval.append(command.commandline(**self.variables))
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
103 return '\n'.join(retval)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
104
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
105 class UbuntuPackages(Step):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
106 commands = [['sudo', 'apt-get', '-y', 'update'],
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
107 ['sudo', 'apt-get', '-y', 'upgrade'],
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
108 ]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
109 packages = []
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
110 def __init__(self, **kwargs):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
111 Step.__init__(self, **kwargs)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
112 self.commands.append(['sudo', 'apt-get', '-y', 'install'] + self.packages)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
113
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
114 class InstallXinePrerequisites(UbuntuPackages):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
115 """install the prerequisites for the xine-based client"""
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
116 packages = ['libcurl4-gnutls-dev',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
117 'g++',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
118 'libx11-dev',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
119 'libxext-dev',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
120 'libxine1',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
121 'libxine-dev',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
122 'gxine',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
123 ]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
124
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
125 class InstallFFMPEG(UbuntuPackages):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
126 """install prerequisites for the FFMPEG-based client"""
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
127 packages = ['subversion',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
128 'make',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
129 'gcc',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
130 'g++',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
131 'libcurl4-gnutls-dev']
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
132 yasm = '1.2.0'
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
133 ffmpeg = '1.2'
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
134 def __init__(self, client=os.path.join(BASEDIR, 'linux_client')):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
135 UbuntuPackages.__init__(self, client=client, yasm=self.yasm, ffmpeg=self.ffmpeg)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
136
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
137 self.commands.extend([['mkdir', '-p', '{client}'],
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
138 ['wget', 'http://www.tortall.net/projects/yasm/releases/yasm-{yasm}.tar.gz', '-O', '{client}/yasm-{yasm}.tar.gz'],
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
139 ['wget', 'http://ffmpeg.org/releases/ffmpeg-{ffmpeg}.tar.gz', '-O', '{client}/ffmpeg-{ffmpeg}.tar.gz'],
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
140 ['tar', 'zfvx', '{client}/yasm-{yasm}.tar.gz', '-C', '{client}'],
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
141 ['tar', 'zfvx', '{client}/ffmpeg-{ffmpeg}.tar.gz', '-C', '{client}'],
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
142
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
143 # YASM
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
144 'cd {client}/yasm-{yasm} && ./configure',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
145 'cd {client}/yasm-{yasm} && make',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
146 'cd {client}/yasm-{yasm} && sudo make install',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
147
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
148 # FFMPEG
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
149 'cd {client}/ffmpeg-{ffmpeg} && ./configure --enable-shared',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
150 'cd {client}/ffmpeg-{ffmpeg} && make',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
151 'cd {client}/ffmpeg-{ffmpeg} && sudo make install',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
152 ])
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
153
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
154
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
155 ### functionality for running multiple steps
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
156
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
157 class Steps(object):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
158 """run a series of steps"""
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
159
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
160 comment_character = '#'
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
161
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
162 # instance defaults
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
163 defaults = {}
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
164
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
165 # variable descriptions
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
166 descriptions = dict()
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
167
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
168 def __init__(self, *steps, **variables):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
169 self.steps = steps
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
170 self.variables = variables
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
171 self.step_dict = {step.name():step for step in steps}
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
172
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
173 def parser(self, description=None):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
174 """return argument parser"""
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
175 parser = argparse.ArgumentParser(description=description)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
176 parser.add_argument('--list', dest='list_steps',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
177 action='store_true', default=False,
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
178 help="list available steps and exit")
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
179 parser.add_argument('--commands', dest='list_commands',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
180 action='store_true', default=False,
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
181 help="list commands to be run and exit")
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
182 return parser
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
183
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
184 def comment(self, text, join=False):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
185 """
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
186 comment some text
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
187
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
188 text -- text to comment
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
189 join -- whether to ``str.join`` the lines of text
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
190 """
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
191 return comment(text, self.comment_character, join)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
192
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
193 def script(self, steps=None, description=None, variables=None):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
194 """returns a script"""
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
195 variables = variables or {}
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
196 retval = ['#!/bin/bash', '']
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
197 if description:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
198 retval.extend(self.comment(description))
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
199 retval.append('') # whitespace delimiter
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
200 steps = self.instantiate(*steps, **variables)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
201 for step in steps:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
202 retval.append(step.script())
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
203 retval.append('') # whitespace
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
204
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
205 return '\n'.join(retval)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
206
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
207 @staticmethod
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
208 def args(step):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
209 args, varargs, varkw, defaults = inspect.getargspec(step.__init__)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
210 return args[1:]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
211
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
212 def all_args(self, steps=None):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
213 retval = []
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
214 steps = steps or self.steps
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
215 for step in steps:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
216 args = self.args(step)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
217 retval.extend([arg for arg in args if arg not in retval])
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
218 return retval
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
219
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
220 def get_variables(self, options, steps=None):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
221 """get variables from a namespace"""
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
222 return {i:getattr(options, i, None) or self.defaults.get(i)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
223 for i in self.all_args(steps)}
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
224
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
225 def instantiate(self, *steps, **variables):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
226 """instantiate a set of steps with variables"""
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
227 return [step(**{arg:variables.get(arg)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
228 for arg in self.args(step)})
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
229 for step in steps]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
230
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
231 def parse(self, args=sys.argv[1:], description=None):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
232
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
233 # create a parser
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
234 parser = self.parser(description)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
235
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
236 # add step names as arguments
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
237 parser.add_argument('steps', nargs='*', metavar='step',
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
238 help="steps to run; if omitted, all steps will be run")
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
239
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
240 # add step arguments
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
241 for arg in self.all_args():
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
242 variable_description = self.descriptions.get(arg)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
243 default = self.defaults.get(arg)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
244 if variable_description and default:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
245 variable_description += ' [DEFAULT: %(default)s]'
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
246 parser.add_argument('--{}'.format(arg), dest=arg,
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
247 default=default, help=variable_description)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
248
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
249 # parse arguments
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
250 options = parser.parse_args(args)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
251
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
252 # get steps to run
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
253 if options.steps:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
254 missing = [i for i in options.steps if i not in self.step_dict]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
255 if missing:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
256 parser.error("No such step: {}".format(', '.join(missing)))
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
257 steps = [self.step_dict[i] for i in options.steps]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
258 else:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
259 steps = self.steps[:]
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
260
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
261 # get variables for execution
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
262 variables = self.get_variables(options)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
263
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
264 if options.list_steps:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
265 # list steps and exit
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
266 for step in steps:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
267 print (step.name_and_description())
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
268 variables = self.args(step)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
269 if variables:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
270 print ('Variables: {}'.format(', '.join(variables)))
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
271 print ('') # whitespace
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
272 sys.exit(0)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
273
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
274 # instantiate steps
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
275 step_objs = self.instantiate(*steps, **variables)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
276
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
277 if options.list_commands:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
278 # print commands and exit
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
279 print (self.script(steps, description, variables))
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
280 sys.exit(0)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
281
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
282 # run steps
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
283 for step in step_objs:
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
284 step()
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
285
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
286
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
287 ### main
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
288
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
289 def main(args=sys.argv[1:]):
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
290 """CLI"""
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
291 Steps(*[InstallXinePrerequisites,
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
292 InstallFFMPEG]).parse(args=args, description=__doc__)
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
293
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
294 if __name__ == '__main__':
05fef8e5b8a9 example step of the conductor example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
295 main()