[gobject-introspection] MSVC Builds: Use the Shared replace.py Script



commit e8162cb3319e8b4814ff759423b367c1c0b0b605
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Fri Sep 25 19:14:07 2015 +0800

    MSVC Builds: Use the Shared replace.py Script
    
    ...which can be copied from GLib's $(srcroot)/build/win32, so the
    maintenance of these scripts that generate the needed tool scripts
    and cairo-1.0.gir can be easier maintained.

 build/Makefile.am             |    3 +-
 build/gen-win32-cairo-gir.py  |   35 +++++++-------
 build/gen-win32-g-ir-tools.py |   31 +++++++------
 build/gi_msvc_build_utils.py  |   12 -----
 build/replace.py              |  102 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 139 insertions(+), 44 deletions(-)
---
diff --git a/build/Makefile.am b/build/Makefile.am
index a84067f..89490d1 100644
--- a/build/Makefile.am
+++ b/build/Makefile.am
@@ -9,4 +9,5 @@ EXTRA_DIST =    \
        gi_msvc_build_utils.py          \
        gi-setenv-msvc.mak              \
        gi-tests-msvc.mak               \
-       introspection-msvc.mak
+       introspection-msvc.mak  \
+       replace.py
diff --git a/build/gen-win32-cairo-gir.py b/build/gen-win32-cairo-gir.py
index 419e8d3..614cfd1 100644
--- a/build/gen-win32-cairo-gir.py
+++ b/build/gen-win32-cairo-gir.py
@@ -11,33 +11,34 @@
 
 import os
 import sys
-import optparse
+import argparse
 
-from gi_msvc_build_utils import process_in
-from gi_msvc_build_utils import parent_dir
+import replace
 
-def setup_vars_cairo(src, dest, dllname):
-    vars = {}
-    vars['CAIRO_GIR_PACKAGE'] = 'cairo-gobject'
-    vars['CAIRO_SHARED_LIBRARY'] = '%s' % dllname
-    process_in (src, dest, vars, 1)
+from gi_msvc_build_utils import parent_dir
 
 def main(argv):
