blob: 47e611c378d54112d51ac2529509280b072f22d0 [file] [log] [blame]
rtc@google.comded22402009-10-26 22:36:21 +00001# 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
3# found in the LICENSE file.
4
rtc@google.com21a5ca32009-11-04 18:23:23 +00005import os
rtc@google.com64244662009-11-12 00:52:08 +00006import web
7from datetime import datetime
8import time
rtc@google.com21a5ca32009-11-04 18:23:23 +00009
rtc@google.com64244662009-11-12 00:52:08 +000010class BuildObject:
rtc@google.comded22402009-10-26 22:36:21 +000011 """
rtc@google.com64244662009-11-12 00:52:08 +000012 Common base class that defines key paths in the source tree.
rtc@google.comded22402009-10-26 22:36:21 +000013 """
rtc@google.com64244662009-11-12 00:52:08 +000014 def __init__(self, root_dir, static_dir):
15 self.app_id = "87efface-864d-49a5-9bb3-4b050a7c227a"
16 self.root_dir = root_dir
17 self.scripts_dir = "%s/scripts" % self.root_dir
18 self.static_dir = static_dir
19 self.x86_pkg_dir = "%s/build/x86/local_packages" % self.root_dir
20
21 def AssertSystemCallSuccess(self, err, cmd="unknown"):
22 """
23 TODO(rtc): This code should probably live somewhere else.
24 """
25 if err != 0:
rtc@google.coma9aaa992009-11-13 20:10:34 +000026 raise Exception("%s failed to execute" % cmd)
rtc@google.com64244662009-11-12 00:52:08 +000027
28
29class BuildUtil(BuildObject):
30
31 def GetPackageName(self, pkg_path):
seano@google.comb1b93212009-11-13 08:14:10 +000032 cmd = "cat %s/debian/control | grep Package: | cut -d \" \" -f 2-" % pkg_path
rtc@google.com64244662009-11-12 00:52:08 +000033 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 = "%s/make_pkg.sh" % pkg_properties.get("source_path")
76 err = os.system(cmd)
77 self.AssertSystemCallSuccess(err, cmd)
78
chocobo@google.com24caf312009-11-14 02:04:23 +000079 # 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
rtc@google.com64244662009-11-12 00:52:08 +000083 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
rtc@google.com64244662009-11-12 00:52:08 +0000112 build_path = self.GetPackageBuildPath(pkg_name)
rtc@google.coma9aaa992009-11-13 20:10:34 +0000113
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
rtc@google.com64244662009-11-12 00:52:08 +0000120 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