Skip to content

Commit

Permalink
Use dpkg-deb instead of ar
Browse files Browse the repository at this point in the history
After having a read through of the official Debian package format
(see man deb(5)) it seems there are a couple of nuances to how the
archive is created. We should probably be using the official tool,
dpkg-deb, to manipulate packages to ensure that we create it
correctly.
  • Loading branch information
oskirby committed Sep 5, 2024
1 parent 7424bb3 commit 27ae26d
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions taskcluster/scripts/build/dpkg-repack.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def parse_control(filename):
if restart.match(line):
section, text = line.split(':', 1)
section = convert_case(section)
contents[section] = text
contents[section] = text.lstrip()
elif section is not None:
contents[section] = contents[section] + '\n' + line

Expand Down Expand Up @@ -70,17 +70,11 @@ def write_control(contents, file=sys.stdout):
with tempfile.TemporaryDirectory() as tempdir:
# Unpack the Debian package.
archivedir = os.path.join(tempdir, 'archive')
os.mkdir(archivedir)
subprocess.check_call(['ar', 'x', f'--output={archivedir}', args.filename])

# Unpack the control files.
# TODO: Support other compression algorithms besides gzip?
controldir = os.path.join(tempdir, 'control')
os.mkdir(controldir)
subprocess.check_call(['tar', '-C', controldir, '-xf', os.path.join(archivedir, 'control.tar.gz')])
subprocess.check_call(['dpkg-deb', '-R', args.filename, archivedir])

# Parse the control file
contents = parse_control(os.path.join(controldir, 'control'))
controlfile = os.path.join(archivedir, 'DEBIAN', 'control')
contents = parse_control(controlfile)

# Set/update extra values in the control file
for keyval in args.set:
Expand All @@ -90,10 +84,9 @@ def write_control(contents, file=sys.stdout):
# Ensure the 'Description' always goes last and then write the updated control file.
contents.move_to_end('Description')
write_control(contents, file=sys.stderr)
with open(os.path.join(controldir, 'control'), 'w') as fp:
with open(controlfile, 'w') as fp:
write_control(contents, file=fp)

# Repack the Debian package files.
print(f"Writing updated package to {os.path.abspath(args.output)}", file=sys.stderr)
subprocess.check_call(['tar', '-C', controldir, '-cf', os.path.join(archivedir, 'control.tar.gz')] + os.listdir(controldir))
subprocess.check_call(['ar', 'ru', os.path.abspath(args.output)] + os.listdir(archivedir), cwd=archivedir)
subprocess.check_call(['dpkg-deb', '-Zgzip', '-b', archivedir, args.output])

0 comments on commit 27ae26d

Please sign in to comment.