#!/usr/bin/python3
"""
Assemble a tar archive

Assembles the tree into a tar archive named `filename`.

Uses the buildhost's `tar` command, like: `tar -cf $FILENAME -C $TREE`

If the `compression` option is given, the archive will be compressed by passing
the `--{compression}` option to `tar`. (This option is non-standard and might
not work for anything other than GNU tar.)

Known options for `compression`: "bzip2", "xz", "lzip", "lzma", "lzop", "gzip".

Note that using `compression` does not add an extension to `filename`, so the
caller is responsible for making sure that `compression` and `filename` match.

Buildhost commands used: `tar` and any named `compression` program.
"""


import subprocess
import sys

import osbuild.api


SCHEMA = """
"additionalProperties": false,
"required": ["filename"],
"properties": {
  "filename": {
    "description": "Filename for tar archive",
    "type": "string"
  },
  "compression": {
    "description": "Name of compression program",
    "type": "string",
    "enum": ["bzip2", "xz", "lzip", "lzma", "lzop", "gzip"]
  }
}
"""


def main(tree, output_dir, options):
    filename = options["filename"]
    compression = options.get("compression")

    extra_args = []
    if compression is not None:
        if compression not in {"bzip2", "xz", "lzip", "lzma", "lzop", "gzip"}:
            return 1
        extra_args.append(f"--{compression}")

    # Set environment variables for the tar operation.
    tar_env = {
        # Speed up xz by allowing it to use all CPU cores for compression.
        "XZ_OPT": "--threads 0"
    }

    # Set up the tar command.
    tar_cmd = [
        "tar",
        *extra_args,
        "-cf", f"{output_dir}/{filename}",
        "-C", tree,
        "."
    ]

    # Make a tarball of the tree.
    subprocess.run(
        tar_cmd,
        stdout=subprocess.DEVNULL,
        check=True,
        env=tar_env
    )

    return 0


if __name__ == '__main__':
    args = osbuild.api.arguments()
    r = main(args["tree"], args["output_dir"], args["options"])
    sys.exit(r)

b4e09850fefd455cc6976050b4cb619a9a8369a5
<mask token>


def main(tree, output_dir, options):
    filename = options['filename']
    compression = options.get('compression')
    extra_args = []
    if compression is not None:
        if compression not in {'bzip2', 'xz', 'lzip', 'lzma', 'lzop', 'gzip'}:
            return 1
        extra_args.append(f'--{compression}')
    tar_env = {'XZ_OPT': '--threads 0'}
    tar_cmd = ['tar', *extra_args, '-cf', f'{output_dir}/{filename}', '-C',
        tree, '.']
    subprocess.run(tar_cmd, stdout=subprocess.DEVNULL, check=True, env=tar_env)
    return 0


<mask token>

<mask token>


def main(tree, output_dir, options):
    filename = options['filename']
    compression = options.get('compression')
    extra_args = []
    if compression is not None:
        if compression not in {'bzip2', 'xz', 'lzip', 'lzma', 'lzop', 'gzip'}:
            return 1
        extra_args.append(f'--{compression}')
    tar_env = {'XZ_OPT': '--threads 0'}
    tar_cmd = ['tar', *extra_args, '-cf', f'{output_dir}/{filename}', '-C',
        tree, '.']
    subprocess.run(tar_cmd, stdout=subprocess.DEVNULL, check=True, env=tar_env)
    return 0


if __name__ == '__main__':
    args = osbuild.api.arguments()
    r = main(args['tree'], args['output_dir'], args['options'])
    sys.exit(r)

<mask token>
SCHEMA = """
"additionalProperties": false,
"required": ["filename"],
"properties": {
  "filename": {
    "description": "Filename for tar archive",
    "type": "string"
  },
  "compression": {
    "description": "Name of compression program",
    "type": "string",
    "enum": ["bzip2", "xz", "lzip", "lzma", "lzop", "gzip"]
  }
}
"""


