Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utility that creates a requirements.txt file from settings.ini #1202

Merged
merged 3 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion nbdev/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@
'nbdev.release.release_gh': ('api/release.html#release_gh', 'nbdev/release.py'),
'nbdev.release.release_git': ('api/release.html#release_git', 'nbdev/release.py'),
'nbdev.release.release_pypi': ('api/release.html#release_pypi', 'nbdev/release.py'),
'nbdev.release.write_conda_meta': ('api/release.html#write_conda_meta', 'nbdev/release.py')},
'nbdev.release.write_conda_meta': ('api/release.html#write_conda_meta', 'nbdev/release.py'),
'nbdev.release.write_requirements': ('api/release.html#write_requirements', 'nbdev/release.py')},
'nbdev.serve': { 'nbdev.serve._is_qpy': ('api/serve.html#_is_qpy', 'nbdev/serve.py'),
'nbdev.serve._proc_file': ('api/serve.html#_proc_file', 'nbdev/serve.py'),
'nbdev.serve.proc_nbs': ('api/serve.html#proc_nbs', 'nbdev/serve.py')},
Expand Down
25 changes: 17 additions & 8 deletions nbdev/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# %% auto 0
__all__ = ['GH_HOST', 'Release', 'changelog', 'release_git', 'release_gh', 'pypi_json', 'latest_pypi', 'pypi_details',
'conda_output_path', 'write_conda_meta', 'anaconda_upload', 'release_conda', 'chk_conda_rel', 'release_pypi',
'release_both', 'bump_version', 'nbdev_bump_version']
'conda_output_path', 'write_conda_meta', 'write_requirements', 'anaconda_upload', 'release_conda',
'chk_conda_rel', 'release_pypi', 'release_both', 'bump_version', 'nbdev_bump_version']

# %% ../nbs/api/release.ipynb 14
from fastcore.all import *
Expand Down Expand Up @@ -236,6 +236,15 @@ def write_conda_meta(path='conda'):
_write_yaml(path, *_get_conda_meta())

# %% ../nbs/api/release.ipynb 43
# This function is used as a utility for creating HF spaces.
def write_requirements(directory=None):
"Writes a `requirements.txt` file to `directory` based on settings.ini."
cfg = get_config()
d = Path(directory) if directory else cfg.config_path
req = '\n'.join([cfg.get(k, '').replace(' ', '\n') for k in ['requirements', 'pip_requirements']])
(d/'requirements.txt').mk_write(req)

# %% ../nbs/api/release.ipynb 45
def anaconda_upload(name, loc=None, user=None, token=None, env_token=None):
"Upload `name` to anaconda"
user = f'-u {user} ' if user else ''
Expand All @@ -245,7 +254,7 @@ def anaconda_upload(name, loc=None, user=None, token=None, env_token=None):
if not loc: raise Exception("Failed to find output")
return _run(f'anaconda {token} upload {user} {loc} --skip-existing')

# %% ../nbs/api/release.ipynb 45
# %% ../nbs/api/release.ipynb 47
@call_parse
def release_conda(
path:str='conda', # Path where package will be created
Expand Down Expand Up @@ -275,7 +284,7 @@ def release_conda(
if 'anaconda upload' not in res: return print(f"{res}\n\Failed. Check auto-upload not set in .condarc. Try `--do_build False`.")
return anaconda_upload(name, loc)

# %% ../nbs/api/release.ipynb 46
# %% ../nbs/api/release.ipynb 48
def chk_conda_rel(
nm:str, # Package name on pypi
apkg:str=None, # Anaconda Package (defaults to {nm})
Expand All @@ -289,7 +298,7 @@ def chk_conda_rel(
pypitag = latest_pypi(nm)
if force or not condatag or pypitag > max(condatag): return f'{pypitag}'

# %% ../nbs/api/release.ipynb 48
# %% ../nbs/api/release.ipynb 50
@call_parse
def release_pypi(
repository:str="pypi" # Respository to upload to (defined in ~/.pypirc)
Expand All @@ -299,7 +308,7 @@ def release_pypi(
system(f'cd {_dir} && rm -rf dist && python setup.py sdist bdist_wheel')
system(f'twine upload --repository {repository} {_dir}/dist/*')

# %% ../nbs/api/release.ipynb 49
# %% ../nbs/api/release.ipynb 51
@call_parse
def release_both(
path:str='conda', # Path where package will be created
Expand All @@ -315,15 +324,15 @@ def release_both(
release_conda.__wrapped__(path, do_build=do_build, build_args=build_args, skip_upload=skip_upload, mambabuild=mambabuild, upload_user=upload_user)
nbdev_bump_version.__wrapped__()

# %% ../nbs/api/release.ipynb 51
# %% ../nbs/api/release.ipynb 53
def bump_version(version, part=2, unbump=False):
version = version.split('.')
incr = -1 if unbump else 1
version[part] = str(int(version[part]) + incr)
for i in range(part+1, 3): version[i] = '0'
return '.'.join(version)

# %% ../nbs/api/release.ipynb 52
# %% ../nbs/api/release.ipynb 54
@call_parse
def nbdev_bump_version(
part:int=2, # Part of version to bump
Expand Down
23 changes: 23 additions & 0 deletions nbs/api/release.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,29 @@
"**NB**: you need to first of all upload your package to PyPi, before creating the conda package."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"# This function is used as a utility for creating HF spaces.\n",
"def write_requirements(directory=None):\n",
" \"Writes a `requirements.txt` file to `directory` based on settings.ini.\"\n",
" cfg = get_config()\n",
" d = Path(directory) if directory else cfg.config_path\n",
" req = '\\n'.join([cfg.get(k, '').replace(' ', '\\n') for k in ['requirements', 'pip_requirements']])\n",
" (d/'requirements.txt').mk_write(req)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This function can be used in situations where you need to generate a `requirements.txt` file for a project."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down