aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/make_java_srclists.py
blob: f5227f1dd079c60723cb5930b472397f655c1bb2 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import sys
import argparse

# Explicitly enable local imports
# Don't forget to add imported scripts to inputs of the calling command!
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
import process_command_files as pcf
import java_pack_to_file as jcov
from autotar_gendirs import unpack_dir

def writelines(f, rng):
    f.writelines(item + '\n' for item in rng)


class SourcesSorter:
    FILE_ARG = 1
    RESOURCES_DIR_ARG = 2
    SRCDIR_ARG = 3
    JSOURCES_DIR_ARG = 4

    def __init__(self, moddir):
        self.moddir = moddir
        self.next_arg = SourcesSorter.FILE_ARG

        self.cur_resources_list_file = None
        self.cur_jsources_list_file = None
        self.cur_srcdir = None
        self.cur_resources = []
        self.cur_jsources = []

    def sort_args(self, remaining_args):
        for src in remaining_args:
            if self.next_arg == SourcesSorter.RESOURCES_DIR_ARG:
                assert self.cur_resources_list_file is None
                self.cur_resources_list_file = src
                self.next_arg = SourcesSorter.FILE_ARG
                continue
            elif self.next_arg == SourcesSorter.JSOURCES_DIR_ARG:
                assert self.cur_jsources_list_file is None
                self.cur_jsources_list_file = src
                self.next_arg = SourcesSorter.FILE_ARG
                continue
            elif self.next_arg == SourcesSorter.SRCDIR_ARG:
                assert self.cur_srcdir is None
                self.cur_srcdir = src if os.path.isabs(src) else os.path.join(self.moddir, src)
                self.next_arg = SourcesSorter.FILE_ARG
                continue

            if src == '--resources':
                if self.cur_resources_list_file is not None:
                    with open(self.cur_resources_list_file, 'w') as f:
                        writelines(f, self.cur_resources)
                self.cur_resources_list_file = None
                self.cur_srcdir = None
                self.cur_resources = []
                self.next_arg = SourcesSorter.RESOURCES_DIR_ARG
                continue
            if src == '--jsources':
                if self.cur_jsources_list_file is not None:
                    with open(self.cur_jsources_list_file, 'w') as f:
                        writelines(f, self.cur_jsources)
                self.cur_jsources_list_file = None
                self.cur_jsources = []
                self.next_arg = SourcesSorter.JSOURCES_DIR_ARG
                continue
            elif src == '--srcdir':
                self.next_arg = SourcesSorter.SRCDIR_ARG
                continue

            yield src

        if self.cur_resources_list_file is not None:
            with open(self.cur_resources_list_file, 'w') as f:
                writelines(f, self.cur_resources)
        if self.cur_jsources_list_file is not None:
            with open(self.cur_jsources_list_file, 'w') as f:
                writelines(f, self.cur_jsources)


def add_rel_src_to_coverage(coverage, src, source_root):
    rel = os.path.relpath(src, source_root)
    if not rel.startswith('..' + os.path.sep):
        coverage.append(rel)


def main():
    args = pcf.get_args(sys.argv[1:])
    parser = argparse.ArgumentParser()
    parser.add_argument('--moddir')
    parser.add_argument('--java')
    parser.add_argument('--kotlin')
    parser.add_argument('--coverage')
    parser.add_argument('--source-root')
    args, remaining_args = parser.parse_known_args(args)

    java = []
    kotlin = []
    coverage = []

    src_sorter = SourcesSorter(args.moddir)
    for src in src_sorter.sort_args(remaining_args):
        # Handle archived sources here
        if src.endswith(".gentar"):
            unpack_dir(src, os.path.dirname(src))
            continue

        # Handle regular souce files there
        if src.endswith(".java"):
            java.append(src)
            kotlin.append(src)
            if args.coverage and args.source_root:
                add_rel_src_to_coverage(coverage, src, args.source_root)
        elif args.kotlin and src.endswith(".kt"):
            kotlin.append(src)
            if args.coverage and args.source_root:
                add_rel_src_to_coverage(coverage, src, args.source_root)
        else:
            assert src_sorter.cur_srcdir is not None and src_sorter.cur_resources_list_file is not None
            src_sorter.cur_resources.append(os.path.relpath(src, src_sorter.cur_srcdir))

        if src_sorter.cur_jsources_list_file is not None:
            assert src_sorter.cur_srcdir is not None
            src_sorter.cur_jsources.append(os.path.relpath(src, src_sorter.cur_srcdir))

    if args.java:
        with open(args.java, 'w') as f:
            writelines(f, java)
    if args.kotlin:
        with open(args.kotlin, 'w') as f:
            writelines(f, kotlin)
    if args.coverage:
        jcov.write_coverage_sources(args.coverage, args.source_root, coverage)

    return 0


if __name__ == '__main__':
    sys.exit(main())