Moved sort_pdkfiles.py to common/, and modified behavior to always use the
default sort (which is a natural sort), given that doing "glob.glob" on a
file directory results in a pretty arbitrary and meaningless ordering.
diff --git a/common/foundry_install.py b/common/foundry_install.py
index 3fa7f1c..a3d048b 100755
--- a/common/foundry_install.py
+++ b/common/foundry_install.py
@@ -106,15 +106,16 @@
# Each file is passed through the filter script
# before writing into the staging area.
#
-# sort: Optionally followed by "=" and the name of a script.
+# sort: Followed by "=" and the name of a script.
# The list of files to process (after applying items
# from "exclude") will be written to a file
# "filelist.txt", which will be used by the
-# library compile routines, if present. If a script
-# name is specified, then the sort script will rewrite
-# the file with the order in which entries should
-# appear in the compiled library. Only useful when
-# used with "compile" or "compile-only".
+# library compile routines, if present. The sort
+# script will rewrite the file with the order in
+# which entries should appear in the compiled library.
+# Only useful when used with "compile" or "compile-only".
+# If not specified, files are sorted by "natural sort"
+# order.
#
# noconvert : Install only; do not attempt to convert to other
# formats (applies only to GDS, CDL, and LEF).
@@ -458,6 +459,9 @@
# Create the target directory
os.makedirs(targetdir, exist_ok=True)
+ # Here's where common scripts are found:
+ scriptdir = os.path.split(os.getcwd())[0] + '/common'
+
#----------------------------------------------------------------
# Installation part 1: Install files into the staging directory
#----------------------------------------------------------------
@@ -726,16 +730,14 @@
else:
print('Renaming file to: ' + newname)
- # Option 'sort' may have an argument. . .
+ # Option 'sort' has an argument. . .
try:
sortscript = list(item.split('=')[1] for item in option if item.startswith('sort'))[0]
except IndexError:
- sortscript = None
+ # If option 'sort' is not specified, then use the "natural sort" script
+ sortscript = scriptdir + '/sort_pdkfiles.py'
else:
print('Sorting files with script ' + sortscript)
- # . . . or not.
- if 'sort' in option:
- sortscript = True
# 'anno' may be specified for LEF, in which case the LEF is used only
# to annotate GDS and is not itself installed; this allows LEF to
@@ -890,12 +892,10 @@
vfilter(targname)
if do_remove_spec:
- scriptdir = os.path.split(os.getcwd())[0] + '/common'
local_filter_scripts.append(scriptdir + '/remove_specify.py')
elif option[0] == 'cdl' or option[0] == 'spi' or option[0] == 'spice':
if do_stub:
- scriptdir = os.path.split(os.getcwd())[0] + '/common'
local_filter_scripts.append(scriptdir + '/makestub.py')
for filter_script in local_filter_scripts:
@@ -905,7 +905,6 @@
destfilelist.append(os.path.split(targname)[1])
if sortscript:
- print('Diagnostic: Sorting files to compile.')
with open(destlibdir + '/filelist.txt', 'w') as ofile:
for destfile in destfilelist:
print(destfile, file=ofile)
@@ -1841,7 +1840,6 @@
# The directory with scripts should be in ../common with respect
# to the Makefile that determines the cwd.
- scriptdir = os.path.split(os.getcwd())[0] + '/common'
# Run cdl2spi.py script to read in the CDL file and write out SPICE
for cdlfile in cdlfiles:
diff --git a/common/sort_pdkfiles.py b/common/sort_pdkfiles.py
new file mode 100755
index 0000000..ac0a107
--- /dev/null
+++ b/common/sort_pdkfiles.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+#
+# sort_pdkfiles.py
+#
+# Read "filelist.txt" which is a list of all files to be compiled.
+# Do a natural sort, which puts the "base" files (those without a
+# drive strength) in front of "top level" files (those with a drive
+# strength), and orders the drive strengths numerically.
+#
+# Note that foundry_install.py executes this script using 'subprocess'
+# in the directory where "filelist.txt" and the files are located. No
+# path components are in the file list.
+
+import re
+import os
+import sys
+
+# Natural sort thanks to Mark Byers in StackOverflow
+def natural_sort(l):
+ convert = lambda text: int(text) if text.isdigit() else text.lower()
+ alphanum_key= lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
+ return sorted(l, key = alphanum_key)
+
+def pdk_sort(destdir):
+ if not os.path.isfile(destdir + '/filelist.txt'):
+ print('No file "filelist.txt" in ' + destdir + '. . . Nothing to sort.')
+ sys.exit()
+
+ with open(destdir + '/filelist.txt', 'r') as ifile:
+ vlist = ifile.read().splitlines()
+
+ vlist = natural_sort(vlist)
+
+ with open(destdir + '/filelist.txt', 'w') as ofile:
+ for vfile in vlist:
+ print(vfile, file=ofile)
+
+if __name__ == '__main__':
+
+ # This script expects to get one argument, which is a path name
+
+ options = []
+ arguments = []
+ for item in sys.argv[1:]:
+ if item.find('-', 0) == 0:
+ options.append(item[1:])
+ else:
+ arguments.append(item)
+
+ if len(arguments) > 0:
+ destdir = arguments[0]
+
+ pdk_sort(destdir)
+ sys.exit(0)
+