def _GetUname(self, uname_option=None): args = ["uname"] if uname_option: args.append(uname_option) # TODO: Don't fork during unit tests ret, stdout, unused_stderr = shell.ShellCommand(args, allow_error=False) return stdout.strip()
def _GetSrv4PkginfosStream(self): """Calls pkginfo if file is not specified.""" if self.infile_pkginfo: pkginfo_stream = open(self.infile_pkginfo, "r") else: args = ["pkginfo"] ret, stdout, stderr = shell.ShellCommand(args) pkginfo_stream = stdout.splitlines() return pkginfo_stream
def pkgname(self): """It's necessary to figure out the pkgname from the .pkg file. # nawk 'NR == 2 {print $1; exit;} $f """ if self._pkgname is None: gunzipped_path = self.gunzipped_path args = ["nawk", "NR == 2 {print $1; exit;}", gunzipped_path] ret_code, stdout, stderr = shell.ShellCommand(args) self._pkgname = stdout.strip() logging.debug("GetPkgname(): %s", repr(self.pkgname)) return self._pkgname
def _CollectElfdumpData(self): logging.debug("Elfdump data.") binary_md5_sums = [] for binary in self.ListBinaries(): binary_abs_path = os.path.join(self._dir_format_base_dir, self.GetFilesDir(), binary) args = [ os.path.join(os.path.dirname(__file__), 'collect_binary_elfinfo.py'), '--input', binary_abs_path ] se = None if self.debug: args.append('--debug') se = sys.stderr ret_code, stdout, stderr = shell.ShellCommand(args, stderr=se) if ret_code: raise ShellCommandError(stderr) binary_data = cjson.decode(stdout) binary_md5_sums.append((binary, binary_data['md5_sum'])) return binary_md5_sums
def _Gunzip(self): """Gunzipping the package.""" gzip_suffix = ".gz" pkg_suffix = ".pkg" if self.pkg_path.endswith("%s%s" % (pkg_suffix, gzip_suffix)): # Causing the class to stat the .gz file. This call throws away the # result, but the result will be cached as a object member. self.mtime self.md5_sum base_name = os.path.split(self.pkg_path)[1][:(-len(gzip_suffix))] self._gunzipped_path = os.path.join(self.work_dir, base_name) with open(self._gunzipped_path, 'w') as gunzipped_file: args = ["gunzip", "-f", "-c", self.pkg_path] unused_ret_code, _, _ = shell.ShellCommand( args, stdout=gunzipped_file) elif self.pkg_path.endswith(pkg_suffix): self._gunzipped_path = self.pkg_path else: raise Error("The file name should end in either " "%s or %s, but it's %s." % (gzip_suffix, pkg_suffix, repr(self.pkg_path)))
def GetUsernameAndPassword(): username = os.environ["LOGNAME"] password = None authfile = os.path.join('/etc/opt/csw/releases/auth', username) try: with open(authfile, 'r') as af: password = af.read().strip() except IOError as e: logging.debug("%s could not be read: %s", authfile, e) if password is None: # This part is specific to OpenCSW buildfarm. args = ['ssh', '-o BatchMode=yes', '-o StrictHostKeyChecking=no', 'login', 'cat', authfile] ret_code, stdout, stderr = shell.ShellCommand(args, allow_error=True) if not ret_code: password = stdout.strip() else: logging.debug('Failed running %r', args) logging.debug('Could not find password for user %r.', username) return username, password
def _TransformToDir(self): """Transforms the file to the directory format. This uses the Pkgtrans function at the top, because pkgtrans behaves differently on Solaris 8 and 10. Having our own implementation helps achieve consistent behavior. """ if not self._transformed: gunzipped_path = self.gunzipped_path pkgname = self.pkgname args = [ os.path.join(os.path.dirname(__file__), "..", "..", "bin", "custom-pkgtrans"), gunzipped_path, self.work_dir, pkgname ] shell.ShellCommand(args, allow_error=False) dirs = self.DirsInWorkdir() if len(dirs) != 1: raise Error( "Exactly one package in the package stream is expected; actual: " "%s." % (dirs)) self._transformed = True self._dir_format_base_dir = os.path.join(self.work_dir, pkgname)
def CollectStatsFromCatalogEntries(self, catalog_entries, force_unpack=False): """Returns: A list of md5 sums of collected statistics.""" args_display = [x['file_basename'] for x in catalog_entries] if len(args_display) > 5: args_display = args_display[:5] + ["...more..."] self.logger.debug("Processing: %s, please be patient", args_display) md5_sum_list = [] # Reversing the item order in the list, so that the pop() method can be used # to get packages, and the order of processing still matches the one in the # catalog file. total_packages = len(catalog_entries) if not total_packages: raise PackageError("The length of package list is zero.") counter = itertools.count(1) self.logger.info("Juicing the svr4 package stream files...") if self.debug: pbar = mute_progressbar.MuteProgressBar() else: pbar = progressbar.ProgressBar(widgets=[ progressbar.widgets.Percentage(), ' ', progressbar.widgets.ETA(), ' ', progressbar.widgets.Bar() ]) pbar.maxval = total_packages pbar.start() base_dir, _ = os.path.split(__file__) collect_pkg_metadata = os.path.join(base_dir, "collect_pkg_metadata.py") for catalog_entry in catalog_entries: pkg_file_name = catalog_entry['pkg_path'] args = [collect_pkg_metadata] stderr_file = subprocess.PIPE if self.debug: args.append('--debug') stderr_file = None if force_unpack: args += ['--force-unpack'] args += ['--input', pkg_file_name] ret_code, stdout, stderr = shell.ShellCommand(args, allow_error=False, stderr=stderr_file) try: data_back = cjson.decode(stdout) if data_back['md5_sum'] != catalog_entry['md5sum']: msg = ('Unexpected file content: on disk (or in catalog) the file ' '%r (%r) has MD5 sum %r but it turned out to be %r as ' 'seen by collect_pkg_metadata.py. ' 'We cannot continue, because we have no ' 'access to the data we are asked to examine. ' 'This can happen when you run mgar on intel and sparc in ' 'parallel, and you have some arch=all packages in the ' 'package set. This error will not happen if you run ' 'mgar platforms.' % (catalog_entry['file_basename'], catalog_entry['pkg_path'], catalog_entry['md5sum'], data_back['md5_sum'])) raise PackageError(msg) md5_sum_list.append(data_back['md5_sum']) except cjson.DecodeError: logging.fatal('Could not deserialize %r', stdout) raise pbar.update(counter.next()) pbar.finish() return md5_sum_list
def GetPkgchkOutput(self): """Returns: (exit code, stdout, stderr).""" if not self._transformed: self._TransformToDir() args = ["/usr/sbin/pkgchk", "-d", self.work_dir, self.pkgname] return shell.ShellCommand(args)
def _GetIpsPkginfosStream(self): args = ["pkg", "list", "-H", "-s"] ret, stdout, stderr = shell.ShellCommand(args) pkglist_stream = stdout.splitlines() return pkglist_stream
def _GetIpsPkgcontentStream(self): args = ["pkg", "contents", "-H", "-o", "path,action.name,pkg.name,target,mode,owner,group", "-t", "dir,file,hardlink,link"] ret, stdout, unused_stderr = shell.ShellCommand(args) return stdout.splitlines()