scripts/pythondistdeps: Do anything only when called as a main script

Note that the code is completely unchanged except for the indentation
under the new if __name__ == "__main__":

Note that this change is necessary, but not sufficient to use the
RpmVersion class.
The init of the RpmVersion class will fail when called from an outside
script, because the `parse_version()` function is lazily imported from
the code outside the class.  However, adding the import of
parse_version() to RpmVersion class is not done right now, because while
we would import it from `pkg_resources`, other scripts might want to
rely instead of the lightweight `packaging` module for the import. Thus
I'm leaving this conondrum to be addressed in the future.
This commit is contained in:
Tomas Orsava 2020-04-20 16:37:29 +02:00
parent 1523def34e
commit d48f3500d8

View File

@ -143,54 +143,57 @@ def normalize_name(name):
return re.sub(r'[-_.]+', '-', name).lower() return re.sub(r'[-_.]+', '-', name).lower()
parser = argparse.ArgumentParser(prog=argv[0]) if __name__ == "__main__":
group = parser.add_mutually_exclusive_group(required=True) """To allow this script to be importable (and its classes/functions
group.add_argument('-P', '--provides', action='store_true', help='Print Provides') reused), actions are performed only when run as a main script."""
group.add_argument('-R', '--requires', action='store_true', help='Print Requires')
group.add_argument('-r', '--recommends', action='store_true', help='Print Recommends') parser = argparse.ArgumentParser(prog=argv[0])
group.add_argument('-C', '--conflicts', action='store_true', help='Print Conflicts') group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-E', '--extras', action='store_true', help='Print Extras') group.add_argument('-P', '--provides', action='store_true', help='Print Provides')
group_majorver = parser.add_mutually_exclusive_group() group.add_argument('-R', '--requires', action='store_true', help='Print Requires')
group_majorver.add_argument('-M', '--majorver-provides', action='store_true', help='Print extra Provides with Python major version only') group.add_argument('-r', '--recommends', action='store_true', help='Print Recommends')
group_majorver.add_argument('--majorver-provides-versions', action='append', group.add_argument('-C', '--conflicts', action='store_true', help='Print Conflicts')
group.add_argument('-E', '--extras', action='store_true', help='Print Extras')
group_majorver = parser.add_mutually_exclusive_group()
group_majorver.add_argument('-M', '--majorver-provides', action='store_true', help='Print extra Provides with Python major version only')
group_majorver.add_argument('--majorver-provides-versions', action='append',
help='Print extra Provides with Python major version only for listed ' help='Print extra Provides with Python major version only for listed '
'Python VERSIONS (appended or comma separated without spaces, e.g. 2.7,3.9)') 'Python VERSIONS (appended or comma separated without spaces, e.g. 2.7,3.9)')
parser.add_argument('-m', '--majorver-only', action='store_true', help='Print Provides/Requires with Python major version only') parser.add_argument('-m', '--majorver-only', action='store_true', help='Print Provides/Requires with Python major version only')
parser.add_argument('-n', '--normalized-names-format', action='store', parser.add_argument('-n', '--normalized-names-format', action='store',
default="legacy-dots", choices=["pep503", "legacy-dots"], default="legacy-dots", choices=["pep503", "legacy-dots"],
help='Format of normalized names according to pep503 or legacy format that allows dots [default]') help='Format of normalized names according to pep503 or legacy format that allows dots [default]')
parser.add_argument('--normalized-names-provide-both', action='store_true', parser.add_argument('--normalized-names-provide-both', action='store_true',
help='Provide both `pep503` and `legacy-dots` format of normalized names (useful for a transition period)') help='Provide both `pep503` and `legacy-dots` format of normalized names (useful for a transition period)')
parser.add_argument('-L', '--legacy-provides', action='store_true', help='Print extra legacy pythonegg Provides') parser.add_argument('-L', '--legacy-provides', action='store_true', help='Print extra legacy pythonegg Provides')
parser.add_argument('-l', '--legacy', action='store_true', help='Print legacy pythonegg Provides/Requires instead') parser.add_argument('-l', '--legacy', action='store_true', help='Print legacy pythonegg Provides/Requires instead')
parser.add_argument('files', nargs=argparse.REMAINDER) parser.add_argument('files', nargs=argparse.REMAINDER)
args = parser.parse_args() args = parser.parse_args()
py_abi = args.requires
py_deps = {}
py_abi = args.requires if args.majorver_provides_versions:
py_deps = {}
if args.majorver_provides_versions:
# Go through the arguments (can be specified multiple times), # Go through the arguments (can be specified multiple times),
# and parse individual versions (can be comma-separated) # and parse individual versions (can be comma-separated)
args.majorver_provides_versions = [v for vstring in args.majorver_provides_versions args.majorver_provides_versions = [v for vstring in args.majorver_provides_versions
for v in vstring.split(",")] for v in vstring.split(",")]
# If normalized_names_require_pep503 is True we require the pep503 # If normalized_names_require_pep503 is True we require the pep503
# normalized name, if it is False we provide the legacy normalized name # normalized name, if it is False we provide the legacy normalized name
normalized_names_require_pep503 = args.normalized_names_format == "pep503" normalized_names_require_pep503 = args.normalized_names_format == "pep503"
# If normalized_names_provide_pep503/legacy is True we provide the # If normalized_names_provide_pep503/legacy is True we provide the
# pep503/legacy normalized name, if it is False we don't # pep503/legacy normalized name, if it is False we don't
normalized_names_provide_pep503 = \ normalized_names_provide_pep503 = \
args.normalized_names_format == "pep503" or args.normalized_names_provide_both args.normalized_names_format == "pep503" or args.normalized_names_provide_both
normalized_names_provide_legacy = \ normalized_names_provide_legacy = \
args.normalized_names_format == "legacy-dots" or args.normalized_names_provide_both args.normalized_names_format == "legacy-dots" or args.normalized_names_provide_both
# At least one type of normalization must be provided # At least one type of normalization must be provided
assert normalized_names_provide_pep503 or normalized_names_provide_legacy assert normalized_names_provide_pep503 or normalized_names_provide_legacy
for f in (args.files or stdin.readlines()): for f in (args.files or stdin.readlines()):
f = f.strip() f = f.strip()
lower = f.lower() lower = f.lower()
name = 'python(abi)' name = 'python(abi)'
@ -400,9 +403,9 @@ for f in (args.files or stdin.readlines()):
if spec not in py_deps[name]: if spec not in py_deps[name]:
py_deps[name].append(spec) py_deps[name].append(spec)
names = list(py_deps.keys()) names = list(py_deps.keys())
names.sort() names.sort()
for name in names: for name in names:
if py_deps[name]: if py_deps[name]:
# Print out versioned provides, requires, recommends, conflicts # Print out versioned provides, requires, recommends, conflicts
spec_list = [] spec_list = []