import glob
import re
-USAGE = ''' Usage: makelsr.py LSR_SNIPPETS_DIR
+USAGE = ''' Usage: makelsr.py [LSR_SNIPPETS_DIR]
This script must be run from top of the source tree;
it updates snippets input/lsr with snippets in input/new or LSR_SNIPPETS_DIR.
+If a snippet is present in both directories, the one from input/new is preferred.
'''
LY_HEADER_LSR = '''%% Do not edit this file; it is auto-generated from LSR http://lsr.dsi.unimi.it
sys.stderr.write (USAGE)
sys.exit (n)
-try:
+if len (sys.argv) >= 2:
in_dir = sys.argv[1]
-except:
- exit_with_usage (2)
-
-if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
- exit_with_usage (3)
+ if len (sys.argv) >= 3:
+ exit_with_usage (2)
+ if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
+ exit_with_usage (3)
+else:
+ in_dir = ''
unsafe = []
unconverted = []
texidoc_translations = texidoc_translations.replace ('\\', '\\\\')
s = begin_header_re.sub ('\\g<0>\n' + texidoc_translations, s, 1)
- if in_dir in srcdir:
+ if in_dir and in_dir in srcdir:
s = LY_HEADER_LSR + add_tags (s, tags)
else:
s = LY_HEADER_NEW + s
return s, l
-def dump_file_list (file, list):
+def dump_file_list (file, file_list, update=False):
+ if update:
+ old_list = set (open (file, 'r').read ().splitlines ())
+ old_list.update (file_list)
+ new_list = list (old_list)
+ else:
+ new_list = file_list
f = open (file, 'w')
- f.write ('\n'.join (list) + '\n')
+ f.write ('\n'.join (sorted (new_list)) + '\n')
+
+if in_dir:
+ ## clean out existing lys and generated files
+ map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
+ glob.glob (os.path.join (DEST, '*.snippet-list')))
-## clean out existing lys and generated files
-map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
- glob.glob (os.path.join (DEST, '*.snippet-list')))
+ # read LSR source where tags are defined by subdirs
+ snippets, tag_lists = read_source_with_dirs (in_dir)
-# read LSR source where tags are defined by subdirs
-snippets, tag_lists = read_source_with_dirs (in_dir)
-# read input/new where tags are directly
-s, l = read_source (NEW_LYS)
-snippets.update (s)
-for t in TAGS:
- tag_lists[t].update (l[t])
+ # read input/new where tags are directly defined
+ s, l = read_source (NEW_LYS)
+ snippets.update (s)
+ for t in TAGS:
+ tag_lists[t].update (l[t])
+else:
+ snippets, tag_lists = read_source (NEW_LYS)
for (name, (srcdir, tags)) in snippets.items ():
copy_ly (srcdir, name, tags)
-
for (tag, file_set) in tag_lists.items ():
- dump_file_list (os.path.join (DEST, tag + '.snippet-list'), sorted(file_set))
-
+ dump_file_list (os.path.join (DEST, tag + '.snippet-list'), file_set, update=not(in_dir))
if unconverted:
sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
sys.stderr.write ('\n'.join (unconverted) + '\n\n')
-
if notags_files:
sys.stderr.write ('No tags could be found in these files:\n')
sys.stderr.write ('\n'.join (notags_files) + '\n\n')