452def _suggest_normalized_version(s):
453 """Suggest a normalized version close to the given version string.
454
455 If you have a version string that isn't rational (i.e. NormalizedVersion
456 doesn't like it) then you might be able to get an equivalent (or close)
457 rational version from this function.
458
459 This does a number of simple normalizations to the given string, based
460 on observation of versions currently in use on PyPI. Given a dump of
461 those version during PyCon 2009, 4287 of them:
462 - 2312 (53.93%) match NormalizedVersion without change
463 with the automatic suggestion
464 - 3474 (81.04%) match when using this suggestion method
465
466 @param s {str} An irrational version string.
467 @returns A rational version string, or None, if couldn't determine one.
468 """
469 try:
470 _normalized_key(s)
471 return s
472 except UnsupportedVersionError:
473 pass
474
476
477
478 for orig, repl in (('-alpha', 'a'), ('-beta', 'b'), ('alpha', 'a'),
479 ('beta', 'b'), ('rc', 'c'), ('-final', ''),
480 ('-pre', 'c'),
481 ('-release', ''), ('.release', ''), ('-stable', ''),
482 ('+', '.'), ('_', '.'), (' ', ''), ('.final', ''),
483 ('final', '')):
485
486
487 rs =
re.sub(
r"pre$",
r"pre0", rs)
488 rs =
re.sub(
r"dev$",
r"dev0", rs)
489
490
491
492
493 rs =
re.sub(
r"([abc]|rc)[\-\.](\d+)$",
r"\1\2", rs)
494
495
496
497 rs =
re.sub(
r"[\-\.](dev)[\-\.]?r?(\d+)$",
r".\1\2", rs)
498
499
500 rs =
re.sub(
r"[.~]?([abc])\.?",
r"\1", rs)
501
502
504 rs = rs[1:]
505
506
507
508
509 rs =
re.sub(
r"\b0+(\d+)(?!\d)",
r"\1", rs)
510
511
512
513
514 rs =
re.sub(
r"(\d+[abc])$",
r"\g<1>0", rs)
515
516
517 rs =
re.sub(
r"\.?(dev-r|dev\.r)\.?(\d+)$",
r".dev\2", rs)
518
519
520 rs =
re.sub(
r"-(a|b|c)(\d+)$",
r"\1\2", rs)
521
522
523 rs =
re.sub(
r"[\.\-](dev|devel)$",
r".dev0", rs)
524
525
526 rs =
re.sub(
r"(?![\.\-])dev$",
r".dev0", rs)
527
528
529 rs =
re.sub(
r"(final|stable)$",
"", rs)
530
531
532
533
534
535 rs =
re.sub(
r"\.?(r|-|-r)\.?(\d+)$",
r".post\2", rs)
536
537
538
539
540
541
542
543
544 rs =
re.sub(
r"\.?(dev|git|bzr)\.?(\d+)$",
r".dev\2", rs)
545
546
547
548
549
550
551 rs =
re.sub(
r"\.?(pre|preview|-c)(\d+)$",
r"c\g<2>", rs)
552
553
554 rs =
re.sub(
r"p(\d+)$",
r".post\1", rs)
555
556 try:
557 _normalized_key(rs)
558 except UnsupportedVersionError:
559 rs = None
560 return rs
561
562
563
564
565