Skip to content

Instantly share code, notes, and snippets.

@pirogoeth
Last active March 5, 2018 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirogoeth/19ea425cc7405dcdbdceb65ae64ff639 to your computer and use it in GitHub Desktop.
Save pirogoeth/19ea425cc7405dcdbdceb65ae64ff639 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import io
import os
import re
import sys
from os import path
basedir = os.getcwd()
import_line = re.compile(r'''^import.+'(?P<importpath>(?P<backtrack>(?:\.\./?){1,})(?P<fileref>.+))';?$''')
try:
target_file = sys.argv[1]
except IndexError:
print('usage: {} [target_file]'.format(sys.argv[0]))
sys.exit(1)
target_file = path.abspath(target_file)
if not path.exists(target_file):
print('{}: path does not exist'.format(target_file))
sys.exit(1)
def path_diff(base, target):
''' Difference between base and target.
Returns the part of target that is not common.
'''
common = path.commonprefix([base, target])
return target[len(common)+1:]
parent_path = path.dirname(target_file)
print('- base dir:', basedir, file=sys.stderr)
print(' parent dir:', parent_path, file=sys.stderr)
print(' rewriting target:', target_file, file=sys.stderr)
print(' imports:', file=sys.stderr)
contents = []
with io.open(target_file, 'r') as f:
for line in f.readlines():
imp = import_line.match(line)
if not imp:
contents.append(line.rstrip())
continue
relative_path = imp.group('importpath')
_, extension = path.splitext(relative_path)
if len(extension) == 0:
# Assume no extension == `.js`
relative_path_ext = '{}.js'.format(relative_path)
else:
relative_path_ext = relative_path
absolute_path = path.join(parent_path, relative_path_ext)
resolved_path = path.abspath(absolute_path)
new_import_path, _ = path.splitext(path_diff(basedir, resolved_path))
old_string = imp.string.strip()
new_string = old_string.replace(relative_path, new_import_path)
print(' - string: {}'.format(old_string), file=sys.stderr)
print(' groups:', file=sys.stderr)
print(' backtrack: {}'.format(imp.group('backtrack')), file=sys.stderr)
print(' fileref: {}'.format(imp.group('fileref')), file=sys.stderr)
print(' importpath: {}'.format(imp.group('importpath')), file=sys.stderr)
print(' absolute_import_path: {}'.format(absolute_path), file=sys.stderr)
print(' resolved_import_path: {}'.format(resolved_path), file=sys.stderr)
if not path.exists(resolved_path):
print(' error: resolve_import_path does not exist', file=sys.stderr)
continue
print(' new_import_path: {}'.format(new_import_path), file=sys.stderr)
print(' new_import_string: {}'.format(new_string), file=sys.stderr)
contents.append(new_string)
if os.getenv('DRY_RUN', 'false').lower() in ['1', 't', 'true', 'y', 'yes']:
sys.exit(0)
with io.open(target_file, 'r') as src:
with io.open(target_file + '.bak', 'w') as dest:
dest.write(src.read())
with io.open(target_file, 'w') as dest:
dest.write('\n'.join(contents))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment