Last active
October 16, 2017 10:16
-
-
Save Rassibassi/dbf105ed82f8486abdf65a44b8642eb3 to your computer and use it in GitHub Desktop.
HackYourFuture - change absolute links within the repository to relative links
This file contains hidden or 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
from __future__ import print_function | |
import os, re, shutil | |
def removeAbsolutePath(rawHaystack,file,overwrite=False): | |
filePathParts = file.split(".") | |
newFile = "".join(filePathParts[0:-1])+"_new."+filePathParts[-1] | |
# run through file and remove absolute path | |
f = open(file,'r') | |
f2 = open(newFile,'w') | |
fileChanged = False | |
for l in f: | |
if re.search(rawHaystack,l): | |
l = re.sub(rawHaystack,r'',l) | |
fileChanged = True | |
f2.write(l) | |
f.close() | |
f2.close() | |
# if nothing changed delete new file | |
if not fileChanged: | |
os.remove(newFile) | |
else: | |
# check for overwrite option | |
if overwrite: | |
shutil.copy(newFile, file) | |
os.remove(newFile) | |
rawHaystack = r'https:\/\/github.com\/HackYourFuture\/{}\/.*?\/{}' | |
repository = 'JavaScript' | |
branch = 'master' | |
overwrite = True | |
rawHaystack = rawHaystack.format(repository,branch) | |
path = 'JavaScript' | |
for (dirpath, dirnames, fileNames) in os.walk(path): | |
print(dirpath,dirnames) | |
if not ".git" in dirpath: | |
for filename in fileNames: | |
file = "".join([dirpath, '/', filename]) | |
print('###') | |
print(file) | |
removeAbsolutePath(rawHaystack,file,overwrite) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment