]> git.decadent.org.uk Git - dak.git/blob - dak/init_dirs.py
import dak.lib.foo as foo for library modules.
[dak.git] / dak / init_dirs.py
1 #!/usr/bin/env python
2
3 # Initial setup of an archive
4 # Copyright (C) 2002, 2004, 2006  James Troup <james@nocrew.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21
22 import os, sys
23 import apt_pkg
24 import dak.lib.utils as utils
25
26 ################################################################################
27
28 Cnf = None
29 AptCnf = None
30
31 ################################################################################
32
33 def usage(exit_code=0):
34     print """Usage: dak init-dirs
35 Creates directories for an archive based on dak.conf configuration file.
36
37   -h, --help                show this help and exit."""
38     sys.exit(exit_code)
39
40 ################################################################################
41
42 def do_dir(target, config_name):
43     if os.path.exists(target):
44         if not os.path.isdir(target):
45             utils.fubar("%s (%s) is not a directory." % (target, config_name))
46     else:
47         print "Creating %s ..." % (target)
48         os.makedirs(target)
49
50 def process_file(config, config_name):
51     if config.has_key(config_name):
52         target = os.path.dirname(config[config_name])
53         do_dir(target, config_name)
54
55 def process_tree(config, tree):
56     for entry in config.SubTree(tree).List():
57         entry = entry.lower()
58         if tree == "Dir":
59             if entry in [ "poolroot", "queue" , "morguereject" ]:
60                 continue
61         config_name = "%s::%s" % (tree, entry)
62         target = config[config_name]
63         do_dir(target, config_name)
64
65 def process_morguesubdir(subdir):
66     config_name = "%s::MorgueSubDir" % (subdir)
67     if Cnf.has_key(config_name):
68         target = os.path.join(Cnf["Dir::Morgue"], Cnf[config_name])
69         do_dir(target, config_name)
70
71 ######################################################################
72
73 def create_directories():
74     # Process directories from apt.conf
75     process_tree(Cnf, "Dir")
76     process_tree(Cnf, "Dir::Queue")
77     for file in [ "Dinstall::LockFile", "Rm::LogFile", "Import-Archive::ExportDir" ]:
78         process_file(Cnf, file)
79     for subdir in [ "Clean-Queues", "Clean-Suites" ]:
80         process_morguesubdir(subdir)
81
82     # Process directories from apt.conf
83     process_tree(AptCnf, "Dir")
84     for tree in AptCnf.SubTree("Tree").List():
85         config_name = "Tree::%s" % (tree)
86         tree_dir = os.path.join(Cnf["Dir::Root"], tree)
87         do_dir(tree_dir, tree)
88         for file in [ "FileList", "SourceFileList" ]:
89             process_file(AptCnf, "%s::%s" % (config_name, file))
90         for component in AptCnf["%s::Sections" % (config_name)].split():
91             for architecture in AptCnf["%s::Architectures" % (config_name)].split():
92                 if architecture != "source":
93                     architecture = "binary-"+architecture
94                 target = os.path.join(tree_dir,component,architecture)
95                 do_dir(target, "%s, %s, %s" % (tree, component, architecture))
96
97
98 ################################################################################
99
100 def main ():
101     global AptCnf, Cnf, projectB
102
103     Cnf = utils.get_conf()
104     Arguments = [('h',"help","Init-Dirs::Options::Help")]
105     for i in [ "help" ]:
106         if not Cnf.has_key("Init-Dirs::Options::%s" % (i)):
107             Cnf["Init-Dirs::Options::%s" % (i)] = ""
108
109     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
110
111     Options = Cnf.SubTree("Init-Dirs::Options")
112     if Options["Help"]:
113         usage()
114
115     AptCnf = apt_pkg.newConfiguration()
116     apt_pkg.ReadConfigFileISC(AptCnf,utils.which_apt_conf_file())
117
118     create_directories()
119
120 ################################################################################
121
122 if __name__ == '__main__':
123     main()
124