77 lines
1.8 KiB
Python
77 lines
1.8 KiB
Python
|
# -*- Encoding: utf-8 -*- #
|
||
|
|
||
|
|
||
|
# [0.0] Import
|
||
|
#=======================================================#
|
||
|
import os; # ls, etc
|
||
|
import re; # regex
|
||
|
|
||
|
|
||
|
|
||
|
# [0.1] Dépendances
|
||
|
#=======================================================#
|
||
|
def filter(files, filter):
|
||
|
filtered = [];
|
||
|
for file in files:
|
||
|
if( re.search('\.'+filter+'$', file) != None ):
|
||
|
filtered.append(file);
|
||
|
return filtered;
|
||
|
|
||
|
|
||
|
|
||
|
# [1] On récupère la racine souhaitée
|
||
|
#=======================================================#
|
||
|
new_root = raw_input('New root: ');
|
||
|
print "Validate '%s'" % new_root,
|
||
|
root_validate = raw_input('[y/n] ? ');
|
||
|
|
||
|
# (1) Non validation de la route -> on quitte #
|
||
|
if( root_validate != 'y' ):
|
||
|
print 'Exiting.\n';
|
||
|
exit();
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
# [2] On récupère tous les fichiers
|
||
|
#=======================================================#
|
||
|
all_files = [];
|
||
|
for (dir, _, files) in os.walk('.'):
|
||
|
for f in files:
|
||
|
path = os.path.join(dir, f)
|
||
|
if os.path.exists(path):
|
||
|
all_files.append(path);
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
# [3] On remplace dans tous les fichiers
|
||
|
#=======================================================#
|
||
|
# (1) Pour chaque fichier #
|
||
|
for filename in filter(all_files,'php')+filter(all_files,'js')+filter(all_files,'scss')+filter(all_files, 'css'):
|
||
|
print '%s ... ' % filename,;
|
||
|
|
||
|
content = [];
|
||
|
|
||
|
# (2) On lit le fichier et récupère toutes les lignes #
|
||
|
with open(filename, 'r') as f:
|
||
|
content = f.readlines();
|
||
|
|
||
|
# (3) Pour chaque ligne, on remplace #
|
||
|
for (i, line) in enumerate(content):
|
||
|
newline = re.sub(
|
||
|
'(?P<bef>["\'])([^\']+/?)(?P<aft>f/[^\'.]+["\'])',
|
||
|
'\g<bef>/'+ new_root +'/\g<aft>',
|
||
|
line
|
||
|
);
|
||
|
content[i] = newline;
|
||
|
|
||
|
# (4) On écrit ce qu'on a modifié #
|
||
|
with open(filename, 'w') as f:
|
||
|
for line in content:
|
||
|
f.write(line);
|
||
|
|
||
|
|
||
|
print ' done';
|