blob: c4901052c8c62aad9b03e1066b6180405eacce8a [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001# osmo_gsm_tester: trial: directory of binaries to be tested
2#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Neels Hofmeyr <neels@hofmeyr.de>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Affero General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Affero General Public License for more details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
21import time
22import shutil
23import tarfile
24
25from . import log, util
26
27FILE_MARK_TAKEN = 'taken'
28FILE_CHECKSUMS = 'checksums.md5'
29TIMESTAMP_FMT = '%Y-%m-%d_%H-%M-%S'
30FILE_LAST_RUN = 'last_run'
31
32class Trial(log.Origin):
33 path = None
34 dir = None
35 _run_dir = None
36 bin_tars = None
37
38 @staticmethod
39 def next(trials_dir):
40
41 with trials_dir.lock('Trial.next'):
42 trials = [e for e in trials_dir.children()
43 if trials_dir.isdir(e) and not trials_dir.exists(e, FILE_MARK_TAKEN)]
44 if not trials:
45 return None
46 # sort by time to get the one that waited longest
47 trials.sort(key=lambda e: os.path.getmtime(trials_dir.child(e)))
48 next_trial = trials[0]
49 return Trial(trials_dir.child(next_trial)).take()
50
51 def __init__(self, trial_dir):
52 self.path = trial_dir
Your Name3c6673a2017-04-08 18:52:39 +020053 self.set_name(os.path.basename(self.path))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020054 self.set_log_category(log.C_TST)
55 self.dir = util.Dir(self.path)
56 self.inst_dir = util.Dir(self.dir.child('inst'))
57 self.bin_tars = []
58
59 def __repr__(self):
60 return self.name()
61
62 def __enter__(self):
63 self.log('Trial start')
Neels Hofmeyr506edbc2017-05-06 21:56:27 +020064 self.take()
Neels Hofmeyr3531a192017-03-28 14:30:28 +020065 super().__enter__()
66
67 def __exit__(self, *exc_info):
68 super().__exit__(*exc_info)
69 self.log('Trial end')
70
71 def take(self):
72 self.dir.touch(FILE_MARK_TAKEN)
73 return self
74
75 def get_run_dir(self):
76 if self._run_dir is not None:
77 return self._run_dir
78 self._run_dir = util.Dir(self.dir.new_child('run.%s' % time.strftime(TIMESTAMP_FMT)))
79 self._run_dir.mkdir()
80
81 last_run = self.dir.child(FILE_LAST_RUN)
82 if os.path.islink(last_run):
83 os.remove(last_run)
84 if not os.path.exists(last_run):
85 os.symlink(self.dir.rel_path(self._run_dir.path), last_run)
86 return self._run_dir
87
88 def verify(self):
89 "verify checksums"
90
91 if not self.dir.exists():
92 raise RuntimeError('Trial dir does not exist: %r' % self.dir)
93 if not self.dir.isdir():
94 raise RuntimeError('Trial dir is not a dir: %r' % self.dir)
95
96 checksums = self.dir.child(FILE_CHECKSUMS)
97 if not self.dir.isfile(FILE_CHECKSUMS):
98 raise RuntimeError('No checksums file in trial dir: %r', checksums)
99
100 with open(checksums, 'r') as f:
101 line_nr = 0
102 for line in [l.strip() for l in f.readlines()]:
103 line_nr += 1
104 if not line:
105 continue
106 md5, filename = line.split(' ')
107 file_path = self.dir.child(filename)
108
109 if not self.dir.isfile(filename):
110 raise RuntimeError('File listed in checksums file but missing in trials dir:'
111 ' %r vs. %r line %d' % (file_path, checksums, line_nr))
112
113 if md5 != util.md5_of_file(file_path):
114 raise RuntimeError('Checksum mismatch for %r vs. %r line %d'
115 % (file_path, checksums, line_nr))
116
117 if filename.endswith('.tgz'):
118 self.bin_tars.append(filename)
119
120 def has_bin_tar(self, bin_name):
121 bin_tar_start = '%s.' % bin_name
122 matches = [t for t in self.bin_tars if t.startswith(bin_tar_start)]
123 self.dbg(bin_name=bin_name, matches=matches)
124 if not matches:
125 return None
126 if len(matches) > 1:
127 raise RuntimeError('More than one match for bin name %r: %r' % (bin_name, matches))
128 bin_tar = matches[0]
129 bin_tar_path = self.dir.child(bin_tar)
130 if not os.path.isfile(bin_tar_path):
131 raise RuntimeError('Not a file or missing: %r' % bin_tar_path)
132 return bin_tar_path
133
134 def get_inst(self, bin_name):
135 bin_tar = self.has_bin_tar(bin_name)
136 if not bin_tar:
Your Name3c6673a2017-04-08 18:52:39 +0200137 raise RuntimeError('No such binary available: %r' % bin_name)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200138 inst_dir = self.inst_dir.child(bin_name)
139
140 if os.path.isdir(inst_dir):
141 # already unpacked
142 return inst_dir
143
144 t = None
145 try:
146 os.makedirs(inst_dir)
147 t = tarfile.open(bin_tar)
148 t.extractall(inst_dir)
149 return inst_dir
150
151 except:
152 shutil.rmtree(inst_dir)
153 raise
154 finally:
155 if t:
156 try:
157 t.close()
158 except:
159 pass
160
161# vim: expandtab tabstop=4 shiftwidth=4