Guest User

fix.py

a guest
Feb 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import shutil
  4. import sys
  5. import tarfile
  6. import tempfile
  7. from sh import wget
  8. import os
  9. import yaml
  10.  
  11. FILE_PATH = os.path.dirname(os.path.realpath(__file__))
  12.  
  13.  
  14. def read_yaml(path):
  15.     with open(path, 'rt') as f:
  16.         data = yaml.safe_load(f)
  17.     return data
  18.  
  19.  
  20. def write_yaml(path, data):
  21.     with open(path, 'wt') as f:
  22.         yaml.safe_dump(data, f, default_flow_style=False)
  23.  
  24.  
  25. def extract_github_tar_gz(data):
  26.     for entry in data:
  27.         if 'tar' not in entry:
  28.             continue
  29.         tar = entry['tar']
  30.         if 'github.com' not in tar['uri'] or not tar['uri'].endswith('.tar.gz'):
  31.             continue
  32.         version = tar['uri'][tar['uri'].rindex('/') + 1: -len('.tar.gz')]
  33.         if not tar['version'].endswith(version):
  34.             continue
  35.         yield tar, version
  36.  
  37.  
  38. def download_file(url, path):
  39.     wget(
  40.         '--progress=dot:meg', '-O', path, url, _out=sys.stdout, _err=sys.stderr
  41.     )
  42.  
  43.  
  44. def check_tar_gz_version(path, version):
  45.     with tarfile.open(path, 'r:gz') as f:
  46.         name = f.getmembers()[0].name
  47.     return name.endswith(version)
  48.  
  49.  
  50. def github_bug_workaround(rosinstall, rosinstall_out):
  51.     tmp_dir = tempfile.mkdtemp()
  52.     data = read_yaml(rosinstall)
  53.     for entry, version in extract_github_tar_gz(data):
  54.         file_name = os.path.join(tmp_dir, 'tmp.tar.gz')
  55.         download_file(entry['uri'], file_name)
  56.         has_version = check_tar_gz_version(file_name, version)
  57.         os.remove(file_name)
  58.         if not has_version:
  59.             entry['version'] = entry['version'][: -(len(version) + 1)]
  60.  
  61.     write_yaml(rosinstall_out, data)
  62.  
  63.     shutil.rmtree(tmp_dir)
  64.  
  65.  
  66. if __name__ == '__main__':
  67.     rosinstall_out = os.path.join(FILE_PATH, 'kinetic-ros_comm-wet-fixed.rosinstall')
  68.     rosinstall = os.path.join(FILE_PATH, 'kinetic-ros_comm-wet.rosinstall')
  69.     github_bug_workaround(rosinstall, rosinstall_out)
Advertisement
Add Comment
Please, Sign In to add comment