def __init__(self, aggregator, root_dir, include_prefixes=None): self.aggregator = aggregator if root_dir.endswith("\\") or root_dir.endswith("//"): self.root_dir = root_dir[0:-1] else: self.root_dir = root_dir self.root_dir = file_utils.normalize_path(self.root_dir) self.cdir = None self.cabsdir = None self.prefixes = [file_utils.normalize_path(p) for p in include_prefixes]
def discover(self): for d, subdlist, flist in os.walk(self.root_dir): normalized_d = file_utils.normalize_path(d) if flist: for f in flist: full_path = file_utils.normalize_path( os.path.abspath(os.path.join(normalized_d, f))) consider = False for prefix in self.prefixes: if normalized_d.startswith(prefix): consider = True break if not consider: continue file_ext = file_utils.get_extension(full_path) if file_ext.lower() not in set(['py']): continue parent_dir = file_utils.normalize_path( os.path.dirname(full_path)) pkg_parent_dir = None # os.path.commonpath() if parent_dir == self.root_dir: pkg_parent_dir = "" else: pkg_parent_dir = parent_dir[parent_dir. index(self.root_dir) + len(self.root_dir) + 1:] file_ext = file_utils.get_extension(full_path) df = DiscoveredFile() df.set_attr(DiscoveredFileAttributeEnum.NAME, file_utils.get_nonext_basename(full_path)) df.set_attr(DiscoveredFileAttributeEnum.EXTENSION, file_ext) df.set_attr(DiscoveredFileAttributeEnum.FULL_NAME, f) df.set_attr( DiscoveredFileAttributeEnum.PACKAGE_DOT_NOTATION, pkg_parent_dir.replace("/", ".")) df.set_attr( DiscoveredFileAttributeEnum.DIRECTORY_RELATIVE_PATH, pkg_parent_dir) df.set_attr( DiscoveredFileAttributeEnum.DIRECTORY_ABSOLUTE_PATH, parent_dir) replaced = df.attr(DiscoveredFileAttributeEnum. DIRECTORY_RELATIVE_PATH).replace( "/", "|") replaced = replaced.replace("\\", "|") df.set_attr( DiscoveredFileAttributeEnum. COMMA_SEPATARED_RELATIVE_PATH, ",".join(replaced.split("|"))) # df.set_attr(DiscoveredFileAttributeEnum.CONTAINER, attr.NA_STRING) # df.set_attr(DiscoveredFileAttributeEnum.CONTAINER_TYPE, attr.NA_STRING) self.aggregator.add(df) self.aggregator.freeze()