Re: [Spice-devel] [PATCH spice-common 2/9] codegen: Factor out a function to write output file

2019-03-07 Thread Christophe Fergeau

Acked-by: Christophe Fergeau 

On Sun, Mar 03, 2019 at 07:10:23PM +, Frediano Ziglio wrote:
> This will be reused to generate C declaration automatically.
> 
> Signed-off-by: Frediano Ziglio 
> ---
>  spice_codegen.py | 46 +-
>  1 file changed, 25 insertions(+), 21 deletions(-)
> 
> diff --git a/spice_codegen.py b/spice_codegen.py
> index 76d7c5e..7e22092 100755
> --- a/spice_codegen.py
> +++ b/spice_codegen.py
> @@ -105,6 +105,30 @@ def write_enums(writer, describe=False):
>  
>  writer.writeln("#endif /* _H_SPICE_ENUMS */")
>  
> +def write_content(dest_file, content, keep_identical_file):
> +if keep_identical_file:
> +try:
> +f = open(dest_file, 'rb')
> +old_content = f.read()
> +f.close()
> +
> +if content == old_content:
> +six.print_("No changes to %s" % dest_file)
> +return
> +
> +except IOError:
> +pass
> +
> +f = open(dest_file, 'wb')
> +if six.PY3:
> +f.write(bytes(content, 'UTF-8'))
> +else:
> +f.write(content)
> +f.close()
> +
> +six.print_("Wrote %s" % dest_file)
> +
> +
>  parser = OptionParser(usage="usage: %prog [options]  
> ")
>  parser.add_option("-e", "--generate-enums",
>action="store_true", dest="generate_enums", default=False,
> @@ -290,25 +314,5 @@ if options.header:
>  content = writer.header.getvalue()
>  else:
>  content = writer.getvalue()
> -if options.keep_identical_file:
> -try:
> -f = open(dest_file, 'rb')
> -old_content = f.read()
> -f.close()
> -
> -if content == old_content:
> -six.print_("No changes to %s" % dest_file)
> -sys.exit(0)
> -
> -except IOError:
> -pass
> -
> -f = open(dest_file, 'wb')
> -if six.PY3:
> -f.write(bytes(content, 'UTF-8'))
> -else:
> -f.write(content)
> -f.close()
> -
> -six.print_("Wrote %s" % dest_file)
> +write_content(dest_file, content, options.keep_identical_file)
>  sys.exit(0)
> -- 
> 2.20.1
> 
> ___
> Spice-devel mailing list
> Spice-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/spice-devel


signature.asc
Description: PGP signature
___
Spice-devel mailing list
Spice-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/spice-devel

[Spice-devel] [PATCH spice-common 2/9] codegen: Factor out a function to write output file

2019-03-03 Thread Frediano Ziglio
This will be reused to generate C declaration automatically.

Signed-off-by: Frediano Ziglio 
---
 spice_codegen.py | 46 +-
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/spice_codegen.py b/spice_codegen.py
index 76d7c5e..7e22092 100755
--- a/spice_codegen.py
+++ b/spice_codegen.py
@@ -105,6 +105,30 @@ def write_enums(writer, describe=False):
 
 writer.writeln("#endif /* _H_SPICE_ENUMS */")
 
+def write_content(dest_file, content, keep_identical_file):
+if keep_identical_file:
+try:
+f = open(dest_file, 'rb')
+old_content = f.read()
+f.close()
+
+if content == old_content:
+six.print_("No changes to %s" % dest_file)
+return
+
+except IOError:
+pass
+
+f = open(dest_file, 'wb')
+if six.PY3:
+f.write(bytes(content, 'UTF-8'))
+else:
+f.write(content)
+f.close()
+
+six.print_("Wrote %s" % dest_file)
+
+
 parser = OptionParser(usage="usage: %prog [options]  
")
 parser.add_option("-e", "--generate-enums",
   action="store_true", dest="generate_enums", default=False,
@@ -290,25 +314,5 @@ if options.header:
 content = writer.header.getvalue()
 else:
 content = writer.getvalue()
-if options.keep_identical_file:
-try:
-f = open(dest_file, 'rb')
-old_content = f.read()
-f.close()
-
-if content == old_content:
-six.print_("No changes to %s" % dest_file)
-sys.exit(0)
-
-except IOError:
-pass
-
-f = open(dest_file, 'wb')
-if six.PY3:
-f.write(bytes(content, 'UTF-8'))
-else:
-f.write(content)
-f.close()
-
-six.print_("Wrote %s" % dest_file)
+write_content(dest_file, content, options.keep_identical_file)
 sys.exit(0)
-- 
2.20.1

___
Spice-devel mailing list
Spice-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/spice-devel