xref: /aosp_15_r20/pdk/build/prepare_pdk_tree.py (revision ac7df209e3ba002eb4fcfc7c8d4b818a0371fca4)
1*ac7df209SYuntao Xu#!/usr/bin/env python
2*ac7df209SYuntao Xu#
3*ac7df209SYuntao Xu# Copyright (C) 2012 The Android Open Source Project
4*ac7df209SYuntao Xu#
5*ac7df209SYuntao Xu# Licensed under the Apache License, Version 2.0 (the 'License');
6*ac7df209SYuntao Xu# you may not use this file except in compliance with the License.
7*ac7df209SYuntao Xu# You may obtain a copy of the License at
8*ac7df209SYuntao Xu#
9*ac7df209SYuntao Xu#      http://www.apache.org/licenses/LICENSE-2.0
10*ac7df209SYuntao Xu#
11*ac7df209SYuntao Xu# Unless required by applicable law or agreed to in writing, software
12*ac7df209SYuntao Xu# distributed under the License is distributed on an 'AS IS' BASIS,
13*ac7df209SYuntao Xu# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*ac7df209SYuntao Xu# See the License for the specific language governing permissions and
15*ac7df209SYuntao Xu# limitations under the License.
16*ac7df209SYuntao Xu#
17*ac7df209SYuntao Xu
18*ac7df209SYuntao Xu# prepare_pdk_tree.py target_dir [-m manifest] pdk_groups
19*ac7df209SYuntao Xu# Ex: prepare_pdk_tree.py ../tmp/pdk grouper
20*ac7df209SYuntao Xu# create mount_pdk.sh and umount_pdk.sh, which mounts/umounts pdk sources.
21*ac7df209SYuntao Xu
22*ac7df209SYuntao Xuimport os
23*ac7df209SYuntao Xuimport re
24*ac7df209SYuntao Xuimport sys
25*ac7df209SYuntao Xuimport subprocess
26*ac7df209SYuntao Xu
27*ac7df209SYuntao Xu
28*ac7df209SYuntao Xuclass ManifestHandler(object):
29*ac7df209SYuntao Xu
30*ac7df209SYuntao Xu    def __init__(self):
31*ac7df209SYuntao Xu        # current pattern
32*ac7df209SYuntao Xu        self.current = 0
33*ac7df209SYuntao Xu        self.patterns = [re.compile('path=\"([^\"]*)\".*groups=\"([^\"]*)\"'), \
34*ac7df209SYuntao Xu                         re.compile('groups=\"([^\"]*)\".*path=\"([^\"]*)\"')]
35*ac7df209SYuntao Xu
36*ac7df209SYuntao Xu    def getAttribs(self, line):
37*ac7df209SYuntao Xu        attrib = [None, None] # list of path, groups
38*ac7df209SYuntao Xu        m = self.patterns[self.current].search(line)
39*ac7df209SYuntao Xu        # if match fails, try both pattens and change default one
40*ac7df209SYuntao Xu        # if match founds
41*ac7df209SYuntao Xu        if m is None:
42*ac7df209SYuntao Xu            notCurrent = 1 - self.current
43*ac7df209SYuntao Xu            mOther = self.patterns[notCurrent].search(line)
44*ac7df209SYuntao Xu            if mOther is not None:
45*ac7df209SYuntao Xu                # toggle
46*ac7df209SYuntao Xu                self.current = notCurrent
47*ac7df209SYuntao Xu                m = mOther
48*ac7df209SYuntao Xu        if m is not None:
49*ac7df209SYuntao Xu            if (self.current == 0):
50*ac7df209SYuntao Xu                attrib[0] = m.group(1)
51*ac7df209SYuntao Xu                attrib[1] = m.group(2)
52*ac7df209SYuntao Xu            else:
53*ac7df209SYuntao Xu                attrib[0] = m.group(2)
54*ac7df209SYuntao Xu                attrib[1] = m.group(1)
55*ac7df209SYuntao Xu        return attrib
56*ac7df209SYuntao Xu
57*ac7df209SYuntao Xudef isInGroups(groupsAttrib, groups):
58*ac7df209SYuntao Xu    if groupsAttrib is None:
59*ac7df209SYuntao Xu        return False
60*ac7df209SYuntao Xu    groupsAttribList = groupsAttrib.split(',')
61*ac7df209SYuntao Xu    for group in groups:
62*ac7df209SYuntao Xu        if group in groupsAttribList:
63*ac7df209SYuntao Xu            return True
64*ac7df209SYuntao Xu    return False
65*ac7df209SYuntao Xu
66*ac7df209SYuntao Xudef getPDKDirs(manifest, groups):
67*ac7df209SYuntao Xu    subdirs = []
68*ac7df209SYuntao Xu    handler = ManifestHandler()
69*ac7df209SYuntao Xu    f = open(manifest, 'r')
70*ac7df209SYuntao Xu    for line in f:
71*ac7df209SYuntao Xu        [path, groupsAttrib] = handler.getAttribs(line)
72*ac7df209SYuntao Xu        if isInGroups(groupsAttrib, groups):
73*ac7df209SYuntao Xu            subdirs.append(path)
74*ac7df209SYuntao Xu    f.close()
75*ac7df209SYuntao Xu    return subdirs
76*ac7df209SYuntao Xu
77*ac7df209SYuntao Xudef create_symbolic_link(src_top, dest_top, dir_name):
78*ac7df209SYuntao Xu    src_full = src_top + "/" + dir_name
79*ac7df209SYuntao Xu    dest_full = dest_top + "/" + dir_name
80*ac7df209SYuntao Xu    #print "create symbolic link from " + dest_full + " to " + src_full
81*ac7df209SYuntao Xu    # remove existing link first to prevent recursive loop
82*ac7df209SYuntao Xu    os.system("rm -rf " + dest_full)
83*ac7df209SYuntao Xu    os.system("ln -s " + src_full + " " + dest_full)
84*ac7df209SYuntao Xu
85*ac7df209SYuntao Xu# The only file not from manifest.
86*ac7df209SYuntao Xucopy_files_list = [ "Makefile" ]
87*ac7df209SYuntao XuMOUNT_FILE = 'mount_pdk.sh'
88*ac7df209SYuntao XuUMOUNT_FILE = 'umount_pdk.sh'
89*ac7df209SYuntao XuSH_HEADER = "#!/bin/bash\n#Auto-generated file, do not edit!\n"
90*ac7df209SYuntao Xu
91*ac7df209SYuntao Xudef main(argv):
92*ac7df209SYuntao Xu    manifestFile = ".repo/manifest.xml"
93*ac7df209SYuntao Xu    groups = ["pdk"]
94*ac7df209SYuntao Xu    if len(argv) < 2:
95*ac7df209SYuntao Xu        print "create_pdk_tree.py target_dir [-m manifest] [-a dir_to_add] pdk_groups"
96*ac7df209SYuntao Xu        print " ex) create_pdk_tree.py ../tmp grouper"
97*ac7df209SYuntao Xu        print " -a option is to include a directory which does not belong to specified group"
98*ac7df209SYuntao Xu        print "   multiple -a options can be specified like -a frameworks/base -a external/aaa"
99*ac7df209SYuntao Xu        print " Note that pdk group is included by default"
100*ac7df209SYuntao Xu        print " Do not create target_dir under the current source tree. This will cause build error."
101*ac7df209SYuntao Xu        sys.exit(1)
102*ac7df209SYuntao Xu    targetDir = argv[1]
103*ac7df209SYuntao Xu    argc = 2
104*ac7df209SYuntao Xu    subdirs = []
105*ac7df209SYuntao Xu    if len(argv) > 2:
106*ac7df209SYuntao Xu        if argv[2] == "-m":
107*ac7df209SYuntao Xu            manifestFile = argv[3]
108*ac7df209SYuntao Xu            argc += 2
109*ac7df209SYuntao Xu    while argc < len(argv):
110*ac7df209SYuntao Xu        if argv[argc] == "-a":
111*ac7df209SYuntao Xu            argc += 1
112*ac7df209SYuntao Xu            subdirs.append(argv[argc])
113*ac7df209SYuntao Xu        else:
114*ac7df209SYuntao Xu            groups.append(argv[argc])
115*ac7df209SYuntao Xu        argc += 1
116*ac7df209SYuntao Xu    sourceDir = os.path.abspath('.')
117*ac7df209SYuntao Xu    targetDir = os.path.abspath(targetDir)
118*ac7df209SYuntao Xu
119*ac7df209SYuntao Xu    p = subprocess.Popen("mount", stdout = subprocess.PIPE)
120*ac7df209SYuntao Xu    targetMounted = False
121*ac7df209SYuntao Xu    for line in p.stdout:
122*ac7df209SYuntao Xu        if targetDir in line:
123*ac7df209SYuntao Xu            targetMounted = True
124*ac7df209SYuntao Xu    p.stdout.close()
125*ac7df209SYuntao Xu
126*ac7df209SYuntao Xu    if targetMounted:
127*ac7df209SYuntao Xu        print "target dir already mounted"
128*ac7df209SYuntao Xu        if os.path.exists(targetDir + '/' + UMOUNT_FILE):
129*ac7df209SYuntao Xu            print "Use existing file", UMOUNT_FILE, "to unmount"
130*ac7df209SYuntao Xu            sys.exit(1)
131*ac7df209SYuntao Xu        else:
132*ac7df209SYuntao Xu            print "Will create scripts, but may need manual unmount"
133*ac7df209SYuntao Xu
134*ac7df209SYuntao Xu    subdirs += getPDKDirs(manifestFile, groups)
135*ac7df209SYuntao Xu    print subdirs
136*ac7df209SYuntao Xu    os.system("mkdir -p " + targetDir)
137*ac7df209SYuntao Xu    mountf = open(targetDir + '/' + MOUNT_FILE, 'w+')
138*ac7df209SYuntao Xu    mountf.write(SH_HEADER)
139*ac7df209SYuntao Xu    umountf = open(targetDir + '/' + UMOUNT_FILE, 'w+')
140*ac7df209SYuntao Xu    umountf.write(SH_HEADER)
141*ac7df209SYuntao Xu    for subdir in subdirs:
142*ac7df209SYuntao Xu        os.system("mkdir -p " + targetDir + '/' + subdir)
143*ac7df209SYuntao Xu        mountf.write("mount --bind " + sourceDir + "/" + subdir + " " + targetDir + "/" + subdir + \
144*ac7df209SYuntao Xu                        "\n")
145*ac7df209SYuntao Xu        umountf.write("umount " + targetDir + "/" + subdir + "\n")
146*ac7df209SYuntao Xu    for file_name in copy_files_list:
147*ac7df209SYuntao Xu        create_symbolic_link(sourceDir, targetDir, file_name)
148*ac7df209SYuntao Xu    mountf.close()
149*ac7df209SYuntao Xu    umountf.close()
150*ac7df209SYuntao Xu    os.system("chmod 700 " + targetDir + '/' + MOUNT_FILE)
151*ac7df209SYuntao Xu    os.system("chmod 700 " + targetDir + '/' + UMOUNT_FILE)
152*ac7df209SYuntao Xu
153*ac7df209SYuntao Xuif __name__ == '__main__':
154*ac7df209SYuntao Xu    main(sys.argv)
155