grub/import_gcry.py

158 lines
6.0 KiB
Python
Raw Normal View History

2009-11-09 16:13:17 +00:00
import re
import sys
import os
import datetime
if len (sys.argv) < 3:
print ("Usage: %s SOURCE DESTINATION" % sys.argv[0])
exit (0)
indir = sys.argv[1]
outdir = sys.argv[2]
basedir = os.path.join (outdir, "gcry")
os.makedirs (basedir)
cipher_dir_in = os.path.join (indir, "cipher")
cipher_dir_out = os.path.join (basedir, "cipher")
os.makedirs (cipher_dir_out)
cipher_files = os.listdir (cipher_dir_in)
conf = open (os.path.join (outdir, "conf", "gcry.rmk"), "w")
conf.write ("# -*- makefile -*-\n\n")
chlog = ""
for cipher_file in cipher_files:
infile = os.path.join (cipher_dir_in, cipher_file)
outfile = os.path.join (cipher_dir_out, cipher_file)
if cipher_file == "ChangeLog":
continue
chlognew = " * %s" % cipher_file
nch = False
if re.match (".*\.[ch]$", cipher_file):
isc = re.match (".*\.c$", cipher_file)
f = open (infile, "r")
fw = open (outfile, "w")
fw.write ("/* This file was automatically imported with \n")
fw.write (" import_gcry.py. Please don't modify it */\n");
ciphernames = []
2009-11-10 00:01:11 +00:00
mdnames = []
2009-11-09 16:13:17 +00:00
for line in f:
m = re.match ("#include <.*>", line)
if not m is None:
chmsg = "Removed including of %s" % \
m.group () [len ("#include <"):len (m.group ()) - 1]
if nch:
chlognew = "%s\n %s" % (chlognew, chmsg)
else:
chlognew = "%s: %s" % (chlognew, chmsg)
nch = True
continue
m = re.match ("gcry_cipher_spec_t", line)
if isc and not m is None:
ciphername = line [len ("gcry_cipher_spec_t"):].strip ()
ciphername = re.match("[a-zA-Z0-9_]*",ciphername).group ()
ciphernames.append (ciphername)
2009-11-10 00:01:11 +00:00
m = re.match ("gcry_md_spec_t", line)
if isc and not m is None:
mdname = line [len ("gcry_md_spec_t"):].strip ()
mdname = re.match("[a-zA-Z0-9_]*",mdname).group ()
mdnames.append (mdname)
2009-11-09 16:13:17 +00:00
fw.write (line)
2009-11-10 00:01:11 +00:00
if len (ciphernames) > 0 or len (mdnames) > 0:
2009-11-09 16:13:17 +00:00
modname = cipher_file [0:len(cipher_file) - 2]
2009-11-10 00:01:11 +00:00
if re.match (".*-glue$", modname):
modfiles = "gcry/cipher/%s gcry/cipher/%s" \
% (cipher_file, cipher_file.replace ("-glue.c", ".c"))
modname = modname.replace ("-glue", "")
else:
modfiles = "gcry/cipher/%s" % cipher_file
modname = "gcry_%s" % modname
2009-11-09 16:13:17 +00:00
chmsg = "(GRUB_MOD_INIT(%s)): New function\n" % modname
if nch:
chlognew = "%s\n %s" % (chlognew, chmsg)
else:
chlognew = "%s%s" % (chlognew, chmsg)
nch = True
fw.write ("\n\nGRUB_MOD_INIT(%s)\n" % modname)
fw.write ("{\n")
for ciphername in ciphernames:
chmsg = "Register cipher %s" % ciphername
chlognew = "%s\n %s" % (chlognew, chmsg)
fw.write (" grub_cipher_register (&%s);\n" % ciphername)
2009-11-10 00:01:11 +00:00
for mdname in mdnames:
chmsg = "Register digest %s" % mdname
chlognew = "%s\n %s" % (chlognew, chmsg)
fw.write (" grub_md_register (&%s);\n" % mdname)
2009-11-09 16:13:17 +00:00
fw.write ("}")
chmsg = "(GRUB_MOD_FINI(%s)): New function\n" % modname
chlognew = "%s\n %s" % (chlognew, chmsg)
fw.write ("\n\nGRUB_MOD_FINI(%s)\n" % modname)
fw.write ("{\n")
for ciphername in ciphernames:
chmsg = "Unregister cipher %s" % ciphername
chlognew = "%s\n %s" % (chlognew, chmsg)
fw.write (" grub_cipher_unregister (&%s);\n" % ciphername)
2009-11-10 00:01:11 +00:00
for mdname in mdnames:
chmsg = "Unregister MD %s" % mdname
chlognew = "%s\n %s" % (chlognew, chmsg)
fw.write (" grub_md_unregister (&%s);\n" % mdname)
2009-11-09 16:13:17 +00:00
fw.write ("}\n")
conf.write ("pkglib_MODULES += %s.mod\n" % modname)
2009-11-10 00:01:11 +00:00
conf.write ("%s_mod_SOURCES = %s\n" %\
(modname, modfiles))
2009-11-09 19:30:40 +00:00
conf.write ("%s_mod_CFLAGS = $(COMMON_CFLAGS) -Wno-missing-field-initializers -Wno-error\n" % modname)
conf.write ("%s_mod_LDFLAGS = $(COMMON_LDFLAGS)\n\n" % modname)
2009-11-10 00:01:11 +00:00
elif isc and cipher_file != "camellia.c":
print ("WARNING: C file isn't a module: %s" % cipher_file)
2009-11-09 16:13:17 +00:00
f.close ()
fw.close ()
if nch:
chlog = "%s%s\n" % (chlog, chlognew)
continue
if re.match ("(Manifest|Makefile\.am)$", cipher_file):
chlog = "%s%sRemoved\n" % (chlog, chlognew)
continue
# Autogenerated files. Not even worth mentionning in ChangeLog
if re.match ("Makefile\.in$", cipher_file):
chlog = "%s%sRemoved\n" % (chlog, chlognew)
continue
chlog = "%s%sSkipped unknown file\n" % (chlog, chlognew)
print ("WARNING: unknown file %s" % cipher_file)
outfile = os.path.join (cipher_dir_out, "types.h")
fw=open (outfile, "w")
fw.write ("#include <grub/types.h>\n")
fw.write ("#include <grub/gcry_wrap.h>\n")
chlog = "%s * types.h: New file.\n" % chlog
fw.close ()
outfile = os.path.join (cipher_dir_out, "cipher.h")
fw=open (outfile, "w")
fw.write ("#include <grub/crypto.h>\n")
fw.write ("#include <grub/gcry_wrap.h>\n")
chlog = "%s * cipher.h: Likewise.\n" % chlog
fw.close ()
outfile = os.path.join (cipher_dir_out, "g10lib.h")
fw=open (outfile, "w")
fw.write ("#include <grub/gcry_wrap.h>\n")
chlog = "%s * g10lib.h: Likewise.\n" % chlog
fw.close ()
infile = os.path.join (cipher_dir_in, "ChangeLog")
outfile = os.path.join (cipher_dir_out, "ChangeLog")
f=open (infile, "r")
fw=open (outfile, "w")
dt = datetime.date.today ()
fw.write ("%04d-%02d-%02d Automatic import tool\n" % \
(dt.year,dt.month, dt.day))
fw.write ("\n")
fw.write (" Imported ciphers to GRUB\n")
fw.write ("\n")
fw.write (chlog)
fw.write ("\n")
for line in f:
fw.write (line)
f.close ()
fw.close ()