init_dirs.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #! /usr/bin/env python3
  2. """Initial setup of an archive."""
  3. # Copyright (C) 2002, 2004, 2006 James Troup <james@nocrew.org>
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. ################################################################################
  16. import os
  17. import sys
  18. import apt_pkg
  19. from daklib import utils
  20. from daklib.dbconn import *
  21. ################################################################################
  22. Cnf = None
  23. ################################################################################
  24. def usage(exit_code=0):
  25. """Print a usage message and exit with 'exit_code'."""
  26. print("""Usage: dak init-dirs
  27. Creates directories for an archive based on dak.conf configuration file.
  28. -h, --help show this help and exit.""")
  29. sys.exit(exit_code)
  30. ################################################################################
  31. def do_dir(target, config_name):
  32. """If 'target' exists, make sure it is a directory. If it doesn't, create
  33. it."""
  34. if os.path.exists(target):
  35. if not os.path.isdir(target):
  36. utils.fubar("%s (%s) is not a directory."
  37. % (target, config_name))
  38. else:
  39. print("Creating {} ...".format(target))
  40. os.makedirs(target)
  41. def process_file(config, config_name):
  42. """Create directories for a config entry that's a filename."""
  43. if config_name in config:
  44. target = os.path.dirname(config[config_name])
  45. do_dir(target, config_name)
  46. def process_tree(config, tree):
  47. """Create directories for a config tree."""
  48. for entry in config.subtree(tree).list():
  49. entry = entry.lower()
  50. config_name = "%s::%s" % (tree, entry)
  51. target = config[config_name]
  52. do_dir(target, config_name)
  53. def process_morguesubdir(subdir):
  54. """Create directories for morgue sub directories."""
  55. config_name = "%s::MorgueSubDir" % (subdir)
  56. if config_name in Cnf:
  57. target = os.path.join(Cnf["Dir::Morgue"], Cnf[config_name])
  58. do_dir(target, config_name)
  59. def process_keyring(fullpath, secret=False):
  60. """Create empty keyring if necessary."""
  61. if os.path.exists(fullpath):
  62. return
  63. keydir = os.path.dirname(fullpath)
  64. if not os.path.isdir(keydir):
  65. print("Creating {} ...".format(keydir))
  66. os.makedirs(keydir)
  67. if secret:
  68. # Make sure secret keyring directories are 0700
  69. os.chmod(keydir, 0o700)
  70. # Touch the file
  71. print("Creating {} ...".format(fullpath))
  72. with open(fullpath, 'w') as fh:
  73. if secret:
  74. os.fchmod(fh.fileno(), 0o600)
  75. else:
  76. os.fchmod(fh.fileno(), 0o644)
  77. ######################################################################
  78. def create_directories():
  79. """Create directories referenced in dak.conf."""
  80. session = DBConn().session()
  81. # Process directories from dak.conf
  82. process_tree(Cnf, "Dir")
  83. # Hardcode creation of the unchecked directory
  84. if "Dir::Base" in Cnf:
  85. do_dir(os.path.join(Cnf["Dir::Base"], "queue", "unchecked"), 'unchecked directory')
  86. # Process queue directories
  87. for queue in session.query(PolicyQueue):
  88. do_dir(queue.path, '%s queue' % queue.queue_name)
  89. # If we're doing the NEW queue, make sure it has a COMMENTS directory
  90. if queue.queue_name == 'new':
  91. do_dir(os.path.join(queue.path, "COMMENTS"), '%s queue comments' % queue.queue_name)
  92. for config_name in ["Rm::LogFile",
  93. "Import-Archive::ExportDir"]:
  94. process_file(Cnf, config_name)
  95. for subdir in ["Clean-Queues", "Clean-Suites"]:
  96. process_morguesubdir(subdir)
  97. suite_suffix = "%s" % (Cnf.find("Dinstall::SuiteSuffix"))
  98. # Process secret keyrings
  99. if 'Dinstall::SigningKeyring' in Cnf:
  100. process_keyring(Cnf['Dinstall::SigningKeyring'], secret=True)
  101. if 'Dinstall::SigningPubKeyring' in Cnf:
  102. process_keyring(Cnf['Dinstall::SigningPubKeyring'], secret=True)
  103. # Process public keyrings
  104. for keyring in session.query(Keyring).filter_by(active=True):
  105. process_keyring(keyring.keyring_name)
  106. # Process dists directories
  107. # TODO: Store location of each suite in database
  108. for suite in session.query(Suite):
  109. suite_dir = os.path.join(suite.archive.path, 'dists', suite.suite_name, suite_suffix)
  110. # TODO: Store valid suite/component mappings in database
  111. for component in session.query(Component):
  112. component_name = component.component_name
  113. sc_dir = os.path.join(suite_dir, component_name)
  114. do_dir(sc_dir, "%s/%s" % (suite.suite_name, component_name))
  115. for arch in suite.architectures:
  116. if arch.arch_string == 'source':
  117. arch_string = 'source'
  118. else:
  119. arch_string = 'binary-%s' % arch.arch_string
  120. suite_arch_dir = os.path.join(sc_dir, arch_string)
  121. do_dir(suite_arch_dir, "%s/%s/%s" % (suite.suite_name, component_name, arch_string))
  122. ################################################################################
  123. def main():
  124. """Initial setup of an archive."""
  125. global Cnf
  126. Cnf = utils.get_conf()
  127. arguments = [('h', "help", "Init-Dirs::Options::Help")]
  128. for i in ["help"]:
  129. key = "Init-Dirs::Options::%s" % i
  130. if key not in Cnf:
  131. Cnf[key] = ""
  132. d = DBConn()
  133. arguments = apt_pkg.parse_commandline(Cnf, arguments, sys.argv)
  134. options = Cnf.subtree("Init-Dirs::Options")
  135. if options["Help"]:
  136. usage()
  137. elif arguments:
  138. utils.warn("dak init-dirs takes no arguments.")
  139. usage(exit_code=1)
  140. create_directories()
  141. ################################################################################
  142. if __name__ == '__main__':
  143. main()