1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#!/usr/bin/env python3
"""
To update examples on website:
1) Run in your build directory
ctest -R PyExamples
This will run all existing examples and generate intensity images for web site.
2) Run this script
python update-examples.py <website-source-dir> <example_root> <img-dir>
"""
import argparse, datetime, filecmp, os, shutil
def log(msg):
flog.write(msg+"\n")
def log2(msg):
print(msg)
log(msg)
def find_files(dir_name):
"""
Return recursive list of files in given dir_name.
"""
for subdir, dirs, files in os.walk(dir_name):
for filename in files:
yield subdir, filename
def get_files(dir_name, extension):
"""
Return list of all files in subdirectories of dir_name with given extension
"""
result = []
for subdir, filename in find_files(dir_name):
if os.path.basename(subdir)=="utils":
continue
name, ext = os.path.splitext(filename)
if ext in extension:
result.append(os.sep.join([subdir, filename]))
return result
def find_files_with_same_name(filename_list, name_to_find):
same_names = []
for filename in filename_list:
if os.path.basename(filename) == name_to_find:
same_names.append(filename)
return same_names
def update_one_file(source_list, dest):
"""
Update destination file 'dest', using a suitable source file from 'example_root'.
Returns 2=error, 1=modified, 0=unchanged.
On succes (0 or 1), the source file is removed from source_list.
"""
likely_sources = find_files_with_same_name(source_list, os.path.basename(dest))
if len(likely_sources) == 0:
log2(f'! file {dest}\n not found in source dir (ERROR)')
return 2
if len(likely_sources) > 1:
log2(f'! {dest}\n has {len(likely_sources)} possible sources (ERROR):')
for f in likely_sources:
log2(f' - {f}')
return 2
src = likely_sources[0]
source_list.remove(src)
if filecmp.cmp(src, dest):
log(f'. {dest}\n is same as {src}')
return 0
shutil.copyfile(src, dest)
log2(f'< {dest}\n updated from {src}')
return 1
def update_all_files_of_one_type(example_root, dest_dir, extension):
"""
Every file in dest_list will be replaced by the file with the same
basename found in source_list
"""
source_list = get_files(example_root, extension)
dest_list = get_files(dest_dir, extension)
log2(f'Update {len(dest_list)} {extension} files')
log2(f' from {example_root}')
log2(f' to {dest_dir}')
log(f' with source list')
for f in source_list:
log(f' - {f}')
nError = 0
nModified = 0
nUnchanged = 0
for dest in dest_list:
ret = update_one_file(source_list, dest)
if ret == 0:
nUnchanged += 1
elif ret == 1:
nModified += 1
elif ret == 2:
nError += 1
log2(f'=> {nUnchanged} files unchanged, {nModified} files updated, {nError} errors')
if len(source_list)>0:
log2(f'!! WARNING: {len(source_list)} source files are unused')
for src in source_list:
log(f' unused: {src}')
def update_website(website_root, example_root, img_dir):
"""
Updates example scripts and images on website.
"""
# Start logging
website_root = os.path.expanduser(website_root)
log_path = os.path.join(website_root, "update.examples.log")
global flog
flog = open(log_path, "a")
print(f'Appending log to {log_path}')
log(f'\n===\n{datetime.datetime.now().strftime("%d%b%y %H:%M:%S")}')
# Update scripts
update_all_files_of_one_type(
example_root,
os.path.join(website_root, "static/files/python"),
'.py')
# Update images
update_all_files_of_one_type(
img_dir,
os.path.join(website_root, "static/files/simulated"),
'.png')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("website_root", type=str)
parser.add_argument("example_root", type=str)
parser.add_argument("img_dir", type=str)
args = parser.parse_args()
update_website(args.website_root, args.example_root, args.img_dir)
|