def env_check(self, *args, **kwargs): """ This method provides a common entry for any checks on the environment (input / output dirs, etc) """ b_status = True str_error = '' if not len(self.str_outputDir): b_status = False str_error = 'output directory not specified.' self.dp.qprint(str_error, comms='error') error.warn(self, 'outputDirFail', drawBox=True) return {'status': b_status, 'str_error': str_error}
def outputSaveCallback(self, *args, **kwags) -> dict: """ Callback stub for saving outputs. Here, we simply "touch" each file in the analyzed list to the output tree. This dummy method is mostly for illustration. """ str_outputPath: str = "" d_inputAnalyzeCallback: dict = {} filesSaved: int = 0 b_status: bool = False str_fileToSave: str = "" if len(args): at_data = args[0] str_outputPath = at_data[0] d_inputAnalyzeCallback = at_data[1] if 'l_fileAnalyzed' in d_inputAnalyzeCallback.keys() and \ len(str_outputPath): other.mkdir(self.args['outputDir']) other.mkdir(str_outputPath) for f in d_inputAnalyzeCallback['l_fileAnalyzed']: str_fileToSave = os.path.join(str_outputPath, f) if os.path.exists(str_fileToSave): if self.args['overwrite']: os.remove(str_fileToSave) else: error.warn(self, 'outputFileExists', drawBox=True) b_status = False break os.mknod('%s/%s' % (str_outputPath, f)) b_status = True self.dp.qprint("saving: %s/%s" % (str_outputPath, f), level=5) filesSaved += 1 return { 'status': b_status, 'filesSaved': filesSaved, 'overwrite': self.args['overwrite'] }
def run(self, *args, **kwargs): """ Probe the input tree and print. """ b_status = True d_probe = {} d_tree = {} d_stats = {} str_error = '' b_timerStart = False d_test = {} for k, v in kwargs.items(): if k == 'timerStart': b_timerStart = bool(v) if b_timerStart: other.tic() if not os.path.exists(self.str_inputDir): b_status = False self.dp.qprint( "input directory either not specified or does not exist.", comms='error') error.warn(self, 'inputDirFail', exitToOS=True, drawBox=True) str_error = 'error captured while accessing input directory' if b_status: str_origDir = os.getcwd() if self.b_relativeDir: os.chdir(self.str_inputDir) str_rootDir = '.' else: str_rootDir = self.str_inputDir d_probe = self.tree_probe(root=str_rootDir) b_status = b_status and d_probe['status'] d_tree = self.tree_construct(l_files=d_probe['l_files'], constructCallback=self.dirsize_get) b_status = b_status and d_tree['status'] if self.b_test: d_test = self.test_run(*args, **kwargs) b_status = b_status and d_test['status'] else: if self.b_stats or self.b_statsReverse: d_stats = self.stats_compute() self.dp.qprint('Total size (raw): %d' % d_stats['totalSize'], level=1) self.dp.qprint('Total size (human): %s' % d_stats['totalSize_human'], level=1) self.dp.qprint('Total files: %s' % d_stats['files'], level=1) self.dp.qprint('Total dirs: %s' % d_stats['dirs'], level=1) b_status = b_status and d_stats['status'] if self.b_jsonStats: print(json.dumps(d_stats, indent=4, sort_keys=True)) if self.b_relativeDir: os.chdir(str_origDir) d_ret = { 'status': b_status, 'd_probe': d_probe, 'd_tree': d_tree, 'd_stats': d_stats, 'd_test': d_test, 'str_error': str_error, 'runTime': other.toc() } if self.b_json: print(json.dumps(d_ret, indent=4, sort_keys=True)) return d_ret