-
Notifications
You must be signed in to change notification settings - Fork 3
/
_tasks.py
216 lines (194 loc) · 8.91 KB
/
_tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from argparse import ArgumentParser
import logging
import os
from pathlib import Path
import re
import subprocess
import sys
def module_name_pep8_compliance(module_name):
"""Validate that the plugin module name is PEP8 compliant."""
if not re.match(r"^[a-z][_a-z0-9]+$", module_name):
link = "https://www.python.org/dev/peps/pep-0008/#package-and-module-names"
logger.error("Module name should be pep-8 compliant.")
logger.error(f" More info: {link}")
sys.exit(1)
def pypi_package_name_compliance(plugin_name):
"""Check there are no underscores in the plugin name"""
if re.search(r"_", plugin_name):
logger.error("PyPI.org and pip discourage package names with underscores.")
sys.exit(1)
def validate_manifest(module_name, project_directory):
"""Validate the new plugin repository against napari requirements."""
try:
from npe2 import PluginManifest
except ImportError as e:
logger.error("npe2 is not installed. Skipping manifest validation.")
return True
current_directory = Path('.').absolute()
if (current_directory.match(project_directory) and not Path(project_directory).is_absolute()):
project_directory = current_directory
path=Path(project_directory) / "src" / Path(module_name) / "napari.yaml"
valid = False
try:
pm = PluginManifest.from_file(path)
msg = f"✔ Manifest for {(pm.display_name or pm.name)!r} valid!"
valid = True
except PluginManifest.ValidationError as err:
msg = f"🅇 Invalid! {err}"
logger.error(msg.encode("utf-8"))
sys.exit(1)
except Exception as err:
msg = f"🅇 Failed to read {path!r}. {type(err).__name__}: {err}"
logger.error(msg.encode("utf-8"))
sys.exit(1)
else:
logger.info(msg.encode("utf-8"))
return valid
def initialize_new_repository(
install_precommit=False,
plugin_name="napari-foobar",
github_repository_url="provide later",
github_username_or_organization="githubuser",
):
"""Initialize new plugin repository with git, and optionally pre-commit."""
msg = ""
# Configure git line ending settings
# https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
if os.name == 'nt': # if on Windows, configure git line ending characters
subprocess.run(["git", "config", "--global", "core.autocrlf", "true"])
else: # for Linux and Mac
subprocess.run(["git", "config", "--global", "core.autocrlf", "input"])
# try to run git init
try:
subprocess.run(["git", "init", "-q"])
subprocess.run(["git", "checkout", "-b", "main"])
except Exception:
logger.error("Error in git initialization.")
if install_precommit is True:
# try to install and update pre-commit
try:
print("install pre-commit ...")
subprocess.run(["python", "-m", "pip", "install", "pre-commit"], stdout=subprocess.DEVNULL)
print("updating pre-commit...")
subprocess.run(["pre-commit", "autoupdate"], stdout=subprocess.DEVNULL)
subprocess.run(["git", "add", "."])
subprocess.run(["pre-commit", "run", "black", "-a"], capture_output=True)
except Exception:
logger.error("Error pip installing then running pre-commit.")
try:
subprocess.run(["git", "add", "."])
subprocess.run(["git", "commit", "-q", "-m", "initial commit"])
except Exception:
logger.error("Error creating initial git commit.")
msg += f"""
Your plugin template is ready! Next steps:
1. `cd` into your new directory and initialize a git repo
(this is also important for version control!)
cd {plugin_name}
git init -b main
git add .
git commit -m 'initial commit'
# you probably want to install your new package into your environment
pip install -e .
"""
else:
msg +=f"""
Your plugin template is ready! Next steps:
1. `cd` into your new directory
cd {plugin_name}
# you probably want to install your new package into your env
pip install -e .
"""
# Ensure full reqd/write/execute permissions for .git files
if os.name == 'nt': # if on Windows OS
# Avoid permission denied errors on Github Actions CI
subprocess.run(["attrib", "-h", "rr", ".git", "/s", "/d"])
if install_precommit is True:
# try to install and update pre-commit
# installing after commit to avoid problem with comments in setup.cfg.
try:
print("install pre-commit hook...")
subprocess.run(["pre-commit", "install"])
except Exception:
logger.error("Error at pre-commit install, skipping pre-commit")
if github_repository_url != 'provide later':
msg += f"""
2. Create a github repository with the name '{plugin_name}':
https://github.com/{github_username_or_organization}/{plugin_name}.git
3. Add your newly created github repo as a remote and push:
git remote add origin https://github.com/{github_username_or_organization}/{plugin_name}.git
git push -u origin main
4. The following default URLs have been added to `setup.cfg`:
Bug Tracker = https://github.com/{github_username_or_organization}/{plugin_name}/issues
Documentation = https://github.com/{github_username_or_organization}/{plugin_name}#README.md
Source Code = https://github.com/{github_username_or_organization}/{plugin_name}
User Support = https://github.com/{github_username_or_organization}/{plugin_name}/issues
These URLs will be displayed on your plugin's napari hub page.
You may wish to change these before publishing your plugin!"""
else:
msg += """
2. Create a github repository for your plugin:
https://github.com/new
3. Add your newly created github repo as a remote and push:
git remote add origin https://github.com/your-repo-username/your-repo-name.git
git push -u origin main
Don't forget to add this url to setup.cfg!
[metadata]
url = https://github.com/your-repo-username/your-repo-name.git
4. Consider adding additional links for documentation and user support to setup.cfg
using the project_urls key e.g.
[metadata]
project_urls =
Bug Tracker = https://github.com/your-repo-username/your-repo-name/issues
Documentation = https://github.com/your-repo-username/your-repo-name#README.md
Source Code = https://github.com/your-repo-username/your-repo-name
User Support = https://github.com/your-repo-username/your-repo-name/issues"""
msg += """
5. Read the README for more info: https://github.com/napari/napari-plugin-template
6. We've provided a template description for your plugin page on the napari hub at `.napari-hub/DESCRIPTION.md`.
You'll likely want to edit this before you publish your plugin.
7. Consider customizing the rest of your plugin metadata for display on the napari hub:
https://github.com/chanzuckerberg/napari-hub/blob/main/docs/customizing-plugin-listing.md
"""
return msg
if __name__=="__main__":
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("pre_gen_project")
parser = ArgumentParser()
parser.add_argument("--plugin_name",
dest="plugin_name",
help="The name of your plugin")
parser.add_argument("--module_name",
dest="module_name",
help="Plugin module name")
parser.add_argument("--project_directory",
dest="project_directory",
help="Project directory")
parser.add_argument("--install_precommit",
dest="install_precommit",
help="Install pre-commit",
default="False")
parser.add_argument("--github_repository_url",
dest="github_repository_url",
help="Github repository URL",
default='provide later')
parser.add_argument("--github_username_or_organization",
dest="github_username_or_organization",
help="Github user or organisation name",
default='githubuser')
args = parser.parse_args()
# Since bool("False") returns True, we need to check the actual string value
if str(args.install_precommit).lower() == "true":
install_precommit = True
else:
install_precommit = False
module_name_pep8_compliance(args.module_name)
pypi_package_name_compliance(args.plugin_name)
validate_manifest(args.module_name, args.project_directory)
msg = initialize_new_repository(
install_precommit=install_precommit,
plugin_name=args.plugin_name,
github_repository_url=args.github_repository_url,
github_username_or_organization=args.github_username_or_organization,
)
print(msg)