def make_package(self, name, version, relationships, architecture='all', files=[]): tmp_dir = self.make_temporary_directory() filename = '%s_%s_%s' % (name, version, architecture) packaging_dir = os.path.join(tmp_dir, filename) os.mkdir(packaging_dir) os.mkdir(os.path.join(packaging_dir, 'DEBIAN')) relationship_strs = [] for relationship_name, relationship_value in relationships.items(): relationship_strs.append('%s: %s\n' % (relationship_name, relationship_value)) subst_vars = dict( architecture=architecture, name=name, relationships=''.join(relationship_strs), version=version, ) control_file_text = self.control_file_template.safe_substitute( subst_vars) # If any files have been specified, create them for file_path in files: os.makedirs(os.path.join(packaging_dir, os.path.dirname(file_path))) with open(os.path.join(packaging_dir, file_path), 'w') as new_file: new_file.write(name + " " + file_path) with open(os.path.join(packaging_dir, 'DEBIAN', 'control'), 'w') as control_file: control_file.write(control_file_text) env = os.environ env['LC_ALL'] = 'C' env['NO_PKG_MANGLE'] = '1' proc = cmd_runner.Popen(['dpkg-deb', '-b', '-Zgzip', packaging_dir], env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdoutdata, stderrdata) = proc.communicate() if proc.returncode: raise ValueError("dpkg-deb failed!\n%s" % stderrdata) if stderrdata: raise ValueError("dpkg-deb had warnings:\n%s" % stderrdata) deb_file_path_match = re.match( "dpkg-deb: building package `.*' in `(.*)'", stdoutdata) if not deb_file_path_match: raise ValueError( "failed to find filename in dpkg-deb output:\n%s" % stdoutdata) return deb_file_path_match.group(1)
def verify_file_integrity(sig_file_list): """Verify a list of signature files. The parameter is a list of filenames of gpg signature files which will be verified using gpg. For each of the files it is assumed that there is an sha1 hash file with the same file name minus the '.asc' extension. Each of the sha1 files will be checked using sha1sums. All files listed in the sha1 hash file must be found in the same directory as the hash file. """ gpg_sig_ok = True gpg_out = "" verified_files = [] for sig_file in sig_file_list: hash_file = sig_file[0:-len('.asc')] tmp = tempfile.NamedTemporaryFile() try: cmd_runner.run([ 'gpg', '--status-file={0}'.format(tmp.name), '--verify', sig_file ]).wait() except cmd_runner.SubcommandNonZeroReturnValue: gpg_sig_ok = False gpg_out = gpg_out + tmp.read() tmp.close() if os.path.dirname(hash_file) == '': sha_cwd = None else: sha_cwd = os.path.dirname(hash_file) try: sha1sums_out, _ = cmd_runner.Popen(['sha1sum', '-c', hash_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=sha_cwd).communicate() except cmd_runner.SubcommandNonZeroReturnValue as inst: sha1sums_out = inst.stdout for line in sha1sums_out.splitlines(): sha1_check = re.search(r'^(.*):\s+OK', line) if sha1_check: verified_files.append(sha1_check.group(1)) return verified_files, gpg_sig_ok, gpg_out
def is_tar_support_selinux(): try: tar_help, _ = cmd_runner.Popen( ['tar', '--help'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ).communicate() except cmd_runner.SubcommandNonZeroReturnValue as inst: return False for line in tar_help.splitlines(): selinux_support = re.search('--selinux', line) if selinux_support: return True return False
def test_Popen(self): proc = cmd_runner.Popen('true') returncode = proc.wait() self.assertEqual(0, returncode)