Debian Codegraph @

build/download.py

raw symbols SCIP
1 #!/usr/bin/python3
2
3 # Script to download the Ada Reference Manual and its formatting tool.
4
5 # Copyright (c) 2010, 2012 Stephen Leake <[email protected]>
6 # Copyright (c) 2013 Nicolas Boulenguez <[email protected]>
7
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 import os.path
23 import re
24 import urllib.request
25 import sys
26
27 USE_EXISTING_FILES = True
28
29 ######################################################################
30 class CVSWeb ():
31
32 def __init__ (self, hostname, top_directory):
33 # Instantiation requires distant root.
34
35 self._host = 'http://' + hostname
36 self._cgi = '/cgi-bin/cvsweb.cgi/' + top_directory + '/'
37
38 def files_in_directory (self, path):
39 # List text files in the head revision of a directory.
40
41 # I did not find a way to list a tagged revision, except a
42 # direct tag list for each file.
43
44 cgi = self._cgi + str.lower (path) + '/'
45 with urllib.request.urlopen (self._host + cgi) as f:
46 contents = f.read ()
47 # Refuse directory names (with a trailing slash).
48 # Ignore case of the displayed file name.
49 pattern = '<a href="' + re.escape (cgi) + '([^"/]+)">(\\1)</a><br>'
50 matches = re.finditer (str.encode (pattern), contents, re.IGNORECASE)
51 return (bytes.decode (m.group (2)) for m in matches)
52
53 def download_file (self, path,
54 tag = None): # None means the head revision.
55 # Return the contents of the tagged revision of path, as bytes.
56 # If tag is provided, but no revision of this file carries it,
57 # None is returned.
58
59 cgi = self._cgi + str.lower (path)
60 if tag != None:
61 with urllib.request.urlopen (self._host + cgi) as f:
62 contents = f.read ()
63 pattern = '<a href="' + re.escape (cgi) \
64 + '\?rev=([0-9.]+)">\\1</a></b></span><br>\r\n' \
65 + 'Modified <i>[^<]*</i>\r\n' \
66 + 'by <i>[^<]*</i>( with line changes <i>[^<]*</i>)?<br>\r\n' \
67 + 'CVS Tags: <b>[^<]*' + re.escape (tag) + '[^<]*</b><br>'
68 match = re.search (str.encode (pattern), contents)
69 if not match:
70 return None
71 rev = bytes.decode (match.group (1))
72 cgi += '?rev=' + rev
73 print ('{:<30} : {}'.format (path, rev))
74
75 else:
76 cgi += '?rev=HEAD'
77 with urllib.request.urlopen (self._host + cgi) as f:
78 contents = f.read ()
79 return contents
80
81 ######################################################################
82 def download_subdir (cvsweb, subdir,
83 tag = None, # None means the head revision.
84 rename_lowercase = False,
85 strip_carriage_returns = False):
86
87 def fmt (a, b):
88 print ('{:<30} : {}'.format (a, b))
89
90 fmt ('Subdirectory', subdir)
91 if tag:
92 fmt ('Revision', tag)
93 else:
94 fmt ('Revision', 'latest')
95 fmt ('Renaming files to lowercase', rename_lowercase)
96 fmt ('Stripping carriage returns', strip_carriage_returns)
97
98 try:
99 os.mkdir (subdir) # Fails if the directory exists.
100 except FileExistsError:
101 if not USE_EXISTING_FILES:
102 raise
103
104 failed_count = 0
105 no_such_tag_count = 0
106
107 for basename in cvsweb.files_in_directory (subdir):
108 src = subdir + '/' + basename
109 if rename_lowercase:
110 basename = str.lower (basename)
111 dst = os.path.join (subdir, basename) # Local path join.
112 if USE_EXISTING_FILES and os.path.exists (dst):
113 fmt (dst, 'using existing file')
114 continue
115 try:
116 contents = cvsweb.download_file (src, tag)
117 if not contents:
118 fmt (dst, 'no such tag')
119 no_such_tag_count = no_such_tag_count + 1
120 continue
121 if strip_carriage_returns:
122 contents = re.sub (b'\r\n', b'\n', contents)
123 with open (dst, 'bw') as o:
124 o.write (contents)
125 fmt (dst, 'downloaded')
126 except urllib.error.URLError:
127 fmt (dst, 'download failed')
128 failed_count = failed_count + 1
129
130 fmt ('no such tag :', no_such_tag_count)
131 fmt ('failed downloads:', failed_count)
132
133
134 ######################################################################
135
136 cvsweb = CVSWeb (hostname = 'www.ada-auth.org',
137 top_directory = 'arm')
138 tags = { '1995':'Final_TC1',
139 '2005':'Amend_Final',
140 '2012':'Ada2012_TC1',
141 '2020':'Ada202x_D25' }
142
143 if len (sys.argv) == 2 and sys.argv [1] == 'progs':
144 download_subdir (cvsweb, 'progs',
145 rename_lowercase = True)
146 print ("""
147 No certification or signature used for downloaded code sources.
148 Hint: diff -rN --ignore-file-name-case --strip-trailing-cr old/ progs/
149 """)
150
151 elif len (sys.argv) == 2 and sys.argv [1] in tags:
152 download_subdir (cvsweb, 'source',
153 tag = tags [sys.argv [1]],
154 rename_lowercase = True,
155 strip_carriage_returns = True)
156
157 elif len (sys.argv) == 2 and sys.argv [1] == 'HEAD':
158 download_subdir (cvsweb, 'source',
159 tag = 'HEAD',
160 rename_lowercase = True,
161 strip_carriage_returns = True)
162
163 else:
164 print ('Usage: {} [progs | {}]'.format (
165 sys.argv [0],
166 str.join (' | ', sorted ([str (year) for year in tags.keys ()]))))