This repository has been archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add find_coffee_requirements.py so you can find coffee
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#! /usr/bin/env python | ||
from __future__ import print_function | ||
|
||
import argparse | ||
import re | ||
|
||
# = require blah | ||
#= require blah | ||
#= require "blah" | ||
#=require blah | ||
require_regex = re.compile("#[ ]*=[ ]*require[ \"']*(.+?)[\"'\s]+") | ||
|
||
|
||
def get_require_lines(filename): | ||
require_lines = [] | ||
|
||
with open(filename) as f: | ||
for line in f: | ||
matches = re.match(require_regex, line) | ||
|
||
if matches is not None: | ||
require = matches.groups()[0] | ||
require_lines.append(require) | ||
return require_lines | ||
|
||
|
||
# get_requirement_filenames(starting_filename: str) -> Tuple[List[str], List[str]] | ||
def get_requirement_filenames(assets_dir, starting_filename): | ||
all_file_names = [] | ||
missing_file_names = [] | ||
|
||
require_lines = get_require_lines(starting_filename) | ||
|
||
for require in require_lines: | ||
try: | ||
current_require_lines, current_missing_filenames = get_requirement_filenames( | ||
assets_dir, | ||
"{assets_dir}{require}.js.coffee".format( | ||
assets_dir=assets_dir, | ||
require=require | ||
) | ||
) | ||
all_file_names.extend(current_require_lines) | ||
missing_file_names.extend(current_missing_filenames) | ||
except IOError: | ||
missing_file_names.append(require) | ||
continue | ||
|
||
all_file_names.extend( | ||
require for require in require_lines | ||
if require not in missing_file_names | ||
) | ||
|
||
return (list(set(all_file_names)), list(set(missing_file_names))) | ||
|
||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Check deps matching between a parent and a sub') | ||
|
||
# example 'app/assets/javascripts/teach/course-creation-component.js.coffee' | ||
parser.add_argument('filename', help='The file to use for starting the requirement search') | ||
args = parser.parse_args() | ||
parser.add_argument('--asset_dir', help='Asset dir to look into', default='./') | ||
|
||
(requirement_filenames, missing_filenames) = get_requirement_filenames(args.asset_dir, args.filename) | ||
|
||
print('\n'.join(requirement_filenames)) | ||
print('--------------\n But I couldn\'t find the following files:\n\n') | ||
print('\n'.join(missing_filenames)) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |