blob: 1d83b3670eeb9ab7a3c4a90e25f689c5d9cd3604 [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:
26 raise Exception("%s failed to execute % cmd")
27
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
79 cmd = "cp %s %s" % (pkg_properties.get("output_path"), self.static_dir)
80 err = os.system(cmd)
81 self.AssertSystemCallSuccess(err, cmd)
82
83 return pkg_properties.get("output_file_name")
84
85
86 def GetPackages(self):
87 """
88 Lists all of the packages that can be built with a make_pkg.sh script.
89
90 Returns a dictionary with the following keys
91 name: the name of the package.
92 build_time: the time the package was last built (in UTC).
93 source_path: the path to the package in the source tree.
94 output_path: the path to the deb created by make_pkg.sh.
95 output_file_name: the name of the deb created by make_pkg.sh
96 """
97 pkgs = {}
98 cli = os.popen("find %s -name make_pkg.sh" % self.root_dir).read().split('\n')
99 for pkg in cli:
100 if pkg == "":
101 continue
102 pkg_path = pkg.replace("/make_pkg.sh", "", 1)
103 pkg_name = self.GetPackageName(pkg_path)
104 if pkg_name == "":
105 web.debug("unable to find a package info for %s" % pkg_path)
106 continue
107
108 build_time = self.GetLastBuildTime(pkg_name)
109 build_path = self.GetPackageBuildPath(pkg_name)
110 build_file = self.GetPackageBuildFile(build_path)
111 pkgs[pkg_name] = {
112 "name": pkg_name,
113 "build_time": build_time,
114 "source_path": pkg_path,
115 "output_path": build_path,
116 "output_file_name": build_file
117 }
118 return pkgs