<commit_before>from __future__ import print_function
from builtins import object, str
import argparse
import os
import re
import ruamel.yaml
import subprocess
import sys
from distutils.version import LooseVersion

class UnsupportedSrcError(Exception):
    def __init__(self, value = "Unsupported src Error"):
        self.value = value
    def __str__(self):
        return repr(self.value)

class git_tags(object):
    """ Retrieve git tags from a git url """

    def __init__(self, src):
        if 'git' in src:
            output = self._get_tags_from_git(src)
        elif 'file://' in src:
            output = self._get_tags_from_file(src)
        else:
            raise UnsupportedSrcError(
                  "Unsupported source type: {0}".format(src))

        self.tags = re.findall(r'refs/tags/([\d\w\.]+)$', 
                    output, re.MULTILINE)

    def latest(self):
        """ Return list of tags sorted by version number """

        if len(self.tags) < 1:
            return None
        return sorted(self.tags, key=LooseVersion)[-1]

    def _get_tags_from_git(self, src):
        """ 
           Retrieve tags from git url 
           Uses subprocess to take advantage of local auth/keys settings
        """
        p = subprocess.Popen(["git", "ls-remote", src], 
                             stdout=subprocess.PIPE,
                             universal_newlines=True)
        return p.communicate()[0]

    def _get_tags_from_file(self, src):
        """ Testing interface """
        src_path = src.split('//')[-1]
        with open(src_path, 'r') as f:
            return f.read()

class updater(object):
    """ Find latest version of each role """

    def __init__(self, requirement_file):
        self.requirement_file = requirement_file
        with open(requirement_file, 'r') as f:
            self.reqs = ruamel.yaml.load(f, ruamel.yaml.RoundTripLoader)
        #import pdb; pdb.set_trace()
        

    def find_latest_versions(self, replace_inline = False, 
                             update_unversioned = True):
        """ 
          Return a list of available updates

          If replace_inline is set, actually modify the original
          requirements file.
        """
        output = []
        for i, req in enumerate(self.reqs):
            assert req['src'], "Error, src key not found in {0}".format(req) 
            src = req['src']
            short_name = src.split('/')[-1].split('.')[0]
            version = req.get('version')
            g = git_tags(src)

            if not g.latest():
                continue
            if not version and not update_unversioned:
                continue

            if ( (not version) or
                 (LooseVersion(version) < LooseVersion(g.latest())) ):
                # If version is not set, suggest the latest version
                output.append("{0}: {1} -> {2}".format( 
                              short_name, version, g.latest()))
                if replace_inline:
                    self.reqs[i]["version"] = g.latest()

        if replace_inline:
            # Modify existing file
            with open(self.requirement_file, 'w') as f:
                f.write(ruamel.yaml.dump(self.reqs, Dumper=ruamel.yaml.RoundTripDumper))

        return output

def main():
    parser = argparse.ArgumentParser(
             description='Update ansible-galaxy requirements file')
    parser.add_argument('--inline', help="Edit requirements file in-place", 
                        action='store_true')
    parser.add_argument('--yolo', help="Ignore unversioned roles", 
                        action='store_true')
    parser.add_argument('requirement_file', help="ansible-galaxy yaml file")
    args = parser.parse_args()

    if not os.path.isfile(args.requirement_file):
        parser.print_help()
        sys.exit(1)
    u = updater(args.requirement_file)
    for line in u.find_latest_versions(replace_inline = args.inline,
                                       update_unversioned = not args.yolo):
        if args.yolo and "None" in line:
            continue
        print(line)

if __name__ == '__main__':<commit_msg>Implement --version and some short options<commit_after>from __future__ import print_function
from builtins import object, str
import argparse
import os
import re
import ruamel.yaml
import subprocess
import sys
from distutils.version import LooseVersion
from __init__ import __version__

class UnsupportedSrcError(Exception):
    def __init__(self, value = "Unsupported src Error"):
        self.value = value
    def __str__(self):
        return repr(self.value)

class git_tags(object):
    """ Retrieve git tags from a git url """

    def __init__(self, src):
        if 'git' in src:
            output = self._get_tags_from_git(src)
        elif 'file://' in src:
            output = self._get_tags_from_file(src)
        else:
            raise UnsupportedSrcError(
                  "Unsupported source type: {0}".format(src))

        self.tags = re.findall(r'refs/tags/([\d\w\.]+)$', 
                    output, re.MULTILINE)

    def latest(self):
        """ Return list of tags sorted by version number """

        if len(self.tags) < 1:
            return None
        return sorted(self.tags, key=LooseVersion)[-1]

    def _get_tags_from_git(self, src):
        """ 
           Retrieve tags from git url 
           Uses subprocess to take advantage of local auth/keys settings
        """
        p = subprocess.Popen(["git", "ls-remote", src], 
                             stdout=subprocess.PIPE,
                             universal_newlines=True)
        return p.communicate()[0]

    def _get_tags_from_file(self, src):
        """ Testing interface """
        src_path = src.split('//')[-1]
        with open(src_path, 'r') as f:
            return f.read()

class updater(object):
    """ Find latest version of each role """

    def __init__(self, requirement_file):
        self.requirement_file = requirement_file
        with open(requirement_file, 'r') as f:
            self.reqs = ruamel.yaml.load(f, ruamel.yaml.RoundTripLoader)
        

    def find_latest_versions(self, replace_inline = False, 
                             update_unversioned = True):
        """ 
          Return a list of available updates

          If replace_inline is set, actually modify the original
          requirements file.
        """
        output = []
        for i, req in enumerate(self.reqs):
            assert req['src'], "Error, src key not found in {0}".format(req) 
            src = req['src']
            short_name = src.split('/')[-1].split('.')[0]
            version = req.get('version')
            g = git_tags(src)

            if not g.latest():
                continue
            if not version and not update_unversioned:
                continue

            if ( (not version) or
                 (LooseVersion(version) < LooseVersion(g.latest())) ):
                # If version is not set, suggest the latest version
                output.append("{0}: {1} -> {2}".format( 
                              short_name, version, g.latest()))
                if replace_inline:
                    self.reqs[i]["version"] = g.latest()

        if replace_inline:
            # Modify existing file
            with open(self.requirement_file, 'w') as f:
                f.write(ruamel.yaml.dump(self.reqs, Dumper=ruamel.yaml.RoundTripDumper))

        return output

def main():
    parser = argparse.ArgumentParser(
             description='Update ansible-galaxy requirements file')
    parser.add_argument('-i', '--inline', help="Edit requirements file in-place", 
                        action='store_true')
    parser.add_argument('-y', '--yolo', help="Ignore unversioned roles", 
                        action='store_true')
    parser.add_argument('-V', '--version',
                        action='version',
                        version='galaxy-updater {0}'.format(__version__))
    parser.add_argument('requirement_file', help="ansible-galaxy yaml file")
    args = parser.parse_args()

    if not os.path.isfile(args.requirement_file):
        parser.print_help()
        sys.exit(1)
    u = updater(args.requirement_file)
    for line in u.find_latest_versions(replace_inline = args.inline,
                                       update_unversioned = not args.yolo):
        if args.yolo and "None" in line:
            continue
        print(line)

if __name__ == '__main__':
{'fraction_non_alphanumeric': 0.10836277974087162, 'fraction_numerical': 0.002944640753828033, 'mean_word_length': 1.935015554787418, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4647038', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2336, 'n_words': 614}