-    parser = optparse.OptionParser()
-    parser.add_option('--dllname', dest='dllname', action='store', help='Full file name of the Cairo-GObject 
DLL for the Cairo Introspection File')
-    opt, args = parser.parse_args(argv)
-    if opt.dllname is None:
-        print ('dllname must be specified.  Please refer to %s -h for more information' % 
os.path.basename(__file__))
-        sys.exit()
+    parser = argparse.ArgumentParser(description='Generate the complete cairo-1.0.gir')
+    parser.add_argument('--dllname',
+                        required=True,
+                        help='Full file name of the Cairo-GObject DLL for the Cairo Introspection File')
+    args = parser.parse_args()
 
     # Get the srcroot and the path where the bundled .gir files reside in the package
     srcroot = parent_dir(__file__)
     preset_gir_path = os.path.join(srcroot, 'gir')
 
     # Set up variables in cairo-1.0.gir.in to produce cairo-1.0.gir
-    setup_vars_cairo(os.path.join(preset_gir_path, 'cairo-1.0.gir.in'),
-                     'cairo-1.0.gir',
-                     opt.dllname)
+    replace.replace(os.path.join(preset_gir_path, 'cairo-1.0.gir.in'),
+                    os.path.join(preset_gir_path, 'cairo-1.0.gir.tmp'),
+                    '%CAIRO_GIR_PACKAGE%',
+                    'cairo-gobject')
+
+    replace.replace(os.path.join(preset_gir_path, 'cairo-1.0.gir.tmp'),
+                    os.path.join(preset_gir_path, 'cairo-1.0.gir'),
+                    '%CAIRO_SHARED_LIBRARY%',
+                    args.dllname)
+    os.unlink(os.path.join(preset_gir_path, 'cairo-1.0.gir.tmp'))
 
 if __name__ == '__main__':
     sys.exit(main(sys.argv))
diff --git a/build/gen-win32-g-ir-tools.py b/build/gen-win32-g-ir-tools.py
index c598641..444fe80 100644
--- a/build/gen-win32-g-ir-tools.py
+++ b/build/gen-win32-g-ir-tools.py
@@ -13,26 +13,29 @@ import os
 import sys
 import optparse
 
-from gi_msvc_build_utils import process_in
+import replace
 from gi_msvc_build_utils import parent_dir
 
 def setup_vars_tools(module, func, srcfile, outfile):
-    vars = {}
-
-    # Well, we are using the "relocatable" feature on Windows...
-    blah = 'this\\\\is\\\\ignored\\\\on\\\\windows'
-    vars['datarootdir'] = blah
-    vars['libdir'] = blah
-
     # This doesn't really matter for cmd.exe usage, but
     # let's just set this like this here, in case one
     # wants to use MinGW with the scripts generated here
-    vars['PYTHON'] = 'python'
-
-    # The parts that really matter.
-    vars['TOOL_MODULE'] = module
-    vars['TOOL_FUNCTION'] = func
-    process_in(srcfile, outfile, vars, 2)
+    replace.replace(srcfile,
+                    outfile + '.tmp0',
+                    '@PYTHON@',
+                    'python')
+
+    # Now replace the needed items...
+    replace.replace(outfile + '.tmp0',
+                    outfile + '.tmp',
+                    '@TOOL_MODULE@',
+                    module)
+    os.unlink(outfile + '.tmp0')
+    replace.replace(outfile + '.tmp',
+                    outfile,
+                    '@TOOL_FUNCTION@',
+                    func)
+    os.unlink(outfile + '.tmp')
 
 def main(argv):
     modules = ['scannermain','annotationmain','docmain']
diff --git a/build/gi_msvc_build_utils.py b/build/gi_msvc_build_utils.py
index 619f0c5..150f66e 100644
--- a/build/gi_msvc_build_utils.py
+++ b/build/gi_msvc_build_utils.py
@@ -1,16 +1,4 @@
 import os
-import re
-
-def process_in(src, dest, vars, mode):
-    if mode == 1:
-        RE_VARS = re.compile(r'%(\w+?)%')
-    if mode == 2:
-        RE_VARS = re.compile(r'@(\w+?)@')
-    with open(src, 'r') as s:
-        with open(dest, 'w') as d:
-            for i in s:
-                i = RE_VARS.sub(lambda x: str(vars[x.group(1)]), i)
-                d.write(i)
 
 def parent_dir(path):
     if not os.path.isabs(path):
diff --git a/build/replace.py b/build/replace.py
new file mode 100644
index 0000000..69ef417
--- /dev/null
+++ b/build/replace.py
@@ -0,0 +1,102 @@
+#!/usr/bin/python
+#
+# Simple utility script to manipulate
+# certain types of strings in a file
+
+# This can be used in various projects where
+# there is the need to replace strings in files,
+# and is copied from GLib's $(srcroot)/build/win32
+
+# Author: Fan, Chun-wei
+# Date: September 03, 2014
+
+import os
+import sys
+import re
+import string
+import argparse
+
+valid_actions = ['remove-prefix',
+                 'replace-var',
+                 'replace-str',
+                 'remove-str']
+
+def replace(src, dest, instring, outstring):
+    with open(src, 'r') as s:
+        with open(dest, 'w') as d:
+            for line in s:
+                i = line.replace(instring, outstring)
+                d.write(i)
+
+def check_required_args(args, params):
+    for param in params:
+        if getattr(args, param, None) is None:
+            raise SystemExit('%s: error: --%s argument is required' % (__file__, param))
+
+def warn_ignored_args(args, params):
+    for param in params:
+        if getattr(args, param, None) is not None:
+            print('%s: warning: --%s argument is ignored' % (__file__, param))
+
+def main(argv):
+
+    parser = argparse.ArgumentParser(description='Process strings in a file.')
+    parser.add_argument('-a',
+                        '--action',
+                        help='Action to carry out.  Can be one of:\n'
+                             'remove-prefix\n'
+                             'replace-var\n'
+                             'replace-str\n'
+                             'remove-str',
+                        choices=valid_actions)
+    parser.add_argument('-i', '--input', help='Input file')
+    parser.add_argument('-o', '--output', help='Output file')
+    parser.add_argument('--instring', help='String to replace or remove')
+    parser.add_argument('--var', help='Autotools variable name to replace')
+    parser.add_argument('--outstring',
+                        help='New String to replace specified string or variable')
+    parser.add_argument('--removeprefix', help='Prefix of string to remove')
+
+    args = parser.parse_args()
+
+    input_string = ''
+    output_string = ''
+
+    # We must have action, input, output for all operations
+    check_required_args(args, ['action','input','output'])
+
+    # Build the arguments by the operation that is to be done,
+    # to be fed into replace()
+
+    # Get rid of prefixes from a string
+    if args.action == 'remove-prefix':
+        check_required_args(args, ['instring','removeprefix'])
+        warn_ignored_args(args, ['outstring','var'])
+        input_string = args.removeprefix + args.instring
+        output_string = args.instring
+
+    # Replace an m4-style variable (those surrounded by @...@)
+    if args.action == 'replace-var':
+        check_required_args(args, ['var','outstring'])
+        warn_ignored_args(args, ['instring','removeprefix'])
+        input_string = '@' + args.var + '@'
+        output_string = args.outstring
+
+    # Replace a string
+    if args.action == 'replace-str':
+        check_required_args(args, ['instring','outstring'])
+        warn_ignored_args(args, ['var','removeprefix'])
+        input_string = args.instring
+        output_string = args.outstring
+
+    # Remove a string
+    if args.action == 'remove-str':
+        check_required_args(args, ['instring'])
+        warn_ignored_args(args, ['var','outstring','removeprefix'])
+        input_string = args.instring
+        output_string = ''
+
+    replace(args.input, args.output, input_string, output_string)
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]