From 8a5579ba137e9eb4ee8a7a2aff962fd1255f9f65 Mon Sep 17 00:00:00 2001 From: anatoly techtonik Date: Tue, 18 Dec 2012 17:57:09 +0300 Subject: [PATCH 1/3] + Crossplatform helper to compile *.map files to *.bsp with ufo2map Features: * only changed .map files are compiled (checks timestamp) --- contrib/scripts/maps_compile.py | 88 +++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 contrib/scripts/maps_compile.py diff --git a/contrib/scripts/maps_compile.py b/contrib/scripts/maps_compile.py new file mode 100644 index 00000000000..53eb851b490 --- /dev/null +++ b/contrib/scripts/maps_compile.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +""" +Crossplatform helper to compile *.map files to *.bsp with ufo2map + +Features: + * only changed .map files are compiled (checks timestamp) + +License: Public domain +Authors: + anatoly techtonik +""" + +import os +import subprocess +import sys +from os.path import abspath, dirname, exists, getmtime + + +__version__ = '0.1' + +# UFOAI source checkout +UFOROOT = abspath(abspath(dirname(__file__)) + '/../../') + '/' +# base directory with ending slash +BASEDIR = 'base/' +# maps directory, relative to 'base/' directory for some reason +# see bug #3441: https://sourceforge.net/p/ufoai/bugs/3441/ +MAPSDIR = 'maps/' +FLAGS = '-soft' +# ufo2map path +UFO2MAP = UFOROOT + 'ufo2map' + +# --- functions library --- + +def get_maps(path, verbose=True): + ''' Return three lists (all, not compiled, updated) of .map + files from the given path ''' + all, notcomp, updated = [], [], [] + for root, dirs, files in os.walk(path): + # print(root.replace(path, '')[1:]) + for x in files: + if x.endswith('.bsp'): + continue + if x.endswith('.ump'): + continue + elif x.endswith('.map'): + bspname = x[:-4] + '.bsp' + bspfile = root + '/' + bspname + mapfile = root + '/' + x + all.append(mapfile) + if not exists(bspfile): + # print(' ' + x + ' (not compiled)') + notcomp.append(mapfile) + elif getmtime(mapfile) > getmtime(bspfile): + # print(' ' + x + ' (updated)') + updated.append(mapfile) + else: + # print(' ' + x) + pass + else: + if verbose: + print('warning: unknown file - ' + x) + return all, notcomp, updated + + +def runret(command): + """ Run command through shell, return ret code """ + print command + return subprocess.call(command, shell=True) + +# --- /functions --- + +""" +[ ] check dirs exist +[ ] check ufo2map exists +[ ] detect features .bsp was compiled with to compare with given settings +""" + +if __name__ == '__main__': + all, notcomp, updated = get_maps(UFOROOT + BASEDIR + MAPSDIR) + print('Total: %s Not compiled: %s Updated: %s' + % (len(all), len(notcomp), len(updated))) + for i,m in enumerate(notcomp + updated): + name = m.replace(UFOROOT + BASEDIR + MAPSDIR, '')[1:] + print('--- Compiling %s/%s - %s' % (i+1, len(notcomp + updated), name)) + res = runret(UFO2MAP + ' ' + FLAGS + ' ' + m.replace(UFOROOT + BASEDIR, '')) + if res != 0: + print('Error: ufo2map returned bad result - %s' % res) + sys.exit(res) From 6d507834e5eeec53b77eac7522d0c05bcca04332 Mon Sep 17 00:00:00 2001 From: anatoly techtonik Date: Tue, 18 Dec 2012 18:00:28 +0300 Subject: [PATCH 2/3] * Cleanup outdated maps/compile.* scripts Use contrib/scripts/maps_compile.py instead --- base/maps/compile.pl | 114 ------------------------------------------- base/maps/compile.py | 91 ---------------------------------- 2 files changed, 205 deletions(-) delete mode 100755 base/maps/compile.pl delete mode 100755 base/maps/compile.py diff --git a/base/maps/compile.pl b/base/maps/compile.pl deleted file mode 100755 index c9950c21e19..00000000000 --- a/base/maps/compile.pl +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/perl -use File::stat; -use strict; - -# If you have a "make" program installed, you're likely to prefer the -# Makefile to this script, which cannot run jobs in parallel, is not -# able to rebuild only the maps that changed since last time you -# built, and does not handle the case when qrad3 gets interrupted -# mid-way. - -my $extra = "-bounce 0 -soft"; - -my $ufo2map = "../../ufo2map"; - -if ($^O =~ /windows/i || $^O =~ /mswin/i) { - $ufo2map .= ".exe"; -} - -sub readDir -{ - my $dir = shift || die "No dir given in sub readDir\n"; - my $status = opendir (DIR, "$dir"); - my @files = (); - unless ( $status ) - { - print "Could not read $dir\n"; - return (); - } - - @files = readdir ( DIR ); - closedir (DIR); - @files; -} - -sub compile -{ - my $dir = shift || die "No dir given in sub compile\n"; - my $found = 0; - my $stat1; - my $stat2; - my $map_path; # Path to the "source" file of the map (i.e. the *.map file) - my $compile_path; # Path to the compiled map (i.e. the *.bsp file) - - print "...entering $dir\n"; - foreach ( readDir( $dir ) ) - { - # The foreach/readDir combination sets "$_" to the next file or directory in $dir - - next if $_ =~ /^\./; - if ( -d "$dir/$_" ) - { - print "...found dir $_\n"; - $found += compile("$dir/$_"); - print "...dir $dir/$_ finished\n"; - next; - } - next unless $_ =~ /\.map$/; - $map_path = $_; - next if $map_path =~ /^(tutorial)|(prefab)|(autosave)/i; - - $map_path = "$dir/$map_path"; # results in something like "$dir/xxx.map" - $compile_path = $map_path; - $compile_path =~ s/\.map$/.bsp/; # results in something like "$dir/xxx.bsp" - - # print "DEBUG: ", $compile_path; #DEBUG - if (-e $compile_path) - { - $stat1 = stat($map_path); - $stat2 = stat($compile_path); - - if ($stat1->mtime > $stat2->mtime) - { - unlink($compile_path); - } - } - - unless ( -e $compile_path ) - { - print "..found $dir/$_\n"; - if (system("$ufo2map $extra $dir/$_") != 0) - { - die "ufo2map failed"; - } - $found++; - } - else - { - print "..already compiled $_\n"; - } - } - return $found; -} - -#read the given dir -my $dir = $ARGV[0] || "."; -print "=====================================================\n"; -print "Mapcompiler for UFO:AI (http://sf.net/projects/ufoai)\n"; -print "Giving base/maps as parameter or start from base/maps\n"; -print "will compile all maps were no bsp-file exists\n"; -print "Keep in mind that ufo2map needs to be in path\n"; -print "=====================================================\n"; -print "Running at: $^O\n"; - -die "Dir $dir does not exists\n" if ( !-e $dir ); - -if (!-e "$ufo2map") { - print "$ufo2map wasn't found\n"; - $ufo2map = "ufo2map"; -} - -print "Found ufo2map in \"$ufo2map\"\n"; -print "Found ". compile($dir) ." maps\n"; - -print "...finished\n" diff --git a/base/maps/compile.py b/base/maps/compile.py deleted file mode 100755 index 1f25dd06cfe..00000000000 --- a/base/maps/compile.py +++ /dev/null @@ -1,91 +0,0 @@ -#! /usr/bin/env python - -# If you have a "make" program installed, you're likely to prefer the -# Makefile to this script, which cannot run jobs in parallel. - -# ported from perl to python by techtonik // php.net - -import os, fnmatch, sys, stat, platform - -extra = "-soft" - -def getFile(root): - pattern = "*.map" - antipathpattern = "*prefabs*" - for path, subdirs, files in os.walk(root): - if ".svn" in subdirs: subdirs.remove(".svn") - files.sort() - if fnmatch.fnmatch(path, antipathpattern): - continue - for name in files: - if fnmatch.fnmatch(name, pattern): - file = os.path.join(path, name) - file = file.replace("base/", "").replace("base\\", "") - yield file - - -def compile(root): - total = compiled = 0 - - sys.stdout.write("...looking for ufo2map: "); sys.stdout.flush(); - ufo2map = "ufo2map" - if platform.system() == "Windows": ufo2map = ufo2map + ".exe" - if not os.access(ufo2map, os.X_OK): - ufo2map = "../../%s" % ufo2map - if platform.system() == "Windows": - ufo2map = os.path.normpath(ufo2map) - if not os.access(ufo2map, os.X_OK): - print "not found. exiting..." - sys.exit(1) - print "found %s" % ufo2map - - - print "...looking for maps in: %s" % root - for f in getFile(root): - total += 1 - name, ext = os.path.splitext(f) - bspf = name + ".bsp" - if os.path.isfile(bspf): - if os.stat(f)[stat.ST_MTIME] > os.stat(bspf)[stat.ST_MTIME]: - print "...removing old .bsp file %s" % bspf - os.unlink(bspf) - if not os.path.isfile(bspf): - print "...compiling %s" % f - try: - z = os.system("%s %s %s" % (ufo2map, extra, os.path.normpath(f))) - if z != 0: - print "ufo2map error. Deleting %s" % bspf - os.unlink(bspf) - sys.exit("Terminated.") - compiled += 1 - - except KeyboardInterrupt: - print "User terminated. Deleting %s" % bspf - os.unlink(bspf) - sys.exit("Terminated.") - print "...%s maps found so far, %s of them compiled..." % (total, compiled) - return total, compiled - - - -#read the given dir -if len(sys.argv) < 2: - mapdir = "base" -else: - mapdir = sys.argv[1] - -print "=====================================================" -print "Mapcompiler for UFO:AI (http://sf.net/projects/ufoai)" -print "Giving base/maps as parameter or start from base/maps" -print "will compile all maps were no bsp-file exists or that" -print "were modified since last access" -print "Keep in mind that ufo2map needs to be accessible" -print "=====================================================" - -if not os.path.isdir(mapdir): - print "Dir %s does not exists" % mapdir - sys.exit(1) - -print "Found %d maps, %d compiled" % compile(mapdir) - -print "Done." From 7240398b85485e33472a3cc2128005bf37aa318c Mon Sep 17 00:00:00 2001 From: anatoly techtonik Date: Tue, 18 Dec 2012 22:14:09 +0300 Subject: [PATCH 3/3] Python 3 compatibility --- contrib/scripts/maps_compile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/scripts/maps_compile.py b/contrib/scripts/maps_compile.py index 53eb851b490..ee26f862ab6 100644 --- a/contrib/scripts/maps_compile.py +++ b/contrib/scripts/maps_compile.py @@ -64,7 +64,7 @@ def get_maps(path, verbose=True): def runret(command): """ Run command through shell, return ret code """ - print command + print(command) return subprocess.call(command, shell=True) # --- /functions ---