Created
July 2, 2024 22:21
-
-
Save JeremyGrosser/dadd28162ec8f543a3a479b5a4bdad8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
''' | |
Create symlinks to .ads and .adb files with expanded filenames. | |
''' | |
import os.path | |
import os | |
import sys | |
sources = ['src'] | |
links = 'links' | |
def package_name(path): | |
with open(path, 'r') as fd: | |
for line in fd.readlines(): | |
if line.startswith('package '): | |
line = line.split(' ')[1:] | |
if line[0] == 'body': | |
line = line[1:] | |
return line[0].strip('\r\n\t $') | |
def basename(filename): | |
return filename.split('.', 1)[0] | |
bases = {} | |
if not os.path.exists('links'): | |
os.makedirs('links') | |
for source in sources: | |
for dirpath, dirnames, filenames in os.walk(source): | |
for filename in filenames: | |
filename = filename.strip('\r\n\t $') | |
path = os.path.join(dirpath, filename) | |
pn = package_name(path) | |
if pn is not None: | |
bases[basename(filename)] = pn | |
for dirpath, dirnames, filenames in os.walk(source): | |
for filename in filenames: | |
filename = filename.strip('\r\n\t $') | |
path = os.path.join(dirpath, filename) | |
pn = bases.get(basename(filename), None) | |
if pn is not None: | |
newname = '%s.%s' % (pn.lower().replace('.', '-'), filename.rsplit('.', 1)[-1]) | |
newname = newname.strip('\r\n\t ') | |
newname = os.path.join(links, newname) | |
path = os.path.join('../', path) | |
print('ln', '-s', path, newname) | |
os.symlink(path, newname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment