1# This Source Code Form is subject to the terms of the Mozilla Public 2# License, v. 2.0. If a copy of the MPL was not distributed with this 3# file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5'''A generic script to add entries to a file 6if the entry does not already exist. 7 8Usage: buildlist.py <filename> <entry> [<entry> ...] 9''' 10from __future__ import absolute_import, print_function 11 12import sys 13import os 14 15from mozbuild.util import ( 16 ensureParentDir, 17 lock_file, 18) 19 20def addEntriesToListFile(listFile, entries): 21 """Given a file |listFile| containing one entry per line, 22 add each entry in |entries| to the file, unless it is already 23 present.""" 24 ensureParentDir(listFile) 25 lock = lock_file(listFile + ".lck") 26 try: 27 if os.path.exists(listFile): 28 f = open(listFile) 29 existing = set(x.strip() for x in f.readlines()) 30 f.close() 31 else: 32 existing = set() 33 for e in entries: 34 if e not in existing: 35 existing.add(e) 36 with open(listFile, 'wb') as f: 37 f.write("\n".join(sorted(existing))+"\n") 38 finally: 39 lock = None 40 41 42def main(args): 43 if len(args) < 2: 44 print("Usage: buildlist.py <list file> <entry> [<entry> ...]", 45 file=sys.stderr) 46 return 1 47 48 return addEntriesToListFile(args[0], args[1:]) 49 50 51if __name__ == '__main__': 52 sys.exit(main(sys.argv[1:])) 53