Skip to content

Instantly share code, notes, and snippets.

@chengsoonong
Created May 31, 2016 00:30
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 chengsoonong/772b8bfde9aac78cffd469a0ed4a3ebc to your computer and use it in GitHub Desktop.
Save chengsoonong/772b8bfde9aac78cffd469a0ed4a3ebc to your computer and use it in GitHub Desktop.
Get repo URLs. Look one level deeper for git, hg or svn repositories, and get their original URLs
"""Look one level deeper for git, hg or svn repositories, and get their original URLs"""
import os
import subprocess
import argparse
def scan_repo():
"""Extract the URL of the repository"""
get_git_url = 'git config --get remote.origin.url'
get_hg_url = 'hg paths default'
get_svn_url = "svn info | sed -ne 's/URL: //p'"
try:
url = subprocess.check_output(get_git_url, shell=True)
except subprocess.CalledProcessError:
try:
url = subprocess.check_output(get_hg_url, shell=True)
except subprocess.CalledProcessError:
try:
url = b'SVN:' + subprocess.check_output(get_svn_url, shell=True)
except subprocess.CalledProcessError:
print('Could not find git, hg or svn, on folder {}'.format(os.getcwd()))
url = b''
return url.decode(encoding='UTF-8')
def scan_one_level(root_dir, outfile):
"""For all directories in root_dir, get the URL,
and save to outfile. (Not recursive).
"""
cwd = os.getcwd()
all_urls = []
for cur_dir in os.listdir(root_dir):
if os.path.isdir(root_dir+cur_dir):
print('scanning {}'.format(root_dir+cur_dir))
os.chdir(root_dir+cur_dir)
all_urls.append(scan_repo())
os.chdir(cwd)
with open(outfile, 'w') as f:
f.writelines(all_urls)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('root_dir', help='Directory where you scan')
parser.add_argument('outfile', help='File where URLs are saved')
args = parser.parse_args()
scan_one_level(args.root_dir, args.outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment