この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
以前の記事で Python の独自パッケージを AWS CodeArtifact からインストールする方法を紹介しました。
今回は、独自パッケージの保存先として GitHub のプライベートリポジトリを使う方法を試してみました。
作業の流れ
- プライベートリポジトリを作成
- Python の独自パッケージを作成
- パッケージを GitHub に push
- 動作確認
プライベートリポジトリの作成
まずは、適当な名前で GitHub のプライベートリポジトリを作成します。
前回作成したパッケージである myawsname
を今回も使おうと思うので、リポジトリ名を myawsname
としました。
Python の独自パッケージの作成
次に、下記の構成で必要なファイルを作成していきます。
作業ディレクトリ
├── LICENSE.txt
├── README.md
├── myawsname
│ ├── __init__.py
│ └── myawsname.py
└── setup.py
LICENSE.txt の作成
下記の内容で LICENSE.txt
を作成します。
LICENSE.txt
Copyright (c) 2022 Taro CM
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
README.md
の作成
次に README.md
を作成します。
README.md
# myawsname
This is a sample package.
# License
This software is released under the MIT License, see LICENSE.
setup.py
の作成
url
は先程作成したリポジトリのものを指定します。
setup.py
import setuptools
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setuptools.setup(
name="myawsname",
version="0.0.1",
author="cm-taro",
author_email="cmtaro@example.com",
license='MIT',
description="You can receive AWS Service Name.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/cm-ichida/myawsname",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
packages=setuptools.find_packages(),
python_requires=">=3.7",
)
myawsname/__init__.py
の作成
以前と同様の内容で作成します。
myawsname/__init__.py
from myawsname.myawsname import *
myawsname/myawsname.py
の作成
こちらも前回と同じ内容で作成します。
myawsname/myawsname.py
import json
def common(message):
data = json.loads(message)
json_message = json.dumps(data, indent=2)
return json_message
def s3():
message = '{"ServiceName": "Amazon S3"}'
return common(message)
def dynamodb():
message = '{"ServiceName": "Amazon DynamoDB"}'
return common(message)
def greengrass():
message = '{"ServiceName": "AWS IoT Greengrass"}'
return common(message)
パッケージを GitHub に push
準備ができたので commit して GitHub へ push します。 (事前に作業 PC から GitHub に SSH 鍵で接続できるようにしています)
$ git init
$ git add .
$ git commit -m "first commit"
$ git branch -M main
$ git remote add origin git@github.com:cm-ichida/myawsname.git
$ git push -u origin main
以下の通りプライベートリポジトリに push できました。
動作確認
最後にプライベートリポジトリからパッケージをインストールしてみましょう。
$ pip install git+ssh://git@github.com/cm-ichida/myawsname.git
下記のようなログが流れますが、GitHub のプライベートリポジトリからインストールされている事が分かります。(見やすさのため一部加工しています)
Collecting git+ssh://****@github.com/cm-ichida/myawsname.git
Cloning ssh://****@github.com/cm-ichida/myawsname.git to /private/var/folders/xxxxx/pip-req-build-xxxxxx
Running command git clone --filter=blob:none --quiet 'ssh://****@github.com/cm-ichida/myawsname.git' /private/var/folders/xxxxx/pip-req-build-xxxxxx
Resolved ssh://****@github.com/cm-ichida/myawsname.git to commit xxxxxx
Preparing metadata (setup.py) ... done
Building wheels for collected packages: myawsname
Building wheel for myawsname (setup.py) ... done
Created wheel for myawsname: filename=myawsname-0.0.1-py3-none-any.whl size=2602 sha256=xxxxx
Stored in directory: /private/var/folders/5k/xxx_xxxxx/T/pip-ephem-wheel-cache-xxx/wheels/47/40/8c/c xxxxxxxxxxxxx
Successfully built myawsname
Installing collected packages: myawsname
Successfully installed myawsname-0.0.1
myawsname
パッケージの詳細を確認できるようになりました。
$ pip show myawsname
Name: myawsname
Version: 0.0.1
Summary: You can receive AWS Service Name.
Home-page: https://github.com/cm-ichida/mypypi
Author: cm-taro
Author-email: cmtaro@example.com
License: MIT
Location: /Users/ichida.yoshihisa/.pyenv/versions/3.8.7/lib/python3.8/site-packages
Requires:
Required-by:
パッケージを正しく利用できることも確認できました。
$ python
Python 3.8.7 (default, Jul 8 2021, 15:28:23)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import myawsname
>>> myawsname.dynamodb()
'{\n "ServiceName": "Amazon DynamoDB"\n}'
>>>
最後に
CodeArtifact に比べると、リポジトリの構成が単純でインストール時の URL の制御なども必要ないので、直感的に利用できる印象を受けました。コードの管理を GitHub に集約できる点も嬉しいですね。
一方で CodeArtifact では、有効期限のあるトークンを都度取得して接続することで IoT デバイスから利用しやすいという一面もあるかと思います。
次回は、Greengrass デバイスで GitHub のプライベートリポジトリからインストールする手順を確認したいと思います。