Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / src / gen-ragel-artifacts.py
1 #!/usr/bin/env python3
2
3 import os, re, sys, subprocess, shutil, tempfile
4
5 os.chdir (os.path.dirname (__file__))
6
7 if len (sys.argv) < 2:
8         ragel_sources = [x for x in os.listdir ('.') if x.endswith ('.rl')]
9 else:
10         ragel_sources = sys.argv[2:]
11
12 ragel = shutil.which ('ragel')
13
14 if not ragel:
15         print ('You have to install ragel if you are going to develop HarfBuzz itself')
16         exit (1)
17
18 if not len (ragel_sources):
19         exit (77)
20
21 tempdir = tempfile.mkdtemp ()
22
23 for rl in ragel_sources:
24         hh = rl.replace ('.rl', '.hh')
25         shutil.copy (rl, tempdir)
26         # writing to stdout has some complication on Windows
27         subprocess.Popen ([ragel, '-e', '-F1', '-o', hh, rl], cwd=tempdir).wait ()
28
29         generated_path = os.path.join (tempdir, hh)
30         with open (generated_path, "rb") as temp_file:
31                 generated = temp_file.read()
32
33         with open (hh, "rb") as current_file:
34                 current = current_file.read()
35
36         # overwrite only if is changed
37         if generated != current:
38                 shutil.copyfile (generated_path, hh)
39
40 shutil.rmtree (tempdir)