예제 #1
0
def make_version(static_map, fingerprint_map, reverse=True):
    """
    return version expression. compare to each version fingerprint.
    make compare expressions. like [(">=", 'v2.3.3.3')]
    """
    version_compare_set = set()
    key_lst = fingerprint_map.keys()
    version_lst = sorted([str2version(ver_) for ver_ in key_lst],
                         reverse=reverse)

    # head version in reverse version list is different
    head_version_str = version_lst[0].vstring
    fingerprint = head_fingerprint = fingerprint_map.get(head_version_str)
    match_head = match(static_map, head_fingerprint)
    if match_head and reverse:
        version_compare_set.add(('>', version_lst[1].vstring))
    elif match_head and not reverse:
        version_compare_set.add(('>=', head_version_str))

    for version in version_lst[1:]:
        logger.debug('create operator in version: %s', version.vstring)
        fingerprint.update(fingerprint_map.get(version.vstring))
        if match(static_map, fingerprint):
            operator = OPERATOR_MAP.get(reverse)
            version_compare_set.add((operator, version.vstring))
            logger.verbose('create version opreator: %s %s', operator,
                           version.vstring)
    logger.debug("operator: %s", version_compare_set)
    return version_compare_set
예제 #2
0
def run():
    """main function."""
    args = call_parser()
    check_run_options(args)
    depend = args.depend
    logger.info('searching %s fingerprint infomation.....', depend)
    plugin_info = search(depend)
    if not plugin_info:
        logger.error('%s can not find a fingerprint of %s', APPNAME, depend)
        logger.info('your can use --all to print all fingerprint supported.')
        # TODO: show the request fingerprint url in github
        sys.exit()
    logger.info('already found %s fingerprint.', depend)
    distri = file_distribute(plugin_info)
    logger.info('start to request hash map on %s in depth %d.', args.url,
                args.depth)
    hash_map = static_hash_map(args.url, distri, args.depth)
    logger.verbose('show the hash map: %s',
                   json.dumps(hash_map, indent=4, sort_keys=True))
    logger.info('let\'s observer which version of %s.', depend)
    version_set = make_all(hash_map, plugin_info)
    cond_lst = [
        VersionCond.from_str(''.join(comp)) for comp in calc(version_set)
    ]
    logger.info('show the possible versions of %s on %s', depend, args.url)
    result_lst = [('possible version', )]
    for version_str in plugin_info.get('versions'):
        if all((cond.match(version_str) for cond in cond_lst)):
            info = '{} v{}'.format(depend, version_str)
            logger.verbose(info)
            result_lst.append((info, ))
    show_output(AsciiTable(result_lst).table)
    sys.exit(0)
예제 #3
0
 def forward_diff(self):
     """Comparison between versions one by one from new to old."""
     version_lst = self.version_lst
     self._git_exec('checkout', self.version_lst[0].vstring)
     logger.verbose('forward version list %s', version_lst)
     self.last_hash(version_lst, 'fingerprint')
     self.make_diff(version_lst, 'fingerprint')
     self._git_exec('checkout', '-')
예제 #4
0
    def last_hash(self, version_lst, keyname):
        """Get all static file's hash string."""
        static_file_lst = self.last_static()
        last_ver = str(version_lst[0])
        logger.verbose('last hash dictionary, key: %s, version: %s', keyname, last_ver)
        _dic_link = self.info_result[keyname][last_ver] = {}

        for filepath in static_file_lst:
            if self._is_disable_suffix(filepath):
                continue
            md5_string = file_hash(filepath)
            web_file = self.web_file_path(filepath)
            _dic_link[web_file] = md5_string
