Example #1
0
def classify_file(filepath):
    """Parsing is too slow, guess by extension unless filetype is ambiguous"""
    # XXX very similar to classify_file in trace_program
    filetypes = FileTypes.get_filetypes_from_extension(filepath)
    filetype = filetypes[0]

    # filetype is ambiguous
    if len(filetypes) > 1:
        if os.path.exists(filepath) and FileTypes.is_source_file(filepath):
            filecontent = open(filepath).read()
            header = finders.find_programHeader(filecontent, stripcomments=True)
            if header:
                source_type_name, _ = header
                filetype = FileTypes.get_filetype_from_source_name(source_type_name)

    return filetype
Example #2
0
def classify_file(filepath, searchpath, stdlibpath):
    located = locate_file(filepath, searchpath + stdlibpath)
    filepath = located and located or filepath
    exists = located and True or False

    # XXX if the filetype is ambiguous and the file is not found there is no way to
    # decide its filetype, just pick the first filetype from the list
    filetypes = FileTypes.get_filetypes_from_extension(filepath)
    filetype = filetypes[0]

    if exists and FileTypes.is_source_file(filepath):
        filepaths = io.iglob(filepath)
        filepath = filepaths[0]
        filecontent = open(filepath).read()
        header = finders.find_programHeader(filecontent, stripcomments=True)
        if header:
            source_type_name, _ = header
            filetype = FileTypes.get_filetype_from_source_name(source_type_name)
    return filepath, exists, filetype