def check_file_checksum(local_repository, filename, md5sum=None, cksum=None, dry_run=False): """Compute the md5 checksum or cksum for file, if available. Returns True if successful, False if there was a mismatch or problem. """ local_repository = fixpath(local_repository) filepath = os.path.join(local_repository, filename) def compare(expected, actual): """We can compare either the full output line or just the checksum part. """ if ' ' in expected: return expected==actual else: actual_first_part = actual.split()[0] return expected==actual_first_part if md5sum and os.path.exists('/usr/bin/md5sum'): if dry_run: message("/usr/bin/md5sum %s" % filename, dry_run=dry_run) return True try: o = run_program_and_capture_results(['/usr/bin/md5sum', filename], None, logger, cwd=local_repository) o = o.rstrip() except Exception, e: raise Exception("Got exception when attempting to compute md5sum for %s: %s" % (filename, e)) if compare(md5sum, o): logger.debug("%s matches md5sum" % filename) return True else: logger.error("%s has a md5sum mismatch, computed sum was %s" % (filename, o)) return False
def get_nfs_export_path_list(host_or_ip): path_list = [] cmd = ['/sbin/showmount', '-e', host_or_ip] logger.info("use showmount to get NFS export paths:") if os.getuid()!=0: cmd = ['/usr/bin/sudo',] + cmd logger.info("/usr/bin/sudo /sbin/showmount -e %s" % host_or_ip) else: logger.info("/sbin/showmount -e %s" % host_or_ip) try: results = process.run_program_and_capture_results(cmd, None, logger, expected_rcs=[0]) for line in results.split('\n'): line = line.strip() if line.startswith('/'): path_list.append(line) except process.SubprocBadRcError, e: # check the various error codes if e.rc==110: ue = convert_exc_to_user_error(sys.exc_info(), errors[ERR_CONN_TIMEOUT], msg_args={'host':host_or_ip}) else: ue = convert_exc_to_user_error(sys.exc_info(), errors[ERR_NFS_LIST_EXC], msg_args={"host":host_or_ip, "exc":"%s(%s)" % (e.__class__.__name__, unicode(e))}) raise ue
def get_mounted_filesystems(): """Call df and parse the output (which is tricky). """ if sys.platform=='darwin': BLOCKSIZE = 512L else: # linux BLOCKSIZE = KB result = process.run_program_and_capture_results(['/bin/mount',], None, logger) rows = result.split('\n') fs_list = [] map_by_mp = {} def df_blocks_to_size(blks): return blks*BLOCKSIZE if (blks is not None) else None for row in rows: mo = MOUNT_OUTPUT_RE.match(row) if mo!=None and (mo.group(3) in ['ext3', 'ext4', 'nfs', 'cifs']): fs_name = mo.group(1) mount_point = mo.group(2) df_re = get_df_output_re(fs_name, mount_point) df_result = process.run_program_and_capture_results(['/bin/df', '-P', mount_point], None, logger) df_rows = df_result.split('\n') (total, used, available) = (None, None, None) for df_row in df_rows: mo = df_re.match(df_row) if mo!=None: total = long(mo.group(1)) used = long(mo.group(2)) available = long(mo.group(3)) break fs_info = FsInfo(fs_name, df_blocks_to_size(total), df_blocks_to_size(used), df_blocks_to_size(available), mount_point) fs_list.append(fs_info) assert not map_by_mp.has_key(mount_point), "duplicate for mount point %s" % mount_point map_by_mp[mount_point] = fs_info return MountedFilesystems(fs_list, map_by_mp)
def run_smbinfo(credentials_file): with capture_logging(logging.getLogger()) as l: r = re.compile(re.escape("Share: ") + r'(.+)$') shares = [] cmd = [smbinfo, '-a', credentials_file, 'list-shares', host_or_ip] if os.getuid()!=0: cmd = ['/usr/bin/sudo',] + cmd try: results = process.run_program_and_capture_results(cmd, None, logger, expected_rcs=[0]) for line in results.split('\n'): mo = r.match(line) if mo!=None: shares.append(mo.group(1)) except process.SubprocBadRcError, e: # check the various error codes if e.rc==4: ue = convert_exc_to_user_error(sys.exc_info(), errors[ERR_SHARE_LIST_PERM], msg_args={'host':host_or_ip}) elif e.rc==110: ue = convert_exc_to_user_error(sys.exc_info(), errors[ERR_SHARE_CONN_TIMEOUT], msg_args={'host':host_or_ip}) else: ue = convert_exc_to_user_error(sys.exc_info(), errors[ERR_SHARE_LIST_EXC], msg_args={"host":host_or_ip, "exc":"%s(%s)" % (e.__class__.__name__, unicode(e))}) ue.developer_msg = l.getvalue() raise ue except Exception, e: ue = convert_exc_to_user_error(sys.exc_info(), errors[ERR_SHARE_LIST_EXC], msg_args={"host":host_or_ip, "exc":"%s(%s)" % (e.__class__.__name__, unicode(e))}) ue.developer_msg = l.getvalue() raise ue
def get_disk_usage(directory, assume_zero_if_error=False, follow_symbolic_links=False): for d in SPECIAL_DIRECTORIES: if directory.startswith(d): if assume_zero_if_error: return 0L else: raise Exception("Cannot determine size for special directory %s" % directory) options = '-csxkH' if follow_symbolic_links else '-csxk' try: result = process.run_program_and_capture_results(['/usr/bin/du', options, directory], None, logger) for row in result.split('\n'): fields = row.split() if len(fields)==2 and fields[1]=='total': return KB*long(fields[0]) except Exception: logger.exception("Could not get size for %s" % directory) if assume_zero_if_error: return 0L else: raise Exception("Could not get size for %s" % directory)
raise Exception("Got exception when attempting to compute md5sum for %s: %s" % (filename, e)) if compare(md5sum, o): logger.debug("%s matches md5sum" % filename) return True else: logger.error("%s has a md5sum mismatch, computed sum was %s" % (filename, o)) return False elif cksum: assert os.path.exists("cksum") if dry_run: message("/usr/bin/cksum %s" % filename, dry_run=dry_run) return True try: o = run_program_and_capture_results(['/usr/bin/cksum', filename], None, logger, cwd=local_repository) o = o.rstrip() except Exception, e: raise Exception("Got exception when attempting to compute cksum for %s: %s" % (filename, e)) if compare(cksum, o): logger.debug("%s matches cksum" % filename) return True else: logger.error("%s has a cksum mismatch, computed sum was %s" % (filename, o)) return False else: return True # no checksums to compute