summaryrefslogtreecommitdiff
path: root/replace_urls.py
diff options
context:
space:
mode:
Diffstat (limited to 'replace_urls.py')
-rwxr-xr-xreplace_urls.py101
1 files changed, 101 insertions, 0 deletions
diff --git a/replace_urls.py b/replace_urls.py
new file mode 100755
index 0000000..7fe4ffd
--- /dev/null
+++ b/replace_urls.py
@@ -0,0 +1,101 @@
+#!/usr/bin/env python
+import argparse
+import entomb
+import fnmatch
+import os
+
+
+def get_input_dirs(d):
+ result = []
+ for root, dirs, files in os.walk(d):
+ for dname in files:
+ path = os.path.join(root, dname)
+ if not fnmatch.fnmatch(path, '*/latest-*'):
+ continue
+ result.append(entomb.channel_dir(path))
+
+ return set(result)
+
+
+def get_template_dirs(d):
+ result = []
+ for root, dirs, files in os.walk(d):
+ for dname in files:
+ path = os.path.join(root, dname)
+ if not fnmatch.fnmatch(path, '*/*.tar.bz2'):
+ continue
+ kludge = '/'.join(os.path.dirname(path).rsplit('/', 3))
+ result.append(kludge)
+
+ return sorted(set(result))
+
+
+def channel_from_template(templates, needle):
+ if fnmatch.fnmatch(needle, "*/conda-dev/*"):
+ needle = needle.replace('conda-dev', 'astroconda-dev')
+
+ for dname in templates:
+ if needle in dname:
+ return dname
+
+
+def channel_from_url(url):
+ if '://' not in url:
+ raise ValueError('{} is not a valid URL')
+
+ uri_delim = '://'
+ uri_end = url.find(uri_delim) + len(uri_delim)
+ uri_orig = url[:uri_end]
+ url_parts = url[uri_end:].split('/', 2)
+
+ return uri_orig, url_parts
+
+
+def replace_urls(spec, prefix, templates, new_url):
+ with open(spec, 'r') as fp:
+ data = fp.read()
+
+ new_data = []
+
+ for line in data.splitlines():
+ line = line.strip()
+ if line.startswith('#') or line.startswith('@'):
+ new_data.append(line)
+ continue
+ tail = '/'.join(os.path.dirname(line).rsplit('/', 2)[1:])
+ needle = prefix + '/' + tail
+ intermediate = channel_from_template(templates, needle)
+ intermediate = '/'.join(intermediate.split('/')[1:])
+ url, package = line.rsplit('/', 1)
+ url = '/'.join([new_url, intermediate])
+ new_data.append('/'.join([url, package]))
+
+ return '\n'.join(new_data)
+
+ #data.replace(old_url, new_url)
+ #data.replace(old_channel, new_channel)
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+ parser.add_argument('-i', '--input-dir', required=True, help='Path to astroconda-releases')
+ parser.add_argument('-t', '--template-dir', required=True, help='Path to parent of new channel tree')
+ parser.add_argument('new_url')
+ args = parser.parse_args()
+
+ input_dir = args.input_dir
+ template_dir = args.template_dir
+ templates = get_template_dirs(template_dir)
+ new_url = args.new_url
+ dirs = get_input_dirs(input_dir)
+ specs = []
+
+ for spec_base in dirs:
+ spec_root = os.path.join(input_dir, spec_base)
+ specs.append(entomb.spec_search(spec_root, ['*{}/latest-*'.format(spec_root)]))
+
+ for spec_tree in specs:
+ for spec_file in spec_tree:
+ print("Processing: {}".format(spec_file))
+ tree = '/'.join(spec_file.split(os.sep, 3)[1:-1])
+ spec_new = replace_urls(spec_file, tree, templates, new_url)
+ open(spec_file, 'w+').write(spec_new)