def main(tree, output_dir, options):
    filename = options['filename']
    compression = options.get('compression')
    extra_args = []
    if compression is not None:
        if compression not in {'bzip2', 'xz', 'lzip', 'lzma', 'lzop', 'gzip'}:
            return 1
        extra_args.append(f'--{compression}')
    tar_env = {'XZ_OPT': '--threads 0'}
    tar_cmd = ['tar', *extra_args, '-cf', f'{output_dir}/{filename}', '-C',
        tree, '.']
    subprocess.run(tar_cmd, stdout=subprocess.DEVNULL, check=True, env=tar_env)
    return 0


if __name__ == '__main__':
    args = osbuild.api.arguments()
    r = main(args['tree'], args['output_dir'], args['options'])
    sys.exit(r)

<mask token>
import subprocess
import sys
import osbuild.api
SCHEMA = """
"additionalProperties": false,
"required": ["filename"],
"properties": {
  "filename": {
    "description": "Filename for tar archive",
    "type": "string"
  },
  "compression": {
    "description": "Name of compression program",
    "type": "string",
    "enum": ["bzip2", "xz", "lzip", "lzma", "lzop", "gzip"]
  }
}
"""


def main(tree, output_dir, options):
    filename = options['filename']
    compression = options.get('compression')
    extra_args = []
    if compression is not None:
        if compression not in {'bzip2', 'xz', 'lzip', 'lzma', 'lzop', 'gzip'}:
            return 1
        extra_args.append(f'--{compression}')
    tar_env = {'XZ_OPT': '--threads 0'}
    tar_cmd = ['tar', *extra_args, '-cf', f'{output_dir}/{filename}', '-C',
        tree, '.']
    subprocess.run(tar_cmd, stdout=subprocess.DEVNULL, check=True, env=tar_env)
    return 0


if __name__ == '__main__':
    args = osbuild.api.arguments()
    r = main(args['tree'], args['output_dir'], args['options'])
    sys.exit(r)

#!/usr/bin/python3
"""
Assemble a tar archive

Assembles the tree into a tar archive named `filename`.

Uses the buildhost's `tar` command, like: `tar -cf $FILENAME -C $TREE`

If the `compression` option is given, the archive will be compressed by passing
the `--{compression}` option to `tar`. (This option is non-standard and might
not work for anything other than GNU tar.)

Known options for `compression`: "bzip2", "xz", "lzip", "lzma", "lzop", "gzip".

Note that using `compression` does not add an extension to `filename`, so the
caller is responsible for making sure that `compression` and `filename` match.

Buildhost commands used: `tar` and any named `compression` program.
"""


import subprocess
import sys

import osbuild.api


SCHEMA = """
"additionalProperties": false,
"required": ["filename"],
"properties": {
  "filename": {
    "description": "Filename for tar archive",
    "type": "string"
  },
  "compression": {
    "description": "Name of compression program",
    "type": "string",
    "enum": ["bzip2", "xz", "lzip", "lzma", "lzop", "gzip"]
  }
}
"""


def main(tree, output_dir, options):
    filename = options["filename"]
    compression = options.get("compression")

    extra_args = []
    if compression is not None:
        if compression not in {"bzip2", "xz", "lzip", "lzma", "lzop", "gzip"}:
            return 1
        extra_args.append(f"--{compression}")

    # Set environment variables for the tar operation.
    tar_env = {
        # Speed up xz by allowing it to use all CPU cores for compression.
        "XZ_OPT": "--threads 0"
    }

    # Set up the tar command.
    tar_cmd = [
        "tar",
        *extra_args,
        "-cf", f"{output_dir}/{filename}",
        "-C", tree,
        "."
    ]

    # Make a tarball of the tree.
    subprocess.run(
        tar_cmd,
        stdout=subprocess.DEVNULL,
        check=True,
        env=tar_env
    )

    return 0


if __name__ == '__main__':
    args = osbuild.api.arguments()
    r = main(args["tree"], args["output_dir"], args["options"])
    sys.exit(r)
