Skip to content

Commit 693d30a

Browse files
committed
Fix memory leak
1 parent b85496b commit 693d30a

File tree

9 files changed

+484
-18
lines changed

9 files changed

+484
-18
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ coverage.txt
3434

3535
# Terraform
3636
.terraform
37-
.terraform.lock.hcl
37+
.terraform.*
3838
*.tfplan
3939
*.tfstate

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
path = terraform
33
url = https://github.com/hashicorp/terraform
44
shallow = true
5+
[submodule "go-plugin"]
6+
path = go-plugin
7+
url = https://github.com/hashicorp/go-plugin
8+
shallow = true

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![libterraform](https://img.shields.io/pypi/v/libterraform.svg)](https://pypi.python.org/pypi/libterraform)
44
[![libterraform](https://img.shields.io/pypi/l/libterraform.svg)](https://pypi.python.org/pypi/libterraform)
55
[![libterraform](https://img.shields.io/pypi/pyversions/libterraform.svg)](https://pypi.python.org/pypi/libterraform)
6-
[![Release](https://github.com/Prodesire/py-libterraform/actions/workflows/release.yml/badge.svg)](https://github.com/Prodesire/py-libterraform/actions/workflows/release.yml)
6+
[![Test](https://github.com/Prodesire/py-libterraform/actions/workflows/test.yml/badge.svg)](https://github.com/Prodesire/py-libterraform/actions/workflows/release.yml)
77
[![libterraform](https://img.shields.io/pypi/dm/libterraform)](https://pypi.python.org/pypi/libterraform)
88

99
Python binding for [Terraform](https://www.terraform.io/).
@@ -14,6 +14,10 @@ Python binding for [Terraform](https://www.terraform.io/).
1414
$ pip install libterraform
1515
```
1616

17+
> **NOTE**
18+
> - Please install version **0.3.1** or above, which solves the memory leak problem.
19+
> - This library does **not support** multithreading.
20+
1721
## Usage
1822

1923
### Terraform CLI

build.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
terraform_dirname = os.path.join(root, 'terraform')
1111
tf_path = os.path.join(root, tf_filename)
1212
tf_package_name = 'github.com/hashicorp/terraform'
13+
plugin_patch_filename = 'plugin_patch.go'
14+
plugin_dirname = os.path.join(root, 'go-plugin')
15+
plugin_patch_path = os.path.join(root, plugin_patch_filename)
16+
plugin_package_name = 'github.com/hashicorp/go-plugin'
1317

1418

1519
class BuildError(Exception):
@@ -23,10 +27,27 @@ def build(setup_kwargs):
2327
if not os.path.exists(os.path.join(terraform_dirname, '.git')):
2428
raise BuildError(f'The directory {terraform_dirname} not exists or init. '
2529
f'Please execute `git submodule init && git submodule update` to init it.')
30+
if not os.path.exists(os.path.join(plugin_dirname, '.git')):
31+
raise BuildError(f'The directory {plugin_dirname} not exists or init. '
32+
f'Please execute `git submodule init && git submodule update` to init it.')
2633

34+
target_plugin_patch_path = os.path.join(plugin_dirname, plugin_patch_filename)
2735
target_tf_path = os.path.join(terraform_dirname, tf_filename)
36+
target_tf_mod_path = os.path.join(terraform_dirname, 'go.mod')
2837
lib_path = os.path.join(terraform_dirname, lib_filename)
2938
header_path = os.path.join(terraform_dirname, header_filename)
39+
40+
# Patch go-plugin
41+
print(' - Patching go-plugin package')
42+
shutil.copyfile(plugin_patch_path, target_plugin_patch_path)
43+
with open(target_tf_mod_path) as f:
44+
mod_content = f.read()
45+
with open(target_tf_mod_path, 'w') as f:
46+
modified_mod_content = f'{mod_content}\n' \
47+
f'replace github.com/hashicorp/go-plugin v1.4.3 => ../go-plugin'
48+
f.write(modified_mod_content)
49+
50+
# Build libterraform
3051
shutil.copyfile(tf_path, target_tf_path)
3152
try:
3253
print(' - Building libterraform')
@@ -36,9 +57,13 @@ def build(setup_kwargs):
3657
)
3758
shutil.move(lib_path, os.path.join(root, 'libterraform', lib_filename))
3859
finally:
39-
for path in (target_tf_path, header_path, lib_path):
60+
# Remove external files
61+
for path in (target_plugin_patch_path, target_tf_path, header_path, lib_path):
4062
if os.path.exists(path):
4163
os.remove(path)
64+
# Recover go.mod
65+
with open(target_tf_mod_path, 'w') as f:
66+
f.write(mod_content)
4267

4368
return setup_kwargs
4469

go-plugin

Submodule go-plugin added at d555eeb

0 commit comments

Comments
 (0)