1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#!/usr/bin/python
# Copyright (c) 2014 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This library keeps track of all the standard locations for package files"""
import os
import posixpath
SHARED_FOLDER = 'shared'
ARCHIVE_DIR = 'package_archives'
def GetRemotePackageKey(is_shared, rev_num, package_target, package_name):
"""Returns key for package files in the google storage cloud.
Args:
is_shared: Whether or not the package is marked as shared.
rev_num: The SVN revision number of when the package was built.
package_target: The package target which this package belongs to.
package_name: The name of the package.
Returns:
The google cloud storage key where the package file should be found.
"""
if is_shared:
intermediate_dir = SHARED_FOLDER
else:
intermediate_dir = package_target
return posixpath.join('builds',
str(rev_num),
intermediate_dir,
package_name + '.json')
def GetRemotePackageArchiveKey(archive_name, hash_value):
"""Returns key for package archive files in the google storage cloud.
Args:
archive_name: The name of the archive.
hash_value: The hash of the archive.
Returns:
The google cloud storage key where the package archive should be found.
"""
return posixpath.join('archives',
archive_name,
hash_value)
def GetLocalPackageFile(tar_dir, package_target, package_name):
"""Returns the local package file location.
Args:
tar_dir: The tar directory for where package archives would be found.
package_target: The package target of the package.
package_name: The name of the package.
Returns:
The standard location where local package file is found.
"""
return os.path.join(tar_dir,
package_target,
package_name + '.json')
def GetArchiveExtension(archive_name):
"""Returns the extension of an archive.
Note that the archive extension is different from how os.path.splitext splits
extensions. The standard python one splits on the last period, while this one
will split on the first period.
Args:
archive_name: The name of the archive.
Returns:
The extension of the archive.
"""
name_split = archive_name.split('.', 1)
if len(name_split) == 2:
return '.' + name_split[1]
return ''
def GetLocalPackageArchiveDir(tar_dir, archive_name):
"""Returns directory where local package archive files live.
Args:
tar_dir: The tar root directory for where package archives would be found.
archive_name: The name of the archive contained within the package.
Returns:
The standard location where local package archive files are found.
"""
return os.path.join(tar_dir,
ARCHIVE_DIR,
archive_name)
def GetLocalPackageArchiveFile(tar_dir, archive_name, archive_hash):
"""Returns the local package archive file location.
Args:
tar_dir: The tar root directory for where package archives would be found.
archive_name: The name of the archive contained within the package.
archive_hash: The hash of the archive, which will be part of the final name.
Returns:
The standard location where local package archive file is found.
"""
if isinstance(archive_hash, (int, long)):
archive_hash = '%040x' % archive_hash
archive_directory = GetLocalPackageArchiveDir(tar_dir, archive_name)
# Have the file keep the extension so that extractions know the file type.
archive_filename = archive_hash + GetArchiveExtension(archive_name)
return os.path.join(archive_directory, archive_filename)
def GetLocalPackageArchiveLogFile(archive_file):
"""Returns the local package archive log file location.
Args:
archive_file: Location of the local archive file location.
Returns:
The standard location where local package archive log file is found.
"""
return os.path.splitext(archive_file)[0] + '.log'
def GetRevisionFile(revision_dir, package_name):
"""Returns the local revision file location.
Args:
revision_dir: Revision directory where revision files should be found.
package_name: The name of the package revision file represents.
Returns:
The standard location where the revision file is found.
"""
return os.path.join(revision_dir,
package_name + '.json')
def GetFullDestDir(dest_dir, package_target, package_name):
"""Returns the destination directory for a package archive.
Args:
dest_dir: Destination directory root.
package_target: Package target of the package to extract.
package_name: The package name of the package to extract.
Returns:
The package directory within the destination directory.
"""
return os.path.join(dest_dir, package_target, package_name)
def GetDestPackageFile(dest_dir, package_target, package_name):
"""Returns the package file stored in the destination directory.
The package file is normally stored inside of the tar directory, but upon
extraction a copy of the package file is also made into the extraction
directory for book keeping purposes.
Args:
dest_dir: Destination directory root.
package_target: Package target of the package to extract.
package_name: The package name of the package to extract.
Returns:
The location of the package file within the destination directory.
"""
return os.path.join(GetFullDestDir(dest_dir, package_target, package_name),
package_name + '.json')
def WalkPackages(tar_dir):
"""Generator for local package target packages within a root tar directory.
Use this generator to walk through the list of package targets and their
respective packages found within a local tar directory. This function does
not guarantee that these are valid package targets or packages, so it could
yield invalid names for malformed tar directories.
Args:
tar_dir: The tar root directory where package archives would be found.
Yields:
Tuple containing (package_target, [list-of package_names]).
"""
if os.path.isdir(tar_dir):
for package_target_dir in os.listdir(tar_dir):
# Skip the package archive directory
if package_target_dir == ARCHIVE_DIR:
continue
full_package_target_dir = os.path.join(tar_dir, package_target_dir)
if os.path.isdir(full_package_target_dir):
packages = [os.path.splitext(package_name)[0]
for package_name in os.listdir(full_package_target_dir)
if package_name.endswith('.json') and
os.path.isfile(os.path.join(full_package_target_dir,
package_name))]
yield (package_target_dir, packages)
|