~milo/lava-tool/lava-165

« back to all changes in this revision

Viewing changes to lava/commands.py

  • Committer: Milo Casagrande
  • Date: 2013-07-25 15:24:00 UTC
  • Revision ID: milo@ubuntu.com-20130725152400-cdd1gd2gtc3st25n
Refactored the lava init command.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
    expand_template,
55
55
    set_value
56
56
)
57
 
from lava.job import JOB_FILE_EXTENSIONS
 
57
from lava.job import (
 
58
    JOB_FILE_EXTENSIONS,
 
59
)
58
60
from lava.job.templates import (
59
61
    LAVA_TEST_SHELL_TAR_REPO_KEY,
60
62
)
68
70
)
69
71
from lava.tool.errors import CommandError
70
72
from lava_tool.utils import (
 
73
    edit_file,
71
74
    retrieve_file,
 
75
    create_dir,
 
76
    write_file,
72
77
)
73
78
 
74
79
# Default directory structure name.
104
109
            raise CommandError("'{0}' already exists, and is a "
105
110
                               "file.".format(self.args.DIR))
106
111
 
107
 
        if not os.path.isdir(full_path):
108
 
            try:
109
 
                os.makedirs(full_path)
110
 
            except OSError:
111
 
                raise CommandError("Cannot create directory "
112
 
                                   "'{0}'.".format(self.args.DIR))
113
 
 
114
 
        test_path = os.path.join(full_path, TESTS_DIR)
115
 
        if not os.path.isdir(test_path):
116
 
            try:
117
 
                os.makedirs(test_path)
118
 
            except OSError:
119
 
                raise CommandError("Cannot create directory "
120
 
                                   "'{0}'.".format(self.args.DIR))
 
112
        create_dir(full_path)
121
113
 
122
114
        data = self._update_data()
123
 
        self._create_files(data, full_path, test_path)
 
115
 
 
116
        test_path = create_dir(full_path, TESTS_DIR)
 
117
        # TODO
 
118
        self._create_script(test_path)
 
119
 
 
120
        testdef_file = self.create_test_definition(
 
121
            os.path.join(test_path, DEFAULT_TESTDEF_FILE))
 
122
 
 
123
        job = data[JOBFILE_ID]
 
124
        self.create_tar_repo_job(
 
125
            os.path.join(full_path, job), testdef_file, test_path)
124
126
 
125
127
    def _update_data(self):
126
128
        """Updates the template and ask values to the user.
135
137
 
136
138
        return data
137
139
 
138
 
    def _create_files(self, data, full_path, test_path):
 
140
    def _create_script(self, test_path):
139
141
        # This is the default script file as defined in the testdef template.
140
142
        default_script = os.path.join(test_path, DEFAULT_TESTDEF_SCRIPT)
141
143
 
145
147
            print >> sys.stdout, ("\nCreating default test script "
146
148
                                  "'{0}'.".format(DEFAULT_TESTDEF_SCRIPT))
147
149
 
148
 
            with open(default_script, "w") as write_file:
149
 
                write_file.write(DEFAULT_TESTDEF_SCRIPT_CONTENT)
 
150
            write_file(default_script, DEFAULT_TESTDEF_SCRIPT_CONTENT)
150
151
 
151
 
            # Prompt the user to write the script file.
152
 
            self.edit_file(default_script)
 
152
        # Prompt the user to write the script file.
 
153
        edit_file(default_script)
153
154
 
154
155
        # Make sure the script is executable.
155
156
        os.chmod(default_script,
156
157
                 stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH)
157
158
 
158
 
        print >> sys.stdout, ("\nCreating test definition "
159
 
                              "'{0}':".format(DEFAULT_TESTDEF_FILE))
160
 
        self._create_test_file(os.path.join(test_path,
161
 
                                            DEFAULT_TESTDEF_FILE))
162
 
 
163
 
        job = data[JOBFILE_ID]
164
 
        print >> sys.stdout, "\nCreating job file '{0}':".format(job)
165
 
        self._create_job_file(os.path.join(full_path, job), test_path)
166
 
 
167
 
    def _create_job_file(self, job_file, test_path=None):
168
 
        """Creates the job file on the filesystem."""
169
 
 
170
 
        # Invoke the command to create new job files: make a copy of the local
171
 
        # args and add what is necessary for the command.
172
 
        from lava.job.commands import new
173
 
 
174
 
        args = copy.copy(self.args)
175
 
        args.FILE = job_file
176
 
 
177
 
        job_cmd = new(self.parser, args)
178
 
        job_cmd.invoke(tests_dir=test_path)
179
 
 
180
 
    def _create_test_file(self, test_file):
181
 
        """Creates the test definition file on the filesystem."""
182
 
 
183
 
        # Invoke the command to create new testdef files: make a copy of the
184
 
        # local args and add what is necessary for the command.
185
 
        from lava.testdef.commands import new
186
 
 
187
 
        args = copy.copy(self.args)
188
 
        args.FILE = test_file
189
 
 
190
 
        testdef_cmd = new(self.parser, args)
191
 
        testdef_cmd.invoke()
192
 
 
193
159
 
194
160
class run(BaseCommand):
195
161
    """Runs a job on the local dispatcher."""
209
175
        full_path = os.path.abspath(self.args.JOB)
210
176
        job_file = retrieve_file(full_path, JOB_FILE_EXTENSIONS)
211
177
 
212
 
        self._run_job(job_file)
213
 
 
214
 
    def _run_job(self, job_file):
215
 
        """Runs a job on the dispatcher."""
216
 
        from lava.job.commands import run
217
 
 
218
 
        args = copy.copy(self.args)
219
 
        args.FILE = job_file
220
 
 
221
 
        run_cmd = run(self.parser, args)
222
 
        run_cmd.invoke()
 
178
        super(run, self).run(job_file)
223
179
 
224
180
 
225
181
class submit(BaseCommand):
240
196
        full_path = os.path.abspath(self.args.JOB)
241
197
        job_file = self.retrieve_file(full_path, JOB_FILE_EXTENSIONS)
242
198
 
243
 
        self._submit_job(job_file)
244
 
 
245
 
    def _submit_job(self, job_file):
246
 
        """Submits a job file to LAVA."""
247
 
        from lava.job.commands import submit
248
 
 
249
 
        args = copy.copy(self.args)
250
 
        args.FILE = job_file
251
 
 
252
 
        submit_cmd = submit(self.parser, args)
253
 
        submit_cmd.invoke()
 
199
        super(submit, self).submit(job_file)
254
200
 
255
201
 
256
202
class update(BaseCommand):
273
219
        tests_dir = os.path.join(job_dir, TESTS_DIR)
274
220
 
275
221
        if os.path.isdir(tests_dir):
 
222
            # TODO
276
223
            encoded_tests = None
277
224
 
278
225
            json_data = None
285
232
                    raise CommandError("Cannot read job file '{0}'.".format(
286
233
                        job_file))
287
234
 
288
 
            with open(job_file, "w") as write_file:
289
 
                try:
290
 
                    write_file.write(json.dumps(json_data, indent=4))
291
 
                except Exception:
292
 
                    raise CommandError("Cannot update job file "
293
 
                                       "'{0}'.".format(json_file))
 
235
            content = json.dumps(json_data, indent=4)
 
236
            write_file(job_file, content)
 
237
 
294
238
            print >> sys.stdout, "Job definition updated."
295
239
        else:
296
240
            raise CommandError("Cannot find tests directory.")