aboutsummaryrefslogtreecommitdiff
blob: f673e0f2d16288e50a1c5850449b603aeb5ed235 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import glob
from filetypes.ctypefiles import scanincludes
from filetypes.makefiles import scanmakefile

def scandirfor(dir, filetypes):
    files = []
    dirs = [f for f in os.listdir(dir)
        if os.path.isdir(os.path.join(dir, f))]
    for filetype in filetypes:
        files += glob.glob(dir + "/*" + filetype)
    for dir_path in dirs:
        files += scandirfor(dir + "/" + dir_path, filetypes)
    return files

def scanmakefiledeps(makefile):
    curdir = os.path.split(makefile)[0] + "/"
    makefile = openfile(makefile)
    binaries = set() #the binaries that the .o file create
    filestoscan = []
    impfiles = [] #look for these files
    targets = scanmakefile(makefile)
    deps = targets[0][1] #Use first make target
    while deps != []:
        newdeps = []
        for dep in deps:
            for target in targets:
                if target[0] == dep:
                    newdeps += target[1]
                    if ".o" in dep or dep in impfiles:
                        impfiles += target[1]
                    elif ".o" in target[1][0]:
                        binaries.add(target[0])
        deps = newdeps

    #impfiles.sort()
    for impfile in impfiles:
        filestoscan.append(curdir + impfile)
    #print(filestoscan)
    return filestoscan,binaries,targets

def scanfilelist(filelist):
    global_hfiles = set()
    local_hfiles = set()
    inclst = [global_hfiles,local_hfiles,{}]

    for file in filelist:
        filestring = openfile(file)
        if not filestring == None:
            inclst = scanincludes(filestring,inclst,os.path.split(file)[0])

    return(inclst)

def scanproject(dir,projecttype):
    if projecttype == "guess":
        filestolookfor = ["Makefile","makefile"] #add more later
    elif projecttype == "makefile":
        filestolookfor = ["Makefile","makeifle"]

    mfile = scandirfor(dir, filestolookfor)[0] #use first file found
    print(mfile)
    (scanlist,binaries,targets) = scanmakefiledeps(mfile)
    return scanfilelist(scanlist),binaries,targets

def openfile(file):
    try:
        with open(file, encoding="utf-8", errors="replace") as inputfile:
            return inputfile.read()
    except IOError:
        print('cannot open', file)