00001
00002
00003 import urllib
00004 import yaml
00005
00006
00007 def parse_package(package_string, rosdistro):
00008 depends = []
00009 for l in package_string.splitlines():
00010 if 'Package: ' in l:
00011 package = deb_to_stack(l.split('Package: ')[1], rosdistro)
00012 elif 'Depends' in l:
00013 dep_str = l.split('Depends: ')[1]
00014 for d in dep_str.split(', '):
00015 if '(' in d:
00016 dep = deb_to_stack(d.split(' ')[0], rosdistro)
00017 if dep:
00018 depends.append(dep)
00019 elif 'Description: Meta package for' in l:
00020 return (None, None)
00021
00022 return (package, depends)
00023
00024
00025
00026 def deb_to_stack(deb_name, rosdistro):
00027 if not deb_name.startswith('ros-%s'%rosdistro):
00028 return None
00029 return '_'.join(deb_name.split('-')[2:])
00030
00031
00032
00033 class AptParser:
00034 def __init__(self, string, rosdistro):
00035 self.depends_list = {}
00036 self.depends_on_list = {}
00037
00038 for package in string.split('Package: '):
00039 if len(package) > 0:
00040 (package, depends) = parse_package('Package: '+package, rosdistro)
00041 if package:
00042 if not package in self.depends_on_list:
00043 self.depends_on_list[package] = []
00044
00045 self.depends_list[package] = depends
00046 for d in depends:
00047 if d in self.depends_on_list:
00048 if not package in self.depends_on_list[d]:
00049 self.depends_on_list[d].append(package)
00050 else:
00051 self.depends_on_list[d] = [package]
00052
00053 def depends(self, pkg):
00054 return self._get_one_depth(pkg, self.depends_list)
00055
00056 def depends_on(self, pkg):
00057 return self._get_one_depth(pkg, self.depends_on_list)
00058
00059 def depends_all(self, pkg):
00060 return self._get_all_depth(pkg, self.depends_list)
00061
00062 def depends_on_all(self, pkg):
00063 return self._get_all_depth(pkg, self.depends_on_list)
00064
00065 def has_debian_package(self, pkg):
00066 if type(pkg) != list:
00067 pkg = [pkg]
00068 for p in pkg:
00069 if not p in self.depends_list:
00070 return False
00071 return True
00072
00073
00074 def _get_all_depth(self, pkg, my_list):
00075 res = []
00076 for p in self._get_one_depth(pkg, my_list):
00077 self._add_recursive(p, res, my_list)
00078 return res
00079
00080 def _get_one_depth(self, pkg, my_list):
00081 if (type(pkg) != list):
00082 return my_list[pkg]
00083 res = []
00084 for p in pkg:
00085 for d in my_list[p]:
00086 if not d in res:
00087 res.append(d)
00088 return res
00089
00090
00091 def _add_recursive(self, pkg, res_list, my_list):
00092 if not pkg in res_list:
00093 res_list.append(pkg)
00094 for p in self._get_one_depth(pkg, my_list):
00095 self._add_recursive(p, res_list, my_list)
00096
00097
00098
00099 def parse_apt(ubuntudistro, arch, rosdistro):
00100 url_name = 'http://packages.ros.org/ros-shadow-fixed/ubuntu/dists/%s/main/binary-%s/Packages'%(ubuntudistro, arch)
00101 parser = AptParser(urllib.urlopen(url_name).read(), rosdistro)
00102 return parser
00103
00104
00105
00106 def main():
00107 parser = parse_apt('lucid', 'amd64', 'diamondback')
00108
00109 dep = parser.depends('robot_model')
00110 dep.sort()
00111 print dep
00112
00113 dep_all = parser.depends_all('robot_model')
00114 dep_all.sort()
00115 print dep_all
00116
00117 dep_on = parser.depends_on('robot_model')
00118 dep_on.sort()
00119 print dep_on
00120
00121 dep_on_all = parser.depends_on_all('robot_model')
00122 dep_on_all.sort()
00123 print dep_on_all
00124
00125
00126
00127
00128 if __name__ == '__main__':
00129 main()