"""Dockerfile Top N experiment data."""


# Imports.
from collections import namedtuple
from textwrap import dedent

from lib.strace.collection.untraced import COLLECTOR_NAME


# Constants
SYSTEM = 'linux'


# Named tuple for ad-hoc executable definitions.
Exe = namedtuple(
    'Executable',
    ['count', 'system', 'executable', 'arguments_hash', 'arguments', 'setup'],
    defaults=('',)
)


def _bytes(hex_str: str) -> bytes:
    """Convert a hex string to bytes.

    Parameters
    ----------
    hex_str : str
        Input hex string.

    Returns
    -------
    bytes
        Bytes value of hex.
    """
    return bytes.fromhex(hex_str)


def _setup(script: str) -> str:
    """Format a setup script.

    Parameters
    ----------
    script : str
        Multiline script, potentially with each line indented by a margin.

    Returns
    -------
    str
        Setup script without leading/trailing whitespace or margin.
    """
    return dedent(script.strip('\n'))


# Dockerfile executables in (count, executable, arguments) tuples.
#
# Generated by the following query against the dozer database.
#
# ```
# SELECT
#     COUNT(*) as count, `system`, executable, HEX(arguments_hash), arguments
# FROM
#     dozer.untraced_executables
# WHERE
#     `system` = 'linux'
# GROUP BY
#     `system`, executable, arguments_hash, arguments
# ORDER BY
#     count DESC
# LIMIT
#     100
# ;
# ```
#
# Commented executables have been filtered if the executable is:
#
# - Not a command. For example, `DEBIAN_FRONTEND=noninteractive`. This is just
#   noise that the collection process did not filter out.
# - A shell builtin command.
# - A build system missing a script (make, ./configure, etc.).
# - An interpreter.
# - A symbiotic command (modifies current process state to affect subsequent
#   processes - cd, etc.).
# - Expecting user input, either via stdin or interactive prompts.
DOCKERFILE_EXECUTABLES = (
    Exe(4468, 'linux', 'apt-get', _bytes('CC446CE7F49E953799D618B983B1AC209BD041B2'), ['update'], _setup("""
        rm -rf /var/lib/apt/lists/*
    """)),
    Exe(1310, 'linux', 'apt-get', _bytes('189224F0D7BDE9C7A44315DCA1959DF098A87D48'), ['clean'], _setup("""
        apt-get install -y --download-only git
    """)),
    Exe(1154, 'linux', 'rm', _bytes('231295DB6B336248EE1C7780CAE04D69B4C297D4'), ['-rf', '/var/lib/apt/lists/*'], _setup("""
        apt-get update
    """)),
    # Exe(587, 'linux', 'make', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    # Exe(585, 'linux', 'make', _bytes('709DE561DB1A46BEB6694C8DC4994F2EFE01D29E'), ['install'], ''),
    Exe(393, 'linux', 'apt-get', _bytes('7D1DA4A51CBC42C97457B5C3FACF9DD84C978647'), ['update', '-y'], _setup("""
        rm -rf /var/lib/apt/lists/*
    """)),
    Exe(369, 'linux', 'rm', _bytes('E013B55F192620127D778AF54AF026A162220910'), ['-rf', '/var/lib/apt/lists/*', '/tmp/*', '/var/tmp/*'], _setup("""
        apt-get update
        touch /tmp/file
        touch /var/tmp/file
    """)),
    Exe(352, 'linux', 'apt-get', _bytes('B0AA7F031E50240C7C64492D90FB980E9801354C'), ['upgrade', '-y'], _setup("""
        apt-get install -y git=1:2.20.1-2+deb10u1
    """)),
    Exe(339, 'linux', 'apt-get', _bytes('8E4F9F9A453DC0101039802ABEB3916F08359AE8'), ['-y', 'upgrade'], _setup("""
        apt-get install -y git=1:2.20.1-2+deb10u1
    """)),
    Exe(338, 'linux', 'apt-get', _bytes('778427A17CE205BD87F3B921EAE2CA3303114872'), ['-y', 'update'], _setup("""
        rm -rf /var/lib/apt/lists/*
    """)),
    Exe(289, 'linux', 'locale-gen', _bytes('CD5E24BA25BC96764045A286911D6F6FC907AE23'), ['en_US.UTF-8'], _setup("""
        apt-get install -y locales
        sed -E -i.bak -e 's/# (en_US.UTF-8 UTF-8)/\\1/' /etc/locale.gen
    """)),
    # Exe(285, 'linux', 'npm', _bytes('709DE561DB1A46BEB6694C8DC4994F2EFE01D29E'), ['install'], ''),
    # Exe(263, 'linux', 'pip', _bytes('DF4476B62D09FC3408C432E4660F9980F930934A'), ['install', '-r', 'requirements.txt'], ''),
    Exe(231, 'linux', 'apt-get', _bytes('326EA0CB5A0A71955037072CA7B4723ECC71D3B4'), ['update', '-qq'], _setup("""
        rm -rf /var/lib/apt/lists/*
    """)),
    Exe(223, 'linux', 'apt-get', _bytes('F556AEA9CF5B2BD8643FDF78B00CEFC3B07E81E3'), ['install', '-y', 'software-properties-common'], ''),
    # Exe(211, 'linux', 'apt-key', _bytes('122A90DBFF217B67BF121791017D0F0DF957D3D3'), ['add', '-'], ''),
    Exe(201, 'linux', 'a2enmod', _bytes('A3804B97FDCE0637E8500B1F035D4034376CB3CD'), ['rewrite'], _setup("""
        apt-get install -y apache2
    """)),
    # Exe(193, 'linux', 'cd', _bytes('CF7CD535688164698FCE69FCB4B704C041A64765'), ['/tmp'], ''),
    # Exe(187, 'linux', 'bash', _bytes('983CFDF595BFB7C5CEB95AC69033C3C272DEA0D4'), ['-'], ''),
    # Exe(186, 'linux', './configure', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    Exe(184, 'linux', 'curl', _bytes('042BFFC11684B8726DB8F02D702D43DC290C1F3E'), ['-sS', 'https://getcomposer.org/installer'], _setup("""
        apt-get install -y curl
    """)),
    # Exe(177, 'linux', 'debconf-set-selections', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    Exe(170, 'linux', 'apt-get', _bytes('FC4C816E21453D81CA491A1D62ECAC8B4381AC47'), ['autoremove', '-y'], _setup("""
        apt-get install -y git
        apt-get remove -y git
    """)),
    Exe(166, 'linux', 'apt-get', _bytes('919830D68A145FB034C243E480A6A10BDD7A93AB'), ['install', '-y', 'nodejs'], ''),
    # Exe(162, 'linux', 'python', _bytes('2A0E9A82FAD63111D513530E21DE4BA8FB209C8B'), ['setup.py', 'install'], ''),
    # Exe(157, 'linux', 'cd', _bytes('8D92BBCF88FE81BC7C2E59F2228831ED31E75AFB'), ['..'], ''),
    # Exe(151, 'linux', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), 'chpasswd', [], ''),
    # Exe(137, 'linux', 'php', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    Exe(133, 'linux', 'apt-get', _bytes('19211D62ED0C8A86BA61A8787289498848ADF1E7'), ['install', '-y', 'curl'], _setup("""
        apt-get remove -y curl
    """)),
    Exe(129, 'linux', 'apt-get', _bytes('5B7AC0AC3F576AE2C25D4BECF210FBACE93252CA'), ['-qq', 'update'], _setup("""
        rm -rf /var/lib/apt/lists/*
    """)),
    Exe(123, 'linux', 'echo', _bytes('384C7FAD2350EE5BFFAEF29DEB4612D0865D3D32'), ['daemon off;', '>>', '/etc/nginx/nginx.conf'], _setup("""
        apt-get install -y nginx
    """)),
    # Exe(118, 'linux', 'set', _bytes('16F071A79A24C15210CBABA78208E724D61B2FBD'), ['-x'], ''),
    Exe(111, 'linux', 'apt-get', _bytes('2AB3776631A9F0D0DDB327F3F7A47A250F6F6355'), ['install', '-y', 'wget'], _setup("""
        apt-get remove -y wget
    """)),
    # Exe(111, 'linux', './autogen.sh', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    Exe(104, 'linux', 'ldconfig', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    Exe(103, 'linux', 'chmod', _bytes('8B34C8EE525A28EE447D0D2B894074C704BC4B35'), ['755', '/*.sh'], _setup("""
        touch /chmod_0.sh /chmod_1.sh
    """)),
    # Exe(101, 'linux', 'set', _bytes('552879B8A057C32DC10AA57EE33D984006036B13'), ['-ex'], ''),
    Exe(97, 'linux', 'apt', _bytes('CC446CE7F49E953799D618B983B1AC209BD041B2'), ['update'], _setup("""
        rm -rf /var/lib/apt/lists/*
    """)),
    Exe(96, 'linux', 'apt-get', _bytes('3660C76FC896FAA2F7EEE30108BE97AFFED043E5'), ['install', '-y', 'git'], _setup("""
        apt-get remove -y git
    """)),
    Exe(96, 'linux', 'apt-get', _bytes('79CD7E6ACDE79E5D07B21B2B3A243E27360E7E4F'), ['update', '-q'], _setup("""
        rm -rf /var/lib/apt/lists/*
    """)),
    Exe(94, 'linux', 'mkdir', _bytes('3EED32E3C1E9E1F23E343DDA45ACF33ED6073ABC'), ['-p', '/var/run/sshd'], ''),
    Exe(91, 'linux', 'mkdir', _bytes('FC7D2742B0DDD809F14D856EA26055EF49A060F1'), ['/var/run/sshd'], ''),
    # Exe(91, 'linux', 'DEBIAN_FRONTEND=noninteractive', _bytes('4DB9938D8A4B2773F53E5B728EF138F4B65FDC44'), ['apt-get', 'update'], ''),
    Exe(90, 'linux', 'dpkg', _bytes('145D47E23AAA3517D0063687DA23F2E0559CD08A'), ['--add-architecture', 'i386'], ''),
    Exe(86, 'linux', 'mkdir', _bytes('12F7E1171E1DA5B665405A2316E1B4EADB5E8720'), ['-p', '/app'], ''),
    Exe(86, 'linux', 'mkdir', _bytes('850BD7B2BB6E4963BB5FFE718036B88C016720F1'), ['-p', '/var/log/supervisor'], ''),
    Exe(85, 'linux', 'apt-get', _bytes('8F7B91DAB4B8C9113D24C3EF74E90797D229F20D'), ['autoclean'], ''),
    Exe(83, 'linux', 'pip', _bytes('9900BDBF62E8B7B7B31BECF147172BE1C844521A'), ['install', '--upgrade', 'pip'], _setup("""
        apt-get install -y python-pip=18.1-5
    """)),
    # Exe(82, 'linux', 'done', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    # Exe(82, 'linux', 'cd', _bytes('EB31CA1F6373BCABC5A521FA87A2949C6F5E050D'), ['/'], ''),
    # Exe(81, 'linux', 'bundle', _bytes('709DE561DB1A46BEB6694C8DC4994F2EFE01D29E'), ['install'], ''),
    Exe(81, 'linux', 'echo', _bytes('A68D54193DFCFA448C7C82F7609D09EF66244D14'), ['ServerName localhost', '>>', '/etc/apache2/apache2.conf'], _setup("""
        apt-get install -y apache2
    """)),
    Exe(80, 'linux', 'ln', _bytes('65B25C1A5A9223DB8FE6F79899BE88983127A2CF'), ['-s', '/usr/bin/nodejs', '/usr/bin/node'], _setup("""
        apt-get install -y nodejs
        rm /usr/bin/nodejs
        mv /usr/bin/node /usr/bin/nodejs
    """)),
    # Exe(80, 'linux', 'bash', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    Exe(76, 'linux', 'dpkg-divert', _bytes('E58B7D0640FC41EBBFB69D3049C07CC98FB03A83'), ['--local', '--rename', '--add', '/sbin/initctl'], _setup("""
        mkdir -p /usr/share/ansible/plugins/modules
        wget -q -O /usr/share/ansible/plugins/modules/dpkg_divert.py https://raw.githubusercontent.com/debops/debops/stable-1.2/ansible/roles/debops.ansible_plugins/library/dpkg_divert.py
    """)),
    Exe(74, 'linux', 'locale-gen', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], _setup("""
        apt-get install -y locales
        sed -E -i.bak -e 's/# (en_US.UTF-8 UTF-8)/\\1/' /etc/locale.gen
    """)),
    # Exe(74, 'linux', 'python', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    Exe(73, 'linux', 'apt-get', _bytes('34A34C0B4D8C3B68AC227572AE01CA10C9B52441'), ['-y', 'autoremove'], _setup("""
        apt-get install -y git
        apt-get remove -y git
    """)),
    # Exe(73, 'linux', 'true', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    Exe(73, 'linux', 'apt-get', _bytes('03F1F66EB04AD441218ED6C272B285FEEBE4542E'), ['install', '-y', 'build-essential'], ''),
    Exe(72, 'linux', 'useradd', _bytes('FCCC709E0D916CBA645394765E94CF0B41634622'), ['-d', '/home/user', '-m', '-s', '/bin/bash', 'user'], ''),
    Exe(72, 'linux', 'chown', _bytes('3585B10558809C769002DEB3EB0FF8C8C1C58CF9'), ['-R', 'user', '/code/'], _setup("""
        useradd user
        mkdir /code
    """)),
    # Exe(71, 'linux', 'export', _bytes('CE08F50207628141FC9B07DDBD85753FA17BD306'), ['DEBIAN_FRONTEND=noninteractive'], ''),
    Exe(69, 'linux', 'add-apt-repository', _bytes('755A6C877A2EDCFCA4D257F1AEED366C60052D4D'), ['-y', 'ppa:webupd8team/java'], _setup("""
        apt-get install -y software-properties-common
    """)),
    Exe(68, 'linux', 'rm', _bytes('CFBE001118866CA60C4AF1B80C9ED8AEB80A4ACE'), ['-rf', '/tmp/*'], _setup("""
        touch /tmp/file_0 /tmp/file_1
    """)),
    Exe(68, 'linux', 'echo', _bytes('91357DFDC4940F52416836444D6D8A0D5604E97C'), ['oracle-java8-installer', 'shared/accepted-oracle-license-v1-1', 'select', 'true'], ''),
    # Exe(66, 'linux', 'dpkg-reconfigure', _bytes('1A9D1F9B14201BECF05C1E9EBD95FDF541CFB30A'), ['locales'], ''),
    Exe(65, 'linux', 'apt-get', _bytes('6662CB7C365093893660AEB29B21092D288903C5'), ['install', '-y', 'python-pip'], ''),
    # Exe(65, 'linux', ' ', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    # Exe(65, 'linux', 'cd', _bytes('3DEC805774F2C65806B6D7A5F82199F349925290'), ['/opt'], ''),
    # Exe(65, 'linux', 'php5enmod', _bytes('049EF6ED42E4577B410786B6904C7C552D8B62E3'), ['mcrypt'], ''),
    # Exe(64, 'linux', 'apt-get', _bytes('44CFE74E304BAF8A1E07B0F7F65E75D0003EAE49'), ['autoremove'], ''),
    # Exe(64, 'linux', 'cd', _bytes('A48B9245A53860FE8CA2812A76E757614D32989C'), ['build'], ''),
    Exe(64, 'linux', 'rm', _bytes('BE7110137F6ED721D5FD39E46B54E8AB4645D4E4'), ['-fr', '/var/www/html'], _setup("""
        mkdir -p /var/www/html
    """)),
    # Exe(63, 'linux', './configure', _bytes('19016732C8E7C5B52C601834A7E0EB5617A9E1DB'), ['--enable-shared'], ''),
    Exe(62, 'linux', 'ln', _bytes('BD492B4EE3B69507FE309CC8EF41EB5DB4DD3E65'), ['-sf', '/bin/true', '/sbin/initctl'], ''),
    Exe(61, 'linux', 'mkdir', _bytes('A48B9245A53860FE8CA2812A76E757614D32989C'), ['build'], ''),
    Exe(60, 'linux', 'apt-get', _bytes('129FEC50E83767E3319756E973A2F73DE370D369'), ['install', '-y', 'nginx'], ''),
    Exe(60, 'linux', 'apt-get', _bytes('6C2DAC6AF88144B413B068A81D37810D045AB13C'), ['clean', '-y'], _setup("""
        apt-get install -y --download-only git
    """)),
    Exe(60, 'linux', 'ln', _bytes('9DF31B31A8795D0605B5A26D5A85D30179664C94'), ['-s', '/app', '/var/www/html'], _setup("""
        mkdir -p /var/www
    """)),
    # Exe(59, 'linux', '/usr/bin/debconf-set-selections', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    Exe(59, 'linux', 'apt-get', _bytes('A165577959DFFC981D34F62EB9C857AB9F2D5BCE'), ['-q', 'update'], _setup("""
        rm -rf /var/lib/apt/lists/*
    """)),
    # Exe(59, 'linux', 'pip3', _bytes('DF4476B62D09FC3408C432E4660F9980F930934A'), ['install', '-r', 'requirements.txt'], ''),
    Exe(58, 'linux', 'rm', _bytes('1C0B8412E67C460966301F078740DC803B219654'), ['/bin/sh'], ''),
    Exe(58, 'linux', 'curl', _bytes('550204E691AD9A933B890A5309870E4C2819DB9D'), ['-sL', 'https://deb.nodesource.com/setup'], _setup("""
        apt-get install -y curl
    """)),
    # Exe(58, 'linux', 'ln', _bytes('2FD907285E3CF9201C38BC32D533FA0E249E4499'), ['-s', '/bin/bash', '/bin/sh'], _setup("""
    #     rm /bin/sh
    # """)),
    Exe(57, 'linux', 'rm', _bytes('B3FE247391741433149A0C935B5A63372B851847'), ['/etc/nginx/sites-enabled/default'], _setup("""
        mkdir -p /etc/nginx/sites-enabled
        touch /etc/nginx/sites-enabled/default
    """)),
    # Exe(57, 'linux', 'cd', _bytes('A4C8F69597D0024765DF211895AAE23D5B000C96'), ['Python-2.7.9'], ''),
    Exe(57, 'linux', 'rm', _bytes('4BF86CEA813DB90D162B7EC189D95B069A044C05'), ['-rf', '/Python-2.7.9'], _setup("""
        mkdir /Python-2.7.9
    """)),
    Exe(57, 'linux', 'chmod', _bytes('8FE09C4675A5E0CCB5D64C306DAF798E33D4DCB0'), ['+x', '/usr/local/bin/gosu'], _setup("""
        touch /usr/local/bin/gosu
    """)),
    # Exe(56, 'linux', 'cd', _bytes('A601BA05FFE59D93CAAA97C181B1ABBFCF895DBD'), ['pip-7.0.1'], ''),
    Exe(56, 'linux', 'mv', _bytes('5E2107CC06D3CFF150ED413CB4986D5C6938AB67'), ['composer.phar', '/usr/local/bin/composer'], _setup("""
        touch composer.phar
        mkdir -p /usr/local/bin/composer
    """)),
    # Exe(56, 'linux', 'sh', _bytes('97D170E1550EEE4AFC0AF065B78CDA302A97674C'), [], ''),
    Exe(56, 'linux', 'rm', _bytes('945D85F0C3AF6C7A0F2E9176788F88FEA7F636F6'), ['-rf', 'pip-7.0.1'], _setup("""
        touch pip-7.0.1
    """)),
    Exe(56, 'linux', 'easy_install', _bytes('CCE1F49F70B561875A6A261B69175273263EC9DE'), ['pip'], _setup("""
        apt-get install -y python-setuptools curl
        echo '#!/usr/bin/env bash' > /usr/local/bin/easy_install
        echo '' >> /usr/local/bin/easy_install
        echo 'python /usr/lib/python2.7/dist-packages/easy_install.py "$@"' >> /usr/local/bin/easy_install
        chmod +x /usr/local/bin/easy_install
    """)),
    # Exe(55, 'linux', 'python', _bytes('983CFDF595BFB7C5CEB95AC69033C3C272DEA0D4'), ['-'], ''),
    Exe(54, 'linux', 'update-locale', _bytes('2D682EA1FD29FF384BCE4891A6C9C1FB32B55ABC'), ['LANG=en_US.UTF-8'], _setup("""
        apt-get install -y locales
        sed -E -i.bak -e 's/# (en_US.UTF-8 UTF-8)/\\1/' /etc/locale.gen 
        locale-gen
    """)),
    Exe(54, 'linux', 'echo', _bytes('166A5C62FDE5E94ED11E862578607E924A74CC09'), ['y'], ''),
    Exe(53, 'linux', 'curl', _bytes('3C4CC2A78C1A30301322839DAD35D92D3A2D6288'), ['-sL', 'https://deb.nodesource.com/setup_6.x'], _setup("""
        apt-get install -y curl
    """)),
    Exe(53, 'linux', 'gem', _bytes('FB3622CDF9BE972B478B01BC96CCB43BA1DE619E'), ['install', 'bundler'], _setup("""
        apt-get install -y ruby-full
    """)),
)
