def preparePackage()

in publish-scripts/ubuntu/buildDEB.py [0:0]


def preparePackage():
    """
    Prepares and builds a Debian package.
    This includes setting up directories, copying necessary files,
    generating SHA256 hashes, and building the final .deb package.
    """
    os.chdir(constants.DRIVERROOTDIR)

    debianVersion = returnDebVersion(constants.VERSION)
    packageFolder = f"{constants.PACKAGENAME}_{debianVersion}"
    buildFolder = os.path.join(os.getcwd(), constants.BUILDFOLDER, packageFolder)
    helper.linuxOutput(buildFolder)

    os.chdir(buildFolder)
    document = os.path.join("usr", "share", "doc", constants.PACKAGENAME)
    os.makedirs(document)

    # Copy MIT copyright file
    print("include MIT copyright")
    scriptDir = os.path.abspath(os.path.dirname(__file__))
    shutil.copyfile(os.path.join(scriptDir, "copyright"), os.path.join(document, "copyright"))

    # Generate changelog file from template
    with open(os.path.join(scriptDir, "changelog_template")) as f:
        stringData = f.read() # read until EOF
    t = Template(stringData)

    # datetime example: Tue, 06 April 2018 16:32:31
    time = datetime.datetime.utcnow().strftime("%a, %d %b %Y %X")
    with open(os.path.join(document, "changelog.Debian"), "w") as f:
        print(f"writing changelog with date utc: {time}")
        f.write(t.safe_substitute(DEBIANVERSION=debianVersion, DATETIME=time, VERSION=constants.VERSION, PACKAGENAME=constants.PACKAGENAME))

    # Compress changelog using gzip (by default gzip compress file in place)
    helper.printReturnOutput(["gzip", "-9", "-n", os.path.join(document, "changelog.Debian")])
    helper.chmodFolderAndFiles(os.path.join("usr", "share"))

    debian = "DEBIAN"
    os.makedirs(debian)

    # Generate SHA256 hashes for all files in 'usr/'
    print("trying to produce sha256 hashes")
    with open('DEBIAN/sha256sums', 'w') as sha256file:
        # iterate over all files under 'usr/' & get their sha256sum
        for dirpath, _, filenames in os.walk('usr'):
            for f in filenames:
                filepath = os.path.join(dirpath, f)
                if not os.path.islink(filepath):
                    h = helper.produceHashForfile(filepath, 'sha256', Upper=False)
                    sha256file.write(f"{h}  {filepath}\n")

    # Generate the control file with package dependencies from template
    deps = []
    for key, value in constants.LINUXDEPS.items():
        entry = f"{key} ({value})"
        deps.append(entry)
    deps = ','.join(deps)
    with open(os.path.join(scriptDir, "control_template")) as f:
        stringData = f.read()
    t = Template(stringData)
    with open(os.path.join(debian, "control"), "w") as f:
        print("trying to write control file")
        f.write(t.safe_substitute(DEBIANVERSION=debianVersion, PACKAGENAME=constants.PACKAGENAME, DEPENDENCY=deps))
    helper.chmodFolderAndFiles(debian)

    # Generate post-install script
    postinst = ''
    with open(os.path.join(scriptDir, "postinst_template")) as f:
        postinst = f.read()
    with open(os.path.join(debian, "postinst"), "w") as f:
        print("trying to write postinst file")
        f.write(postinst)

    # Ensure post-install script has correct permissions
    # postinstall has to be 0755 in order for it to work.
    os.chmod(os.path.join(debian, "postinst"), 0o755)

    # Build the Debian package using dpkg-deb
    os.chdir(constants.DRIVERROOTDIR)
    output = helper.printReturnOutput(["fakeroot", "dpkg-deb", "--build", "-Zxz",
                   os.path.join(constants.BUILDFOLDER, packageFolder), os.path.join(constants.ARTIFACTFOLDER, packageFolder+".deb")])
    assert(f"building package '{constants.PACKAGENAME}'" in output)