blob: b223955d0f29c36b0623bf588bb5d146c08ff56d [file] [log] [blame]
# Copyright 2015 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.
"""Basic implemenation of FreeBSD pkg file format.
This is just enough to allow us to create archive files
that pkg will then be able to install.
See for information
on this package format.
import collections
import hashlib
import os
import shutil
import tarfile
from webports import binary_package, package, paths, util
from webports.error import Error
def write_ucl(outfile, ucl_dict):
"""Write a dictionary to file in UCL format"""
# See:
with open(outfile, 'w') as f:
for key, value in ucl_dict.iteritems():
if key == 'files':
f.write(' "%s": \n {\n' % key)
for file_name, file_hash in value.iteritems():
f.write(' "%s": "%s",\n' % (file_name, file_hash))
f.write(' }\n')
elif key == 'deps':
f.write(' "%s": \n {\n' % key)
for dep_name, dep_dict in value.iteritems():
f.write(' "%s": \n {\n' % dep_name)
for dep_dict_key, dep_dict_value in dep_dict.iteritems():
f.write(' "%s": "%s",\n' % (dep_dict_key, dep_dict_value))
f.write(' },\n')
f.write(' }\n')
f.write(' "%s": "%s",\n' % (key, value))
def parse_dir(payload_dir, file_dict, prefix):
for filename in os.listdir(payload_dir):
if not filename.startswith('+'):
fullname = os.path.join(payload_dir, filename)
if os.path.isdir(fullname):
parse_dir(fullname, file_dict, prefix + filename + '/')
elif os.path.islink(fullname):
with open(fullname, 'rb') as f:
# TODO(sbc): These extensions should probably be stripped out earlier
# or never generated.
basename, ext = os.path.splitext(filename)
if ext in ('.nexe', '.pexe'):
filename = basename
file_dict[prefix + filename] = hashlib.sha256(
def add_files_in_dir(content_dir, tar, prefix):
for filename in os.listdir(content_dir):
fullname = os.path.join(content_dir, filename)
arcname = fullname.replace(prefix, '')
# Resolve symbolic link by duplicating the target file
# TODO(sbc): remove this once devenv (nacl_io) supports symlinks.
if os.path.islink(fullname):
link_target = os.path.realpath(fullname)
if os.path.isabs(link_target):
link_target = link_target.replace(binary_package.INSTALL_PREFIX,
prefix + '/' + INSTALL_PREFIX)
if not os.path.exists(link_target):
raise Exception('Package contains dangling link: %s' % fullname)
if os.path.isdir(link_target):
shutil.copyfile(link_target, fullname)
if os.path.isdir(fullname):
add_files_in_dir(fullname, tar, prefix)
elif os.path.islink(fullname):
info = tar.gettarinfo(fullname) = arcname
# Rather convoluted way to add files to a tar archive that are
# abolute (i.e. start with /). pkg requires this, but python's
# calls lstrip('/') on arcname in the tar.add() method.
with open(fullname, 'r') as f:
info = tar.gettarinfo(fileobj=f) = arcname
# TODO(sbc): These extensions should probably be stripped out earlier
# or never generated.
basename, ext = os.path.splitext(
if ext in ('.nexe', '.pexe'): = basename
tar.addfile(info, fileobj=f)
DEFAULT_LOCATIONS = ('ports', 'ports/python_modules')
def add_package_dep(dep_dict, dep):
dep_dict['origin'] = dep
if os.path.isdir(dep):
pkg = package.Package(info_file=os.path.join(dep, 'pkg_info'))
dep_dict['version'] = pkg.VERSION
for subdir in DEFAULT_LOCATIONS:
pkg_info_file = os.path.join(paths.NACLPORTS_ROOT, subdir, dep, 'pkg_info')
if os.path.exists(pkg_info_file):
dep_dict['version'] = package.Package(info_file=pkg_info_file).VERSION
raise Error("Package not found: %s" % dep)
# These packages are are built-time only depednecies and we won't want
# encode them into the pkg file deps.
def create_dependencies(depends_dict, depends):
for dep in depends:
if dep in BUILD_ONLY_DEPS:
dep_dict = collections.OrderedDict()
add_package_dep(dep_dict, dep)
depends_dict[dep] = dep_dict
def create_pkg_file(name, version, arch, payload_dir, outfile, depends):
"""Create an archive file in FreeBSD's pkg file format"""
util.log('Creating pkg package: %s' % outfile)
manifest = collections.OrderedDict()
manifest['name'] = name
manifest['version'] = version
manifest['arch'] = 'nacl:0:%s' % arch
# The following fields are required by 'pkg' but we don't have
# meaningful values for them yet
manifest['origin'] = name,
manifest['comment'] = 'comment not available'
manifest['desc'] = 'desc not available'
manifest['maintainer'] = ''
manifest['www'] = ''
manifest['prefix'] = INSTALL_PREFIX
if depends:
depends_dict = collections.OrderedDict()
create_dependencies(depends_dict, depends)
manifest['deps'] = depends_dict
temp_dir = os.path.splitext(outfile)[0] + '.tmp'
if os.path.exists(temp_dir):
content_dir = os.path.join(temp_dir, INSTALL_PREFIX)
shutil.copytree(payload_dir, content_dir, symlinks=True)
write_ucl(os.path.join(temp_dir, '+COMPACT_MANIFEST'), manifest)
file_dict = collections.OrderedDict()
parse_dir(temp_dir, file_dict, '/')
manifest['files'] = file_dict
write_ucl(os.path.join(temp_dir, '+MANIFEST'), manifest)
with, 'w:bz2') as tar:
for filename in os.listdir(temp_dir):
if filename.startswith('+'):
fullname = os.path.join(temp_dir, filename)
tar.add(fullname, arcname=filename)
for filename in os.listdir(temp_dir):
if not filename.startswith('+'):
fullname = os.path.join(temp_dir, filename)
add_files_in_dir(fullname, tar, temp_dir)