2 # Copyright 1999-2013 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
6 # archive-conf -- save off a config file in the dispatch-conf archive dir
8 # Written by Wayne Davison <gentoo@blorf.net> with code snagged from
9 # Jeremy Wohl's dispatch-conf script and the portage chkcontents script.
12 from __future__ import print_function
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)
20 portage._internal_caller = True
22 import portage.dispatch_conf
23 from portage import os
25 FIND_EXTANT_CONTENTS = "find %s -name CONTENTS"
27 MANDATORY_OPTS = [ 'archive-dir' ]
31 def perform_checksum(filename): return fchksum.fmd5t(filename)
34 def md5_to_hex(md5sum):
36 for ix in range(len(md5sum)):
37 hexform = hexform + "%02x" % ord(md5sum[ix])
38 return hexform.lower()
40 def perform_checksum(filename):
41 f = open(filename, 'rb')
43 data = f.read(blocksize)
48 size = size + len(data)
49 data = f.read(blocksize)
50 return (md5_to_hex(checksum.digest()), size)
57 options = portage.dispatch_conf.read_config(MANDATORY_OPTS)
59 for conf in sys.argv[1:]:
60 if not os.path.isabs(conf):
61 conf = os.path.abspath(conf)
63 md5_match_hash[conf] = ''
65 # Find all the CONTENT files in VDB_PATH.
66 with os.popen(FIND_EXTANT_CONTENTS % (os.path.join(portage.settings['EROOT'], portage.VDB_PATH))) as f:
67 content_files += f.readlines()
69 # Search for the saved md5 checksum of all the specified config files
70 # and see if the current file is unmodified or not.
73 for filename in content_files:
74 filename = filename.rstrip()
76 contents = open(filename, "r")
78 print('archive-conf: Unable to open %s: %s' % (filename, e), file=sys.stderr)
80 lines = contents.readlines()
86 stored = items[2].lower()
87 real = perform_checksum(conf)[0].lower()
89 md5_match_hash[conf] = conf
97 archive = os.path.join(options['archive-dir'], conf.lstrip('/'))
98 if options['use-rcs'] == 'yes':
99 portage.dispatch_conf.rcs_archive(archive, conf, md5_match_hash[conf], '')
100 if md5_match_hash[conf]:
101 portage.dispatch_conf.rcs_archive_post_process(archive)
103 portage.dispatch_conf.file_archive(archive, conf, md5_match_hash[conf], '')
104 if md5_match_hash[conf]:
105 portage.dispatch_conf.file_archive_post_process(archive)
108 if len(sys.argv) > 1:
111 print('Usage: archive-conf /CONFIG/FILE [/CONFIG/FILE...]', file=sys.stderr)