1#!/usr/bin/env python 2# Copyright 2017 Google Inc. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16################################################################################ 17 18import os 19import subprocess 20 21import apt 22 23SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) 24 25 26def ApplyPatch(source_directory, patch_name): 27 """Apply custom patch.""" 28 subprocess.check_call(['patch', '-p1', '-i', 29 os.path.join(SCRIPT_DIR, patch_name)], 30 cwd=source_directory) 31 32 33class PackageException(Exception): 34 """Base package exception.""" 35 36 37class Package(object): 38 """Base package.""" 39 40 def __init__(self, name, apt_version): 41 self.name = name 42 self.apt_version = apt_version 43 44 def PreBuild(self, source_directory, env, custom_bin_dir): 45 return 46 47 def PostBuild(self, source_directory, env, custom_bin_dir): 48 return 49 50 def PreDownload(self, download_directory): 51 return 52 53 def PostDownload(self, source_directory): 54 return 55 56 def InstallBuildDeps(self): 57 """Install build dependencies for a package.""" 58 subprocess.check_call(['apt-get', 'update']) 59 subprocess.check_call(['apt-get', 'build-dep', '-y', self.name]) 60 61 # Reload package after update. 62 self.apt_version = ( 63 apt.Cache()[self.apt_version.package.name].candidate) 64 65 def DownloadSource(self, download_directory): 66 """Download the source for a package.""" 67 self.PreDownload(download_directory) 68 69 source_directory = self.apt_version.fetch_source(download_directory) 70 71 self.PostDownload(source_directory) 72 return source_directory 73 74 def Build(self, source_directory, env, custom_bin_dir): 75 """Build .deb packages.""" 76 self.PreBuild(source_directory, env, custom_bin_dir) 77 subprocess.check_call( 78 ['dpkg-buildpackage', '-us', '-uc', '-B'], 79 cwd=source_directory, env=env) 80 self.PostBuild(source_directory, env, custom_bin_dir) 81 82 83