/bin/make-apt-cdrom.py

https://repo.or.cz/camarabuntu.git
Python | 114 lines | 77 code | 28 blank | 9 comment | 13 complexity | 1d445585fcce8f3bb38f90cc8295772a MD5 | raw file
  1. #! /usr/bin/python
  2. from optparse import OptionParser
  3. import os, shutil, tempfile, commands, glob, sys, re, subprocess, shutil, time
  4. from apt import Package
  5. parser = OptionParser()
  6. parser.add_option( "-i", "--iso-file",
  7. dest="isofilename", default=None,
  8. help="The filename of the .iso that'll be made" )
  9. parser.add_option( "-n", "--cd-name",
  10. dest="name", default=None,
  11. help="The name of the CD" )
  12. parser.add_option( "-d", "--deb-dir",
  13. dest="debdir", default=None,
  14. help="The directory that has all the debs" )
  15. parser.add_option( "-D", "--dist",
  16. dest="dist", default=None, type="choice",
  17. choices=['warty', 'hoary', 'breezy', 'dapper', 'edgy', 'feisty', 'gutsy', 'hardy', 'intrepid',
  18. '4.10', '7.04', '7.10', '6.10', '6.06', '5.10', '8.04', '5.04', '8.10'],
  19. help="The ubuntu distrobution we're using" )
  20. parser.add_option( "-k", "--gpg-key",
  21. dest="gpgkey", default=None, type="string",
  22. help="The GPG key used to sign the packages" )
  23. (options, unneeded) = parser.parse_args()
  24. assert options.isofilename is not None, "You must specify the filename for the resultant ISO file with --iso-file/-i"
  25. assert options.name is not None, "You must provide a name for the CD with --name/-n"
  26. assert options.debdir is not None, "You must provide a directory that contains all the debs using --deb-dir/-d"
  27. assert options.gpgkey is not None, "You must provide the GPG key to sign the packages using --gpg-key/-k"
  28. debdir = os.path.abspath( options.debdir )
  29. # this is the CD we work in and will have be the cd image layout
  30. apt_cdrom_dir = tempfile.mkdtemp( prefix="make-apt-cdrom-cdrom-", dir=os.getcwd() )
  31. dist_version_to_name = {'4.10': 'warty', '7.04': 'feisty', '7.10': 'gutsy', '6.10': 'edgy', '6.06': 'dapper', '5.10': 'breezy', '8.04': 'hardy', '5.04': 'hoary', '8.10': 'intrepid'}
  32. if options.dist in dist_version_to_name:
  33. dist = dist_version_to_name[options.dist]
  34. else:
  35. dist = options.dist
  36. for dir in ["pool", "pool/main", "dists", "dists/"+dist, "dists/"+dist+"/main", "dists/"+dist+"/main/binary-i386"]:
  37. os.mkdir( os.path.join( apt_cdrom_dir, dir ) )
  38. for package, filename in [(Package(filename=filename), filename) for filename in glob.glob( os.path.join( debdir, "*.deb" ) )]:
  39. print "Found deb %s" % package
  40. if package.name[0:3] == 'lib':
  41. dir_name = package.name[0:4]
  42. else:
  43. dir_name = package.name[0]
  44. dir_name = os.path.join( apt_cdrom_dir, "pool", "main", dir_name, package.name )
  45. if not os.path.isdir( dir_name ):
  46. os.makedirs( dir_name )
  47. shutil.copy( filename, dir_name )
  48. # remake the packages file
  49. status, output = commands.getstatusoutput( "apt-ftparchive packages \"%(aptdir)s/pool/main/\" | gzip -9c > \"%(aptdir)s/dists/%(dist)s/main/binary-i386/Packages.gz\"" % {'aptdir':apt_cdrom_dir, 'dist':dist} )
  50. if status != 0:
  51. print output
  52. #assert status == 0
  53. # release file:
  54. config_file, config_filename = tempfile.mkstemp( prefix="make-apt-cdrom-release-conf-", dir=os.getcwd() )
  55. # TODO remove tha 'apt-move' stuff. What else should it be called
  56. open( config_filename, "w" ).write("""APT::FTPArchive::Release {
  57. Origin "APT-Move";
  58. Label "APT-Move";
  59. Suite "%(dist)s";
  60. Codename "%(dist)s";
  61. Architectures "i386";
  62. Components "main";
  63. Description "%(name)s";
  64. };""" % {'name':options.name, 'dist':dist} )
  65. status, output = commands.getstatusoutput( "apt-ftparchive -c \"%(config_filename)s\" release %(aptdir)s/dists/%(dist)s/" % {'config_filename':config_filename, 'aptdir':apt_cdrom_dir, 'dist':dist } )
  66. if status != 0:
  67. print output
  68. assert status == 0
  69. open( os.path.join( apt_cdrom_dir, 'dists', dist, 'Release'), 'w').write( output )
  70. status,output = commands.getstatusoutput( "gpg -ba --default-key=%(gpg_key)s -o \"%(aptdir)s/dists/%(dist)s/Release.gpg\" \"%(aptdir)s/dists/%(dist)s/Release\"" % {'gpg_key':options.gpgkey, 'aptdir':apt_cdrom_dir, 'dist':dist } )
  71. if status != 0:
  72. print output
  73. # Add the .disk directory to tell ubuntu about this
  74. os.mkdir( os.path.join( apt_cdrom_dir, ".disk" ) )
  75. open( os.path.join( apt_cdrom_dir, '.disk', 'info' ), 'w' ).write( options.name )
  76. # Add the signature to the disk
  77. status, output = commands.getstatusoutput( "gpg --export -a \"%s\" > \"%s\"" % (options.gpgkey, os.path.join( apt_cdrom_dir, "signature.key" ) ) )
  78. if status != 0:
  79. print output
  80. assert status == 0
  81. # make the cd iso
  82. status, output = commands.getstatusoutput( "mkisofs -r -A \"%(cd_name)s\" -o \"%(iso_filename)s\" \"%(aptdir)s\" " % {'cd_name':options.name, 'iso_filename':options.isofilename, 'aptdir':apt_cdrom_dir } )
  83. if status != 0:
  84. print output
  85. assert status == 0
  86. print "Created the ISO"