def get_uncommitted_oval(): """ Returns list of OVAL files in the repository that are not committed. """ repo = get_repo() uncommitted_oval = set() content_rel_path = lib_repo.get_repository_root_path().replace( lib_repo.get_root_path(), '')[1:] # check for changes in content staged for commit for diff in repo.index.diff(repo.head.commit, paths=content_rel_path): uncommitted_oval.add(diff.a_path) # print('staged: {0}'.format(diff.a_path)) # check for changes in content not staged for commit for diff in repo.index.diff(None, paths=content_rel_path): uncommitted_oval.add(diff.a_path) # print('not staged: {0}'.format(diff.a_path)) # check for untracked files for path in repo.untracked_files: if path.startswith(content_rel_path): uncommitted_oval.add(path) # get full paths AND remove non-xml files, i.e. readme.md uncommitted_oval = { os.path.join(lib_repo.get_root_path(), path) for path in uncommitted_oval if path.endswith('.xml') } #print('Uncommitted files list:\n\t{0}'.format('\n\t'.join(uncommitted_oval))) return uncommitted_oval
def compare_current_oval_to_remote(remote='origin', branch='master'): """ Returns a list of OVAL files in current working directory that differ from a remote/branch. """ repo = get_repo() # get a reference the the remote repo all_remotes = { available_remote.name for available_remote in repo.remotes } if remote not in all_remotes: raise RemoteDoesNotExistError(remote) remote_repo = repo.remotes[remote].repo # get the branch of the remote repo all_branches = { available_branch.name for available_branch in remote_repo.heads } if branch not in all_branches: raise RemoteBranchDoesNotExistError(remote, branch) remote_branch = remote_repo.heads[branch] # get the head commit of the remote branch remote_headcommit = remote_branch.commit content_rel_path = lib_repo.get_repository_root_path().replace(lib_repo.get_root_path(),'')[1:] files = set() for diff in remote_headcommit.diff(None, paths=content_rel_path): files.add(diff.a_path) # check for untracked files for path in repo.untracked_files: if path.startswith(content_rel_path): files.add(path) # get full paths AND remove non-xml files, i.e. readme.md files = { os.path.join(lib_repo.get_root_path(), path) for path in files if path.endswith('.xml') } # print('Files list:\n\t{0}'.format('\n\t'.join(files))) return files
def get_uncommitted_oval(): """ Returns list of OVAL files in the repository that are not committed. """ repo = get_repo() uncommitted_oval = set() content_rel_path = lib_repo.get_repository_root_path().replace(lib_repo.get_root_path(),'')[1:] # check for changes in content staged for commit for diff in repo.index.diff(repo.head.commit, paths=content_rel_path): uncommitted_oval.add(diff.a_path) # print('staged: {0}'.format(diff.a_path)) # check for changes in content not staged for commit for diff in repo.index.diff(None, paths=content_rel_path): uncommitted_oval.add(diff.a_path) # print('not staged: {0}'.format(diff.a_path)) # check for untracked files for path in repo.untracked_files: if path.startswith(content_rel_path): uncommitted_oval.add(path) # get full paths AND remove non-xml files, i.e. readme.md uncommitted_oval = { os.path.join(lib_repo.get_root_path(), path) for path in uncommitted_oval if path.endswith('.xml') } #print('Uncommitted files list:\n\t{0}'.format('\n\t'.join(uncommitted_oval))) return uncommitted_oval
def get_non_def_element_paths_bottom_up(): """ Returns an iterator of all non-definition elements OVAL element paths. """ elements_root = os.path.join(lib_repo.get_repository_root_path()) for element_type in ['variables', 'states', 'objects', 'tests']: for dirpath, dirnames, filenames in os.walk(os.path.join(elements_root, element_type)): for filename in filenames: if filename.endswith('.xml'): yield os.path.join(dirpath, filename)
def get_non_def_element_paths_bottom_up(): """ Returns an iterator of all non-definition elements OVAL element paths. """ elements_root = os.path.join(lib_repo.get_repository_root_path()) for element_type in ['variables', 'states', 'objects', 'tests']: for dirpath, dirnames, filenames in os.walk( os.path.join(elements_root, element_type)): for filename in filenames: if filename.endswith('.xml'): yield os.path.join(dirpath, filename)
def main(): # get index elements_index = lib_search.ElementsIndex(message) for element_type in ['definitions', 'objects', 'states', 'tests', 'variables']: for root, dirs, files in os.walk(os.path.join(lib_repo.get_repository_root_path(), element_type)): for name in files: full_path = os.path.join(root,name) message('info','Searching index for path {0}'.format(full_path)) documents = elements_index.query({ 'path': full_path }) if len(documents) == 0: message('error',"File {0} has no corresponding index entry".format(full_path))
def compare_current_oval_to_remote(remote='origin', branch='master'): """ Returns a list of OVAL files in current working directory that differ from a remote/branch. """ repo = get_repo() # get a reference the the remote repo all_remotes = {available_remote.name for available_remote in repo.remotes} if remote not in all_remotes: raise RemoteDoesNotExistError(remote) remote_repo = repo.remotes[remote].repo # get the branch of the remote repo all_branches = { available_branch.name for available_branch in remote_repo.heads } if branch not in all_branches: raise RemoteBranchDoesNotExistError(remote, branch) remote_branch = remote_repo.heads[branch] # get the head commit of the remote branch remote_headcommit = remote_branch.commit content_rel_path = lib_repo.get_repository_root_path().replace( lib_repo.get_root_path(), '')[1:] files = set() for diff in remote_headcommit.diff(None, paths=content_rel_path): files.add(diff.a_path) # check for untracked files for path in repo.untracked_files: if path.startswith(content_rel_path): files.add(path) # get full paths AND remove non-xml files, i.e. readme.md files = { os.path.join(lib_repo.get_root_path(), path) for path in files if path.endswith('.xml') } # print('Files list:\n\t{0}'.format('\n\t'.join(files))) return files
def main(): """ Breaks the OVAL file into its constituent elements and writes each of those into the repository """ parser = argparse.ArgumentParser(description='Separates an OVAL file into its component parts and saves them to the repository.') options = parser.add_argument_group('options') options.add_argument('-f', '--file', required=True, help='The name of the source file') options.add_argument('-v', '--verbose', required=False, action="store_true", help='Enable more verbose messages') args = vars(parser.parse_args()) oval = OvalDocument(None) filename = args['file'] if args['verbose']: verbose = True else: verbose = False if not oval.parseFromFile(filename): print("\n >> Unable to parse source file '{0}': no actions taken".format(filename)) return deflist = oval.getDefinitions() if not deflist or deflist is None or len(deflist) < 1: print("\n ## Error: this document does not contain any OVAL definitions. No further action will be taken") return if verbose: print(" Number of definitions to process: ", len(deflist)) repository_root = lib_repo.get_repository_root_path() writeFiles(deflist, repository_root, verbose) writeFiles(oval.getTests(), repository_root, verbose) writeFiles(oval.getObjects(), repository_root, verbose) writeFiles(oval.getStates(), repository_root, verbose) writeFiles(oval.getVariables(), repository_root, verbose)
def decompose(filename, verbose): oval = OvalDocument(None) if not oval.parseFromFile(filename): print("\n >> Unable to parse source file '{0}': no actions taken". format(filename)) return deflist = oval.getDefinitions() # if not deflist or deflist is None or len(deflist) < 1: # print("\n ## Error: this document does not contain any OVAL definitions. No further action will be taken") # return if verbose: print(" Number of definitions to process: ", len(deflist)) repository_root = lib_repo.get_repository_root_path() writeFiles(deflist, repository_root, verbose) writeFiles(oval.getTests(), repository_root, verbose) writeFiles(oval.getObjects(), repository_root, verbose) writeFiles(oval.getStates(), repository_root, verbose) writeFiles(oval.getVariables(), repository_root, verbose)
def decompose(filename, verbose): oval = OvalDocument(None) if not oval.parseFromFile(filename): print("\n >> Unable to parse source file '{0}': no actions taken".format(filename)) return deflist = oval.getDefinitions() # if not deflist or deflist is None or len(deflist) < 1: # print("\n ## Error: this document does not contain any OVAL definitions. No further action will be taken") # return if verbose: print(" Number of definitions to process: ", len(deflist)) repository_root = lib_repo.get_repository_root_path() writeFiles(deflist, repository_root, verbose) writeFiles(oval.getTests(), repository_root, verbose) writeFiles(oval.getObjects(), repository_root, verbose) writeFiles(oval.getStates(), repository_root, verbose) writeFiles(oval.getVariables(), repository_root, verbose)