| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 34 |
|
| 35 |
self._host = 'http://' + hostname |
| 36 |
self._cgi = '/cgi-bin/cvsweb.cgi/' + top_directory + '/' |
| 37 |
|
| 38 |
def files_in_directory (self, path): |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
cgi = self._cgi + str.lower (path) + '/' |
| 45 |
with urllib.request.urlopen (self._host + cgi) as f: |
| 46 |
contents = f.read () |
| 47 |
|
| 48 |
|
| 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): |
| 55 |
|
| 56 |
|
| 57 |
|
| 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, |
| 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) |
| 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) |
| 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 ()])))) |