diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2010-06-10 19:35:16 -0300 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2010-06-10 19:35:16 -0300 |
commit | 73e23779dd68549642c54e9bcfc0ca4d50c05a0f (patch) | |
tree | 4415025c2fc9d5fad9c52cc13177f8b5e3c44d25 | |
parent | fixes to the requirements.py script (diff) | |
download | g-octave-73e23779dd68549642c54e9bcfc0ca4d50c05a0f.tar.gz g-octave-73e23779dd68549642c54e9bcfc0ca4d50c05a0f.tar.bz2 g-octave-73e23779dd68549642c54e9bcfc0ca4d50c05a0f.zip |
changes to fix the requirements.py script; lots of trailling spaces
removed.
-rw-r--r-- | g_octave/description.py | 121 | ||||
-rwxr-xr-x | scripts/requirements.py | 68 |
2 files changed, 96 insertions, 93 deletions
diff --git a/g_octave/description.py b/g_octave/description.py index 51ef684..1952880 100644 --- a/g_octave/description.py +++ b/g_octave/description.py @@ -3,13 +3,13 @@ """ description.py ~~~~~~~~~~~~~~ - + This module implements a Python object with the content of a given DESCRIPTION file. - + DESCRIPTION files are basically key/value files with multi-line support. The separator is a ':'. - + :copyright: (c) 2009-2010 by Rafael Goncalves Martins :license: GPL-2, see LICENSE for more details. """ @@ -35,201 +35,200 @@ re_pkg_atom = re.compile(r'^(.+)-([0-9.]+)$') from exception import DescriptionException class Description(object): - + def __init__(self, file, conf=None, parse_sysreq=True): - + if conf is None: conf = Config() self._config = conf - + if not os.path.exists(file): raise DescriptionException('File not found: %s' % file) - + # dictionary with the parsed content of the DESCRIPTION file self._desc = dict() - + # current key key = None - + with open(file, 'r') as fp: for line in fp: line_splited = line.split(':') - + # 'key: value' found? if len(line_splited) >= 2: - + # by default we have a key before the first ':' key = line_splited[0].strip().lower() - + # all the stuff after the first ':' is the value # ':' included. value = ':'.join(line_splited[1:]).strip() - + # the key already exists? if key in self._desc: - + # it's one of the dependencies? if key in ('depends', 'systemrequirements', 'buildrequires'): - + # use ', ' to separate the values self._desc[key] += ', ' - + else: - + # use a single space to separate the values self._desc[key] += ' ' - + # key didn't exists yet. initializing... else: self._desc[key] = '' - + self._desc[key] += value - + # it's not a 'key: value', so it's probably a continuation # of the previous line. else: - + # empty line if len(line) == 0: continue - + # comments (started with '#') if line[0] == '#': continue - + # line continuations starts with a single space if line[0] != ' ': continue - + # the first line can't be a continuation, obviously :) if key is None: continue - + # our line already have a single space at the start. # we only needs strip spaces at the end of the line self._desc[key] += line.rstrip() - + # add the 'self_depends' key self._desc['self_depends'] = list() - + # parse the dependencies for key in self._desc: - + # depends if key == 'depends': depends = self._desc[key] self._desc[key] = self._parse_depends(depends) self._desc['self_depends'] = self._parse_self_depends(depends) - + # requirements if key in ('systemrequirements', 'buildrequires') and parse_sysreq: self._desc[key] = self._parse_depends(self._desc[key]) - + def _parse_depends(self, depends): """returns a list with gentoo atoms for the 'depends' (the other octave-forge packages or the octave itself) """ - + # the list that will be returned depends_list = list() - + for depend in depends.split(','): - + # use the 're_depends' regular expression to filter the # package name, the version an the comparator re_match = re_depends.match(depend.strip()) - + # the depend is valid? if re_match is not None: - + # initialize the atom string empty atom = '' - + # extract the needed values name = re_match.group(1) comparator = re_match.group(3) version = re_match.group(4) - + # we have a comparator and a version? if comparator is not None and version is not None: - + # special case: '==' for octave forge is '=' for gentoo if comparator == '==': atom += '=' else: atom += comparator - + try: conf_dependencies = self._config.dependencies except ConfigException: conf_dependencies = {} - + # as octave is already in the portage tree, the atom is # predefined. if name.lower() == 'octave': atom += 'sci-mathematics/octave' - + elif name in conf_dependencies: + if conf_dependencies[name] == '': + continue atom += conf_dependencies[name] - - elif name == '': - continue - + # the octave-forge packages will be put inside a "fake" # category: g-octave else: atom += 'g-octave/' + str(name) - + # append the version to the atom, if needed if comparator is not None and version is not None: atom += '-' + str(version) - + depends_list.append(atom) - + # invalid dependency atom else: raise DescriptionException('Invalid dependency atom: %s' % depend) - + return depends_list - - + + def _parse_self_depends(self, depends): """returns a list of tuples (name, comparator, version) for the other octave-forge packages. """ - + # the list that will be returned depends_list = list() - + for depend in depends.split(','): - + # use the 're_depends' regular expression to filter the # package name, the version an the comparator re_match = re_depends.match(depend.strip()) - + # the depend is valid? if re_match is not None: - + # extract the needed values name = re_match.group(1) comparator = re_match.group(3) version = re_match.group(4) - + # we need only the octave-forge packages, nor octave if name.lower() != 'octave': depends_list.append((name, comparator, version)) - + # invalid dependency atom else: raise DescriptionException('Invalid dependency atom: %s' % depend) - + return depends_list - + def __getattr__(self, name): """method that overloads the object atributes, returning the needed atribute based on the dict with the previously parsed content. """ - + return self._desc.get(name, None) diff --git a/scripts/requirements.py b/scripts/requirements.py index bc892ca..03b90b9 100755 --- a/scripts/requirements.py +++ b/scripts/requirements.py @@ -4,11 +4,11 @@ """ requirements.py ~~~~~~~~~~~~~~~ - + a simple script that creates a JSON file with the list of dependencies that are not from octave-forge package (SystemRequirements and BuildRequires). It writes the JSON content to the stdout. - + :copyright: (c) 2010 by Rafael Goncalves Martins :license: GPL-2, see LICENSE for more details. """ @@ -28,80 +28,84 @@ if os.path.exists(os.path.join(current_dir, '..', 'g_octave')): from g_octave import description, description_tree, exception def main(argv): - + if len(argv) <= 1: print >> sys.stderr, 'one argument required: the json file.' return 1 - + # init portage stuff settings, trees, mtimedb = load_emerge_config() root_config = trees[settings['ROOT']]['root_config'] s = search(root_config, False, False, False, False, False) - + desc_tree = description_tree.DescriptionTree(parse_sysreq = False) - - dependencies = [] - + + # identifier => list of dependencies + dependencies = dict() + for pkg in desc_tree.packages(): try: desc = desc_tree[pkg] except exception.DescriptionTreeException, err: print >> sys.stderr, 'DescriptionTree error: %s' % err return 1 - + deps = [] - + if desc.systemrequirements is not None: deps += [i.strip() for i in desc.systemrequirements.split(',')] - + if desc.buildrequires is not None: deps += [i.strip() for i in desc.buildrequires.split(',')] - - dep_aux = [] + for dep in deps: match = description.re_depends.match(dep) if match is not None: - my_match = match.group(1).split('-')[0] - if my_match not in dep_aux: - dependencies.append((match.group(1), my_match)) - dep_aux.append(my_match) - + my_dep = match.group(1) + my_match = my_dep.split('-')[0] + if my_match not in dependencies: + dependencies[my_match] = [my_dep] + else: + dependencies[my_match].append(my_dep) + json_dict = dict(dependencies=dict()) - + try: with open(argv[1], 'r') as fp: json_dict = json.load(fp) except: pass - - for dep_name, dep in dependencies: + + for dep in dependencies: s.execute(dep) - print dep_name + print dep temp = [] for i in range(len(s.matches['pkg'])): print ' %i: %s' % (i, s.matches['pkg'][i][0]) temp.append(s.matches['pkg'][i][0]) - - if dep_name in json_dict['dependencies']: + + if dependencies[dep][0] in json_dict['dependencies']: select = raw_input('Select a package [%s]: ' % \ - json_dict['dependencies'][dep_name]) + json_dict['dependencies'][dependencies[dep][0]]) else: select = raw_input('Select a package: ') try: - json_dict['dependencies'][dep_name] = temp[int(select)] + for dep_name in dependencies[dep]: + json_dict['dependencies'][dep_name] = temp[int(select)] except: - if select != '' or dep_name not in json_dict['dependencies']: - json_dict['dependencies'][dep_name] = select - print 'Selected: %s' % json_dict['dependencies'][dep_name] - print - + if select != '' or dependencies[dep][0] not in json_dict['dependencies']: + for dep_name in dependencies[dep]: + json_dict['dependencies'][dep_name] = select + print 'Selected: %s' % json_dict['dependencies'][dependencies[dep][0]] + print + try: with open(argv[1], 'w') as fp: json.dump(json_dict, fp, sort_keys=True, indent=4) except: print >> sys.stderr, 'failed to save the json file.' return 1 - + return 0 if __name__ == '__main__': |