blob: ac0a107caf8d417e32506c15808fd0453249c3e9 [file] [log] [blame]
Tim Edwards995c1332020-09-25 15:33:58 -04001#!/usr/bin/env python3
2#
3# sort_pdkfiles.py
4#
5# Read "filelist.txt" which is a list of all files to be compiled.
6# Do a natural sort, which puts the "base" files (those without a
7# drive strength) in front of "top level" files (those with a drive
8# strength), and orders the drive strengths numerically.
9#
10# Note that foundry_install.py executes this script using 'subprocess'
11# in the directory where "filelist.txt" and the files are located. No
12# path components are in the file list.
13
14import re
15import os
16import sys
17
18# Natural sort thanks to Mark Byers in StackOverflow
19def natural_sort(l):
20 convert = lambda text: int(text) if text.isdigit() else text.lower()
21 alphanum_key= lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
22 return sorted(l, key = alphanum_key)
23
24def pdk_sort(destdir):
25 if not os.path.isfile(destdir + '/filelist.txt'):
26 print('No file "filelist.txt" in ' + destdir + '. . . Nothing to sort.')
27 sys.exit()
28
29 with open(destdir + '/filelist.txt', 'r') as ifile:
30 vlist = ifile.read().splitlines()
31
32 vlist = natural_sort(vlist)
33
34 with open(destdir + '/filelist.txt', 'w') as ofile:
35 for vfile in vlist:
36 print(vfile, file=ofile)
37
38if __name__ == '__main__':
39
40 # This script expects to get one argument, which is a path name
41
42 options = []
43 arguments = []
44 for item in sys.argv[1:]:
45 if item.find('-', 0) == 0:
46 options.append(item[1:])
47 else:
48 arguments.append(item)
49
50 if len(arguments) > 0:
51 destdir = arguments[0]
52
53 pdk_sort(destdir)
54 sys.exit(0)
55