Add anoncvs gentoo-x86 example in documentation of repos.conf.
[portage.git] / bin / binhost-snapshot
1 #!/usr/bin/python
2 # Copyright 2010-2013 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 import io
6 import optparse
7 import os
8 import sys
9 import textwrap
10
11 try:
12         from urllib.parse import urlparse
13 except ImportError:
14         from urlparse import urlparse
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 def parse_args(argv):
23         prog_name = os.path.basename(argv[0])
24         usage = prog_name + ' [options] ' + \
25                 '<src_pkg_dir> <snapshot_dir> <snapshot_uri> <binhost_dir>'
26
27         prog_desc = "This program will copy src_pkg_dir to snapshot_dir " + \
28                 "and inside binhost_dir it will create a Packages index file " + \
29                 "which refers to snapshot_uri. This is intended to solve race " + \
30                 "conditions on binhosts as described at http://crosbug.com/3225."
31
32         usage += "\n\n"
33         for line in textwrap.wrap(prog_desc, 70):
34                 usage += line + "\n" 
35
36         usage += "\n"
37         usage += "Required Arguments:\n\n"
38         usage += "  src_pkg_dir  - the source $PKGDIR\n"
39         usage += "  snapshot_dir - destination snapshot " + \
40                 "directory (must not exist)\n"
41         usage += "  snapshot_uri - URI which refers to " + \
42                 "snapshot_dir from the\n" + \
43                 "                 client side\n"
44         usage += "  binhost_dir  - directory in which to " + \
45                 "write Packages index with\n" + \
46                 "                 snapshot_uri"
47
48         parser = optparse.OptionParser(usage=usage)
49         parser.add_option('--hardlinks', help='create hardlinks (y or n, default is y)',
50                 choices=('y', 'n'))
51         parser.set_defaults(hardlinks='y')
52         options, args = parser.parse_args(argv[1:])
53
54         if len(args) != 4:
55                 parser.error("Required 4 arguments, got %d" % (len(args),))
56
57         return parser, options, args
58
59 def main(argv):
60         parser, options, args = parse_args(argv)
61
62         src_pkg_dir, snapshot_dir, snapshot_uri, binhost_dir = args
63         src_pkgs_index = os.path.join(src_pkg_dir, 'Packages')
64
65         if not os.path.isdir(src_pkg_dir):
66                 parser.error("src_pkg_dir is not a directory: '%s'" % (src_pkg_dir,))
67
68         if not os.path.isfile(src_pkgs_index):
69                 parser.error("src_pkg_dir does not contain a " + \
70                         "'Packages' index: '%s'" % (src_pkg_dir,))
71
72         parse_result = urlparse(snapshot_uri)
73         if not (parse_result.scheme and parse_result.netloc and parse_result.path):
74                 parser.error("snapshot_uri is not a valid URI: '%s'" % (snapshot_uri,))
75
76         if os.path.isdir(snapshot_dir):
77                 parser.error("snapshot_dir already exists: '%s'" % snapshot_dir)
78
79         try:
80                 os.makedirs(os.path.dirname(snapshot_dir))
81         except OSError:
82                 pass
83         if not os.path.isdir(os.path.dirname(snapshot_dir)):
84                 parser.error("snapshot_dir parent could not be created: '%s'" % \
85                         os.path.dirname(snapshot_dir))
86
87         try:
88                 os.makedirs(binhost_dir)
89         except OSError:
90                 pass
91         if not os.path.isdir(binhost_dir):
92                 parser.error("binhost_dir could not be created: '%s'" % binhost_dir)
93
94         cp_opts = 'RP'
95         if options.hardlinks == 'n':
96                 cp_opts += 'p'
97         else:
98                 cp_opts += 'l'
99
100         cp_cmd = 'cp -%s %s %s' % (
101                 cp_opts,
102                 portage._shell_quote(src_pkg_dir),
103                 portage._shell_quote(snapshot_dir)
104         )
105
106         ret = os.system(cp_cmd)
107         if not (os.WIFEXITED(ret) and os.WEXITSTATUS(ret) == os.EX_OK):
108                 return 1
109
110         infile = io.open(portage._unicode_encode(src_pkgs_index,
111                 encoding=portage._encodings['fs'], errors='strict'),
112                 mode='r', encoding=portage._encodings['repo.content'],
113                 errors='strict')
114
115         outfile = portage.util.atomic_ofstream(
116                 os.path.join(binhost_dir, "Packages"),
117                 encoding=portage._encodings['repo.content'],
118                 errors='strict')
119
120         for line in infile:
121                 if line[:4] == 'URI:':
122                         # skip existing URI line
123                         pass
124                 else:
125                         if not line.strip():
126                                 # end of header
127                                 outfile.write("URI: %s\n\n" % snapshot_uri)
128                                 break
129                         outfile.write(line)
130
131         for line in infile:
132                 outfile.write(line)
133
134         infile.close()
135         outfile.close()
136
137         return os.EX_OK
138
139 if __name__ == "__main__":
140         sys.exit(main(sys.argv))