| OLD | NEW |
| 1 # Copyright (c) 2014 The Native Client Authors. All rights reserved. | 1 # Copyright (c) 2014 The Native Client 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 posixpath | 6 import posixpath |
| 7 import shutil | 7 import shutil |
| 8 import stat | 8 import stat |
| 9 import tarfile | 9 import tarfile |
| 10 | 10 |
| 11 from naclports import configuration, package, util, error | 11 from naclports import configuration, package, util, error |
| 12 | 12 |
| 13 PAYLOAD_DIR = 'payload' | 13 PAYLOAD_DIR = 'payload' |
| 14 INSTALL_PREFIX = '/naclports-dummydir' | 14 INSTALL_PREFIX = '/naclports-dummydir' |
| 15 | 15 |
| 16 ELF_MAGIC = '\x7fELF' | 16 ELF_MAGIC = '\x7fELF' |
| 17 PEXE_MAGIC = 'PEXE' |
| 18 |
| 19 def MakeDirIfNeeded(filename): |
| 20 dirname = os.path.dirname(filename) |
| 21 if not os.path.isdir(dirname): |
| 22 util.Makedirs(dirname) |
| 23 |
| 17 | 24 |
| 18 def IsElfFile(filename): | 25 def IsElfFile(filename): |
| 19 if os.path.islink(filename): | 26 if os.path.islink(filename): |
| 20 return False | 27 return False |
| 21 with open(filename) as f: | 28 with open(filename) as f: |
| 22 header = f.read(4) | 29 header = f.read(4) |
| 23 return header == ELF_MAGIC | 30 return header == ELF_MAGIC |
| 24 | 31 |
| 25 | 32 |
| 33 def IsPexeFile(filename): |
| 34 if os.path.islink(filename): |
| 35 return False |
| 36 with open(filename) as f: |
| 37 header = f.read(4) |
| 38 return header == PEXE_MAGIC |
| 39 |
| 40 |
| 26 def InstallFile(filename, old_root, new_root): | 41 def InstallFile(filename, old_root, new_root): |
| 27 """Install a single file by moving it into a new location. | 42 """Install a single file by moving it into a new location. |
| 28 | 43 |
| 29 Args: | 44 Args: |
| 30 filename: Relative name of file to install. | 45 filename: Relative name of file to install. |
| 31 old_root: The current location of the file. | 46 old_root: The current location of the file. |
| 32 new_root: The new desired root for the file. | 47 new_root: The new desired root for the file. |
| 33 """ | 48 """ |
| 34 oldname = os.path.join(old_root, filename) | 49 oldname = os.path.join(old_root, filename) |
| 35 | 50 |
| 36 util.LogVerbose('install: %s' % filename) | 51 util.LogVerbose('install: %s' % filename) |
| 37 | 52 |
| 38 newname = os.path.join(new_root, filename) | 53 newname = os.path.join(new_root, filename) |
| 39 dirname = os.path.dirname(newname) | 54 dirname = os.path.dirname(newname) |
| 40 if not os.path.isdir(dirname): | 55 if not os.path.isdir(dirname): |
| 41 util.Makedirs(dirname) | 56 util.Makedirs(dirname) |
| 42 os.rename(oldname, newname) | 57 os.rename(oldname, newname) |
| 43 | 58 |
| 44 # When install binarie ELF files into the toolchain direcoties, remove | 59 # When install binarie ELF files into the toolchain direcoties, remove |
| 45 # the X bit so that they do not found when searching the PATH. | 60 # the X bit so that they do not found when searching the PATH. |
| 46 if IsElfFile(newname): | 61 if IsElfFile(newname) or IsPexeFile(newname): |
| 47 mode = os.stat(newname).st_mode | 62 mode = os.stat(newname).st_mode |
| 48 mode = mode & ~(stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) | 63 mode = mode & ~(stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
| 49 os.chmod(newname, mode) | 64 os.chmod(newname, mode) |
| 50 | 65 |
| 51 | 66 |
| 52 | 67 |
| 53 def RelocateFile(filename, dest): | 68 def RelocateFile(filename, dest): |
| 54 """Perform in-place mutations on file contents to handle new location. | 69 """Perform in-place mutations on file contents to handle new location. |
| 55 | 70 |
| 56 There are a few file types that have absolute pathnames embedded | 71 There are a few file types that have absolute pathnames embedded |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 """Extract the contents of the pkg_info file from the binary package.""" | 157 """Extract the contents of the pkg_info file from the binary package.""" |
| 143 with tarfile.open(self.filename) as tar: | 158 with tarfile.open(self.filename) as tar: |
| 144 return tar.extractfile('./pkg_info').read() | 159 return tar.extractfile('./pkg_info').read() |
| 145 | 160 |
| 146 def Install(self, force): | 161 def Install(self, force): |
| 147 """Install binary package into toolchain directory.""" | 162 """Install binary package into toolchain directory.""" |
| 148 with util.InstallLock(self.config): | 163 with util.InstallLock(self.config): |
| 149 self._Install(force) | 164 self._Install(force) |
| 150 | 165 |
| 151 def _Install(self, force): | 166 def _Install(self, force): |
| 167 if self.TOOLCHAIN_INSTALL != '0': |
| 168 self._InstallFiles(force) |
| 169 self.WriteStamp() |
| 170 |
| 171 def _InstallFiles(self, force): |
| 152 dest = util.GetInstallRoot(self.config) | 172 dest = util.GetInstallRoot(self.config) |
| 153 dest_tmp = os.path.join(dest, 'install_tmp') | 173 dest_tmp = os.path.join(dest, 'install_tmp') |
| 154 if os.path.exists(dest_tmp): | 174 if os.path.exists(dest_tmp): |
| 155 shutil.rmtree(dest_tmp) | 175 shutil.rmtree(dest_tmp) |
| 156 | 176 |
| 157 if self.IsAnyVersionInstalled(): | 177 if self.IsAnyVersionInstalled(): |
| 158 raise error.Error('package already installed: %s' % self.InfoString()) | 178 raise error.Error('package already installed: %s' % self.InfoString()) |
| 159 | 179 |
| 160 self.LogStatus('Installing') | 180 self.LogStatus('Installing') |
| 161 util.LogVerbose('installing from: %s' % self.filename) | 181 util.LogVerbose('installing from: %s' % self.filename) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 187 | 207 |
| 188 for name in names: | 208 for name in names: |
| 189 InstallFile(name, payload_tree, dest) | 209 InstallFile(name, payload_tree, dest) |
| 190 finally: | 210 finally: |
| 191 shutil.rmtree(dest_tmp) | 211 shutil.rmtree(dest_tmp) |
| 192 | 212 |
| 193 for name in names: | 213 for name in names: |
| 194 RelocateFile(name, dest) | 214 RelocateFile(name, dest) |
| 195 | 215 |
| 196 self.WriteFileList(names) | 216 self.WriteFileList(names) |
| 197 self.WriteStamp() | |
| 198 | 217 |
| 199 def WriteStamp(self): | 218 def WriteStamp(self): |
| 200 """Write stamp file containing pkg_info.""" | 219 """Write stamp file containing pkg_info.""" |
| 201 filename = util.GetInstallStamp(self.NAME, self.config) | 220 filename = util.GetInstallStamp(self.NAME, self.config) |
| 221 MakeDirIfNeeded(filename) |
| 202 util.LogVerbose('stamp: %s' % filename) | 222 util.LogVerbose('stamp: %s' % filename) |
| 203 pkg_info = self.GetPkgInfo() | 223 pkg_info = self.GetPkgInfo() |
| 204 with open(filename, 'w') as f: | 224 with open(filename, 'w') as f: |
| 205 f.write(pkg_info) | 225 f.write(pkg_info) |
| 206 | 226 |
| 207 def WriteFileList(self, file_names): | 227 def WriteFileList(self, file_names): |
| 208 """Write the file list for this package.""" | 228 """Write the file list for this package.""" |
| 209 filename = self.GetListFile() | 229 filename = self.GetListFile() |
| 210 dirname = os.path.dirname(filename) | 230 MakeDirIfNeeded(filename) |
| 211 if not os.path.isdir(dirname): | |
| 212 util.Makedirs(dirname) | |
| 213 with open(filename, 'w') as f: | 231 with open(filename, 'w') as f: |
| 214 for name in file_names: | 232 for name in file_names: |
| 215 f.write(name + '\n') | 233 f.write(name + '\n') |
| OLD | NEW |