From 9dcd9368f6f884424991f17a01d7afde25b5f26e Mon Sep 17 00:00:00 2001 From: Robert-André Mauchin Date: Apr 18 2019 12:52:24 +0000 Subject: Various minor fixes --- diff --git a/go2rpm/__main__.py b/go2rpm/__main__.py index d52de75..3397688 100644 --- a/go2rpm/__main__.py +++ b/go2rpm/__main__.py @@ -1,5 +1,5 @@ import argparse -from datetime import datetime, timedelta, timezone +from datetime import datetime, timezone import json import os import re @@ -19,31 +19,34 @@ DEFAULT_EDITOR = "vi" XDG_CACHE_HOME = os.getenv("XDG_CACHE_HOME", os.path.expanduser("~/.cache")) CACHEDIR = os.path.join(XDG_CACHE_HOME, "go2rpm") GIT_CACHEDIR = os.path.join(XDG_CACHE_HOME, "go2rpm", "src") -JINJA_ENV = jinja2.Environment( - loader=jinja2.ChoiceLoader([ - jinja2.FileSystemLoader(["/"]), - jinja2.PackageLoader("go2rpm", "templates"), - ]), - extensions=["jinja2.ext.do"], - trim_blocks=True, - lstrip_blocks=True) +JINJA_ENV = jinja2.Environment(loader=jinja2.ChoiceLoader([ + jinja2.FileSystemLoader(["/"]), + jinja2.PackageLoader("go2rpm", "templates"), +]), + extensions=["jinja2.ext.do"], + trim_blocks=True, + lstrip_blocks=True) def detect_packager(): rpmdev_packager = shutil.which("rpmdev-packager") if rpmdev_packager is not None: - return subprocess.check_output(rpmdev_packager, universal_newlines=True).strip() + return subprocess.check_output(rpmdev_packager, + universal_newlines=True).strip() gitbinary = shutil.which("git") if gitbinary is not None: - name = subprocess.check_output([gitbinary, "config", "user.name"], universal_newlines=True).strip() - email = subprocess.check_output([gitbinary, "config", "user.email"], universal_newlines=True).strip() + name = subprocess.check_output([gitbinary, "config", "user.name"], + universal_newlines=True).strip() + email = subprocess.check_output([gitbinary, "config", "user.email"], + universal_newlines=True).strip() return f"{name} <{email}>" return None def file_mtime(path): - return datetime.fromtimestamp(os.stat(path).st_mtime, timezone.utc).isoformat() + return datetime.fromtimestamp(os.stat(path).st_mtime, + timezone.utc).isoformat() # Sanitize a Go import path that can then serve as rpm package name @@ -99,23 +102,23 @@ def has_cmd(goipath): def has_other_cmd(goipath): other_cmd = [] path = os.path.join(GIT_CACHEDIR, *get_repo_name(goipath)) - exclude = set(['cmd', 'vendor', 'example', 'examples', 'internal', 'Godeps']) + exclude = set([ + 'cmd', 'vendor', 'example', 'examples', '_example', '_examples', + 'internal', 'Godeps', 'testdata', '_testdata' + ]) for root, dirs, files in os.walk(path, topdown=True): dirs[:] = [d for d in dirs if d not in exclude] if 'main.go' in files: - other_cmd.append(os.path.relpath(root,path)) + other_cmd.append(os.path.relpath(root, path)) return other_cmd def detect_license(goipath): path = os.path.join(GIT_CACHEDIR, *get_repo_name(goipath)) licenses = set() - raw_licenses = subprocess.check_output(['askalono', - '--format', - 'json', - 'crawl', - path - ], universal_newlines=True) + raw_licenses = subprocess.check_output( + ['askalono', '--format', 'json', 'crawl', path], + universal_newlines=True) raw_licenses = to_list(raw_licenses) for j in raw_licenses: try: @@ -129,7 +132,10 @@ def detect_license(goipath): def get_license_files(goipath): license_files = [] path = os.path.join(GIT_CACHEDIR, *get_repo_name(goipath)) - exclude = set(['vendor', 'example', 'examples', 'internal', 'Godeps', 'testdata', '_testdata', '.github']) + exclude = set([ + 'vendor', 'example', 'examples', '_example', '_examples', 'internal', + 'Godeps', 'testdata', '_testdata', '.github' + ]) matcher = re.compile( r"(COPYING|COPYING[\.\-].*|COPYRIGHT|COPYRIGHT[\.\-].*|" r"EULA|EULA[\.\-].*|licen[cs]e|licen[cs]e.*|LICEN[CS]E|" @@ -143,14 +149,20 @@ def get_license_files(goipath): dirs[:] = [d for d in dirs if d not in exclude] for f in files: if matcher.match(f): - license_files.append(os.path.relpath(os.path.join(root, f), path)) + license_files.append( + os.path.relpath(os.path.join(root, f), path)) return license_files def get_doc_files(goipath): doc_files = [] path = os.path.join(GIT_CACHEDIR, *get_repo_name(goipath)) - exclude = set(['vendor', 'example', 'examples', 'internal', 'Godeps', 'testdata', '_testdata', '.github']) + include = set( + ['doc', 'docs', 'example', 'examples', '_example', '_examples']) + exclude = set([ + 'vendor', 'doc', 'docs', 'example', 'examples', '_example', + '_examples', 'internal', 'Godeps', 'testdata', '_testdata', '.github' + ]) matcher = re.compile( r"(.*\.md|.*\.markdown|.*\.mdown|.*\.mkdn|.*\.rst|.*\.txt|AUTHORS|" r"AUTHORS[\.\-].*|CONTRIBUTORS|CONTRIBUTORS[\.\-].*|README|" @@ -165,6 +177,7 @@ def get_doc_files(goipath): r"GFDL-.*[0-9].*|GNU-.*[0-9].*|GPL-.*[0-9].*|LGPL-.*[0-9].*|" r"MIT-.*[0-9].*|MPL-.*[0-9].*|OFL-.*[0-9].*)") for root, dirs, files in os.walk(path, topdown=True): + doc_files = doc_files + [d for d in dirs if d in include] dirs[:] = [d for d in dirs if d not in exclude] for f in files: if matcher.match(f) and not licensesex.match(f): @@ -207,8 +220,11 @@ async def get_description(forge): def normalize_description(description): - description = description.strip() - if description is not "": + if description is not None: + description = description.strip() + else: + return description + if description != '': description = description.capitalize() else: return None @@ -230,7 +246,7 @@ def get_repo_parent_name(goipath): def download(goipath): os.environ['GOPATH'] = CACHEDIR os.environ['GO111MODULE'] = "off" - subprocess.check_output(['go', 'get', goipath + '/...']) + subprocess.check_output(['go', 'get', '-d', goipath]) repo = git.Repo(os.path.join(GIT_CACHEDIR, *get_repo_name(goipath))) repo.git.checkout("master") repo.head.reference = repo.heads.master @@ -253,8 +269,11 @@ def get_version(goipath): else: version = None tag = latest - tag_date = datetime.now(timezone.utc) - tags[-1].commit.committed_datetime - if (tag_date.days > 365 and repo.heads.master.commit.count() - tags[-1].commit.count() > 14): + tag_date = datetime.now( + timezone.utc) - tags[-1].commit.committed_datetime + if (tag_date.days > 365 + and repo.heads.master.commit.count() - tags[-1].commit.count() + > 14): commit = str(repo.heads.master.commit) else: commit = None @@ -303,25 +322,20 @@ def set_repo_version(goipath, version, tag, commit): def get_buildrequires(goipath): os.environ['GOPATH'] = CACHEDIR os.environ['GO111MODULE'] = "off" - buildrequires = subprocess.check_output(['golist', - '--imported', - '--skip-self', - '--package-path', - goipath - ], universal_newlines=True) + buildrequires = subprocess.check_output( + ['golist', '--imported', '--skip-self', '--package-path', goipath], + universal_newlines=True) return buildrequires def get_test_buildrequires(goipath): os.environ['GOPATH'] = CACHEDIR os.environ['GO111MODULE'] = "off" - test_buildrequires = subprocess.check_output(['golist', - '--imported', - '--tests', - '--skip-self', - '--package-path', - goipath - ], universal_newlines=True) + test_buildrequires = subprocess.check_output([ + 'golist', '--imported', '--tests', '--skip-self', '--package-path', + goipath + ], + universal_newlines=True) return test_buildrequires @@ -332,31 +346,54 @@ def to_list(s): def main(): - parser = argparse.ArgumentParser("go2rpm", - formatter_class=argparse.RawTextHelpFormatter) - parser.add_argument("--show-license-map", action="store_true", + parser = argparse.ArgumentParser( + "go2rpm", formatter_class=argparse.RawTextHelpFormatter) + parser.add_argument("--show-license-map", + action="store_true", help="Print license mappings and exit") - parser.add_argument("--no-auto-changelog-entry", action="store_true", + parser.add_argument("--no-auto-changelog-entry", + action="store_true", help="Do not generate a changelog entry") - parser.add_argument("-", "--stdout", action="store_true", + parser.add_argument("-", + "--stdout", + action="store_true", help="Print spec into stdout") - parser.add_argument("-p", "--profile", action="store", nargs="?", - type=int, choices=[1, 2], default=2, + parser.add_argument("-p", + "--profile", + action="store", + nargs="?", + type=int, + choices=[1, 2], + default=2, help="Profile of macros to use. \ 1: old macros. 2: new macros. \ default: 2") - parser.add_argument("-f", "--forge", action="store", nargs="?", + parser.add_argument("-f", + "--forge", + action="store", + nargs="?", help="Forge URL") - parser.add_argument("-a", "--altipaths", action="store", nargs="+", + parser.add_argument("-a", + "--altipaths", + action="store", + nargs="+", help="List of alternate import paths") - parser.add_argument("-v", "--version", action="store", nargs="?", + parser.add_argument("-v", + "--version", + action="store", + nargs="?", help="Package version") - parser.add_argument("-t", "--tag", action="store", nargs="?", + parser.add_argument("-t", + "--tag", + action="store", + nargs="?", help="Package tag") - parser.add_argument("-c", "--commit", action="store", nargs="?", + parser.add_argument("-c", + "--commit", + action="store", + nargs="?", help="Package commit") - parser.add_argument("goipath", help="Import path", - nargs="?") + parser.add_argument("goipath", help="Import path", nargs="?") args = parser.parse_args() if args.show_license_map: @@ -371,7 +408,9 @@ def main(): if (not re.search(r"^(github.com|gitlab.com|bitbucket.org)", goipath) and args.forge is None): - parser.error("forge URL is required for import path other than github, gitlab and bitbucket") + parser.error( + "forge URL is required for import path other than github, gitlab and bitbucket" + ) if args.forge is None: forge = 'https://' + goipath @@ -385,7 +424,8 @@ def main(): # Sort out the versions if args.version is not None or args.tag is not None or args.commit is not None: - if not check_if_version_exists(goipath, args.version, args.tag, args.commit): + if not check_if_version_exists(goipath, args.version, args.tag, + args.commit): version, tag, commit = get_version(goipath) else: version, tag, commit = args.version, args.tag, args.commit @@ -396,7 +436,9 @@ def main(): set_repo_version(goipath, version, tag, commit) buildrequires = to_list(get_buildrequires(goipath)) - test_buildrequires = list(set(to_list(get_test_buildrequires(goipath))).difference(set(buildrequires))) + test_buildrequires = list( + set(to_list(get_test_buildrequires(goipath))).difference( + set(buildrequires))) description = asyncio.run(get_description(forge)) if description is not None: @@ -455,7 +497,7 @@ def main(): kwargs["packager"] = detect_packager() licenses = detect_license(goipath) - if licenses is not '': + if licenses != '': license, comments = licensing.translate_license(licenses) kwargs["licenses"] = licenses kwargs["license"] = license @@ -471,5 +513,6 @@ def main(): fobj.write(spec_contents) fobj.write("\n") + if __name__ == "__main__": main() diff --git a/go2rpm/templates/profile1.spec b/go2rpm/templates/profile1.spec index 8073471..28d6d48 100644 --- a/go2rpm/templates/profile1.spec +++ b/go2rpm/templates/profile1.spec @@ -26,7 +26,7 @@ Version: {{ tag }} %gometa %global common_description %{expand: -{{ description|wordwrap(wrapstring="\\\n")|trim|default("# FIXME", true) }}} +{{ description|default("# FIXME", true)|wordwrap(wrapstring="\\\n")|trim }}} Name: %{goname} {% if version is none and tag is none %} diff --git a/go2rpm/templates/profile2.spec b/go2rpm/templates/profile2.spec index 711e86e..1ed81ed 100644 --- a/go2rpm/templates/profile2.spec +++ b/go2rpm/templates/profile2.spec @@ -24,7 +24,7 @@ Version: {{ tag }} {% endif %} %global common_description %{expand: -{{ description|wordwrap(wrapstring="\\\n")|trim|default("# FIXME", true) }}} +{{ description|default("# FIXME", true)|wordwrap(wrapstring="\\\n")|trim }}} {% if license_files|length > 0 %} %global golicenses {{ license_files|join(' ') }}