def files_exist_linting( self, files_fail: list, files_fail_ifexists: list, files_warn: list, files_warn_ifexists: list, is_subclass_calling=True, handle: str = "general", ) -> None: """ Verifies that passed lists of files exist or do not exist. Depending on the desired result passing, warning or failing results are appended to the linter object. :param self: Linter object :param files_fail: list of files which must exist or linting will fail :param files_fail_ifexists: list of files which are not allowed to exist or linting will fail :param files_warn: list of files which should exist or linting will warn :param files_warn_ifexists: list of files which should exist or linting will warn :param is_subclass_calling: indicates whether the subclass of TemplateLinter called the linting (specific) or itw as the general linting """ # Files that cause an error if they don't exist all_exists = True for files in files_fail: if not any([os.path.isfile(pf(self, f)) for f in files]): all_exists = False self.failed.append((f"{handle}-1", f"File not found: {self._wrap_quotes(files)}")) # flag that indiactes whether all required files exist or not if all_exists: # called linting from a specific template linter if is_subclass_calling: self.passed.append((f"{handle}-1", f"All required {handle} specific files were found!")) # called as general linting else: self.passed.append((f"{handle}-1", f"All required {handle} files were found!")) # Files that cause a warning if they don't exist for files in files_warn: if any([os.path.isfile(pf(self, f)) for f in files]): # pass cause if a file was found it will be summarised in one "all required files found" statement pass else: self.warned.append((f"{handle}-1", f"File not found: {self._wrap_quotes(files)}")) # Files that cause an error if they exist for file in files_fail_ifexists: if os.path.isfile(pf(self, file)): self.failed.append((f"{handle}-1", f"File must be removed: {self._wrap_quotes(file)}")) else: self.passed.append((f"{handle}-1", f"File not found check: {self._wrap_quotes(file)}")) # Files that cause a warning if they exist for file in files_warn_ifexists: if os.path.isfile(pf(self, file)): self.warned.append((f"{handle}-1", f"File should be removed: {self._wrap_quotes(file)}")) else: self.passed.append((f"{handle}-1", f"File not found check: {self._wrap_quotes(file)}"))
def check_files_exist(self, is_subclass_calling=True): """Checks a given project directory for required files. Iterates through the project's directory content and checkmarks files for presence. Files that **must** be present:: 'Dockerfile', 'cookietemple.cfg' 'Makefile' 'README.rst' 'CHANGELOG.rst' '[LICENSE, LICENSE.md, LICENCE, LICENCE.md]' 'docs/index.rst' 'docs/readme.rst' 'docs/changelog.rst' 'docs/installation.rst' 'docs/usage.rst' Files that *should* be present:: '.gitignore', '.github/ISSUE_TEMPLATE/bug_report.md', '.github/ISSUE_TEMPLATE/general_question.md', '.github/ISSUE_TEMPLATE/feature_request.md', '.github/pull_request.md', Files that *must not* be present:: none Files that *should not* be present:: none Raises: An AssertionError if .cookietemple.yml is not found found. """ # NB: Should all be files, not directories # List of lists. Passes if any of the files in the sublist are found. files_fail = [ ['Dockerfile'], ['cookietemple.cfg'], ['Makefile'], ['README.rst'], ['CHANGELOG.rst'], ['LICENSE', 'LICENSE.md', 'LICENCE', 'LICENCE.md'], # NB: British / American spelling [os.path.join('docs', 'index.rst')], [os.path.join('docs', 'readme.rst')], [os.path.join('docs', 'changelog.rst')], [os.path.join('docs', 'installation.rst')], [os.path.join('docs', 'usage.rst')], ] files_warn = [ ['.gitignore'], [os.path.join('.github', 'ISSUE_TEMPLATE', 'bug_report.md')], [os.path.join('.github', 'ISSUE_TEMPLATE', 'feature_request.md')], [os.path.join('.github', 'ISSUE_TEMPLATE', 'general_question.md')], [os.path.join('.github', 'pull_request_template.md')] ] # List of strings. Fails / warns if any of the strings exist. files_fail_ifexists = [] files_warn_ifexists = [] # First - critical files. Check that this is actually a cookietemple based project if not os.path.isfile(pf(self, '.cookietemple.yml')): print( '[bold red] .cookietemple.yml not found! Is this a cookietemple project?' ) sys.exit(1) files_exist_linting(self, files_fail, files_fail_ifexists, files_warn, files_warn_ifexists, is_subclass_calling)