comparison tools/pkg-install.py @ 5494:898c27394c57

Use python3 in scripts (bug #58689). * Makefile.in: native mingw, depend on python3, set PYTHON3 var * tools/msys2-install.py: update for python3 syntax * tools/pkg-install.py: update for python3 syntax
author John Donoghue <john.donoghue@ieee.org>
date Sat, 04 Jul 2020 12:14:29 -0400
parents c752f70af1b6
children
comparison
equal deleted inserted replaced
5493:dace3d372190 5494:898c27394c57
1 #!/usr/bin/python2 1 #!/usr/bin/python3
2 import sys 2 import sys
3 import os 3 import os
4 import re 4 import re
5 import tempfile 5 import tempfile
6 import shutil 6 import shutil
22 m_dir = ""; 22 m_dir = "";
23 arch_dir = ""; 23 arch_dir = "";
24 cleanup = False; 24 cleanup = False;
25 25
26 def show_usage(): 26 def show_usage():
27 print sys.argv(0), "[options] pkg1 [pkg2]" 27 print (sys.argv[0], "[options] pkg1 [pkg2]")
28 28
29 def verify_directory(dirname): 29 def verify_directory(dirname):
30 for f in [ "COPYING", "DESCRIPTION" ]: 30 for f in [ "COPYING", "DESCRIPTION" ]:
31 if os.path.isfile(dirname + "/" + f) == False: 31 if os.path.isfile(dirname + "/" + f) == False:
32 raise Exception, "package is missing file " + f 32 raise Exception("package is missing file " + f)
33 33
34 def get_description(descfile): 34 def get_description(descfile):
35 with open(descfile, 'r') as f: 35 with open(descfile, 'rt', encoding='latin-1') as f:
36 lines = f.read().splitlines() 36 lines = f.read().splitlines()
37 pat_match = re.compile("(?P<name>[-\w]+):\s*(?P<value>\w.*)") 37 pat_match = re.compile("(?P<name>[-\w]+):\s*(?P<value>\w.*)")
38 lineval = "" 38 lineval = ""
39 d={} 39 d={}
40 for l in lines: 40 for l in lines:
50 50
51 return d 51 return d
52 52
53 def extract_pkg(filename, nm): 53 def extract_pkg(filename, nm):
54 pkg = [] 54 pkg = []
55 with open(filename, 'r') as f: 55 with open(filename, 'rt', encoding='latin-1') as f:
56 lines = f.read().splitlines() 56 lines = f.read().splitlines()
57 for l in lines: 57 for l in lines:
58 so = re.search(nm, l, re.M|re.S) 58 so = re.search(nm, l, re.M|re.S)
59 if so: 59 if so:
60 pkg.append(str(so.group(1))) 60 pkg.append(str(so.group(1)))
62 62
63 def extract_test_code(filename): 63 def extract_test_code(filename):
64 body = [] 64 body = []
65 if not os.path.isfile(filename): 65 if not os.path.isfile(filename):
66 return body 66 return body
67 with open(filename, 'rt') as f: 67 with open(filename, 'rt', encoding='latin-1') as f:
68 lines = f.read().splitlines() 68 lines = f.read().splitlines()
69 for l in lines: 69 for l in lines:
70 if l.startswith("%!"): 70 if l.startswith("%!"):
71 body.append(l) 71 body.append(l)
72 if not body: 72 if not body:
73 return body 73 return body
74 return "\n".join(body) 74 return "\n".join(body)
75 75
76 def write_index_file(env, desc, index_nm): 76 def write_index_file(env, desc, index_nm):
77 77
78 with open(index_nm, 'w') as f: 78 with open(index_nm, 'wt', encoding='latin-1') as f:
79 files = os.listdir(env.m_dir) 79 files = os.listdir(env.m_dir)
80 classes = fnmatch.filter(files, "@*") 80 classes = fnmatch.filter(files, "@*")
81 81
82 # check classes 82 # check classes
83 for c in classes: 83 for c in classes:
107 def finish_installation(env, packdir): 107 def finish_installation(env, packdir):
108 # octave would run post_install.m here - instead we will copy the post_install.m 108 # octave would run post_install.m here - instead we will copy the post_install.m
109 # somewhere and then on initial startup, run the post_install 109 # somewhere and then on initial startup, run the post_install
110 if os.path.isfile(packdir + "/post_install.m") == True: 110 if os.path.isfile(packdir + "/post_install.m") == True:
111 if env.verbose: 111 if env.verbose:
112 print "Copying .. post_install.m" 112 print ("Copying .. post_install.m")
113 destdir = env.prefix + "/share/octave/site/m/once_only" 113 destdir = env.prefix + "/share/octave/site/m/once_only"
114 if os.path.exists(destdir) == False: 114 if os.path.exists(destdir) == False:
115 os.makedirs(destdir) 115 os.makedirs(destdir)
116 shutil.copy2(packdir + "/post_install.m", destdir + "/" + env.pkg + "-post_install.m") 116 shutil.copy2(packdir + "/post_install.m", destdir + "/" + env.pkg + "-post_install.m")
117 117
118 def create_pkgadddel (env, packdir, nm): 118 def create_pkgadddel (env, packdir, nm):
119 if env.verbose: 119 if env.verbose:
120 print "Creating...", nm 120 print ("Creating...", nm)
121 121
122 instfid = open(env.m_dir + "/" + nm, "a") 122 instfid = open(env.m_dir + "/" + nm, "a")
123 if os.path.exists(env.arch_dir) == True: 123 if os.path.exists(env.arch_dir) == True:
124 archfid = open(env.arch_dir + "/" + nm, "w") 124 archfid = open(env.arch_dir + "/" + nm, "w")
125 else: 125 else:
146 for a in extract_pkg(f, '^/\** *' + nm + ': *(.*) *\*/$'): 146 for a in extract_pkg(f, '^/\** *' + nm + ': *(.*) *\*/$'):
147 archfid.write("%s\n" % a) 147 archfid.write("%s\n" % a)
148 148
149 # add PKG_XXX from packdir if exists 149 # add PKG_XXX from packdir if exists
150 if os.path.isfile(packdir + "/" + nm) == True: 150 if os.path.isfile(packdir + "/" + nm) == True:
151 with open(packdir + "/" + nm, 'r') as f: 151 with open(packdir + "/" + nm, 'rt', encoding='latin-1') as f:
152 lines = f.read().splitlines() 152 lines = f.read().splitlines()
153 for a in lines: 153 for a in lines:
154 archfid.write("%s\n" % a) 154 archfid.write("%s\n" % a)
155 155
156 # close files 156 # close files
170 if os.path.exists(destdir) == False: 170 if os.path.exists(destdir) == False:
171 os.mkdir(destdir) 171 os.mkdir(destdir)
172 for a in files: 172 for a in files:
173 if os.path.isfile(a): 173 if os.path.isfile(a):
174 if verbose: 174 if verbose:
175 print "copy " + a + " to " + destdir + "/" 175 print ("copy " + a + " to " + destdir + "/")
176 shutil.copy2(a, destdir) 176 shutil.copy2(a, destdir)
177 if os.path.isdir(a): 177 if os.path.isdir(a):
178 name= os.path.basename(a) 178 name= os.path.basename(a)
179 morefiles=(a + "/" + b for b in os.listdir(a)) 179 morefiles=(a + "/" + b for b in os.listdir(a))
180 copyfile(morefiles, destdir + "/" + name, verbose) 180 copyfile(morefiles, destdir + "/" + name, verbose)
183 if os.path.exists(env.m_dir) == False: 183 if os.path.exists(env.m_dir) == False:
184 os.makedirs(env.m_dir) 184 os.makedirs(env.m_dir)
185 instdir = pkgdir + "/inst" 185 instdir = pkgdir + "/inst"
186 186
187 if env.verbose: 187 if env.verbose:
188 print "Copying m files ..." 188 print ("Copying m files ...")
189 189
190 files = list(instdir + "/" + a for a in os.listdir(instdir)) 190 files = list(instdir + "/" + a for a in os.listdir(instdir))
191 # filter for arch folder 191 # filter for arch folder
192 if os.path.exists(instdir + "/" + env.arch) == True: 192 if os.path.exists(instdir + "/" + env.arch) == True:
193 files.remove(instdir + "/" + env.arch) 193 files.remove(instdir + "/" + env.arch)
194 194
195 copyfile(files, env.m_dir) 195 copyfile(files, env.m_dir)
196 196
197 if os.path.exists(instdir + "/" + env.arch) == True: 197 if os.path.exists(instdir + "/" + env.arch) == True:
198 if env.verbose: 198 if env.verbose:
199 print "Copying arch files ..." 199 print ("Copying arch files ...")
200 files = list(instdir + "/" + env.arch + "/" + a for a in os.listdir(instdir + "/" + env.arch)) 200 files = list(instdir + "/" + env.arch + "/" + a for a in os.listdir(instdir + "/" + env.arch))
201 if len(files) > 0: 201 if len(files) > 0:
202 if os.path.exists(env.arch_dir) == False: 202 if os.path.exists(env.arch_dir) == False:
203 os.makedirs(env.arch_dir) 203 os.makedirs(env.arch_dir)
204 copyfile(files, env.arch_dir) 204 copyfile(files, env.arch_dir)
205 shutil.rmtree(instdir + "/" + env.arch) 205 shutil.rmtree(instdir + "/" + env.arch)
206 206
207 # packinfo 207 # packinfo
208 if env.verbose: 208 if env.verbose:
209 print "Copying packinfo files ..." 209 print ("Copying packinfo files ...")
210 if os.path.exists(env.m_dir + "/packinfo") == False: 210 if os.path.exists(env.m_dir + "/packinfo") == False:
211 os.makedirs(env.m_dir + "/packinfo") 211 os.makedirs(env.m_dir + "/packinfo")
212 copyfile([pkgdir + "/DESCRIPTION"], env.m_dir + "/packinfo") 212 copyfile([pkgdir + "/DESCRIPTION"], env.m_dir + "/packinfo")
213 copyfile([pkgdir + "/COPYING"], env.m_dir + "/packinfo") 213 copyfile([pkgdir + "/COPYING"], env.m_dir + "/packinfo")
214 copyfile([pkgdir + "/CITATION"], env.m_dir + "/packinfo") 214 copyfile([pkgdir + "/CITATION"], env.m_dir + "/packinfo")
216 copyfile([pkgdir + "/ONEWS"], env.m_dir + "/packinfo") 216 copyfile([pkgdir + "/ONEWS"], env.m_dir + "/packinfo")
217 copyfile([pkgdir + "/ChangeLog"], env.m_dir + "/packinfo") 217 copyfile([pkgdir + "/ChangeLog"], env.m_dir + "/packinfo")
218 218
219 # index file 219 # index file
220 if env.verbose: 220 if env.verbose:
221 print "Copying/creating INDEX ..." 221 print ("Copying/creating INDEX ...")
222 if os.path.isfile(pkgdir + "/INDEX") == True: 222 if os.path.isfile(pkgdir + "/INDEX") == True:
223 copyfile([pkgdir + "/INDEX"], env.m_dir + "/packinfo") 223 copyfile([pkgdir + "/INDEX"], env.m_dir + "/packinfo")
224 else: 224 else:
225 desc = get_description(pkgdir + "/DESCRIPTION") 225 desc = get_description(pkgdir + "/DESCRIPTION")
226 write_index_file(env, desc, env.m_dir + "/packinfo/INDEX") 226 write_index_file(env, desc, env.m_dir + "/packinfo/INDEX")
229 229
230 # doc dir ? 230 # doc dir ?
231 docdir = pkgdir + "/doc" 231 docdir = pkgdir + "/doc"
232 if os.path.exists(docdir) == True: 232 if os.path.exists(docdir) == True:
233 if env.verbose: 233 if env.verbose:
234 print "Copying doc files ..." 234 print ("Copying doc files ...")
235 files = (docdir + "/" + a for a in os.listdir(docdir)) 235 files = (docdir + "/" + a for a in os.listdir(docdir))
236 copyfile(files, env.m_dir + "/doc") 236 copyfile(files, env.m_dir + "/doc")
237 237
238 # bin dir ? 238 # bin dir ?
239 bindir = pkgdir + "/bin" 239 bindir = pkgdir + "/bin"
240 if os.path.exists(bindir) == True: 240 if os.path.exists(bindir) == True:
241 if env.verbose: 241 if env.verbose:
242 print "Copying bin files ..." 242 print ("Copying bin files ...")
243 files = (bindir + "/" + a for a in os.listdir(bindir)) 243 files = (bindir + "/" + a for a in os.listdir(bindir))
244 copyfile(files, env.m_dir + "/bin") 244 copyfile(files, env.m_dir + "/bin")
245 245
246 def configure_make(env, packdir): 246 def configure_make(env, packdir):
247 if os.path.isdir(packdir + "/inst") == False: 247 if os.path.isdir(packdir + "/inst") == False:
251 src = packdir + "/src" 251 src = packdir + "/src"
252 os.chdir(src) 252 os.chdir(src)
253 253
254 if os.path.isfile(src + "/configure") == True: 254 if os.path.isfile(src + "/configure") == True:
255 if env.verbose: 255 if env.verbose:
256 print "running ./configure " + env.config_opts 256 print ("running ./configure " + env.config_opts)
257 257
258 if os.system("./configure " + env.config_opts + "") != 0: 258 if os.system("./configure " + env.config_opts + "") != 0:
259 raise Exception, "configure failed - stopping install" 259 raise Exception("configure failed - stopping install")
260 260
261 if os.path.isfile(src + "/Makefile") == True: 261 if os.path.isfile(src + "/Makefile") == True:
262 if env.verbose: 262 if env.verbose:
263 print "running make ..." 263 print ("running make ...")
264 if os.system(env.make + " --directory '" + src + "'" ) != 0: 264 if os.system(env.make + " --directory '" + src + "'" ) != 0:
265 raise Exception, "make failed during build - stopping install" 265 raise Exception("make failed during build - stopping install")
266 266
267 # extract any tests 267 # extract any tests
268 if env.verbose: 268 if env.verbose:
269 print "checking for src BIST tests" 269 print ("checking for src BIST tests")
270 270
271 files = os.listdir(src) 271 files = os.listdir(src)
272 srcfiles = fnmatch.filter(files, "*.cc") + fnmatch.filter(files,"*.cpp") + fnmatch.filter(files, "*.cxx") + fnmatch.filter(files, "*.c") 272 srcfiles = fnmatch.filter(files, "*.cc") + fnmatch.filter(files,"*.cpp") + fnmatch.filter(files, "*.cxx") + fnmatch.filter(files, "*.c")
273 for sf in srcfiles: 273 for sf in srcfiles:
274 tst = extract_test_code(sf) 274 tst = extract_test_code(sf)
275 if tst: 275 if tst:
276 with open(sf + "-tst", "w") as f: 276 with open(sf + "-tst", "wt", encoding='latin-1') as f:
277 f.write("## DO NOT EDIT! Generated from " + sf + "\n") 277 f.write("## DO NOT EDIT! Generated from " + sf + "\n")
278 f.write(tst + "\n") 278 f.write(tst + "\n")
279 279
280 # copy files to inst and inst arch 280 # copy files to inst and inst arch
281 files = src + "/FILES" 281 files = src + "/FILES"
282 instdir = packdir + "/inst" 282 instdir = packdir + "/inst"
283 archdir = instdir + "/" + env.arch 283 archdir = instdir + "/" + env.arch
284 284
285 if os.path.isfile(files) == True: 285 if os.path.isfile(files) == True:
286 raise Exception, "make using FILES not supported yet" 286 raise Exception("make using FILES not supported yet")
287 pass # TODO yet 287 pass # TODO yet
288 else: 288 else:
289 # get .m, .oct and .mex files 289 # get .m, .oct and .mex files
290 files = os.listdir(src) 290 files = os.listdir(src)
291 m_files = fnmatch.filter(files, "*.m") 291 m_files = fnmatch.filter(files, "*.m")
303 copyfile(archindependant, instdir) 303 copyfile(archindependant, instdir)
304 copyfile(archdependant, archdir) 304 copyfile(archdependant, archdir)
305 305
306 def add_package_list(env, desc): 306 def add_package_list(env, desc):
307 #pkglist = env.prefix + "/share/octave/octave_packages" 307 #pkglist = env.prefix + "/share/octave/octave_packages"
308 #with open(pkglist, 'r') as f: 308 #with open(pkglist, 'rt', encoding='latin-1') as f:
309 # lines = f.read().splitlines() 309 # lines = f.read().splitlines()
310 # currently doing nothing for adding, will let installer do 310 # currently doing nothing for adding, will let installer do
311 pass 311 pass
312 312
313 def uninstall_pkg(pkg,env): 313 def uninstall_pkg(pkg,env):
314 # uninstall existing directories 314 # uninstall existing directories
315 if env.verbose: 315 if env.verbose:
316 print "Uninstalling " + env.pkg 316 print ("Uninstalling " + env.pkg)
317 317
318 files=glob.glob(env.prefix + "/share/octave/packages/" + env.pkg + "-" + "*") 318 files=glob.glob(env.prefix + "/share/octave/packages/" + env.pkg + "-" + "*")
319 for f in files: 319 for f in files:
320 if env.verbose: 320 if env.verbose:
321 print "removing dir " + f 321 print ("removing dir " + f)
322 shutil.rmtree(f) 322 shutil.rmtree(f)
323 323
324 files=glob.glob(env.prefix + "/lib/octave/packages/" + env.pkg + "-" + "*") 324 files=glob.glob(env.prefix + "/lib/octave/packages/" + env.pkg + "-" + "*")
325 for f in files: 325 for f in files:
326 if env.verbose: 326 if env.verbose:
327 print "removing dir " + f 327 print ("removing dir " + f)
328 shutil.rmtree(f) 328 shutil.rmtree(f)
329 329
330 pass 330 pass
331 331
332 def install_pkg(pkg, env): 332 def install_pkg(pkg, env):
333 pkg = os.path.abspath(pkg) 333 pkg = os.path.abspath(pkg)
334 currdir = os.getcwd() 334 currdir = os.getcwd()
335 335
336 if env.verbose: 336 if env.verbose:
337 print "Installing " + pkg 337 print ("Installing " + pkg)
338 338
339 try: 339 try:
340 ## Check that the directory in prefix exist. If it doesn't: create it! 340 ## Check that the directory in prefix exist. If it doesn't: create it!
341 tmpdir = tempfile.mkdtemp("-pkg","tmp", env.tmp) 341 tmpdir = tempfile.mkdtemp("-pkg","tmp", env.tmp)
342 os.chdir(tmpdir) 342 os.chdir(tmpdir)
348 os.system("tar xzf '" + pkg + "'") 348 os.system("tar xzf '" + pkg + "'")
349 349
350 # get list for files creates 350 # get list for files creates
351 files=os.listdir(tmpdir) 351 files=os.listdir(tmpdir)
352 if len(files) != 1: 352 if len(files) != 1:
353 print "Expected to unpack to only one directory" 353 print ("Expected to unpack to only one directory")
354 354
355 packdir = os.path.abspath(files[0]) 355 packdir = os.path.abspath(files[0])
356 356
357 # verify have expected min files 357 # verify have expected min files
358 verify_directory(packdir) 358 verify_directory(packdir)
379 379
380 # update package list 380 # update package list
381 add_package_list(env, desc) 381 add_package_list(env, desc)
382 finally: 382 finally:
383 if env.verbose: 383 if env.verbose:
384 print "cleaning up" 384 print ("cleaning up")
385 os.chdir(currdir) 385 os.chdir(currdir)
386 386
387 if env.cleanup: 387 if env.cleanup:
388 shutil.rmtree(tmpdir) 388 shutil.rmtree(tmpdir)
389 389
408 408
409 def rebuild_pkg(env): 409 def rebuild_pkg(env):
410 currdir = os.getcwd() 410 currdir = os.getcwd()
411 411
412 if env.verbose: 412 if env.verbose:
413 print "Rebuilding package" 413 print ("Rebuilding package")
414 414
415 try: 415 try:
416 oct_dir = env.prefix + "/share/octave" 416 oct_dir = env.prefix + "/share/octave"
417 pkg_dir = oct_dir + "/packages" 417 pkg_dir = oct_dir + "/packages"
418 arch_dir = env.prefix + "/lib/octave/packages" 418 arch_dir = env.prefix + "/lib/octave/packages"
420 pkg_list_file = oct_dir + "/octave_packages" 420 pkg_list_file = oct_dir + "/octave_packages"
421 421
422 descs=glob.glob(pkg_dir + "/*/packinfo/DESCRIPTION") 422 descs=glob.glob(pkg_dir + "/*/packinfo/DESCRIPTION")
423 423
424 if env.verbose: 424 if env.verbose:
425 print "Rebuilding pkg list {}".format(pkg_list_file) 425 print ("Rebuilding pkg list {}".format(pkg_list_file))
426 426
427 with open(pkg_list_file, "w") as f: 427 with open(pkg_list_file, "wt", encoding='latin-1') as f:
428 f.write("# Created by pkg-install.py\n") 428 f.write("# Created by pkg-install.py\n")
429 f.write("# name: global_packages\n") 429 f.write("# name: global_packages\n")
430 f.write("# type: cell\n"); 430 f.write("# type: cell\n");
431 f.write("# rows: 1\n") 431 f.write("# rows: 1\n")
432 f.write("# columns: {}\n".format(len(descs))) 432 f.write("# columns: {}\n".format(len(descs)))
526 526
527 files = [] 527 files = []
528 528
529 operation = args[0] 529 operation = args[0]
530 if operation != "install" and operation != "rebuild": 530 if operation != "install" and operation != "rebuild":
531 raise Exception, "Expected pkg operation 'install' or 'rebuild'" 531 raise Exception("Expected pkg operation 'install' or 'rebuild'")
532 532
533 533
534 args = args[1:] 534 args = args[1:]
535 535
536 for a in args: 536 for a in args:
595 env.arch = arch + "-" + apiver 595 env.arch = arch + "-" + apiver
596 596
597 env.bindir = os.popen(env.octave_config + " -p BINDIR").read().rstrip("\r\n") 597 env.bindir = os.popen(env.octave_config + " -p BINDIR").read().rstrip("\r\n")
598 598
599 if env.verbose: 599 if env.verbose:
600 print "operation=", operation 600 print ("operation=", operation)
601 print "mkoctfile=", env.mkoctfile 601 print ("mkoctfile=", env.mkoctfile)
602 print "arch=", env.arch 602 print ("arch=", env.arch)
603 print "prefix=", env.prefix 603 print ("prefix=", env.prefix)
604 print "files=", files 604 print ("files=", files)
605 print "verbose=", env.verbose 605 print ("verbose=", env.verbose)
606 606
607 if operation == "install": 607 if operation == "install":
608 for a in files: 608 for a in files:
609 install_pkg(a, env) 609 install_pkg(a, env)
610 # rebuild pkg list afterwards 610 # rebuild pkg list afterwards