Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 843c9b3

Browse files
committed
service: dev: Create fresh archive instead of cleaning
Signed-off-by: John Andersen <[email protected]>
1 parent da2d714 commit 843c9b3

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- Change Core to Official to clarify who maintains each plugin
2020
- Name of output of unsupervised model from "Prediction" to "cluster"
2121
- Test scikit LR documentation examples in CI
22+
- Create a fresh archive of the git repo for release instead of cleaning
23+
existing repo with `git clean` for development service release command.
2224

2325
## [0.3.4] - 2020-02-28
2426
### Added

dffml/service/dev.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import ast
44
import json
55
import pydoc
6+
import shutil
67
import asyncio
78
import pathlib
89
import getpass
10+
import tempfile
911
import importlib
1012
import contextlib
1113
import configparser
@@ -423,17 +425,36 @@ async def run(self):
423425
if package_json["info"]["version"] == version:
424426
print(f"Version {version} of {name} already on PyPi")
425427
return
426-
# Upload if not present
427-
for cmd in [
428-
["git", "clean", "-fdx"],
429-
[sys.executable, "setup.py", "sdist"],
430-
[sys.executable, "-m", "twine", "upload", "dist/*"],
431-
]:
432-
print(f"$ {' '.join(cmd)}")
433-
proc = await asyncio.create_subprocess_exec(*cmd)
434-
await proc.wait()
435-
if proc.returncode != 0:
436-
raise RuntimeError
428+
# Create a fresh copy of the codebase to upload
429+
with tempfile.TemporaryDirectory() as tempdir:
430+
# The directory where the fresh copy will live
431+
clean_dir = pathlib.Path(tempdir, "clean")
432+
clean_dir.mkdir()
433+
archive_file = pathlib.Path(tempdir, "archive.tar")
434+
# Create the archive
435+
with open(archive_file, "wb") as archive:
436+
cmd = ["git", "archive", "--format=tar", "HEAD"]
437+
print(f"$ {' '.join(cmd)}")
438+
proc = await asyncio.create_subprocess_exec(
439+
*cmd, stdout=archive
440+
)
441+
await proc.wait()
442+
if proc.returncode != 0:
443+
raise RuntimeError
444+
# Change directory into the clean copy
445+
with chdir(clean_dir):
446+
# Extract the archive
447+
shutil.unpack_archive(archive_file)
448+
# Upload if not present
449+
for cmd in [
450+
[sys.executable, "setup.py", "sdist"],
451+
[sys.executable, "-m", "twine", "upload", "dist/*"],
452+
]:
453+
print(f"$ {' '.join(cmd)}")
454+
proc = await asyncio.create_subprocess_exec(*cmd)
455+
await proc.wait()
456+
if proc.returncode != 0:
457+
raise RuntimeError
437458

438459

439460
class BumpMain(CMD):

tests/service/test_dev.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import json
55
import glob
66
import inspect
7+
import tarfile
78
import tempfile
89
import unittest
910
import contextlib
1011
import dataclasses
1112
import unittest.mock
12-
from typing import Type
13+
from typing import Type, List, BinaryIO
1314

1415
from dffml.version import VERSION
1516
from dffml.df.types import DataFlow
@@ -156,20 +157,39 @@ async def test_run(self):
156157

157158
@dataclasses.dataclass
158159
class FakeProcess:
160+
cmd: List[str] = None
159161
returncode: int = 0
162+
stdout: BinaryIO = None
163+
164+
def __post_init__(self):
165+
if self.cmd is None:
166+
self.cmd = []
160167

161168
async def communicate(self):
162169
return b"", b""
163170

164171
async def wait(self):
165-
return
172+
if not "archive" in self.cmd:
173+
return
174+
with contextlib.ExitStack() as stack:
175+
# Create the bytes objects to build the tarfile in memory
176+
tar_fileobj = stack.enter_context(io.BytesIO())
177+
hello_txt_fileobj = stack.enter_context(io.BytesIO(b"world"))
178+
# Create the TarInfo objects
179+
hello_txt_tarinfo = tarfile.TarInfo(name="somedir/hello.txt")
180+
hello_txt_tarinfo.size = len(hello_txt_fileobj.getvalue())
181+
# Create the archive using the bytes objects
182+
with tarfile.open(mode="w|", fileobj=tar_fileobj) as archive:
183+
archive.addfile(hello_txt_tarinfo, fileobj=hello_txt_fileobj)
184+
# Write out the contents of the tar to the client
185+
self.stdout.write(tar_fileobj.getvalue())
166186

167187

168188
def mkexec(proc_cls: Type[FakeProcess] = FakeProcess):
169189
async def fake_create_subprocess_exec(
170190
*args, stdin=None, stdout=None, stderr=None
171191
):
172-
return proc_cls()
192+
return proc_cls(cmd=args, stdout=stdout)
173193

174194
return fake_create_subprocess_exec
175195

@@ -227,7 +247,7 @@ async def test_okay(self):
227247
stdout.getvalue().strip(),
228248
inspect.cleandoc(
229249
f"""
230-
$ git clean -fdx
250+
$ git archive --format=tar HEAD
231251
$ {sys.executable} setup.py sdist
232252
$ {sys.executable} -m twine upload dist/*
233253
"""

0 commit comments

Comments
 (0)