Enable BytesWarnings.
[portage.git] / bin / archive-conf
1 #!/usr/bin/python -bb
2 # Copyright 1999-2014 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 #
6 # archive-conf -- save off a config file in the dispatch-conf archive dir
7 #
8 #  Written by Wayne Davison <gentoo@blorf.net> with code snagged from
9 #  Jeremy Wohl's dispatch-conf script and the portage chkcontents script.
10 #
11
12 from __future__ import print_function
13
14 import sys
15
16 from os import path as osp
17 pym_path = osp.join(osp.dirname(osp.dirname(osp.realpath(__file__))), "pym")
18 sys.path.insert(0, pym_path)
19 import portage
20 portage._internal_caller = True
21
22 import portage.dispatch_conf
23 from portage import os
24 from portage.checksum import perform_md5
25
26 FIND_EXTANT_CONTENTS  = "find %s -name CONTENTS"
27
28 MANDATORY_OPTS  = [ 'archive-dir' ]
29
30 def archive_conf():
31     args = []
32     content_files = []
33     md5_match_hash = {}
34
35     options = portage.dispatch_conf.read_config(MANDATORY_OPTS)
36
37     for conf in sys.argv[1:]:
38         if not os.path.isabs(conf):
39             conf = os.path.abspath(conf)
40         args += [ conf ]
41         md5_match_hash[conf] = ''
42
43     # Find all the CONTENT files in VDB_PATH.
44     with os.popen(FIND_EXTANT_CONTENTS % (os.path.join(portage.settings['EROOT'], portage.VDB_PATH))) as f:
45             content_files += f.readlines()
46
47     # Search for the saved md5 checksum of all the specified config files
48     # and see if the current file is unmodified or not.
49     try:
50         todo_cnt = len(args)
51         for filename in content_files:
52             filename = filename.rstrip()
53             try:
54                 contents = open(filename, "r")
55             except IOError as e:
56                 print('archive-conf: Unable to open %s: %s' % (filename, e), file=sys.stderr)
57                 sys.exit(1)
58             lines = contents.readlines()
59             for line in lines:
60                 items = line.split()
61                 if items[0] == 'obj':
62                     for conf in args:
63                         if items[1] == conf:
64                             stored = items[2].lower()
65                             real = perform_md5(conf).lower()
66                             if stored == real:
67                                 md5_match_hash[conf] = conf
68                             todo_cnt -= 1
69                             if todo_cnt == 0:
70                                 raise StopIteration()
71     except StopIteration:
72         pass
73
74     for conf in args:
75         archive = os.path.join(options['archive-dir'], conf.lstrip('/'))
76         if options['use-rcs'] == 'yes':
77             portage.dispatch_conf.rcs_archive(archive, conf, md5_match_hash[conf], '')
78             if md5_match_hash[conf]:
79                 portage.dispatch_conf.rcs_archive_post_process(archive)
80         else:
81             portage.dispatch_conf.file_archive(archive, conf, md5_match_hash[conf], '')
82             if md5_match_hash[conf]:
83                 portage.dispatch_conf.file_archive_post_process(archive)
84
85 # run
86 if len(sys.argv) > 1:
87     archive_conf()
88 else:
89     print('Usage: archive-conf /CONFIG/FILE [/CONFIG/FILE...]', file=sys.stderr)