forked from facelessuser/ApplySyntax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_rails_file.py
32 lines (26 loc) · 1009 Bytes
/
is_rails_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os
import re
import platform
def is_rails_file(file_name):
windows = platform.system() == "Windows"
path = os.path.dirname(file_name)
file_name = os.path.basename(file_name).lower()
name, extension = os.path.splitext(file_name)
if name == 'gemfile':
return True
result = False
# I doubt this is the most elegant way of identifying a Rails directory structure,
# but it does work. The idea here is to work up the tree, checking at each level for
# the existence of config/routes.rb. If it's found, the assumption is made that it's
# a Rails app.
while path != '':
if os.path.exists(os.path.join(path, 'config', 'routes.rb')):
result = True
break
elif windows and re.match(r"^[A-Za-z]{1}:\\$", path) is not None:
path = ''
elif not windows and path == '/':
path = ''
else:
path = os.path.dirname(path)
return extension in ['.rb', '.rake'] and result