예제 #5
0
def calc(version_compare_set):
    """calcute version compare list."""
    def _check(version_compare_lst):
        compare_lst = ['>']
        # avoid [>= 1.1, <= 1.0]
        for compare, _ in version_compare_lst:
            compare = compare.strip('=')
            if compare != compare_lst[-1]:
                compare_lst.append(compare)
        length = len(compare_lst)
        if version_compare_lst and 0 < length < 3:
            return True
        logger.warning('maybe framework or cms had be change by developer')
        if len(version_compare_lst) < 1:
            logger.warning(
                'Reducing depth(--depth), set smaller number maybe useful')
            logger.error('unable to judge version')
            sys.exit()
        elif length > 2:
            logger.warning(
                'Enlarge depth(--depth), set larger number or max(0) maybe useful'
            )
            lst = [('version cond', )]
            for comb, ver in version_compare_lst:
                lst.append(('{} {}'.format(comb, ver), ))
            show_output(AsciiTable(lst).table)
            sys.exit()

    lst = list(version_compare_set)
    lst.sort(key=cmp_to_key(version_compare_sort))
    logger.verbose('compare list after sort: %s', lst)
    if _check(lst):
        if len(lst) == 1:
            show_output(' '.join(lst[0]))
            return lst
        for prev_, next_ in zip(lst[:-1], lst[1:]):
            if prev_[0].strip('=') == '>' \
                and next_[0].strip('=') == '<':
                lst = [('version cond', ), ('{} {}'.format(*prev_), ),
                       ('{} {}'.format(*next_), )]
                show_output(AsciiTable(lst).table)
                return [prev_, next_]
        if lst[0][0].strip('=') == ">":
            res = lst[-1]
        else:
            res = lst[0]
        show_output(' '.join(res))
        return (res, )
    sys.exit()
예제 #6
0
 def make_diff(self, version_lst, keyname):
     """Comparison between versions one by one"""
     for version, prev_version in zip(version_lst[:-1], version_lst[1:]):
         next_ver, prev = version.vstring, prev_version.vstring
         logger.verbose('make hash dictionary, key: %s, version: %s-%s', keyname, next_ver, prev)
         output = self._git_exec('diff', '--name-only', next_ver, prev,
                                 '--', self.static_path, decode=True)
         filelst = output.split()
         if not filelst:
             continue
         _dic_link = self.info_result[keyname][prev] = {}
         for filename in filelst:
             if self._is_disable_suffix(filename):
                 continue
             hash_string = self.ancestor_file(prev, filename)
             web_file = self.web_file_path(filename)
             _dic_link[web_file] = hash_string
예제 #7
0
def request_file_hash(url):
    """Return hash string of file by request url."""
    logger.verbose('request get url: %s', url)
    try:
        response = requests.get(url,
                                verify=False,
                                allow_redirects=True,
                                timeout=10,
                                headers=HTTP_HEADERS)
        if response.status_code != 200:
            raise requests.RequestException('Status code error: {}'.format(
                response.status_code))
    except requests.RequestException as ex:
        logger.trace('request %s, exception: %s', url, str(ex))
        return ''
    else:
        cont = response.content
        return byte_hash(cont)
예제 #8
0
    def reverse_diff(self):
        """Comparison between versions one by one from old to new."""
        # version_lst in here must start from min version in fingerprint
        fingerprint_versions = self.info_result.get('fingerprint').keys()
        min_version = min(
            sorted((str2version(vstr) for vstr in fingerprint_versions)))
        version_lst = self.version_lst
        version_lst.reverse()
        logger.warning(version_lst)
        version_lst = version_lst[version_lst.index(min_version):]

        start_version = version_lst[1]
        logger.verbose('the first version with static file is %s',
                       start_version.vstring)
        logger.verbose('reverse version list %s', version_lst)
        logger.verbose('git checkout to %s', start_version.vstring)
        self._git_exec('checkout', start_version.vstring)
        self.last_hash(version_lst[1:], 'reverse_fingerprint')
        self.make_diff(version_lst[1:], 'reverse_fingerprint')
        logger.verbose('git checkout to HEAD')
        self._git_exec('checkout', '-')