OLD | NEW |
1 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | 5 import os |
6 import web | 6 import web |
7 from datetime import datetime | 7 from datetime import datetime |
8 import time | 8 import time |
9 | 9 |
10 class BuildObject: | 10 class BuildObject: |
11 """ | 11 """ |
12 Common base class that defines key paths in the source tree. | 12 Common base class that defines key paths in the source tree. |
13 """ | 13 """ |
14 def __init__(self, root_dir, static_dir): | 14 def __init__(self, root_dir, static_dir): |
15 self.app_id = "87efface-864d-49a5-9bb3-4b050a7c227a" | 15 self.app_id = "87efface-864d-49a5-9bb3-4b050a7c227a" |
16 self.root_dir = root_dir | 16 self.root_dir = root_dir |
17 self.scripts_dir = "%s/scripts" % self.root_dir | 17 self.scripts_dir = "%s/scripts" % self.root_dir |
18 self.static_dir = static_dir | 18 self.static_dir = static_dir |
19 self.x86_pkg_dir = "%s/build/x86/local_packages" % self.root_dir | 19 self.x86_pkg_dir = "%s/build/x86/local_packages" % self.root_dir |
20 | 20 |
21 def AssertSystemCallSuccess(self, err, cmd="unknown"): | 21 def AssertSystemCallSuccess(self, err, cmd="unknown"): |
22 """ | 22 """ |
23 TODO(rtc): This code should probably live somewhere else. | 23 TODO(rtc): This code should probably live somewhere else. |
24 """ | 24 """ |
25 if err != 0: | 25 if err != 0: |
26 raise Exception("%s failed to execute" % cmd) | 26 raise Exception("%s failed to execute" % cmd) |
27 | |
28 | |
29 class BuildUtil(BuildObject): | |
30 | |
31 def GetPackageName(self, pkg_path): | |
32 cmd = "cat %s/debian/control | grep Package: | cut -d \" \" -f 2-" % pkg_pat
h | |
33 return os.popen(cmd).read().strip() | |
34 | |
35 def GetLastBuildTime(self, pkg_name): | |
36 # TODO(rtc): convert this to local time. | |
37 cmd = "stat -c %s %s/%s*" % ("%Y", self.x86_pkg_dir, pkg_name) | |
38 utc_time = os.popen(cmd).read().strip() | |
39 return datetime.fromtimestamp(int(utc_time)) | |
40 | |
41 def GetPackageBuildPath(self, pkg_name): | |
42 cmd = "stat -c %s %s/%s*" % ("%n", self.x86_pkg_dir, pkg_name) | |
43 return os.popen(cmd).read().strip() | |
44 | |
45 def GetPackageBuildFile(self, build_path): | |
46 return build_path.replace(self.x86_pkg_dir + "/", "") | |
47 | |
48 def BuildPackage(self, pkg="all"): | |
49 """ | |
50 Builds the given package and copies the output to the static dir so that | |
51 it can be downloaded. | |
52 | |
53 If pkg=all is specified then the kernel and all platform packages | |
54 will be built. A new system image will also be created. | |
55 | |
56 If pkg=packages is specified then all platform packages | |
57 will be built and a new system image will be created. | |
58 """ | |
59 if pkg == "all": | |
60 err = os.system("%s/build_all.sh" % self.scripts_dir) | |
61 self.AssertSystemCallSuccess(err) | |
62 return None | |
63 | |
64 if pkg == "packages": | |
65 err = os.system("%s/build_platform_packages.sh" % self.scripts_dir) | |
66 self.AssertSystemCallSuccess(err) | |
67 err = os.system("%s/build_image.sh" % self.scripts_dir) | |
68 self.AssertSystemCallSuccess(err) | |
69 return None | |
70 | |
71 pkg_properties = self.GetPackages().get(pkg, None) | |
72 if pkg_properties == None: | |
73 raise Exception("Unknown package name %s" % pkg) | |
74 | |
75 cmd = "(cd %s; ./make_pkg.sh)" % pkg_properties.get("source_path") | |
76 err = os.system(cmd) | |
77 self.AssertSystemCallSuccess(err, cmd) | |
78 | |
79 # Reset pkg_properties after building so that output_path and | |
80 # output_file_name are set up properly. | |
81 pkg_properties = self.GetPackages().get(pkg, None) | |
82 | |
83 cmd = "cp %s %s" % (pkg_properties.get("output_path"), self.static_dir) | |
84 err = os.system(cmd) | |
85 self.AssertSystemCallSuccess(err, cmd) | |
86 | |
87 return pkg_properties.get("output_file_name") | |
88 | |
89 | |
90 def GetPackages(self): | |
91 """ | |
92 Lists all of the packages that can be built with a make_pkg.sh script. | |
93 | |
94 Returns a dictionary with the following keys | |
95 name: the name of the package. | |
96 build_time: the time the package was last built (in UTC). | |
97 source_path: the path to the package in the source tree. | |
98 output_path: the path to the deb created by make_pkg.sh. | |
99 output_file_name: the name of the deb created by make_pkg.sh | |
100 """ | |
101 pkgs = {} | |
102 cli = os.popen("find %s -name make_pkg.sh" % self.root_dir).read().split('\n
') | |
103 for pkg in cli: | |
104 if pkg == "": | |
105 continue | |
106 pkg_path = pkg.replace("/make_pkg.sh", "", 1) | |
107 pkg_name = self.GetPackageName(pkg_path) | |
108 if pkg_name == "": | |
109 web.debug("unable to find a package info for %s" % pkg_path) | |
110 continue | |
111 | |
112 build_path = self.GetPackageBuildPath(pkg_name) | |
113 | |
114 build_time = None | |
115 build_file = None | |
116 if build_path != "": | |
117 build_time = self.GetLastBuildTime(pkg_name) | |
118 build_file = self.GetPackageBuildFile(build_path) | |
119 | |
120 pkgs[pkg_name] = { | |
121 "name": pkg_name, | |
122 "build_time": build_time, | |
123 "source_path": pkg_path, | |
124 "output_path": build_path, | |
125 "output_file_name": build_file | |
126 } | |
127 return pkgs | |
OLD | NEW |